mergemilitary/Assets/Scripts/Logic/GameLogic.cs

329 lines
11 KiB
C#

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] float mBattleFOV;
[SerializeField] Transform mCtnStage;
[SerializeField] Transform mTsfWinFx;
[SerializeField] GameObject mPfbWinFx;
[SerializeField] BlockManager mPlayerBlockMgr;
[SerializeField] BlockManager mEnemyBlockMgr;
[SerializeField] GroupManager mPlayerGroupMgr;
[SerializeField] GroupManager mEnemyGroupMgr;
[SerializeField] GameObject[] mGuideArrows;
private PanelMain mUIMain;
private PanelGame mUIGame;
private bool mIsOver;
private TimerUnit mOverTimer;
private bool mIsWin;
private void Start()
{
GameManager.Instance.CurrentPlayerBlockMgr = mPlayerBlockMgr;
GameManager.Instance.CurrentEnemyBlockMgr = mEnemyBlockMgr;
GameManager.Instance.CurrentGameCam = mGameCam;
TKGSDKManager.Instance.LogEvent(Const.AdsEvent.LevelEnter, Const.AdsKey.Level, PlayerData.Instance.CurrentLevel.ToString());
int tStageID = ((PlayerData.Instance.CurrentLevel - 1) / 10)% 3 + 1;
Instantiate(ResourceManager.Instance.LoadRes<GameObject>(Const.Path.GetStage(tStageID)), mCtnStage);
UIManager.Instance.OpenUI<PanelTopBar>();
mUIMain = UIManager.Instance.OpenUI<PanelMain>();
mUIMain.DelFocus = OnMainFocus;
mUIMain.DelStart = MoveBattleCam;
mUIMain.DelHuman = mPlayerBlockMgr.CreateBaseHumanBlock;
mUIMain.DelMonster = mPlayerBlockMgr.CreateBaseMonsterBlock;
mUIMain.DelAutoMergeAdd = mPlayerBlockMgr.CheckAutoMerge;
mPlayerGroupMgr.DelSoldierClear = Lose;
mEnemyGroupMgr.DelSoldierClear = Win;
mPlayerBlockMgr.CreateBlocksByData(PlayerData.Instance.ArmyStr);
if (PlayerData.Instance.CurrentLevel <= LevelConfig.Instance.DataList.Count)
{
if (GameConfig.Instance.IsAutoLevel)
{
string tLevelStr = PlayerData.Instance.GetAutoLevel();
if (string.IsNullOrEmpty(tLevelStr))
{
CreateAutoLevel();
}
else
{
mEnemyBlockMgr.CreateBlocksByData(tLevelStr);
}
}
else
{
mEnemyBlockMgr.CreateBlocksByData(PlayerData.Instance.CurrentLevelData.Formation);
}
}
else
{
RandomEnemy();
}
if (!PlayerData.Instance.CheckGuide(0))
{
mPlayerBlockMgr.EnableInteraction(false);
GuideManager.Instance.StartGuide(0, new Dictionary<int, System.Action>() { { 0, MoveBattleCam } });
}
else if (!PlayerData.Instance.CheckGuide(1))
{
mPlayerBlockMgr.EnableInteraction(false);
GuideManager.Instance.StartGuide(1, new Dictionary<int, System.Action>() {
{ 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<int>(){2, 12}); ShowGuideArrow(0); } },
{ 1, () => { ShowGuideArrow(-1); mPlayerBlockMgr.EnableInteraction(false); mPlayerBlockMgr.SetForbidden(false); } },
{ 2, MoveBattleCam }});
}
else if (!PlayerData.Instance.CheckGuide(2))
{
mPlayerBlockMgr.EnableInteraction(false);
GuideManager.Instance.StartGuide(2, new Dictionary<int, System.Action>() {
{ 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<int>(){2, 3}); ShowGuideArrow(1); } },
{ 1, () => { ShowGuideArrow(-1); mPlayerBlockMgr.SetForbidden(false);} } });
}
mOverTimer = TimerManager.Instance.CreateTimerUnit();
mOverTimer.StartTimer(ShowBanner, 1f);
}
private void OnDestroy()
{
if (mOverTimer != null)
{
mOverTimer.Destroy();
}
}
private void ShowBanner()
{
TKGSDKManager.Instance.ShowBanner(2);
}
private void OnMainFocus(bool pFocus)
{
mPlayerBlockMgr.EnableInteraction(pFocus);
}
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<int> tLevelList = new List<int>();
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<int> tNearIndexes = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
List<int> tFarIndexes = new List<int>() { 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<int> tPosIndexes = new List<int>() { 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<int>() { 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<int>() { 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));
}
}
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);
Transform tCamTrans = mGameCam.transform;
tCamTrans.DOMove(mBattlePos, 1).SetEase(Ease.OutCubic);
tCamTrans.DORotate(mBattleRot, 1).SetEase(Ease.OutCubic);
mGameCam.DOFieldOfView(mBattleFOV, 1).SetEase(Ease.OutCubic);//.onComplete = StartGame;
GameStart();
}
private void GameStart()
{
TKGSDKManager.Instance.NotifyGameStart(PlayerData.Instance.CurrentLevel);
mUIGame = UIManager.Instance.OpenUI<PanelGame>();
List<SoldierUnit> 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.NotifyGameEnd(PlayerData.Instance.CurrentLevel, mIsWin);
if (mIsWin)
{
Instantiate(mPfbWinFx, mTsfWinFx);
mPlayerGroupMgr.Win();
AudioManager.Instance.PlaySound(AudioClipType.GameWin);
}
else
{
mEnemyGroupMgr.Win();
AudioManager.Instance.PlaySound(AudioClipType.GameLose);
}
mOverTimer.StartTimer(() =>
{
AdsUtils.PlayInterstitial(mIsWin ? Const.AdsValue.GameSuccess : Const.AdsValue.GameFailure, mIsWin, () =>
{
PanelResult tUIResult = UIManager.Instance.OpenUI<PanelResult>();
tUIResult.InitResult(mIsWin, mEnemyGroupMgr.TotalDamage, TKGSDKManager.Instance.GetConfigBool(TKGParamKey.NativeSwitch.ToString()) && TKGUtils.IsNativeReady());
if (mIsWin)
{
PlayerData.Instance.CurrentLevel++;
}
});
}, 1.5f);
}
private void ShowGuideArrow(int pIndex)
{
for (int i = 0; i < mGuideArrows.Length; i++)
{
mGuideArrows[i].SetActive(i == pIndex);
if (i == pIndex)
{
mGuideArrows[i].GetComponent<DOTweenAnimation>().DOPlay();
}
}
}
}