BattleController.cs 14 KB

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