EnemyCreater.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using cfg;
  5. using System.Threading.Tasks;
  6. using Base.Common;
  7. using System.Linq;
  8. public class EnemyCreater : MonoBehaviour
  9. {
  10. public static EnemyCreater instance;
  11. public List<SingleCreateEnemyConfig> cfgCreateEnemy;
  12. public List<SingleCreateBuildingConfig> cfgCreateBuilding;
  13. public List<bool> createdEnemy;
  14. public List<bool> createdBuilding;
  15. public Dictionary<int, List<Enemy>> enemyDic;
  16. public Dictionary<int, GameObject> buildingDic;
  17. [Header("传送门")]
  18. public float portalsCreateTime = 10; //传送门提前出现时间
  19. public Transform portalUIParent; //传送门出现提示UI的父物体
  20. private void Awake()
  21. {
  22. if (!instance)
  23. {
  24. instance = this;
  25. }
  26. else
  27. {
  28. DestroyImmediate(gameObject);
  29. return;
  30. }
  31. enemyDic = new Dictionary<int, List<Enemy>>();
  32. buildingDic = new Dictionary<int, GameObject>();
  33. }
  34. private void Start()
  35. {
  36. cfgCreateEnemy = GameManager.instance.allCfgData.CfgCreateEnemy.DataList;
  37. cfgCreateBuilding = GameManager.instance.allCfgData.CfgCreateBuilding.DataList;
  38. createdEnemy = new List<bool>();
  39. createdBuilding = new List<bool>();
  40. for (int i = 0; i < cfgCreateEnemy.Count; i++)
  41. {
  42. createdEnemy.Add(false);
  43. }
  44. for (int i = 0; i < cfgCreateBuilding.Count; i++)
  45. {
  46. createdBuilding.Add(false);
  47. }
  48. }
  49. public void OnGameTimeChange(float gameTime)
  50. {
  51. for (int i = 0; i < cfgCreateEnemy.Count; i++)
  52. {
  53. if(cfgCreateEnemy[i].BuildingID == 0)
  54. {
  55. if (cfgCreateEnemy[i].Time <= gameTime && !createdEnemy[i])
  56. {
  57. createdEnemy[i] = true;
  58. StartCreateEnemy(i);
  59. }
  60. }
  61. else
  62. {
  63. GameObject result = null;
  64. buildingDic.TryGetValue(cfgCreateEnemy[i].BuildingID, out result);
  65. if (result != null)
  66. {
  67. EnemyTower enemyTower = result.GetComponent<EnemyTower>();
  68. if (enemyTower.hp * 100 <= enemyTower.totalHp * cfgCreateEnemy[i].BuildingHP && !createdEnemy[i])
  69. {
  70. createdEnemy[i] = true;
  71. StartCreateEnemy(i);
  72. }
  73. }
  74. }
  75. }
  76. for (int i = 0; i < cfgCreateBuilding.Count; i++)
  77. {
  78. if (!createdBuilding[i]
  79. &&(cfgCreateBuilding[i].Type == 0 && cfgCreateBuilding[i].Time - portalsCreateTime <= gameTime
  80. ||cfgCreateBuilding[i].Type == 1 && cfgCreateBuilding[i].Time <= gameTime))
  81. {
  82. createdBuilding[i] = true;
  83. if(gameTime< portalsCreateTime)
  84. {
  85. StartCreateBuilding(i, cfgCreateBuilding[i].Time);
  86. }
  87. else
  88. {
  89. StartCreateBuilding(i, portalsCreateTime);
  90. }
  91. }
  92. }
  93. }
  94. public void StartCreateBuilding(int id,float createTime)
  95. {
  96. SingleCreateBuildingConfig singleCreateBuilding = cfgCreateBuilding[id];
  97. if (!instance)
  98. {
  99. return;
  100. }
  101. switch (singleCreateBuilding.Type)
  102. {
  103. case 0:
  104. List<float> pos = singleCreateBuilding.Position.Concat(singleCreateBuilding.Position1).ToList();
  105. CreatePortal(singleCreateBuilding.ID, pos, singleCreateBuilding.Scale, singleCreateBuilding.HPRatio, createTime);
  106. break;
  107. case 1:
  108. CreateEnemyTower(singleCreateBuilding.ID, singleCreateBuilding.HPRatio, singleCreateBuilding.AttackRatio);
  109. break;
  110. }
  111. }
  112. public async void StartCreateEnemy(int id)
  113. {
  114. SingleCreateEnemyConfig singleCreateEnemy = cfgCreateEnemy[id];
  115. for (int i = 0; i < singleCreateEnemy.Count; i++)
  116. {
  117. if (!instance)
  118. {
  119. return;
  120. }
  121. Vector3 pos = Vector3.zero;
  122. switch (singleCreateEnemy.Type)
  123. {
  124. case 0:
  125. pos = new Vector3(
  126. singleCreateEnemy.Position[0],
  127. singleCreateEnemy.Position[1]
  128. + Random.Range(-singleCreateEnemy.YRandomRange / 2, singleCreateEnemy.YRandomRange / 2),
  129. singleCreateEnemy.Position[2]
  130. + Random.Range(-singleCreateEnemy.ZRandomRange / 2, singleCreateEnemy.ZRandomRange / 2));
  131. break;
  132. case 1:
  133. pos = buildingDic[singleCreateEnemy.BuildingID].transform.position;
  134. pos.y += Random.Range(0, singleCreateEnemy.YRandomRange);
  135. pos.z += Random.Range(0, singleCreateEnemy.ZRandomRange);
  136. break;
  137. }
  138. CreateEnemy(singleCreateEnemy.EnemyID, pos, singleCreateEnemy.HPRatio, singleCreateEnemy.AttackRatio);
  139. await Task.Delay((int)(singleCreateEnemy.TimeInterval * 1000));
  140. }
  141. }
  142. public void CreateEnemyTower(int buildingID, float hpRatio, float attackRatio)
  143. {
  144. SingleBuildingConfig cfgBuilding = GameManager.instance.allCfgData.CfgBuilding.Get(buildingID);
  145. GameObject towerObj = Util.Instantiate(cfgBuilding.BuildingPrefab);
  146. EnemyTower enemyTower = towerObj.GetComponent<EnemyTower>();
  147. buildingDic.Add(buildingID, towerObj);
  148. enemyTower.totalHp = (int)(cfgBuilding.HP * hpRatio);
  149. enemyTower.Init();
  150. AttackInfo attackInfo = enemyTower.attack1Infos[0];
  151. attackInfo.damage = (int)(cfgBuilding.Attack * attackRatio);
  152. enemyTower.attack1Infos[0] = attackInfo;
  153. }
  154. public void CreatePortal(int buildingID, List<float> pos, List<float> scale, float hpRatio, float createTime)
  155. {
  156. SingleBuildingConfig cfgBuilding = GameManager.instance.allCfgData.CfgBuilding.Get(buildingID);
  157. GameObject portalObj = Util.Instantiate(cfgBuilding.BuildingPrefab);
  158. PortalsCreater portalsCreater = portalObj.GetComponent<PortalsCreater>();
  159. portalsCreater.id = buildingID;
  160. buildingDic.Add(buildingID, portalObj);
  161. portalsCreater.totalHp = (int)(cfgBuilding.HP * hpRatio);
  162. portalsCreater.enemyCreater = this;
  163. portalsCreater.portalUIParent = portalUIParent;
  164. portalsCreater.Init(pos,scale,createTime);
  165. portalsCreater.enemyNumber -= 1;
  166. portalsCreater.enemyNumberText.text = portalsCreater.enemyNumber.ToString();
  167. if (portalsCreater.enemyNumber == 0)
  168. {
  169. portalsCreater.enemyNumberText.transform.parent.gameObject.SetActive(false);
  170. if (portalsCreater.onlyEnemy)
  171. {
  172. portalsCreater.coreCharacter.CoreBreak();
  173. }
  174. }
  175. }
  176. public void CreateEnemy(int enemyId, Vector3 pos, float hpRatio, float attackRatio)
  177. {
  178. SingleEnemyConfig cfgEnemy = GameManager.instance.allCfgData.CfgEnemy.Get(enemyId);
  179. GameObject enemyObj = Util.Instantiate(cfgEnemy.EnemyPrefab, pos);
  180. if (enemyId > 1000)
  181. {
  182. Boss boss = enemyObj.GetComponentInChildren<Boss>();
  183. }
  184. else
  185. {
  186. Enemy enemy = enemyObj.GetComponent<Enemy>();
  187. enemy.id = enemyId;
  188. if (!enemyDic.ContainsKey(enemyId))
  189. {
  190. enemyDic.Add(enemyId, new List<Enemy>());
  191. }
  192. enemyDic[enemyId].Add(enemy);
  193. enemy.totalHp = (int)(cfgEnemy.HP * hpRatio);
  194. enemy.moveSpeed = cfgEnemy.MoveSpeed;
  195. for (int i = 0; i < cfgEnemy.Attack1.Count; i++)
  196. {
  197. AttackInfo attackInfo = enemy.attack1Infos[i];
  198. attackInfo.damage = (int)(cfgEnemy.Attack1[i] * attackRatio);
  199. enemy.attack1Infos[i] = attackInfo;
  200. }
  201. for (int i = 0; i < cfgEnemy.Attack1.Count; i++)
  202. {
  203. AttackInfo attackInfo = enemy.attack2Infos[i];
  204. attackInfo.damage = (int)(cfgEnemy.Attack2[i] * attackRatio);
  205. enemy.attack2Infos[i] = attackInfo;
  206. }
  207. if (enemy.canFly)
  208. {
  209. enemy.flyHeight = enemy.transform.position.y;
  210. }
  211. enemy.Init();
  212. GameObject prefab = Resources.Load<GameObject>(cfgEnemy.EnemyPrefab);
  213. int sortingOrder = prefab.GetComponent<Enemy>().sortingOrder;
  214. enemy.SetSortingOrder(sortingOrder + enemyDic[enemyId].Count);
  215. }
  216. }
  217. public void OnEnemyRecycle(Enemy enemy)
  218. {
  219. if (!enemyDic.ContainsKey(enemy.id))
  220. {
  221. return;
  222. }
  223. enemyDic[enemy.id].Remove(enemy);
  224. SingleEnemyConfig cfgEnemy = GameManager.instance.allCfgData.CfgEnemy.Get(enemy.id);
  225. for (int i = 0; i < enemyDic[enemy.id].Count; i++)
  226. {
  227. enemyDic[enemy.id][i].SetSortingOrder(enemy.id * 1000 + i);
  228. }
  229. }
  230. public Enemy GetMinDisOtherEnemy(Enemy self)
  231. {
  232. Enemy minDisEnemy = null;
  233. foreach (var item in enemyDic)
  234. {
  235. for (int i = 0; i < item.Value.Count; i++)
  236. {
  237. if (item.Value[i] != self && !item.Value[i].isDie && item.Value[i].gameObject.activeInHierarchy)
  238. {
  239. if (!minDisEnemy || (minDisEnemy.transform.position - self.transform.position).magnitude
  240. > (item.Value[i].transform.position - self.transform.position).magnitude)
  241. {
  242. minDisEnemy = item.Value[i];
  243. }
  244. }
  245. }
  246. }
  247. return minDisEnemy;
  248. }
  249. }