LogicBattleScene.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.SceneManagement;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. public class LogicBattleScene : BaseBattleScene
  7. {
  8. const string bossintro_camera_go = "bossIntro_cam";
  9. const string scene_root_name = "SceneRoot";
  10. #region fields
  11. private string switchGoName;
  12. private GameObject[] mRootObjs = null;
  13. private List<SpawnPointCfg> mBossSpawnPointList = new List<SpawnPointCfg>();
  14. private List<SpawnPointCfg> mSpawnPointList = new List<SpawnPointCfg>();
  15. private ReadyPoint mActorBornPoint;
  16. private LogicBattle mLogicBattle;
  17. private MapItemData mMapItem;
  18. private int mBattleIdx = -1;
  19. private int[] pathList = null;
  20. private int mCurMonsterSpawnPointIdx = 0;
  21. private SpawnPointCfg mCurrentMonsterSpawnPoint;
  22. private BossBattleEffect mBattleEffect;
  23. private CruiseEffect mCruiseEffect;
  24. private SceneSkyboxMatContainer mSceneSkyboxMatContainer;
  25. private Tree mTree;
  26. public float delTime = 2;
  27. public ReadyPoint ActorBornPoint
  28. {
  29. get { return mActorBornPoint; }
  30. }
  31. public List<SpawnPointCfg> SpawnPointList
  32. {
  33. get { return mSpawnPointList; }
  34. }
  35. public SpawnPointCfg MapMonsterSpawnPoint
  36. {
  37. get
  38. {
  39. return mCurrentMonsterSpawnPoint;
  40. }
  41. }
  42. public SpawnPointCfg BossSpawnPoint
  43. {
  44. get
  45. {
  46. if (mBossSpawnPointList == null || mBossSpawnPointList.Count == 0) return null;
  47. return mBossSpawnPointList[CurrentBattleInfo.bossBornIdx];
  48. }
  49. }
  50. public LogicBattle Battle { get { return mLogicBattle; } }
  51. public MapItemData MapData { get { return mMapItem; } }
  52. public LevelItem CurrentBattleInfo { get; private set; }
  53. public LogicBattleField CurrentBattleField { get; private set; }
  54. public GameMode GameMode { get { return mMapItem != null ? mMapItem.gameMode : GameMode.GameMode_Normal; } }
  55. public GameObject BossIntroCamGo { get; private set; }
  56. public GameObject SceneRootGo { get; private set; }
  57. public int CurrentBattleIdx { get { return mBattleIdx; } }
  58. public bool HasNextBattle { get { return mMapItem != null && mMapItem.battles != null && mBattleIdx + 1 < mMapItem.battles.Length; } }
  59. SceneObjData[] mObjDataList;
  60. #endregion
  61. public LogicBattleScene(LogicBattle battle)
  62. :base(battle,"")
  63. {
  64. mLogicBattle = battle;
  65. if (mMapItem == null)
  66. {
  67. mMapItem = new MapItemData(battle.SceneId,mLogicBattle.CurSceneItem.LevelItems);
  68. LoadMapSpawnPointCfg(mMapItem.levelname);
  69. mSceneName = mMapItem.levelname;
  70. }
  71. mBattleIdx = battle.StartInfo.StartBattleIdx - 1;
  72. RegisterEvents();
  73. }
  74. public void CreateReplayBattle(int battleIdx)
  75. {
  76. CurrentBattleInfo = mMapItem.battles[battleIdx];
  77. pathList = CurrentBattleInfo.PathList;
  78. if (pathList == null || pathList.Length == 0)
  79. {
  80. pathList = new int[mSpawnPointList.Count];
  81. for (int idx = 0; idx < pathList.Length; idx++)
  82. {
  83. pathList[idx] = idx;
  84. }
  85. }
  86. SelectSpawnPoint();
  87. CurrentBattleField = CreateBattleField(CurrentBattleInfo.LevelId);
  88. CurrentBattleField.FieldCenter = mCurrentMonsterSpawnPoint.PointList[1].pos;
  89. mLogicBattle.SetBattleField(CurrentBattleField);
  90. mLogicBattle.StartInfo.curLevelItem = CurrentBattleInfo;
  91. CurrentBattleField.killBoss = true;
  92. CurrentBattleField.IsPlayingRecorder = true;
  93. }
  94. public void ReopenBattle()
  95. {
  96. if (mMapItem != null &&
  97. mMapItem.battles != null &&
  98. mBattleIdx < mMapItem.battles.Length &&
  99. mBattleIdx >= 0)
  100. {
  101. CurrentBattleInfo = mMapItem.battles[mBattleIdx];
  102. pathList = CurrentBattleInfo.PathList;
  103. if (pathList == null || pathList.Length == 0)
  104. {
  105. pathList = new int[mSpawnPointList.Count];
  106. for (int idx = 0; idx < pathList.Length; idx++)
  107. {
  108. pathList[idx] = idx;
  109. }
  110. }
  111. SelectSpawnPoint();
  112. CurrentBattleField = CreateBattleField(CurrentBattleInfo.LevelId);
  113. CurrentBattleField.FieldCenter = mCurrentMonsterSpawnPoint.PointList[1].pos;
  114. mLogicBattle.SetBattleField(CurrentBattleField);
  115. mLogicBattle.StartInfo.curLevelItem = CurrentBattleInfo;
  116. }
  117. }
  118. public bool NextBattle()
  119. {
  120. mBattleIdx++;
  121. if (mMapItem != null &&
  122. mMapItem.battles != null &&
  123. mBattleIdx < mMapItem.battles.Length &&
  124. mBattleIdx >= 0)
  125. {
  126. CurrentBattleInfo = mMapItem.battles[mBattleIdx];
  127. pathList = CurrentBattleInfo.PathList;
  128. if (pathList == null || pathList.Length == 0)
  129. {
  130. pathList = new int[mSpawnPointList.Count];
  131. for (int idx = 0; idx < pathList.Length; idx++)
  132. {
  133. pathList[idx] = idx;
  134. }
  135. }
  136. SelectSpawnPoint();
  137. CurrentBattleField = CreateBattleField(CurrentBattleInfo.LevelId);
  138. CurrentBattleField.FieldCenter = mCurrentMonsterSpawnPoint.PointList[1].pos;
  139. mLogicBattle.SetBattleField(CurrentBattleField);
  140. mLogicBattle.StartInfo.curLevelItem = CurrentBattleInfo;
  141. return true;
  142. }
  143. else
  144. {
  145. CurrentBattleInfo = null;
  146. CurrentBattleField = null;
  147. mLogicBattle.SetBattleField(null);
  148. return false;
  149. }
  150. }
  151. public void SelectSpawnPoint()
  152. {
  153. if (mSpawnPointList == null) return;
  154. int idx = pathList[mCurMonsterSpawnPointIdx++];
  155. if (mCurMonsterSpawnPointIdx >= pathList.Length)
  156. mCurMonsterSpawnPointIdx = 0;
  157. if(idx < 0)
  158. {
  159. DebugHelper.LogError("mCurMonsterSpawnPointIdx 从pathList中获取的下表小于0"+ mCurMonsterSpawnPointIdx);
  160. idx = 0;
  161. }
  162. mCurrentMonsterSpawnPoint = mSpawnPointList[idx];
  163. }
  164. public void SetEffectVisible(bool bossBattle)
  165. {
  166. if(bossBattle)
  167. {
  168. if(mBattleEffect != null)
  169. {
  170. mBattleEffect.gameObject.SetActive(true);
  171. }
  172. if(mCruiseEffect != null)
  173. {
  174. mCruiseEffect.gameObject.SetActive(false);
  175. }
  176. }
  177. else
  178. {
  179. if (mBattleEffect != null)
  180. {
  181. mBattleEffect.gameObject.SetActive(false);
  182. }
  183. if (mCruiseEffect != null)
  184. {
  185. mCruiseEffect.gameObject.SetActive(true);
  186. }
  187. }
  188. }
  189. public void ChangeSkyboxMat(string matName)
  190. {
  191. if (mSceneSkyboxMatContainer == null) return;
  192. if(mSceneSkyboxMatContainer.ChangeSkyboxMat(matName))
  193. {
  194. bRestoreSkyboxDirty = true;
  195. }
  196. }
  197. private bool bRestoreSkyboxDirty = false;
  198. public void RestoreSkyboxMat()
  199. {
  200. if (mSceneSkyboxMatContainer == null) return;
  201. if (bRestoreSkyboxDirty)
  202. {
  203. mSceneSkyboxMatContainer.RestoreSkyboxMat();
  204. bRestoreSkyboxDirty = false;
  205. }
  206. }
  207. private void RegisterEvents()
  208. {
  209. EventMgr.AddEventListener<string>(ECoreEventType.EID_SHOW_SCENE_GO, OnShowSceneGo);
  210. EventMgr.AddEventListener<bool>(ECoreEventType.EID_RESTORE_SCENE, OnRestoreScene);
  211. EventMgr.AddEventListener<string>(ECoreEventType.EID_CHAGNE_SKYBOX_MATERIAL, OnChangeSkyboxMat);
  212. }
  213. private void UnregisterEvents()
  214. {
  215. EventMgr.RemoveEventListener<string>(ECoreEventType.EID_SHOW_SCENE_GO, OnShowSceneGo);
  216. EventMgr.RemoveEventListener<bool>(ECoreEventType.EID_RESTORE_SCENE, OnRestoreScene);
  217. EventMgr.RemoveEventListener<string>(ECoreEventType.EID_CHAGNE_SKYBOX_MATERIAL, OnChangeSkyboxMat);
  218. }
  219. private void OnChangeSkyboxMat(CoreEvent<string> ce)
  220. {
  221. ChangeSkyboxMat(ce.Data);
  222. }
  223. private void OnShowSceneGo(CoreEvent<string> ce)
  224. {
  225. switchGoName = ce.Data;
  226. GameObject go = mRootObjs.FindFirst(a => a.name.CompareTo(switchGoName) == 0);
  227. if (go != null)
  228. go.SetActive(true);
  229. if (SceneRootGo != null)
  230. SceneRootGo.SetActive(false);
  231. }
  232. private void OnRestoreScene(CoreEvent<bool> ce)
  233. {
  234. GameObject go = mRootObjs.FindFirst(a => a.name.CompareTo(switchGoName) == 0);
  235. if (go != null)
  236. go.SetActive(false);
  237. if (SceneRootGo != null)
  238. SceneRootGo.SetActive(true);
  239. }
  240. public override void OnSceneLoaded()
  241. {
  242. mRootObjs = mScene.GetRootGameObjects();
  243. for(int idx =0; idx < mRootObjs.Length;idx++)
  244. {
  245. if(mCruiseEffect == null)
  246. mCruiseEffect = mRootObjs[idx].GetComponentInChildren<CruiseEffect>();
  247. if (mBattleEffect == null)
  248. mBattleEffect = mRootObjs[idx].GetComponentInChildren<BossBattleEffect>();
  249. if (mCruiseEffect != null && mBattleEffect != null)
  250. break;
  251. }
  252. SceneRootGo = mRootObjs.FindFirst(a => a.name.CompareTo(scene_root_name) == 0);
  253. if(SceneRootGo!=null)
  254. {
  255. mSceneSkyboxMatContainer = SceneRootGo.GetComponentInChildren<SceneSkyboxMatContainer>();
  256. SceneRoot root = SceneRootGo.GetComponent<SceneRoot>();
  257. if(root != null)
  258. {
  259. mObjDataList = SceneRootGo.GetComponentsInChildren<SceneObjData>();
  260. if (mObjDataList != null && mObjDataList.Length > 0)
  261. {
  262. if (mTree == null)
  263. {
  264. mTree = new Tree(root.mainBound);
  265. for (int idx = 0; idx < mObjDataList.Length; idx++)
  266. {
  267. mTree.InsertObj(mObjDataList[idx]);
  268. }
  269. root.T = mTree;
  270. root.InitEnd = true;
  271. }
  272. }
  273. }
  274. }
  275. BossIntroCamGo = mRootObjs.FindFirst(a => a.name.CompareTo(bossintro_camera_go) == 0);
  276. GameObject spawnPoint = mRootObjs.FindFirst(a => a.name.CompareTo(Constants.actor_born_point) == 0);
  277. if (spawnPoint != null)
  278. {
  279. mActorBornPoint = spawnPoint.GetComponent<ReadyPoint>();
  280. }
  281. }
  282. private void LoadMapSpawnPointCfg(string sceneName)
  283. {
  284. string ta = ConfigMgr.Instance.GetXmlCfg("Born_" + sceneName);
  285. if (ta == null)
  286. {
  287. DebugHelper.LogError(sceneName + " 出生点文件不存在");
  288. return;
  289. }
  290. mSpawnPointList.Clear();
  291. mBossSpawnPointList.Clear();
  292. Mono.Xml.SecurityParser doc = new Mono.Xml.SecurityParser();
  293. try
  294. {
  295. doc.LoadXml(ta);
  296. }
  297. catch (System.Exception e)
  298. {
  299. DebugHelper.Assert(false, "exception = {0}", e.Message);
  300. return;
  301. }
  302. System.Security.SecurityElement root = doc.SelectSingleNode("SpawnPoints");
  303. if (root == null || root.Children == null) return;
  304. for (int idx = 0; idx < root.Children.Count; idx++)
  305. {
  306. var spNode = root.Children[idx] as System.Security.SecurityElement;
  307. SpawnPointCfg spCfg = new SpawnPointCfg();
  308. spCfg.LoadCfgXml(spNode);
  309. if (spCfg.PointType == SpawnPointType.Boss)
  310. {
  311. mBossSpawnPointList.Add(spCfg);
  312. }
  313. else
  314. {
  315. mSpawnPointList.Add(spCfg);
  316. }
  317. }
  318. //DebugHelper.LogError("----------------LoadMapSpawnPointCfg----------------");
  319. }
  320. private LogicBattleField CreateBattleField (int fieldId)
  321. {
  322. return new LogicBattleField (mLogicBattle, CurrentBattleInfo);
  323. }
  324. public override void SetSceneOjbVis(SceneObjData obj)
  325. {
  326. float dist = Vector3.Distance(obj.Center, mBattle.CurBattleField.TeamFighters[0].Position);
  327. if (dist < 60)
  328. {
  329. CommonUtil.SetGameObjectLayer(obj.gameObject, BattleCamera.defalutLayer);
  330. }
  331. else
  332. {
  333. CommonUtil.SetGameObjectLayer(obj.gameObject, BattleCamera.hideLayer);
  334. }
  335. }
  336. public void UpdateSceneTree(Vector3 pos)
  337. {
  338. if (mTree != null)
  339. {
  340. mTree.TriggerMove(BattleCamera.Instance.RealCamera, this);
  341. }
  342. }
  343. public override void Dispose()
  344. {
  345. if (IsDisposed)
  346. return;
  347. base.Dispose();
  348. mObjDataList = null;
  349. UnregisterEvents();
  350. mRootObjs = null;
  351. mMapItem = null;
  352. mLogicBattle = null;
  353. mBattleIdx = -1;
  354. CurrentBattleInfo = null;
  355. CurrentBattleField = null;
  356. mCurMonsterSpawnPointIdx = 0;
  357. mCurrentMonsterSpawnPoint = null;
  358. if (mActorBornPoint != null)
  359. {
  360. mActorBornPoint.Dispose();
  361. mActorBornPoint = null;
  362. }
  363. mSpawnPointList.Clear();
  364. mBossSpawnPointList.Clear();
  365. }
  366. }