BattleController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. using UnityEngine;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. public class BattleController : MonoBehaviour
  7. {
  8. public enum BattleType
  9. {
  10. Menu,
  11. Battle,
  12. Edit,
  13. }
  14. public static bool isFirstTime = true;
  15. public static BattleType battleType;
  16. public bool useEditor;
  17. public MapData.MapID mapId;
  18. public GameObject targetMarkPrefab;
  19. public GameObject dustPrefab;
  20. public Camera gameCamera;
  21. private BattleSession battleSession;
  22. private Map map;
  23. [System.NonSerialized]
  24. private Craft ctrlCraft;
  25. private InputController inputController;
  26. public bool isBattleStart = false;
  27. public BattleClock clock = new BattleClock();
  28. public MenuUI menuUI;
  29. public BattleUI battleUI;
  30. public int maxScore = 3000;
  31. public int blueScore;
  32. public int redScore;
  33. private bool _isReady;
  34. public bool isExit;
  35. public TeamUtil.Team winTeam;
  36. private float lastCheckReliveTime = -1f;
  37. private float checkReliveInterval = 1f;
  38. public float dustInterval = 5f;
  39. private float lastDustTime;
  40. public bool isReplay;
  41. // Use this for initialization
  42. void Awake ()
  43. {
  44. #if UNITY_EDITOR
  45. Debuger.EnableLog = true;
  46. #endif
  47. if (useEditor)
  48. battleType = BattleType.Edit;
  49. EffectUtil.ClearCache();
  50. BulletFactory.ClearCache();
  51. Camera.SetupCurrent(gameCamera);
  52. map = new Map();
  53. GameTime.timeScale = 1f;
  54. isReplay = ReplayManager.GetInstance ().IsReplaying ();
  55. if(battleType == BattleType.Battle)
  56. {
  57. battleSession = Session.GetInstance().GetBattleSession();
  58. battleSession.registeBattleController(this);
  59. mapId = map.id = battleSession.mapId;
  60. menuUI.gameObject.SetActive(false);
  61. DGAnalytics.CustomEvent (mapId.ToString ());
  62. }
  63. else if(battleType == BattleType.Edit)
  64. {
  65. map.id = mapId;
  66. map.isEditing = true;
  67. menuUI.gameObject.SetActive(false);
  68. }
  69. else if(battleType == BattleType.Menu)
  70. {
  71. map.id = MapData.MapID.Base;
  72. menuUI.gameObject.SetActive(true);
  73. Session.GetInstance ().myUserData.vipNextRound = false;
  74. }
  75. Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/Map/"+mapId.ToString()));
  76. TextAsset textAsset = (TextAsset)Resources.Load (Config.MAP_LOAD_FOLDER+mapId.ToString());
  77. if(textAsset != null)
  78. {
  79. XmlDocument xml = new XmlDocument();
  80. xml.LoadXml(textAsset.text);
  81. Debuger.Log(xml);
  82. map.CreateMap(xml);
  83. }
  84. else
  85. {
  86. map.CreateEmptyMap(128, 128);
  87. }
  88. inputController = GameObject.FindObjectOfType<InputController>();
  89. inputController.SetBattleController(this);
  90. GameObject targetMarkObj = Instantiate(targetMarkPrefab) as GameObject;
  91. targetMarkObj.GetComponent<TargetMark>().init(this);
  92. bool isMenu = (battleType == BattleType.Menu);
  93. battleUI.Init(this, isMenu);
  94. SetCtrlCraft(null);
  95. SoundUtils.gameCamera = gameCamera;
  96. if(battleType == BattleType.Edit)
  97. {
  98. MapEditorController mapEditor = gameObject.AddComponent<MapEditorController>();
  99. mapEditor.CreateAStarNodeObj(GetMap());
  100. }
  101. else if(battleType == BattleType.Menu)
  102. {
  103. EnterBase ();
  104. }
  105. else if(battleType == BattleType.Battle)
  106. {
  107. SoundManager.GetInstatnce().bgSound.Play(SoundManager.GetInstatnce().bgSound.game);
  108. Ready ();
  109. }
  110. lastSyncTime = GameTime.time;
  111. }
  112. void Start()
  113. {
  114. if(battleSession != null)
  115. LoadingPanel.Increase(0.1f);
  116. }
  117. private void EnterBase()
  118. {
  119. if(Session.GetInstance().GetBattleSession() == null)
  120. battleSession = Session.GetInstance().NewBattleSession ();
  121. else
  122. battleSession = Session.GetInstance().GetBattleSession();
  123. battleSession.registeBattleController(this);
  124. battleSession.GetMessageManager ().EnterBase ();
  125. }
  126. // public void OnEnterBase()
  127. // {
  128. // Ready ();
  129. // }
  130. private void Ready()
  131. {
  132. battleSession.GetMessageManager ().Ready ();
  133. }
  134. public BattleSession GetBattleSession()
  135. {
  136. return battleSession;
  137. }
  138. public int aiTakerUserId
  139. {
  140. get{
  141. if(battleSession != null)
  142. return battleSession.aiTakerUserId;
  143. return -1;
  144. }
  145. }
  146. public bool IsAITaker()
  147. {
  148. return aiTakerUserId == Session.myUserId;
  149. }
  150. public MessageManager GetMessageManager()
  151. {
  152. return battleSession.GetMessageManager();
  153. }
  154. public InputController GetInputController()
  155. {
  156. return inputController;
  157. }
  158. public Map GetMap()
  159. {
  160. return map;
  161. }
  162. public Player GetMyPlayer()
  163. {
  164. if(battleSession == null)
  165. return null;
  166. return battleSession.myPlayer;
  167. }
  168. public void ReadyToFight()
  169. {
  170. _isReady = true;
  171. SetCameraToStartPosition();
  172. if(battleType == BattleType.Menu)
  173. {
  174. menuUI.ShowNavBar();
  175. }
  176. DelayCall.Call(()=>{
  177. inputController.state = InputController.State.ZoomIn;
  178. }, 1f);
  179. }
  180. public void ReadyToAction()
  181. {
  182. if(battleType == BattleType.Battle)
  183. {
  184. ShowMission();
  185. }
  186. else if(battleType == BattleType.Menu)
  187. {
  188. // if(battleType == BattleType.Menu)
  189. // menuUI.Ready();
  190. CraftConfigData data = CraftManager.GetInstance().GetDataList()[0];
  191. CraftSelectionPanel.SelectCraft(battleSession.myPlayer, data.id);
  192. battleSession.myPlayer.OnHeroSetted.AddListener(MyHeroSetted);
  193. // MyHeroSetted();
  194. }
  195. }
  196. private void ShowMission()
  197. {
  198. MapData mapData = MapData.GetMapDataById(battleSession.mapId);
  199. battleUI.ShowMission(mapData.mission);
  200. }
  201. private void MyHeroSetted()
  202. {
  203. if(battleType == BattleType.Menu)
  204. menuUI.Ready();
  205. battleSession.myPlayer.OnHeroSetted.RemoveListener (MyHeroSetted);
  206. }
  207. public bool IsReady()
  208. {
  209. return _isReady;
  210. }
  211. public bool isGameOver
  212. {
  213. get
  214. {
  215. return winTeam != TeamUtil.Team.None;
  216. }
  217. }
  218. public void SetCameraToStartPosition()
  219. {
  220. Vector3 startPosition = map.GetStartPosition(battleSession.myPlayer);
  221. // inputController.SetCameraTo (startPosition);
  222. startPosition.x = Map.GetCenterX(Map.XToColumn(startPosition.x));
  223. startPosition.z = Map.GetCenterZ(Map.ZToRow(startPosition.z));
  224. inputController.cameraLockedTarget = new PositionTarget(startPosition);
  225. }
  226. public static void Shake(Vector3 origin, int level = 1)
  227. {
  228. if(NumberUtil.distanceVector3(origin, Camera.main.transform.position, true) <= 16f)
  229. {
  230. CameraShake cameraShake = Camera.main.gameObject.GetComponent<CameraShake>();
  231. if(cameraShake == null)
  232. cameraShake = Camera.main.gameObject.AddComponent<CameraShake> ();
  233. cameraShake.enabled = true;
  234. cameraShake.amplitude = CameraShake.BaseAmplitude * level;
  235. cameraShake.Start();
  236. }
  237. }
  238. public Capsule CreateCapsule(CraftData data)
  239. {
  240. if(isGameOver)
  241. return null;
  242. if(GetCtrlCraft() == null && GetMyPlayer() != null && data.userId == GetMyPlayer().userId)
  243. {
  244. Vector3 startPoint = data.position;
  245. GetInputController().cameraLockedTarget = new PositionTarget(startPoint);
  246. }
  247. GameObject capsuleObj = Instantiate(Resources.Load(Config.CAPSULE_PREFAB)) as GameObject;
  248. Capsule capsule = capsuleObj.GetComponent<Capsule>();
  249. capsule.SetCraftData(data);
  250. return capsule;
  251. }
  252. public Capsule CreateCapsule(int id, int swapId)
  253. {
  254. if(isGameOver)
  255. return null;
  256. Craft craft = map.GetBattleObject(id) as Craft;
  257. if(craft == null)
  258. return null;
  259. GameObject capsuleObj = Instantiate(Resources.Load(Config.CAPSULE_PREFAB)) as GameObject;
  260. capsuleObj.transform.SetParent(craft.transform);
  261. capsuleObj.transform.localScale = new Vector3(1f, 1f, 1f);
  262. capsuleObj.transform.localPosition = Vector3.zero;
  263. Capsule capsule = capsuleObj.GetComponent<Capsule>();
  264. capsule.SetSwap(craft, swapId);
  265. return capsule;
  266. }
  267. public Craft CreateCraft(CraftData data)
  268. {
  269. if(isGameOver)
  270. return null;
  271. Map map = GetMap();
  272. Craft craft = CraftFactory.CreateCraft(data, map);
  273. if(craft == null)
  274. {
  275. return null;
  276. }
  277. map.AddBattleObject(craft);
  278. battleUI.miniMap.CreateCraft(craft);
  279. if(data.isHero)
  280. {
  281. if(data.aiType == AI.AIType.Show)
  282. {
  283. craft.gameObject.AddComponent<ShowAI>().init(this);
  284. }
  285. else
  286. {
  287. craft.gameObject.AddComponent<NormalAI>().init(this);
  288. }
  289. if (craft.userId == Session.myUserId && ctrlCraft == null)
  290. SetCtrlCraft (craft);
  291. }
  292. else
  293. {
  294. SoldierAI ai = craft.gameObject.AddComponent<SoldierAI>();
  295. ai.init(this);
  296. }
  297. Player player = battleSession.GetPlayer(craft.userId);
  298. if(player != null)
  299. {
  300. Player.Hero hero = player.GetHero();
  301. if(data.isHero)
  302. {
  303. if(hero.craftLandingData == data)
  304. hero.craftLandingData = null;
  305. hero.SetCraft(craft);
  306. player.HeroSetted();
  307. CraftAI ai = craft.GetComponent<CraftAI>();
  308. if(ai != null)
  309. ai.SetPlayer(player);
  310. if(player.index == -1 && battleUI.gameObject.activeSelf)
  311. {
  312. battleUI.scoreBar.SetPlayer(player);
  313. }
  314. Power[] powerArr = craft.GetPowerManager ().GetCrystalPowers();
  315. for(int i=0; i<powerArr.Length; i++)
  316. {
  317. Power power = powerArr [i];
  318. power.SetLastLaunchTime(player.GetLastCrystalPowerLaunchTime(power.GetId()));
  319. }
  320. }
  321. else
  322. {
  323. hero.SetSoldier(craft);
  324. SoldierAI ai = craft.GetComponent<SoldierAI>();
  325. ai.hero = hero.GetCraft();
  326. ai.SetPlayer(player);
  327. }
  328. craft.SetHalo(data.haloId);
  329. if(data.skinId > 0)
  330. {
  331. Texture texture = SkinManager.GetInstance().GetSkinTexture(data.GetCraftId(), data.skinId);
  332. craft.SetSkin(data.skinId, texture);
  333. }
  334. }
  335. if(isReplay)
  336. {
  337. if(!(inputController.cameraLockedTarget is Craft))
  338. {
  339. inputController.LockNextCraft ();
  340. }
  341. }
  342. return craft;
  343. }
  344. public void SetCtrlCraft(Craft craft)
  345. {
  346. if (craft != null && craft == ctrlCraft)
  347. return;
  348. if(ctrlCraft != null)
  349. {
  350. ctrlCraft.SetCtrl (false);
  351. }
  352. if (isReplay)
  353. ctrlCraft = null;
  354. else
  355. ctrlCraft = craft;
  356. battleUI.powerIconContainer.Show ();
  357. battleUI.powerIconContainer.SetCraft(ctrlCraft);
  358. GetInputController().cameraLockedTarget = ctrlCraft;
  359. if (ctrlCraft != null) {
  360. ctrlCraft.SetCtrl (true);
  361. }
  362. }
  363. public Craft GetCtrlCraft()
  364. {
  365. return ctrlCraft;
  366. }
  367. public bool AttempUsePower(int powerId)
  368. {
  369. if(ctrlCraft != null)
  370. return ctrlCraft.GetPowerManager().AttempUserPower(powerId, this);
  371. return false;
  372. }
  373. public void coinNotEnough()
  374. {
  375. battleUI.ShowBottomTip(Language.GetStr("coinNotEnough"));
  376. //Instantiate(GetComponent<BattleSounds>().coinNotEnough);
  377. }
  378. public void DealDeath(int userId, int craftId, int killerId)
  379. {
  380. BattleObject battleObj = GetMap().GetBattleObject(craftId) as BattleObject;
  381. if(battleObj != null)
  382. {
  383. DoDeath(battleObj);
  384. }
  385. else
  386. {
  387. Debuger.LogError("onReceiveDeath::can not find craft with id("+craftId+")");
  388. }
  389. Player player = battleSession.GetPlayer(userId);
  390. if(player != null)
  391. {
  392. DealPlayerDeath(player, battleObj as Craft);
  393. player.death++;
  394. }
  395. else
  396. {
  397. Debuger.LogError("onReceiveDeath::can not find player with id("+userId+")");
  398. }
  399. Player killer = battleSession.GetPlayer(killerId);
  400. if(killer != null)
  401. {
  402. killer.kill++;
  403. if(player != null && battleObj is Craft && (battleObj as Craft).IsHero())
  404. {
  405. killer.AddCombo();
  406. string info = Language.GetStr("GameInfo", "deathInfo");
  407. info = info.Replace("%KILLER_NAME%", killer.nick);
  408. info = info.Replace("%DEATH_NAME%", player.nick);
  409. battleUI.ShowBattleInfo(info, killer.GetComboName(), battleSession.IsAlly(killer.team));
  410. }
  411. }
  412. else
  413. {
  414. Debuger.LogError("onReceiveDeath::can not find killer with id("+killerId+")");
  415. }
  416. }
  417. public void DealPlayerDeath(Player player, Craft craft)
  418. {
  419. if(craft == null)
  420. return;
  421. if (craft.IsHero ())
  422. {
  423. Player.Hero hero = player.GetHero();
  424. hero.Dead ();
  425. for (int i = 0; i < hero.soldierArr.Length; i++) {
  426. Craft soldier = hero.soldierArr [i];
  427. if (soldier != null) {
  428. DoDeath (soldier);
  429. }
  430. }
  431. }
  432. else
  433. {
  434. player.RemoveSoldier (craft);
  435. }
  436. DoDeath(craft);
  437. }
  438. public void DoDeath(BattleObject battleObj)
  439. {
  440. if(battleObj == ctrlCraft)
  441. {
  442. SetCtrlCraft(null);
  443. }
  444. Shake (battleObj.position);
  445. battleObj.Dead();
  446. }
  447. public void DealRelive()
  448. {
  449. if (isReplay)
  450. return;
  451. if(GameTime.time - lastCheckReliveTime > checkReliveInterval)
  452. {
  453. lastCheckReliveTime = GameTime.time;
  454. }
  455. else
  456. {
  457. return;
  458. }
  459. if(isGameOver || !_isReady || isExit)
  460. {
  461. return;
  462. }
  463. if(battleType != BattleType.Battle)
  464. {
  465. return;
  466. }
  467. List<Player> playerList = battleSession.GetPlayerList();
  468. for(int i=0; i<playerList.Count; i++)
  469. {
  470. Player player = playerList[i];
  471. Player.Hero hero = player.GetHero();
  472. if(hero.IsSelected())
  473. continue;
  474. float reliveCd = Mathf.Ceil(hero.HeroReliveCD());
  475. TopHeadBar headBar = battleUI.scoreBar.GetHeadBar(player);
  476. if(headBar != null)
  477. {
  478. headBar.timeTxt.text = reliveCd == 0 ? "<color=#FF9900ff>?</color>" : reliveCd.ToString();
  479. }
  480. if(reliveCd == 0)
  481. {
  482. if (player.isMe)
  483. {
  484. if(!CraftSelectionPanel.IsShown ())
  485. CraftSelectionPanel.Show ();
  486. }
  487. else if (IsAITaker() && player.IsAI())
  488. {
  489. int craftId = CraftManager.GetInstance().GetRandomCraftId(player);
  490. // craftId = 1;
  491. CraftSelectionPanel.SelectCraft(player, craftId);
  492. }
  493. }
  494. }
  495. }
  496. private float lastSyncTime;
  497. private void CheckSimpleSync()
  498. {
  499. if (battleType != BattleType.Battle)
  500. return;
  501. if (isReplay)
  502. return;
  503. if(isGameOver || !_isReady || isExit)
  504. return;
  505. if(GameTime.time - lastSyncTime > 4f)
  506. {
  507. lastSyncTime = GameTime.time;
  508. }
  509. else
  510. {
  511. return;
  512. }
  513. GetMessageManager ().SimpleSync ();
  514. }
  515. public void GameOver(TeamUtil.Team team)
  516. {
  517. if(isGameOver)
  518. return;
  519. winTeam = team;
  520. if (isReplay) {
  521. } else {
  522. battleUI.ShowResultPanel ();
  523. }
  524. }
  525. void Update()
  526. {
  527. if(Input.GetKeyDown(KeyCode.Escape))
  528. {
  529. if(!isGameOver && GameObject.FindObjectsOfType<AlertPanel>().Length == 0)
  530. {
  531. if(battleType == BattleType.Battle)
  532. AlertPanel.Show("", Language.GetStr("BattleUI", "exitTip"), AlertPanel.YES|AlertPanel.NO, OnExitBattle);
  533. else if(battleType == BattleType.Menu)
  534. AlertPanel.Show("", Language.GetStr("BattleUI", "quitTip"), AlertPanel.YES|AlertPanel.NO, OnExitBattle);
  535. }
  536. }
  537. }
  538. private void OnExitBattle(AlertCloseEvent evt)
  539. {
  540. if(evt.detail == AlertPanel.OK || evt.detail == AlertPanel.YES)
  541. {
  542. if(battleType == BattleType.Battle)
  543. battleSession.GetMessageManager().ExitBattle();
  544. else if(battleType == BattleType.Menu)
  545. Application.Quit();
  546. }
  547. }
  548. private void CheckDust()
  549. {
  550. if(GameTime.time - lastDustTime > dustInterval)
  551. {
  552. lastDustTime = GameTime.time;
  553. CreateDust();
  554. }
  555. }
  556. private void CreateDust()
  557. {
  558. Dust dust = Dust.CreateDust(dustPrefab);
  559. Vector3 pos = gameCamera.transform.position;
  560. pos.x += Random.Range(-6f, 6f);
  561. pos.y = Random.Range(2f, 3f);
  562. pos.z += Random.Range(-1f, 9f);
  563. dust.transform.position = pos;
  564. dust.Show();
  565. }
  566. void FixedUpdate()
  567. {
  568. if(battleSession == null)
  569. return;
  570. DealRelive();
  571. map.PathSearchDequeue();
  572. CheckDust();
  573. CheckSimpleSync ();
  574. }
  575. void OnApplicationPause(bool pauseStatus)
  576. {
  577. Debuger.Log("OnApplicationPause : "+pauseStatus);
  578. }
  579. }