using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; using URandom = UnityEngine.Random; public class GameLogic : MonoBehaviour { [SerializeField] Camera mGameCam; [SerializeField] Vector3 mBattlePos; [SerializeField] Vector3 mBattleRot; [SerializeField] Transform mCtnStage; [SerializeField] Transform mTsfWinFx; [SerializeField] GameObject mPfbWinFx; [SerializeField] BlockManager mPlayerBlockMgr; [SerializeField] BlockManager mEnemyBlockMgr; [SerializeField] GroupManager mPlayerGroupMgr; [SerializeField] GroupManager mEnemyGroupMgr; [SerializeField] GameObject[] mGuideTips; private PanelMain mUIMain; private PanelGame mUIGame; private bool mIsOver; private TimerUnit mOverTimer; private bool mIsWin; private void Awake() { MMOModule.Instance.OnEnter += OnEnterMMO; MMOModule.Instance.OnExit += OnExitMMO; } private void OnDestroy() { if (mOverTimer != null) { mOverTimer.Destroy(); } MMOModule.Instance.OnEnter -= OnEnterMMO; MMOModule.Instance.OnExit -= OnExitMMO; } private void OnEnterMMO() { if (PlayerData.Instance.CheckGuide(0) && !mIsOver) { OnMainFocus(false); } } private void OnExitMMO() { if (PlayerData.Instance.CheckGuide(0) && !mIsOver) { OnMainFocus(true); } } private void Start() { AdjustCamera(); //debug GameManager.Instance.CurrentPlayerBlockMgr = mPlayerBlockMgr; GameManager.Instance.CurrentEnemyBlockMgr = mEnemyBlockMgr; GameManager.Instance.PlayerGroupMgr = mPlayerGroupMgr; GameManager.Instance.EnemyGroupMgr = mEnemyGroupMgr; GameManager.Instance.CurrentGameCam = mGameCam; int tStageID = ((PlayerData.Instance.CurrentLevel - 1) / 10) % GameConfig.Instance.SceneCount + 1; Instantiate(ResourceManager.Instance.LoadRes(Const.Path.GetStage(tStageID)), mCtnStage); mPlayerGroupMgr.DelSoldierClear = Lose; mEnemyGroupMgr.DelSoldierClear = Win; mPlayerBlockMgr.CreateBlocksByData(PlayerData.Instance.ArmyStr); GenerteLevel(); if (TKGSDKManager.Instance.GetConfigBool(TKGParamKey.UseBoard)) { FloorManager.Instance.SetFloor(PlayerData.Instance.CurrentFloorID); } if (TKGSDKManager.Instance.GetConfigBool(TKGParamKey.UsePet)) { PetManager.Instance.SetPet(PlayerData.Instance.CurrentPetID); } if (PlayerData.Instance.CurrentLevel % TKGSDKManager.Instance.GetConfigInt(TKGParamKey.ExtraLevelDelta) == 0) { ExtraManager.Instance.RestartAvatar(); } mOverTimer = TimerManager.Instance.CreateTimerUnit(); mPlayerBlockMgr.EnableInteraction(false); MMOModule.Instance.StartGame(CheckGuide, PlayerData.Instance.IsMMOUser, PlayerData.Instance.Diamond, PlayerData.Instance.CurrentLevel); UIManager.Instance.OpenUI().RefreshResIcon(MMOModule.Instance.IsAccessible); mUIMain = UIManager.Instance.OpenUI(); mUIMain.DelFocus = OnMainFocus; mUIMain.DelStart = MoveBattleCam; mUIMain.DelHuman = mPlayerBlockMgr.CreateBaseHumanBlock; mUIMain.DelMonster = mPlayerBlockMgr.CreateBaseMonsterBlock; mUIMain.DelAutoMergeAdd = mPlayerBlockMgr.CheckAutoMerge; } private void OnMainFocus(bool pFocus) { mPlayerBlockMgr.EnableInteraction(pFocus); ExtraManager.Instance.EnableInteraction(pFocus); } private void AdjustCamera() { Camera tCam = Camera.main; float tScreenRatio = Screen.height / (float)Screen.width; if (tScreenRatio > 2)//39 : 18 { tCam.fieldOfView = 70; } else if (tScreenRatio > 1.65f)//16 : 9 { tCam.fieldOfView = 60; } else if (tScreenRatio > 1.55f)//16 : 10 { tCam.fieldOfView = 60; } else if (tScreenRatio > 1.45f)//3 : 2 { tCam.fieldOfView = 55; } else//4 : 3 and smaller { tCam.fieldOfView = 55; } } private void CheckGuide() { mPlayerBlockMgr.EnableInteraction(true); if (!PlayerData.Instance.CheckGuide(0)) { mPlayerBlockMgr.EnableInteraction(false); GuideManager.Instance.StartGuide(0, new Dictionary() { { 0, () => { mPlayerBlockMgr.CreateBlock(13, BlockType.Human, 1); AudioManager.Instance.PlaySound(AudioClipType.BuyUnit); PlayerData.Instance.BuyHuman(); mUIMain.RefreshButtons(); mPlayerBlockMgr.EnableInteraction(true); mPlayerBlockMgr.SetForbidden(true, new List(){12, 13}); ShowGuideTip(0); } }, { 1, () => { ShowGuideTip(-1); mPlayerBlockMgr.EnableInteraction(false); mPlayerBlockMgr.SetForbidden(false); PlayerData.Instance.FinishGuide(0); } }, { 2, () => { UIManager.Instance.CloseUI(); mPlayerBlockMgr.EnableInteraction(false);} }, { 3, MoveBattleCam }}); } //if (!PlayerData.Instance.CheckGuide(0)) //{ // mPlayerBlockMgr.EnableInteraction(false); // GuideManager.Instance.StartGuide(0, new Dictionary() { { 0, MoveBattleCam } }); //} //else if (!PlayerData.Instance.CheckGuide(1)) //{ // mPlayerBlockMgr.EnableInteraction(false); // GuideManager.Instance.StartGuide(1, new Dictionary() { // { 0, () => { mPlayerBlockMgr.CreateBlock(11, BlockType.Human, 1); AudioManager.Instance.PlaySound(AudioClipType.BuyUnit); PlayerData.Instance.BuyHuman(); mUIMain.RefreshButtons(); mPlayerBlockMgr.EnableInteraction(true); mPlayerBlockMgr.SetForbidden(true, new List(){2, 12}); ShowGuideTip(0); } }, // { 1, () => { ShowGuideTip(-1); mPlayerBlockMgr.EnableInteraction(false); mPlayerBlockMgr.SetForbidden(false); } }, // { 2, MoveBattleCam }}); //} //else if (!PlayerData.Instance.CheckGuide(2)) //{ // mPlayerBlockMgr.EnableInteraction(false); // GuideManager.Instance.StartGuide(2, new Dictionary() { // { 0, () => { mPlayerBlockMgr.CreateBlock(3, BlockType.Monster, 1); AudioManager.Instance.PlaySound(AudioClipType.BuyUnit); PlayerData.Instance.BuyMonster(); mUIMain.RefreshButtons(); mPlayerBlockMgr.EnableInteraction(true); mPlayerBlockMgr.SetForbidden(true, new List(){2, 3}); ShowGuideTip(1); } }, // { 1, () => { ShowGuideTip(-1); mPlayerBlockMgr.SetForbidden(false);} } }); //} mOverTimer.StartTimer(ShowBanner, 1f); } private void ShowBanner() { TKGSDKManager.Instance.ShowBanner(Touka.TKGBannerAlign.BannerCenterBottomAlign); } #region Level private void GenerteLevel() { if (PlayerData.Instance.CurrentLevel <= TableLevel.Instance.Count) { if (GameConfig.Instance.IsAutoLevel) { string tLevelStr = PlayerData.Instance.GetAutoLevel(); if (string.IsNullOrEmpty(tLevelStr)) { CreateAutoLevel(); } else { mEnemyBlockMgr.CreateBlocksByData(tLevelStr); } } else { string tOnlineLevelJson = TKGSDKManager.Instance.GetConfigStr(TKGParamKey.LevelList); LevelOnlineData tOnlineData = string.IsNullOrEmpty(tOnlineLevelJson) ? null : LevelOnlineData.JsonToData(tOnlineLevelJson); if (tOnlineData != null) { TKGSDKManager.Instance.LogEvent(TKGParamKey.LevelList.ToString() + "_" + tOnlineData.Version); } string tFormation = ""; if (tOnlineData != null && tOnlineData.Formations != null && PlayerData.Instance.CurrentLevel <= tOnlineData.Formations.Count) { tFormation = tOnlineData.Formations[PlayerData.Instance.CurrentLevel - 1]; } else if (PlayerData.Instance.CurrentLevelData != null) { tFormation = PlayerData.Instance.CurrentLevelData.Formation; } if (!string.IsNullOrEmpty(tFormation)) { mEnemyBlockMgr.CreateBlocksByData(tFormation); } else { RandomEnemy(); } } } else { RandomEnemy(); } if (PlayerData.Instance.CurrentLevelData != null && PlayerData.Instance.CurrentLevelData.IsBoss) { mEnemyBlockMgr.SetAsBoss(); } } private void CreateAutoLevel() { int tMaxLv = PlayerData.Instance.CurrentLevelData.MaxLevel; int tMinLv = Mathf.Max(1, tMaxLv / 2); int tTotalUnitCount = PlayerData.Instance.CurrentLevelData.UnitNum; if (PlayerData.Instance.CurrentLevel >= 4 && PlayerData.Instance.CurrentLevel <= 20) { tTotalUnitCount -= 1; } else if (PlayerData.Instance.CurrentLevel > 20) { tTotalUnitCount -= 2; } List tLevelList = new List(); int tLevel = tMaxLv; int tCostUnitCount = 0; while (true) { tCostUnitCount = (int)Mathf.Pow(2, tLevel - 1); if (tTotalUnitCount >= tCostUnitCount) { tTotalUnitCount -= tCostUnitCount; tLevelList.Add(tLevel); //Debug.Log("Level :" + tLevel); //Debug.Log("Left Unit Count :" + tTotalUnitCount); } else { tLevel--; if (tLevel < tMinLv) { break; } } if (tTotalUnitCount <= 0 || tLevelList.Count >= 15) break; } BlockType tBType = BlockType.Monster; List tNearIndexes = new List() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; List tFarIndexes = new List() { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }; int tPosIndex = 0; for (int i = 0; i < tLevelList.Count; i++) { if (tBType == BlockType.Monster) { tPosIndex = tNearIndexes[URandom.Range(0, tNearIndexes.Count)]; tNearIndexes.Remove(tPosIndex); if (tFarIndexes.Contains(tPosIndex)) { tFarIndexes.Remove(tPosIndex); } } else if(tBType == BlockType.Human) { tPosIndex = tFarIndexes[URandom.Range(0, tFarIndexes.Count)]; tFarIndexes.Remove(tPosIndex); if (tNearIndexes.Contains(tPosIndex)) { tNearIndexes.Remove(tPosIndex); } } mEnemyBlockMgr.CreateBlock(tPosIndex, tBType, tLevelList[i]); tBType = tBType == BlockType.Monster ? BlockType.Human : BlockType.Monster; } PlayerData.Instance.SaveAutoLevel(mEnemyBlockMgr.GetFormationStr()); } private void RandomEnemy() { List tPosIndexes = new List() { 0, 1, 2, 3, 4 }; int tCount = URandom.Range(1, 5); for (int i = 0; i < tCount; i++) { int tPosIndex = tPosIndexes[URandom.Range(0, tPosIndexes.Count)]; tPosIndexes.Remove(tPosIndex); mEnemyBlockMgr.CreateBlock(tPosIndex, BlockType.Monster, URandom.Range(6, 11)); } tPosIndexes = new List() { 5, 6, 7, 8, 9 }; tCount = URandom.Range(1, 5); for (int i = 0; i < tCount; i++) { int tPosIndex = tPosIndexes[URandom.Range(0, tPosIndexes.Count)]; tPosIndexes.Remove(tPosIndex); mEnemyBlockMgr.CreateBlock(tPosIndex, (BlockType)URandom.Range(0, 2), URandom.Range(7, 10)); } tPosIndexes = new List() { 10, 11, 12, 13, 14 }; tCount = URandom.Range(1, 5); for (int i = 0; i < tCount; i++) { int tPosIndex = tPosIndexes[URandom.Range(0, tPosIndexes.Count)]; tPosIndexes.Remove(tPosIndex); mEnemyBlockMgr.CreateBlock(tPosIndex, BlockType.Human, URandom.Range(7, 11)); } } #endregion private void MoveBattleCam() { if (string.IsNullOrEmpty(PlayerData.Instance.ArmyStr)) { UIUtils.ShowTips(LanguageConfig.Instance.GetText("ArmyEmpty")); return; } mUIMain.Close(); VibrateManager.Instance.Vibrate(VibrateType.Medium); mPlayerBlockMgr.EnableInteraction(false); mPlayerBlockMgr.ShowFloor(false); FloorManager.Instance.FloorDisappear(); ExtraManager.Instance.StopMoving(); Transform tCamTrans = mGameCam.transform; tCamTrans.DOMove(mBattlePos, 1).SetEase(Ease.OutCubic); tCamTrans.DORotate(mBattleRot, 1).SetEase(Ease.OutCubic); GameStart(); } private void GameStart() { TKGSDKManager.Instance.LevelStart(PlayerData.Instance.CurrentLevel); mUIGame = UIManager.Instance.OpenUI(); PetManager.Instance.Courage(); List tSoldiers = mPlayerBlockMgr.GetAllSoldier(); for (int i = 0; i < tSoldiers.Count; i++) { mPlayerGroupMgr.AddSoldier(tSoldiers[i], mEnemyGroupMgr); } mPlayerGroupMgr.ActiveAllSoldier(); tSoldiers = mEnemyBlockMgr.GetAllSoldier(); for (int i = 0; i < tSoldiers.Count; i++) { mEnemyGroupMgr.AddSoldier(tSoldiers[i], mPlayerGroupMgr); } mEnemyGroupMgr.ActiveAllSoldier(); } private void Win() { mIsWin = true;//win is overridable if (mIsOver) return; mIsOver = true; mUIGame.Close(); mOverTimer.StartTimer(GameOver, 0.5f); } private void Lose() { if (mIsOver) return; mIsOver = true; mIsWin = false; mUIGame.Close(); mOverTimer.StartTimer(GameOver, 0.5f); } private void GameOver() { TKGSDKManager.Instance.LevelEnd(PlayerData.Instance.CurrentLevel, mIsWin ? Touka.StageResult.StageSucc : Touka.StageResult.StageFail); if (mIsWin) { Instantiate(mPfbWinFx, mTsfWinFx); PetManager.Instance.Win(); mPlayerGroupMgr.Win(); AudioManager.Instance.PlaySound(AudioClipType.GameWin); } else { mEnemyGroupMgr.Win(); AudioManager.Instance.PlaySound(AudioClipType.GameLose); } mOverTimer.StartTimer(() => { MMOModule.Instance.PassLevel(PlayerData.Instance.CurrentLevel, (pPos, pDiamond) => { PlayerData.Instance.Diamond += pDiamond; UIUtils.FlyDiamond(pPos, pDiamond); }, ShowResult); }, 1.5f); } private void ShowResult() { AdsUtils.PlayInterstitial(mIsWin ? TKGIVAdPositionName.IV_Success : TKGIVAdPositionName.IV_Fail, mIsWin, () => { PanelResult tUIResult = UIManager.Instance.OpenUI(); tUIResult.InitResult(mIsWin, mEnemyGroupMgr.TotalDamage, TKGSDKManager.Instance.GetConfigBool(TKGParamKey.NativeSwitch) && TKGSDKManager.Instance.IsReadyNativeAd()); if (mIsWin) { PlayerData.Instance.CurrentLevel++; } }); } private void ShowGuideTip(int pIndex) { for (int i = 0; i < mGuideTips.Length; i++) { mGuideTips[i].SetActive(i == pIndex); } } }