EditorBattleMgr.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. public class EditorBattleMgr : SingletonMono<EditorBattleMgr>
  7. {
  8. LogicBattleField battleField = null;
  9. FighterManager fighterMgr = null;
  10. ReadyPoint actorSpawnPoint = null;
  11. SpawnPoint monsterSpawnPoint = null;
  12. bool bInitedFighter = false;
  13. bool bStartGame = false;
  14. public override void InitMgr()
  15. {
  16. base.InitMgr();
  17. }
  18. private void Start()
  19. {
  20. Constants.isOffline = true;
  21. EventMgr.AddEventListener<int>(ECoreEventType.EID_ConfigMgrInit, OnCfgLoaded);
  22. MusicMgr.Instance.InitMgr();
  23. ConfigMgr.CreateInstance();
  24. GameObject readyPointGo = GameObject.Find("ActorBornPoint");
  25. if(readyPointGo!=null)
  26. {
  27. actorSpawnPoint = readyPointGo.GetComponent<ReadyPoint>();
  28. }
  29. GameObject spawnPointGo = GameObject.Find("SpawnPoint");
  30. if(spawnPointGo!=null)
  31. {
  32. monsterSpawnPoint = spawnPointGo.GetComponent<SpawnPoint>();
  33. }
  34. }
  35. private void OnCfgLoaded(CoreEvent<int> ce)
  36. {
  37. if(ce.Data == 1)
  38. {
  39. StartGame();
  40. }
  41. }
  42. void Update()
  43. {
  44. if (!bInitedFighter)
  45. {
  46. if(bStartGame && !BattlePrepareManager.Instance.IsLoading)
  47. {
  48. Spawn();
  49. }
  50. return;
  51. }
  52. if(battleField != null)
  53. {
  54. battleField.Update(Time.deltaTime);
  55. }
  56. if (fighterMgr != null)
  57. {
  58. fighterMgr.FixedUpdateByEditor(Time.deltaTime);
  59. EffectManager.Instance.Update(Time.deltaTime);
  60. }
  61. }
  62. public void Restart()
  63. {
  64. EndGame();
  65. StartGame();
  66. }
  67. public void StartGame()
  68. {
  69. if(actorSpawnPoint == null)
  70. {
  71. EditorUtility.DisplayDialog("错误", "角色出生点没有,请配置!!", "确定");
  72. return;
  73. }
  74. if(monsterSpawnPoint == null)
  75. {
  76. EditorUtility.DisplayDialog("错误", "怪物出生点没有,请配置!", "确定");
  77. return;
  78. }
  79. List<ActorData> teamActors = new List<ActorData>();
  80. List<ActorData> enemyActors = new List<ActorData>();
  81. for (int idx = 0; idx < 4; idx++)
  82. {
  83. string idKey = string.Format("TrainCamp_HeroID_{0}", idx);
  84. string levelKey = string.Format("TrainCamp_HeroLevel_{0}", idx);
  85. string skillIdKey = string.Format("TrainCamp_HeroSkillId_{0}", idx);
  86. string skillLvKey = string.Format("TrainCamp_HeroSkillLv_{0}", idx);
  87. string genderKey = string.Format("TrainCamp_Gender_{0}", idx);
  88. string invincibleKey = string.Format("TrainCamp_Invincible_{0}", idx);
  89. int id = 0, lv = 0, skillId = 0, skillLv = 0;
  90. Role_Gender gender = Role_Gender.Male;
  91. bool invincible = false;
  92. if (PlayerPrefs.HasKey(idKey))
  93. id = PlayerPrefs.GetInt(idKey);
  94. if (PlayerPrefs.HasKey(levelKey))
  95. lv = PlayerPrefs.GetInt(levelKey);
  96. if (PlayerPrefs.HasKey(skillIdKey))
  97. skillId = PlayerPrefs.GetInt(skillIdKey);
  98. if (PlayerPrefs.HasKey(skillLvKey))
  99. skillLv = PlayerPrefs.GetInt(skillLvKey);
  100. if (PlayerPrefs.HasKey(genderKey))
  101. gender = (Role_Gender)PlayerPrefs.GetInt(genderKey);
  102. if (PlayerPrefs.HasKey(invincibleKey))
  103. invincible = PlayerPrefs.GetInt(invincibleKey) > 0 ? true : false;
  104. if (id >0 && lv > 0 && skillId > 0 && skillLv > 0)
  105. {
  106. SkillParam skill = new SkillParam();
  107. skill.skillId = skillId;
  108. skill.skillLv = skillLv;
  109. List<SkillParam> skills = new List<SkillParam>();
  110. skills.Add(skill);
  111. ActorData hero = null;
  112. if (idx == 0)
  113. {
  114. HeroActorData temp = ActorData.CreatePlayerActor(100000 + idx, id * 10 + (int)gender, lv, skills!=null?skills.ToArray():null) as HeroActorData;
  115. temp.SetProfessionId(id);
  116. hero = temp;
  117. }
  118. else
  119. {
  120. hero = ActorData.CreateFellowActor(100000 + idx, id, lv, skills!=null?skills.ToArray():null);
  121. }
  122. hero.Invincible = invincible;
  123. teamActors.Add(hero);
  124. }
  125. }
  126. for (int idx = 0; idx < 6; idx++)
  127. {
  128. string idKey = string.Format("TrainCamp_NpcID_{0}", idx);
  129. string levelKey = string.Format("TrainCamp_NpcLevel_{0}", idx);
  130. string posKey = string.Format("TrainCamp_NpcPos_{0}", idx);
  131. string invincibleKey = string.Format("TrainCamp_NpcInvincible_{0}", idx);
  132. int id = 0, level = 0, pos = 1;
  133. bool invincible = false;
  134. if (PlayerPrefs.HasKey(idKey))
  135. id = PlayerPrefs.GetInt(idKey);
  136. if (PlayerPrefs.HasKey(levelKey))
  137. level = PlayerPrefs.GetInt(levelKey);
  138. else
  139. level = 1;
  140. if (PlayerPrefs.HasKey(posKey))
  141. pos = PlayerPrefs.GetInt(posKey);
  142. if (PlayerPrefs.HasKey(invincibleKey))
  143. invincible = PlayerPrefs.GetInt(invincibleKey) > 0 ? true : false;
  144. if (id > 0 && level > 0 )
  145. {
  146. ActorData npc = ActorData.CreateNpcPlayerActor(200000+ idx, id, level);
  147. npc.PositionValue = pos;
  148. npc.Invincible = invincible;
  149. enemyActors.Add(npc);
  150. }
  151. }
  152. if (teamActors.Count == 0)
  153. {
  154. EditorUtility.DisplayDialog("错误", "我方英雄没有一个合格的测试者", "确定");
  155. return;
  156. }
  157. if(enemyActors.Count == 0)
  158. {
  159. EditorUtility.DisplayDialog("错误", "请正确部署最少一个怪", "确定");
  160. return;
  161. }
  162. bool enableLog = false;
  163. if(PlayerPrefs.HasKey("EnabledLog"))
  164. enableLog = PlayerPrefs.GetInt("EnabledLog") > 0 ? true:false;
  165. if(enableLog)
  166. {
  167. DebugHelper.LogLevel = LogLevel.Info;
  168. }
  169. fighterMgr = new FighterManager(null);
  170. fighterMgr.AddFighters(teamActors.ToArray(), eTeamType.Friend);
  171. fighterMgr.AddFighters(enemyActors.ToArray(), eTeamType.Enemy);
  172. fighterMgr.ParseLoad();
  173. BattlePrepareManager.Instance.StartLoad();
  174. bStartGame = true;
  175. }
  176. void Spawn()
  177. {
  178. if (bInitedFighter) return;
  179. if (battleField == null) {
  180. battleField = new LogicBattleField(null, null);
  181. }
  182. for (int idx = 0; idx < fighterMgr.AllFighters.Count; idx++)
  183. {
  184. Fighter f = fighterMgr.AllFighters[idx];
  185. f.StateData.IsInvincible = f.Actor.Invincible;
  186. if (f.TeamSide == eTeamType.Friend)
  187. {
  188. f.Spawn(actorSpawnPoint.GetBornPointPos(f.ProfType,f.ReadyPosOrder).heroPos, Vector3.forward, Quaternion.identity);
  189. }
  190. else
  191. {
  192. Vector3 pos = Vector3.zero;
  193. Transform p = monsterSpawnPoint.transform.Find("point" + f.PositionValue);
  194. if (p != null)
  195. pos = p.position;
  196. f.Spawn(pos, Vector3.back, Quaternion.identity);
  197. }
  198. battleField.AddFighter(f);
  199. }
  200. bInitedFighter = true;
  201. battleField.CurrentState = LogicBattleFieldStateType.EditorFighting;
  202. }
  203. public void EndGame()
  204. {
  205. if (fighterMgr != null)
  206. {
  207. fighterMgr.Dispose();
  208. fighterMgr = null;
  209. }
  210. //ScaleTimeInfo.Instance.Stop();
  211. EffectManager.Instance.Clear();
  212. if (MusicMgr.HasInstance())
  213. MusicMgr.Instance.CleanBeforeChangeScene();
  214. }
  215. }
  216. #endif