GameManager.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using cfg;
  2. using SimpleJSON;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using UnityEngine;
  7. using TMPro;
  8. using Sirenix.OdinInspector;
  9. public enum SoldierType
  10. {
  11. [LabelText("飞剑")] sword = 0,
  12. [LabelText("胖子")] shield = 1,
  13. [LabelText("林冲")] spear = 2,
  14. [LabelText("阴灵")] Spirits = 3,
  15. }
  16. public enum GameType
  17. {
  18. Gaming,
  19. GameEnd,
  20. Shop,
  21. }
  22. public class GameManager : MonoBehaviour
  23. {
  24. public static GameManager instance;
  25. [FoldoutGroup("组件", order: -1)] public UIHP p1uiHP;
  26. [FoldoutGroup("组件")] public UIHP p1uiMP;
  27. [FoldoutGroup("组件")] public Transform p1uiRush;
  28. [FoldoutGroup("组件")] public GameObject[] demonicNum;
  29. [FoldoutGroup("组件")] public TextMeshProUGUI text;
  30. [FoldoutGroup("组件")] public GameObject chapterBG; //当前关卡的原始背景(用于和boss背景做替换)
  31. [FoldoutGroup("组件")] public GameObject shopButton;
  32. [FoldoutGroup("组件")] public ShopUI shopUI;
  33. [FoldoutGroup("组件")] public TextMeshProUGUI moneyText;
  34. [FoldoutGroup("金币结算", order: -1)][LabelText("限制时间")] public int rewardTime;
  35. [FoldoutGroup("金币结算")][LabelText("金币奖励")] public int totalMoney;
  36. [FoldoutGroup("金币结算")][LabelText("额外金币奖励")] public int extraMoney;
  37. [FoldoutGroup("金币结算")][LabelText("惩罚时间间隔")] public int deductMoneyTime;
  38. [FoldoutGroup("金币结算")][LabelText("每次惩罚扣除金币数量")] public int deductMoney;
  39. public LeveType leveType;
  40. [LabelText("下一关倍率增幅")] public List<float> ratioIncrease = new List<float> { 0.4f, 1.6f, -0.8f };
  41. public int money;
  42. public float gameTime;
  43. public float totalGameTime;
  44. public Tables allCfgData;
  45. static public SoldierType[] curSoldiers; //本局游戏选择的三个士兵
  46. public List<CreateEnemyConfig> createEnemyConfigs;
  47. [DisplayOnly] public GameType gameType;
  48. [DisplayOnly] public float levelRatio = 1;
  49. private JSONNode Loader(string fileName)
  50. {
  51. return JSON.Parse(File.ReadAllText("GenerateDatas/json/" + fileName + ".json"));
  52. }
  53. public void GetAllExcel()
  54. {
  55. allCfgData = new Tables(Loader);
  56. }
  57. public void ReloadCfgCreateEnemyData()
  58. {
  59. createEnemyConfigs = new List<CreateEnemyConfig>(); ;
  60. for (int i = 0; i < 9; i++)
  61. {
  62. JSONNode keyValuePairs = JSON.Parse(File.ReadAllText($"GenerateDatas/json/CfgCreateEnemy{i}.json"));
  63. CreateEnemyConfig createEnemyConfig = new CreateEnemyConfig(keyValuePairs);
  64. createEnemyConfigs.Add(createEnemyConfig);
  65. }
  66. }
  67. private void Awake()
  68. {
  69. //选定本局游戏有哪些士兵,后面兵多了这里要改
  70. curSoldiers = new SoldierType[3];
  71. curSoldiers[0] = SoldierType.sword;
  72. curSoldiers[1] = SoldierType.shield;
  73. curSoldiers[2] = SoldierType.spear;
  74. if (!instance)
  75. {
  76. instance = this;
  77. GetAllExcel();
  78. ReloadCfgCreateEnemyData();
  79. }
  80. else
  81. {
  82. DestroyImmediate(gameObject);
  83. return;
  84. }
  85. leveType = LeveType.Introduction;
  86. }
  87. private void Start()
  88. {
  89. gameTime = 0;
  90. gameType = GameType.Gaming;
  91. }
  92. private void FixedUpdate()
  93. {
  94. if (gameType == GameType.Gaming)
  95. {
  96. gameTime += Time.deltaTime;
  97. totalGameTime += Time.deltaTime;
  98. EnemyCreater.instance.OnGameTimeChange(gameTime);
  99. int timeText = (int)gameTime;
  100. //text.text = $"{timeText / 60:D2}:{timeText % 60:D2}({timeText}s)";
  101. text.text = $"{timeText}";
  102. }
  103. }
  104. //游戏结束
  105. public void GameEnd()
  106. {
  107. gameType = GameType.GameEnd;
  108. foreach (List<Enemy> objs in EnemyCreater.instance.enemyDic.Values)
  109. {
  110. for (int i = 0; i < objs.Count; i++)
  111. {
  112. if (objs[i].gameObject.activeSelf)
  113. {
  114. objs[i].killer = null;
  115. objs[i].ChangeState(CharacterState.Die);
  116. }
  117. }
  118. }
  119. if (leveType == LeveType.Boss)
  120. {
  121. return;
  122. }
  123. AddMoney();
  124. shopButton.SetActive(true);
  125. }
  126. public void AddMoney()
  127. {
  128. int addMoney;
  129. if(gameTime < rewardTime)
  130. {
  131. addMoney = totalMoney + extraMoney;
  132. }
  133. else
  134. {
  135. int extraTime = (int)gameTime - rewardTime;
  136. addMoney = Mathf.Clamp(totalMoney - (extraTime / deductMoneyTime) * deductMoney, 0, totalMoney);
  137. }
  138. money += addMoney;
  139. moneyText.text = $"{money}";
  140. }
  141. //下一关
  142. public void NextLevel()
  143. {
  144. shopButton.SetActive(false);
  145. switch (leveType)
  146. {
  147. case LeveType.Introduction:
  148. EnemyCreater.instance.GetLevelOrientation(0);
  149. leveType = LeveType.Development;
  150. levelRatio += ratioIncrease[0];
  151. break;
  152. case LeveType.Development:
  153. EnemyCreater.instance.GetLevelOrientation(1);
  154. leveType = LeveType.Transition;
  155. levelRatio += ratioIncrease[1];
  156. break;
  157. case LeveType.Transition:
  158. leveType = LeveType.Conclusion;
  159. levelRatio += ratioIncrease[2];
  160. break;
  161. case LeveType.Conclusion:
  162. leveType = LeveType.Boss;
  163. break;
  164. }
  165. PlayersInput.instance[0].PlayerRevive();
  166. foreach(List<GameObject> objs in PoolManager.instance.activeObjs.Values)
  167. {
  168. List<GameObject> newObjs = new List<GameObject>(objs);
  169. for(int i = 0; i < newObjs.Count; i++)
  170. {
  171. newObjs[i].gameObject.SetActive(false);
  172. }
  173. }
  174. EnemyCreater.instance.Init();
  175. LevelSelect.instance.ChangeText();
  176. gameTime = 0;
  177. gameType = GameType.Gaming;
  178. }
  179. public void ShowShop()
  180. {
  181. gameType = GameType.Shop;
  182. shopUI.gameObject.SetActive(true);
  183. }
  184. }