ProcessManager.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace WildAttack
  5. {
  6. /// <summary>
  7. /// 关卡管理类
  8. /// </summary>
  9. public class ProcessManager : Singleton<ProcessManager>
  10. {
  11. #region Members
  12. // 当前关卡
  13. private int currLevel;
  14. public int CurrLevel { get { return currLevel; } }
  15. // 关卡开始后时间
  16. private float currLevelTime;
  17. //private int nextMonsterId;
  18. private ProcessData configData;
  19. // 怪物索引
  20. private int currMonsterIndex;
  21. // 当前关卡死亡数量
  22. private int enemyDeadNum = 0;
  23. public int EnemyDeadNum { get { return enemyDeadNum; } }
  24. // 管理道具
  25. private List<StageProperty> stagePropList;
  26. private AudioSource audioSource;
  27. // 气球生成时间
  28. private float balloonTime;
  29. // 遗留箭矢list
  30. private List<GameObject> stayArrowList;
  31. #endregion
  32. #region Functions
  33. public void Init()
  34. {
  35. stayArrowList = new List<GameObject>();
  36. //RestartLevel();
  37. audioSource = GameObject.Find("ProcessAudioSource").GetComponent<AudioSource>();
  38. InitStageProp();
  39. RestartLevel();
  40. // init 存所有pos 所有obj restart destroyAll create addComponent
  41. }
  42. public void AddStayArrow(GameObject arrowObj)
  43. {
  44. stayArrowList.Add(arrowObj);
  45. }
  46. private void ResetStayArrows()
  47. {
  48. if (stayArrowList != null && stayArrowList.Count > 0)
  49. {
  50. for (int i = stayArrowList.Count - 1; i >= 0; i--)
  51. {
  52. GameObject.Destroy(stayArrowList[i]);
  53. stayArrowList.RemoveAt(i);
  54. }
  55. }
  56. stayArrowList = new List<GameObject>();
  57. }
  58. /// <summary>
  59. /// 初始化list 存放所有道具
  60. /// </summary>
  61. private void InitStageProp()
  62. {
  63. stagePropList = new List<StageProperty>();
  64. Transform trans = GameObject.Find("BarrelTrans").transform;
  65. for (int i = 0; i < trans.childCount; i++)
  66. {
  67. stagePropList.Add(trans.GetChild(i).GetComponent<StageProperty>());
  68. }
  69. }
  70. /// <summary>
  71. /// 重置所有道具
  72. /// </summary>
  73. private void ResetStagePorperty()
  74. {
  75. foreach (var item in stagePropList)
  76. {
  77. if (item != null)
  78. {
  79. item.ResetProp();
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// 关卡重开
  85. /// </summary>
  86. public void RestartLevel()
  87. {
  88. isOver = false;
  89. Vector2 ballSpawn = GameModule.GetInstance().GetDoubleParams("balloonSpawnTime");
  90. balloonTime = Random.Range(ballSpawn.x, ballSpawn.y);
  91. ChangeLevel(0);
  92. EnemyManager.GetInstance().CreateEnemy(1, -1);
  93. }
  94. private bool isOver = false;
  95. private bool ableChanged = false;
  96. /// <summary>
  97. /// 切换新关卡
  98. /// </summary>
  99. /// <param name="curr"></param>
  100. public void ChangeLevel(int curr)
  101. {
  102. currLevel = curr;
  103. // 重开或开始游戏
  104. if (curr == 0 || curr == 1)
  105. {
  106. // 重置道具
  107. ResetStagePorperty();
  108. ResetStayArrows();
  109. GameMananger.GetInstance().barrelTriggerCount = 0;
  110. }
  111. // 判断是否为初始关卡
  112. if (curr <= 0)
  113. {
  114. enemyDeadNum = 0;
  115. UIManager.GetInstance().SetDefaultProcess(false);
  116. }
  117. else
  118. {
  119. ResetStayArrows();
  120. // 关卡开启时播放
  121. audioSource.clip = Resources.Load<AudioClip>("Voice/Process/Change");
  122. if (UserSettings.ins.openEffect) audioSource.Play();
  123. // ui change
  124. UIManager.GetInstance().SetDefaultProcess(true);
  125. // 通关判断
  126. if (currLevel > ProcessModule.GetInstance().processDataDic.Count)
  127. {
  128. // 通关
  129. GameMananger.GetInstance().GameOver(true);
  130. }
  131. // 新关卡属性初始化
  132. currMonsterIndex = 0;
  133. configData = ProcessModule.GetInstance().GetData(currLevel);
  134. currLevelTime = 0;
  135. enemyDeadNum = 0;
  136. }
  137. }
  138. private float processInterval = 0;
  139. public void Update()
  140. {
  141. if (currLevel <= 0)
  142. {
  143. // 初始关卡在随机时间生成气球
  144. if (balloonTime <= 0)
  145. {
  146. GameObject balloon = BalloonPool.GetInstance().GetGameObject();
  147. // 生成位置随机道具后
  148. Transform target = stagePropList[Random.Range(0, stagePropList.Count - 1)].transform;
  149. // 气球初始化
  150. balloon.transform.position = new Vector3(target.position.x, target.position.y, target.position.z) + Vector3.forward * 5;
  151. balloon.SetActive(true);
  152. Vector2 ballSpawn = GameModule.GetInstance().GetDoubleParams("balloonSpawnTime");
  153. balloonTime = Random.Range(ballSpawn.x, ballSpawn.y);
  154. }
  155. else
  156. {
  157. balloonTime -= Time.deltaTime;
  158. }
  159. // 初始关小怪死亡
  160. if (enemyDeadNum == 1)
  161. {
  162. ChangeLevel(++currLevel);
  163. }
  164. }
  165. else
  166. {
  167. // 关卡间隔
  168. if (processInterval > 0)
  169. {
  170. processInterval -= Time.deltaTime;
  171. return;
  172. }
  173. else
  174. {
  175. if (ableChanged)
  176. {
  177. ChangeLevel(++currLevel);
  178. ableChanged = false;
  179. return;
  180. }
  181. }
  182. if (currLevel > ProcessModule.GetInstance().processDataDic.Count) return;
  183. currLevelTime += Time.deltaTime;
  184. if (currMonsterIndex + 1 <= configData.monsterId.Count && currLevelTime >= configData.refreshTime[currMonsterIndex])
  185. {
  186. // 生成怪物 随机路径组 Enemy中组内随机路线
  187. int weight = WeightRandomUtils.GetInstance().GetRandomId(configData.weight[currMonsterIndex]);
  188. EnemyManager.GetInstance().CreateEnemy(configData.monsterId[currMonsterIndex], weight);
  189. currMonsterIndex++;
  190. }
  191. // 当前关卡怪物死光
  192. if (enemyDeadNum == configData.monsterId.Count)
  193. {
  194. processInterval = GameModule.GetInstance().GetData("processInterval");
  195. ableChanged = true;
  196. //ChangeLevel(++currLevel);
  197. }
  198. }
  199. }
  200. /// <summary>
  201. /// 怪物死亡通知关卡类
  202. /// </summary>
  203. public void EnemyDead()
  204. {
  205. enemyDeadNum++;
  206. if (currLevel <= 0) return;
  207. if (enemyDeadNum >= configData.monsterId.Count) return;
  208. if (EnemyManager.GetInstance().enemyList.Count <= 0 && currLevelTime <= configData.refreshTime[enemyDeadNum])
  209. {
  210. currLevelTime = configData.refreshTime[enemyDeadNum];
  211. //currMonsterIndex++;
  212. }
  213. }
  214. /// <summary>
  215. /// 游戏结束
  216. /// </summary>
  217. /// <param name="isWin"></param>
  218. public void GameOver(bool isWin)
  219. {
  220. if (isOver) return;
  221. audioSource.clip = Resources.Load<AudioClip>($"Voice/Process/" + (isWin ? "Success" : "Defeat"));
  222. if (UserSettings.ins.openEffect) audioSource.Play();
  223. isOver = true;
  224. }
  225. #endregion
  226. }
  227. }