GameManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. using cfg;
  2. using SimpleJSON;
  3. using Sirenix.OdinInspector;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using TMPro;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  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. [Header("金币掉落")]
  40. [FoldoutGroup("金币结算")][LabelText("怪物掉落金币数量")] public int enemyGoldDrop;
  41. [FoldoutGroup("金币结算")][LabelText("金币掉落特效")] public GameObject dropGoldFX;
  42. [FoldoutGroup("金币结算")][LabelText("金币掉落字体")] public GameObject dropGoldText;
  43. [Header("商店刷新")]
  44. [ValidateInput("CheckTagWeight","标签池子概率总和不是100%")]
  45. [FoldoutGroup("Rogue")] [LabelText("标签池子比重(%)")] [Tooltip("最高标签池;标签池;公共池")] public List<float> tagWeight;
  46. public bool CheckTagWeight(List<float> weights)
  47. {
  48. float num = 0;
  49. for(int i = 0; i < 3; i++)
  50. {
  51. num += weights[i];
  52. }
  53. if(num == 100)
  54. {
  55. return true;
  56. }
  57. else
  58. {
  59. return false;
  60. }
  61. }
  62. [FoldoutGroup("Rogue")] [LabelText("第几关开始出现(%)")] public List<int> unlockLevel;
  63. [FoldoutGroup("Rogue")] [LabelText("基础几率(%)")] public List<float> baseChance;
  64. [FoldoutGroup("Rogue")] [LabelText("每关增加的几率(%)")] public List<float> chanceIncreasePerLevel;
  65. [FoldoutGroup("Rogue")] [LabelText("最大几率(%)")] public List<float> maximumChance ;
  66. [FoldoutGroup("Rogue")] public List<List<List<SingleTreasureConfig>>> treasuresList;
  67. [FoldoutGroup("Rogue")] [DisplayOnly] public List<float> trueRefreshChance;
  68. [FoldoutGroup("Rogue")] [HideInInspector] public List<float> refreshChance;
  69. [Header("数值加成")]
  70. [FoldoutGroup("Rogue")] [LabelText("伤害")] public int damage;
  71. [FoldoutGroup("Rogue")] [LabelText("暴击率")] public int criticalChance;
  72. [FoldoutGroup("Rogue")] [LabelText("回血")] public int regeneration;
  73. [FoldoutGroup("Rogue")] [LabelText("吸血")] public int lifesteal;
  74. [FoldoutGroup("Rogue")] [LabelText("护甲")] public int armor;
  75. [FoldoutGroup("Rogue")] [LabelText("闪避")] public int dodge;
  76. [FoldoutGroup("Rogue")] [LabelText("血量上限")] public int totalHp;
  77. [FoldoutGroup("Rogue")] [LabelText("金币获得增加")] public int increasedGoldGain;
  78. [FoldoutGroup("Rogue")] [LabelText("经验获得增加")] public int increasedEXPGain;
  79. [FoldoutGroup("Rogue")] [LabelText("冲刺次数")] public int dashCharges;
  80. [FoldoutGroup("Rogue")] [LabelText("蓝量")] public int totalMp;
  81. [FoldoutGroup("Rogue")] [LabelText("回蓝速度")] public int mpRegen;
  82. public LeveType leveType;
  83. [LabelText("下一关倍率增幅")] public List<float> ratioIncrease = new List<float> { 0.4f, 1.6f, -0.8f };
  84. public int nowLevel;
  85. public int money;
  86. public List<Treasure> myTreasures;
  87. public List<int> myTreasuresTag;
  88. public int maxTreasuresTag;
  89. public float gameTime;
  90. public float totalGameTime;
  91. public Tables allCfgData;
  92. static public SoldierType[] curSoldiers; //本局游戏选择的三个士兵
  93. public List<CreateEnemyConfig> createEnemyConfigs;
  94. [DisplayOnly] public GameType gameType;
  95. [DisplayOnly] public float levelRatio = 1;
  96. private JSONNode Loader(string fileName)
  97. {
  98. return JSON.Parse(File.ReadAllText("GenerateDatas/json/" + fileName + ".json"));
  99. }
  100. public void GetAllExcel()
  101. {
  102. allCfgData = new Tables(Loader);
  103. }
  104. public void ReloadCfgCreateEnemyData()
  105. {
  106. createEnemyConfigs = new List<CreateEnemyConfig>(); ;
  107. for (int i = 0; i < 9; i++)
  108. {
  109. JSONNode keyValuePairs = JSON.Parse(File.ReadAllText($"GenerateDatas/json/CfgCreateEnemy{i}.json"));
  110. CreateEnemyConfig createEnemyConfig = new CreateEnemyConfig(keyValuePairs);
  111. createEnemyConfigs.Add(createEnemyConfig);
  112. }
  113. }
  114. private void Awake()
  115. {
  116. //选定本局游戏有哪些士兵,后面兵多了这里要改
  117. curSoldiers = new SoldierType[3];
  118. curSoldiers[0] = SoldierType.sword;
  119. curSoldiers[1] = SoldierType.shield;
  120. curSoldiers[2] = SoldierType.spear;
  121. if (!instance)
  122. {
  123. instance = this;
  124. GetAllExcel();
  125. ReloadCfgCreateEnemyData();
  126. }
  127. else
  128. {
  129. DestroyImmediate(gameObject);
  130. return;
  131. }
  132. leveType = LeveType.Introduction;
  133. myTreasures = new List<Treasure>();
  134. treasuresList = new List<List<List<SingleTreasureConfig>>>();
  135. refreshChance = new List<float>();
  136. for (int i = 0; i < 4; i++)
  137. {
  138. treasuresList.Add(new List<List<SingleTreasureConfig>>());
  139. refreshChance.Add(baseChance[i]);
  140. for(int j = 0; j < 9; j++)
  141. {
  142. treasuresList[i].Add(new List<SingleTreasureConfig>());
  143. }
  144. }
  145. trueRefreshChance = new List<float>();
  146. for (int i = 0; i < 3; i++)
  147. {
  148. trueRefreshChance.Add(refreshChance[i] - refreshChance[i + 1]);
  149. }
  150. trueRefreshChance.Add(refreshChance[3]);
  151. List<SingleTreasureConfig> cfgTreasureList = allCfgData.CfgTreasure.DataList;
  152. for(int i = 0;i< cfgTreasureList.Count; i++)
  153. {
  154. SingleTreasureConfig singleTreasureConfig = cfgTreasureList[i];
  155. treasuresList[singleTreasureConfig.Quality][0].Add(singleTreasureConfig);
  156. for(int j = 0; j < singleTreasureConfig.Tag.Count;j++)
  157. {
  158. treasuresList[singleTreasureConfig.Quality][singleTreasureConfig.Tag[j]].Add(singleTreasureConfig);
  159. }
  160. }
  161. maxTreasuresTag = -1;
  162. nowLevel = 1;
  163. }
  164. private void Start()
  165. {
  166. gameTime = 0;
  167. gameType = GameType.Gaming;
  168. }
  169. private void FixedUpdate()
  170. {
  171. if (gameType == GameType.Gaming)
  172. {
  173. gameTime += Time.deltaTime;
  174. totalGameTime += Time.deltaTime;
  175. EnemyCreater.instance.OnGameTimeChange(gameTime);
  176. int timeText = (int)gameTime;
  177. //text.text = $"{timeText / 60:D2}:{timeText % 60:D2}({timeText}s)";
  178. text.text = $"{timeText}";
  179. }
  180. }
  181. //游戏结束
  182. public void GameEnd()
  183. {
  184. gameType = GameType.GameEnd;
  185. int dropGold = 0;
  186. foreach (List<Enemy> objs in EnemyCreater.instance.enemyDic.Values)
  187. {
  188. for (int i = 0; i < objs.Count; i++)
  189. {
  190. if (objs[i].gameObject.activeSelf)
  191. {
  192. objs[i].killer = null;
  193. objs[i].ChangeState(CharacterState.Die);
  194. dropGold += enemyGoldDrop;
  195. }
  196. }
  197. }
  198. if (leveType == LeveType.Boss)
  199. {
  200. return;
  201. }
  202. AddMoney(dropGold);
  203. shopButton.SetActive(true);
  204. }
  205. public void AddMoney(int enemyDrop)
  206. {
  207. int addMoney;
  208. if(gameTime < rewardTime)
  209. {
  210. addMoney = totalMoney + extraMoney;
  211. }
  212. else
  213. {
  214. int extraTime = (int)gameTime - rewardTime;
  215. addMoney = Mathf.Clamp(totalMoney - (extraTime / deductMoneyTime) * deductMoney, 0, totalMoney);
  216. }
  217. addMoney += enemyDrop;
  218. money += Mathf.RoundToInt(addMoney * (1 + increasedGoldGain / 100f));
  219. moneyText.text = $"{money}";
  220. }
  221. //下一关
  222. public void NextLevel()
  223. {
  224. shopButton.SetActive(false);
  225. if(nowLevel - 1 < ratioIncrease.Count)
  226. {
  227. levelRatio += ratioIncrease[nowLevel - 1];
  228. }
  229. switch (leveType)
  230. {
  231. case LeveType.Introduction:
  232. EnemyCreater.instance.GetLevelOrientation(0);
  233. leveType = LeveType.Development;
  234. break;
  235. case LeveType.Development:
  236. EnemyCreater.instance.GetLevelOrientation(1);
  237. leveType = LeveType.Transition;
  238. break;
  239. case LeveType.Transition:
  240. leveType = LeveType.Conclusion;
  241. break;
  242. case LeveType.Conclusion:
  243. leveType = LeveType.Boss;
  244. break;
  245. }
  246. PlayersInput.instance[0].PlayerRevive();
  247. foreach(List<GameObject> objs in PoolManager.instance.activeObjs.Values)
  248. {
  249. List<GameObject> newObjs = new List<GameObject>(objs);
  250. for(int i = 0; i < newObjs.Count; i++)
  251. {
  252. newObjs[i].gameObject.SetActive(false);
  253. }
  254. }
  255. EnemyCreater.instance.Init();
  256. LevelSelect.instance.ChangeText();
  257. gameTime = 0;
  258. gameType = GameType.Gaming;
  259. nowLevel += 1;
  260. ChangeRogueChance();
  261. }
  262. public void ShowShop()
  263. {
  264. gameType = GameType.Shop;
  265. shopUI.Init();
  266. }
  267. public void GetTreasure(Treasure treasure)
  268. {
  269. PlayerController player = PlayersInput.instance[0];
  270. for (int i = 0; i < treasure.type.Count; i++)
  271. {
  272. float data = treasure.data[i];
  273. switch (treasure.type[i])
  274. {
  275. case "伤害":
  276. damage += (int)data;
  277. break;
  278. case "暴击率":
  279. criticalChance += (int)data;
  280. break;
  281. case "回血":
  282. regeneration += (int)data;
  283. break;
  284. case "吸血":
  285. lifesteal += (int)data;
  286. break;
  287. case "护甲":
  288. armor += (int)data;
  289. break;
  290. case "闪避":
  291. dodge += (int)data;
  292. break;
  293. case "血量上限":
  294. totalHp += (int)data;
  295. break;
  296. case "金币获得增加":
  297. increasedGoldGain += (int)data;
  298. break;
  299. case "经验获得增加":
  300. increasedEXPGain += (int)data;
  301. break;
  302. case "冲刺次数":
  303. dashCharges += (int)data;
  304. player.rushChargeTotalNums += (int)data;
  305. player.rushChargeNums = player.rushChargeTotalNums;
  306. for (int j = 0; i < player.rushChargeTotalNums; j++)
  307. {
  308. if (player.uiRush.childCount <= i)
  309. {
  310. Transform rushUIchid = Instantiate(player.uiRush.GetChild(0), player.uiRush);
  311. rushUIchid.localPosition = player.uiRush.GetChild(0).transform.localPosition + Vector3.right * 50 * i;
  312. }
  313. UIController.ChangeImageFill(player.uiRush.GetChild(i).GetComponent<Image>(), 1);
  314. }
  315. break;
  316. case "蓝量":
  317. totalMp += (int)data;
  318. player.totalMp += (int)data;
  319. player.mp = player.totalMp;
  320. player.uiMp.Show(player.mp, player.totalMp);
  321. break;
  322. case "回蓝速度":
  323. mpRegen += (int)data;
  324. player.mpReplySpeed += (int)data;
  325. break;
  326. default:
  327. Debug.LogError($"宝物配置出错,不存在词条【{treasure.type[i]}】");
  328. break;
  329. }
  330. }
  331. }
  332. public void ChangeRogueChance()
  333. {
  334. for(int i = 0; i < 4; i++)
  335. {
  336. if(nowLevel >= unlockLevel[i])
  337. {
  338. refreshChance[i] += chanceIncreasePerLevel[i];
  339. if(refreshChance[i] > maximumChance[i])
  340. {
  341. refreshChance[i] = maximumChance[i];
  342. }
  343. }
  344. }
  345. for (int i = 0; i < 3; i++)
  346. {
  347. trueRefreshChance.Add(refreshChance[i] - refreshChance[i + 1]);
  348. }
  349. trueRefreshChance.Add(refreshChance[3]);
  350. }
  351. }