123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708 |
- using UnityEngine;
- using System.IO;
- using System.Xml;
- using System.Collections;
- using System.Collections.Generic;
- public class BattleController : MonoBehaviour
- {
- public enum BattleType
- {
- Menu,
- Battle,
- Edit,
- }
- public static bool isFirstTime = true;
- public static BattleType battleType;
- public bool useEditor;
- public MapData.MapID mapId;
- public GameObject targetMarkPrefab;
- public GameObject dustPrefab;
- public Camera gameCamera;
- private BattleSession battleSession;
- private Map map;
- [System.NonSerialized]
- private Craft ctrlCraft;
- private InputController inputController;
- public bool isBattleStart = false;
- public BattleClock clock = new BattleClock();
- public MenuUI menuUI;
- public BattleUI battleUI;
- public int maxScore = 3000;
- public int blueScore;
- public int redScore;
- private bool _isReady;
- public bool isExit;
- public TeamUtil.Team winTeam;
- private float lastCheckReliveTime = -1f;
- private float checkReliveInterval = 1f;
- public float dustInterval = 5f;
- private float lastDustTime;
- public bool isReplay;
- // Use this for initialization
- void Awake ()
- {
- #if UNITY_EDITOR
- Debuger.EnableLog = true;
- #endif
- if (useEditor)
- battleType = BattleType.Edit;
- EffectUtil.ClearCache();
- BulletFactory.ClearCache();
- Camera.SetupCurrent(gameCamera);
- map = new Map();
- GameTime.timeScale = 1f;
- isReplay = ReplayManager.GetInstance ().IsReplaying ();
- if(battleType == BattleType.Battle)
- {
- battleSession = Session.GetInstance().GetBattleSession();
- battleSession.registeBattleController(this);
- mapId = map.id = battleSession.mapId;
- menuUI.gameObject.SetActive(false);
- DGAnalytics.CustomEvent (mapId.ToString ());
- }
- else if(battleType == BattleType.Edit)
- {
- map.id = mapId;
- map.isEditing = true;
- menuUI.gameObject.SetActive(false);
- }
- else if(battleType == BattleType.Menu)
- {
- map.id = MapData.MapID.Base;
- menuUI.gameObject.SetActive(true);
- Session.GetInstance ().myUserData.vipNextRound = false;
- }
- Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/Map/"+mapId.ToString()));
- TextAsset textAsset = (TextAsset)Resources.Load (Config.MAP_LOAD_FOLDER+mapId.ToString());
- if(textAsset != null)
- {
- XmlDocument xml = new XmlDocument();
- xml.LoadXml(textAsset.text);
- Debuger.Log(xml);
- map.CreateMap(xml);
- }
- else
- {
- map.CreateEmptyMap(128, 128);
- }
- inputController = GameObject.FindObjectOfType<InputController>();
- inputController.SetBattleController(this);
- GameObject targetMarkObj = Instantiate(targetMarkPrefab) as GameObject;
- targetMarkObj.GetComponent<TargetMark>().init(this);
- bool isMenu = (battleType == BattleType.Menu);
- battleUI.Init(this, isMenu);
- SetCtrlCraft(null);
- SoundUtils.gameCamera = gameCamera;
- if(battleType == BattleType.Edit)
- {
- MapEditorController mapEditor = gameObject.AddComponent<MapEditorController>();
- mapEditor.CreateAStarNodeObj(GetMap());
- }
- else if(battleType == BattleType.Menu)
- {
- EnterBase ();
- }
- else if(battleType == BattleType.Battle)
- {
- SoundManager.GetInstatnce().bgSound.Play(SoundManager.GetInstatnce().bgSound.game);
- Ready ();
- }
- lastSyncTime = GameTime.time;
- }
- void Start()
- {
- if(battleSession != null)
- LoadingPanel.Increase(0.1f);
- }
- private void EnterBase()
- {
- if(Session.GetInstance().GetBattleSession() == null)
- battleSession = Session.GetInstance().NewBattleSession ();
- else
- battleSession = Session.GetInstance().GetBattleSession();
- battleSession.registeBattleController(this);
- battleSession.GetMessageManager ().EnterBase ();
- }
- // public void OnEnterBase()
- // {
- // Ready ();
- // }
- private void Ready()
- {
- battleSession.GetMessageManager ().Ready ();
- }
- public BattleSession GetBattleSession()
- {
- return battleSession;
- }
- public int aiTakerUserId
- {
- get{
- if(battleSession != null)
- return battleSession.aiTakerUserId;
- return -1;
- }
- }
- public bool IsAITaker()
- {
- return aiTakerUserId == Session.myUserId;
- }
- public MessageManager GetMessageManager()
- {
- return battleSession.GetMessageManager();
- }
- public InputController GetInputController()
- {
- return inputController;
- }
- public Map GetMap()
- {
- return map;
- }
- public Player GetMyPlayer()
- {
- if(battleSession == null)
- return null;
- return battleSession.myPlayer;
- }
- public void ReadyToFight()
- {
- _isReady = true;
- SetCameraToStartPosition();
- if(battleType == BattleType.Menu)
- {
- menuUI.ShowNavBar();
- }
- DelayCall.Call(()=>{
- inputController.state = InputController.State.ZoomIn;
- }, 1f);
- }
- public void ReadyToAction()
- {
- if(battleType == BattleType.Battle)
- {
- ShowMission();
- }
- else if(battleType == BattleType.Menu)
- {
- // if(battleType == BattleType.Menu)
- // menuUI.Ready();
- CraftConfigData data = CraftManager.GetInstance().GetDataList()[0];
- CraftSelectionPanel.SelectCraft(battleSession.myPlayer, data.id);
- battleSession.myPlayer.OnHeroSetted.AddListener(MyHeroSetted);
- // MyHeroSetted();
- }
- }
- private void ShowMission()
- {
- MapData mapData = MapData.GetMapDataById(battleSession.mapId);
- battleUI.ShowMission(mapData.mission);
- }
- private void MyHeroSetted()
- {
- if(battleType == BattleType.Menu)
- menuUI.Ready();
- battleSession.myPlayer.OnHeroSetted.RemoveListener (MyHeroSetted);
- }
- public bool IsReady()
- {
- return _isReady;
- }
- public bool isGameOver
- {
- get
- {
- return winTeam != TeamUtil.Team.None;
- }
- }
- public void SetCameraToStartPosition()
- {
- Vector3 startPosition = map.GetStartPosition(battleSession.myPlayer);
- // inputController.SetCameraTo (startPosition);
- startPosition.x = Map.GetCenterX(Map.XToColumn(startPosition.x));
- startPosition.z = Map.GetCenterZ(Map.ZToRow(startPosition.z));
- inputController.cameraLockedTarget = new PositionTarget(startPosition);
- }
- public static void Shake(Vector3 origin, int level = 1)
- {
- if(NumberUtil.distanceVector3(origin, Camera.main.transform.position, true) <= 16f)
- {
- CameraShake cameraShake = Camera.main.gameObject.GetComponent<CameraShake>();
- if(cameraShake == null)
- cameraShake = Camera.main.gameObject.AddComponent<CameraShake> ();
- cameraShake.enabled = true;
- cameraShake.amplitude = CameraShake.BaseAmplitude * level;
- cameraShake.Start();
- }
- }
- public Capsule CreateCapsule(CraftData data)
- {
- if(isGameOver)
- return null;
- if(GetCtrlCraft() == null && GetMyPlayer() != null && data.userId == GetMyPlayer().userId)
- {
- Vector3 startPoint = data.position;
- GetInputController().cameraLockedTarget = new PositionTarget(startPoint);
- }
- GameObject capsuleObj = Instantiate(Resources.Load(Config.CAPSULE_PREFAB)) as GameObject;
- Capsule capsule = capsuleObj.GetComponent<Capsule>();
- capsule.SetCraftData(data);
- return capsule;
- }
- public Capsule CreateCapsule(int id, int swapId)
- {
- if(isGameOver)
- return null;
- Craft craft = map.GetBattleObject(id) as Craft;
- if(craft == null)
- return null;
- GameObject capsuleObj = Instantiate(Resources.Load(Config.CAPSULE_PREFAB)) as GameObject;
- capsuleObj.transform.SetParent(craft.transform);
- capsuleObj.transform.localScale = new Vector3(1f, 1f, 1f);
- capsuleObj.transform.localPosition = Vector3.zero;
- Capsule capsule = capsuleObj.GetComponent<Capsule>();
- capsule.SetSwap(craft, swapId);
- return capsule;
- }
- public Craft CreateCraft(CraftData data)
- {
- if(isGameOver)
- return null;
- Map map = GetMap();
- Craft craft = CraftFactory.CreateCraft(data, map);
- if(craft == null)
- {
- return null;
- }
- map.AddBattleObject(craft);
- battleUI.miniMap.CreateCraft(craft);
- if(data.isHero)
- {
- if(data.aiType == AI.AIType.Show)
- {
- craft.gameObject.AddComponent<ShowAI>().init(this);
- }
- else
- {
- craft.gameObject.AddComponent<NormalAI>().init(this);
- }
- if (craft.userId == Session.myUserId && ctrlCraft == null)
- SetCtrlCraft (craft);
- }
- else
- {
- SoldierAI ai = craft.gameObject.AddComponent<SoldierAI>();
- ai.init(this);
- }
- Player player = battleSession.GetPlayer(craft.userId);
- if(player != null)
- {
- Player.Hero hero = player.GetHero();
- if(data.isHero)
- {
- if(hero.craftLandingData == data)
- hero.craftLandingData = null;
- hero.SetCraft(craft);
- player.HeroSetted();
- CraftAI ai = craft.GetComponent<CraftAI>();
- if(ai != null)
- ai.SetPlayer(player);
- if(player.index == -1 && battleUI.gameObject.activeSelf)
- {
- battleUI.scoreBar.SetPlayer(player);
- }
- Power[] powerArr = craft.GetPowerManager ().GetCrystalPowers();
- for(int i=0; i<powerArr.Length; i++)
- {
- Power power = powerArr [i];
- power.SetLastLaunchTime(player.GetLastCrystalPowerLaunchTime(power.GetId()));
- }
- }
- else
- {
- hero.SetSoldier(craft);
- SoldierAI ai = craft.GetComponent<SoldierAI>();
- ai.hero = hero.GetCraft();
- ai.SetPlayer(player);
- }
- craft.SetHalo(data.haloId);
- if(data.skinId > 0)
- {
- Texture texture = SkinManager.GetInstance().GetSkinTexture(data.GetCraftId(), data.skinId);
- craft.SetSkin(data.skinId, texture);
- }
- }
- if(isReplay)
- {
- if(!(inputController.cameraLockedTarget is Craft))
- {
- inputController.LockNextCraft ();
- }
- }
- return craft;
- }
- public void SetCtrlCraft(Craft craft)
- {
- if (craft != null && craft == ctrlCraft)
- return;
- if(ctrlCraft != null)
- {
- ctrlCraft.SetCtrl (false);
- }
- if (isReplay)
- ctrlCraft = null;
- else
- ctrlCraft = craft;
-
- battleUI.powerIconContainer.Show ();
- battleUI.powerIconContainer.SetCraft(ctrlCraft);
- GetInputController().cameraLockedTarget = ctrlCraft;
- if (ctrlCraft != null) {
- ctrlCraft.SetCtrl (true);
- }
- }
- public Craft GetCtrlCraft()
- {
- return ctrlCraft;
- }
- public bool AttempUsePower(int powerId)
- {
- if(ctrlCraft != null)
- return ctrlCraft.GetPowerManager().AttempUserPower(powerId, this);
- return false;
- }
- public void coinNotEnough()
- {
- battleUI.ShowBottomTip(Language.GetStr("coinNotEnough"));
- //Instantiate(GetComponent<BattleSounds>().coinNotEnough);
- }
- public void DealDeath(int userId, int craftId, int killerId)
- {
- BattleObject battleObj = GetMap().GetBattleObject(craftId) as BattleObject;
- if(battleObj != null)
- {
- DoDeath(battleObj);
- }
- else
- {
- Debuger.LogError("onReceiveDeath::can not find craft with id("+craftId+")");
- }
- Player player = battleSession.GetPlayer(userId);
- if(player != null)
- {
- DealPlayerDeath(player, battleObj as Craft);
- player.death++;
- }
- else
- {
- Debuger.LogError("onReceiveDeath::can not find player with id("+userId+")");
- }
- Player killer = battleSession.GetPlayer(killerId);
- if(killer != null)
- {
- killer.kill++;
- if(player != null && battleObj is Craft && (battleObj as Craft).IsHero())
- {
- killer.AddCombo();
- string info = Language.GetStr("GameInfo", "deathInfo");
- info = info.Replace("%KILLER_NAME%", killer.nick);
- info = info.Replace("%DEATH_NAME%", player.nick);
- battleUI.ShowBattleInfo(info, killer.GetComboName(), battleSession.IsAlly(killer.team));
- }
- }
- else
- {
- Debuger.LogError("onReceiveDeath::can not find killer with id("+killerId+")");
- }
- }
- public void DealPlayerDeath(Player player, Craft craft)
- {
- if(craft == null)
- return;
- if (craft.IsHero ())
- {
- Player.Hero hero = player.GetHero();
- hero.Dead ();
- for (int i = 0; i < hero.soldierArr.Length; i++) {
- Craft soldier = hero.soldierArr [i];
- if (soldier != null) {
- DoDeath (soldier);
- }
- }
- }
- else
- {
- player.RemoveSoldier (craft);
- }
- DoDeath(craft);
- }
- public void DoDeath(BattleObject battleObj)
- {
- if(battleObj == ctrlCraft)
- {
- SetCtrlCraft(null);
- }
- Shake (battleObj.position);
- battleObj.Dead();
- }
- public void DealRelive()
- {
- if (isReplay)
- return;
- if(GameTime.time - lastCheckReliveTime > checkReliveInterval)
- {
- lastCheckReliveTime = GameTime.time;
- }
- else
- {
- return;
- }
- if(isGameOver || !_isReady || isExit)
- {
- return;
- }
- if(battleType != BattleType.Battle)
- {
- return;
- }
- List<Player> playerList = battleSession.GetPlayerList();
- for(int i=0; i<playerList.Count; i++)
- {
- Player player = playerList[i];
- Player.Hero hero = player.GetHero();
- if(hero.IsSelected())
- continue;
- float reliveCd = Mathf.Ceil(hero.HeroReliveCD());
- TopHeadBar headBar = battleUI.scoreBar.GetHeadBar(player);
- if(headBar != null)
- {
- headBar.timeTxt.text = reliveCd == 0 ? "<color=#FF9900ff>?</color>" : reliveCd.ToString();
- }
- if(reliveCd == 0)
- {
- if (player.isMe)
- {
- if(!CraftSelectionPanel.IsShown ())
- CraftSelectionPanel.Show ();
- }
- else if (IsAITaker() && player.IsAI())
- {
- int craftId = CraftManager.GetInstance().GetRandomCraftId(player);
- // craftId = 1;
- CraftSelectionPanel.SelectCraft(player, craftId);
- }
- }
- }
- }
- private float lastSyncTime;
- private void CheckSimpleSync()
- {
- if (battleType != BattleType.Battle)
- return;
- if (isReplay)
- return;
- if(isGameOver || !_isReady || isExit)
- return;
- if(GameTime.time - lastSyncTime > 4f)
- {
- lastSyncTime = GameTime.time;
- }
- else
- {
- return;
- }
- GetMessageManager ().SimpleSync ();
- }
- public void GameOver(TeamUtil.Team team)
- {
- if(isGameOver)
- return;
- winTeam = team;
- if (isReplay) {
-
- } else {
- battleUI.ShowResultPanel ();
- }
- }
- void Update()
- {
- if(Input.GetKeyDown(KeyCode.Escape))
- {
- if(!isGameOver && GameObject.FindObjectsOfType<AlertPanel>().Length == 0)
- {
- if(battleType == BattleType.Battle)
- AlertPanel.Show("", Language.GetStr("BattleUI", "exitTip"), AlertPanel.YES|AlertPanel.NO, OnExitBattle);
- else if(battleType == BattleType.Menu)
- AlertPanel.Show("", Language.GetStr("BattleUI", "quitTip"), AlertPanel.YES|AlertPanel.NO, OnExitBattle);
- }
- }
- }
- private void OnExitBattle(AlertCloseEvent evt)
- {
- if(evt.detail == AlertPanel.OK || evt.detail == AlertPanel.YES)
- {
- if(battleType == BattleType.Battle)
- battleSession.GetMessageManager().ExitBattle();
- else if(battleType == BattleType.Menu)
- Application.Quit();
- }
- }
- private void CheckDust()
- {
- if(GameTime.time - lastDustTime > dustInterval)
- {
- lastDustTime = GameTime.time;
- CreateDust();
- }
- }
- private void CreateDust()
- {
- Dust dust = Dust.CreateDust(dustPrefab);
- Vector3 pos = gameCamera.transform.position;
- pos.x += Random.Range(-6f, 6f);
- pos.y = Random.Range(2f, 3f);
- pos.z += Random.Range(-1f, 9f);
- dust.transform.position = pos;
- dust.Show();
- }
- void FixedUpdate()
- {
- if(battleSession == null)
- return;
- DealRelive();
- map.PathSearchDequeue();
- CheckDust();
- CheckSimpleSync ();
- }
- void OnApplicationPause(bool pauseStatus)
- {
- Debuger.Log("OnApplicationPause : "+pauseStatus);
- }
- }
|