EnemyCreater.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. using System;
  9. [Serializable]
  10. public struct CreaterControl
  11. {
  12. public bool isCreated;
  13. public GameMapTile tile;
  14. public GameMapLayer mapLayer;
  15. public SpawnTime spawnTime;
  16. public int waveStartTime;
  17. public List<Vector3> pos;
  18. }
  19. public class EnemyCreater : MonoBehaviour
  20. {
  21. public static EnemyCreater instance;
  22. public List<CreaterControl> createCharacter = new();
  23. public Dictionary<int, List<Enemy>> enemyDic;
  24. public Dictionary<int, List<GameObject>> buildingDic;
  25. private LevelSelect.Level curLevel;
  26. public GameMapsAsset mapsAsset;
  27. //import
  28. public LevelSelect levelSelect;
  29. private void Awake()
  30. {
  31. if (!instance)
  32. {
  33. instance = this;
  34. }
  35. else
  36. {
  37. DestroyImmediate(gameObject);
  38. return;
  39. }
  40. enemyDic = new Dictionary<int, List<Enemy>>();
  41. buildingDic = new Dictionary<int, List<GameObject>>();
  42. }
  43. private void Start()
  44. {
  45. levelSelect = GetComponent<LevelSelect>();
  46. curLevel = GetComponent<LevelSelect>().levelConfig[levelSelect.curLevelID - 1];
  47. LoadMapAsset(0);
  48. //把所有怪物prefab生成一遍防止后面初次生成卡顿
  49. //foreach (var item in createCharacter)
  50. //{
  51. // for(int i = 0; i < item.tile.spawnTime[item.spawnTimeId].num; i++)
  52. // {
  53. // CreateEnemy(item.tile, Vector3.zero, 1, 1,false);
  54. // }
  55. //}
  56. }
  57. public void LoadMapAsset(int mapIdx)
  58. {
  59. GameMap map = mapsAsset.maps[mapIdx];
  60. int startTime = 0;
  61. for (int i = 0; i < map.layers.Count(); i++)
  62. {
  63. GameMapLayer layer = map.layers[i];
  64. List<SpawnTime> spawnTimes = layer.spawnTimes;
  65. for(int j = 0; j < spawnTimes.Count; j++)
  66. {
  67. SpawnTime spawnTime = layer.spawnTimes[j];
  68. CreaterControl createrControl = new();
  69. createrControl.isCreated = false;
  70. GameMapTile t = mapsAsset.tileAsset.GetTile(spawnTime.id);
  71. createrControl.tile = t;
  72. createrControl.spawnTime = spawnTime;
  73. createrControl.pos = new List<Vector3>();
  74. for (int k = 0; k < spawnTime.pos.Count; k++)
  75. {
  76. int posInt = spawnTime.pos[k];
  77. float posX = posInt % map.width;
  78. posX = posX + UnityEngine.Random.Range(-t.radius.x / 2, t.radius.x / 2);
  79. float posY = posInt / map.width;
  80. posY = posY + UnityEngine.Random.Range(-t.radius.y / 2, t.radius.y / 2);
  81. posY = posY < 0 ? 0 : posY;
  82. createrControl.pos.Add(new Vector3(posX, posY, 0));
  83. }
  84. createrControl.waveStartTime = startTime;
  85. createrControl.mapLayer = layer;
  86. createCharacter.Add(createrControl);
  87. }
  88. startTime += map.layers[i].duration;
  89. }
  90. }
  91. public void OnGameTimeChange(float gameTime)
  92. {
  93. for (int i = 0; i < createCharacter.Count; i++)
  94. {
  95. if (!createCharacter[i].isCreated && createCharacter[i].spawnTime.startTime + createCharacter[i].waveStartTime <= gameTime)
  96. {
  97. CreaterControl createrControl = createCharacter[i];
  98. createrControl.isCreated = true;
  99. createCharacter[i] = createrControl;
  100. StartCreateEnemy(createCharacter[i]);
  101. }
  102. }
  103. }
  104. public async void StartCreateEnemy(CreaterControl createrControl)
  105. {
  106. SpawnTime spawnTime = createrControl.spawnTime;
  107. for (int i = 0; i < spawnTime.num; i++)
  108. {
  109. if (!instance)
  110. {
  111. return;
  112. }
  113. GameMapLayer gameMapLayer = createrControl.mapLayer;
  114. for(int j= 0; j < createrControl.pos.Count; j++)
  115. {
  116. CreateEnemy(createrControl.tile, createrControl.pos[j], gameMapLayer.Hp, gameMapLayer.moveSpeed, gameMapLayer.attack);
  117. }
  118. if(spawnTime.num == 1)
  119. {
  120. return;
  121. }
  122. float TimeInterval = (float)(spawnTime.endTime - spawnTime.startTime) / (spawnTime.num - 1);
  123. if (TimeInterval <= 0)
  124. {
  125. return;
  126. }
  127. await Task.Delay((int)(TimeInterval * 1000));
  128. }
  129. }
  130. public GameObject CreateEnemy(GameMapTile gameMapTile, Vector3 pos, float hpRatio, float moveSpeedRatio, float attackRatio, bool active = true)
  131. {
  132. string enemyStr = $"Prefab/{gameMapTile.type}/{gameMapTile.ch}";
  133. GameObject enemyObj = Util.Instantiate(enemyStr, pos, active: active);
  134. Parameter parameter = gameMapTile.parameter;
  135. AttackInfo attackInfo;
  136. switch (gameMapTile.type)
  137. {
  138. case GameMapTile.Type.Tower:
  139. EnemyTower enemyTower = enemyObj.GetComponent<EnemyTower>();
  140. Tower tower = enemyObj.GetComponent<Tower>();
  141. if (enemyTower != null)
  142. {
  143. enemyTower.id = gameMapTile.id;
  144. enemyTower.totalHp = (int)(parameter.HP * hpRatio);
  145. enemyTower.hp = enemyTower.totalHp;
  146. attackInfo = enemyTower.attackController.Attack_summonInfos[0];
  147. attackInfo.damage = (int)(parameter.Attack_summon * attackRatio);
  148. enemyTower.attackController.Attack_summonInfos[0] = attackInfo;
  149. enemyTower.Init();
  150. }
  151. if (tower != null)
  152. {
  153. tower.id = gameMapTile.id;
  154. tower.totalHp = (int)(parameter.HP * hpRatio);
  155. tower.hp = tower.totalHp;
  156. attackInfo = tower.attackController.Attack_summonInfos[0];
  157. attackInfo.damage = (int)(parameter.Attack_summon * attackRatio);
  158. tower.attackController.Attack_summonInfos[0] = attackInfo;
  159. tower.Init();
  160. }
  161. if (!buildingDic.ContainsKey(gameMapTile.id))
  162. {
  163. buildingDic.Add(gameMapTile.id, new List<GameObject>());
  164. }
  165. buildingDic[gameMapTile.id].Add(enemyObj);
  166. break;
  167. case GameMapTile.Type.Enemy:
  168. Enemy enemy = enemyObj.GetComponent<Enemy>();
  169. enemy.id = gameMapTile.id;
  170. if (!enemyDic.ContainsKey(gameMapTile.id))
  171. {
  172. enemyDic.Add(gameMapTile.id, new List<Enemy>());
  173. }
  174. enemyDic[gameMapTile.id].Add(enemy);
  175. enemy.totalHp = (int)(parameter.HP * hpRatio);
  176. enemy.hp = enemy.totalHp;
  177. enemy.minMoveSpeed = parameter.MinMoveSpeed * moveSpeedRatio;
  178. enemy.maxMoveSpeed = parameter.MaxMoveSpeed * moveSpeedRatio;
  179. attackInfo = enemy.attackController.Attack_summonInfos[0];
  180. attackInfo.damage = (int)(parameter.Attack_summon * attackRatio);
  181. enemy.attackController.Attack_summonInfos[0] = attackInfo;
  182. attackInfo = enemy.attackController.Attack_marchInfos[0];
  183. attackInfo.damage = (int)(parameter.Attack_march * attackRatio);
  184. enemy.attackController.Attack_marchInfos[0] = attackInfo;
  185. if (enemy.canFly)
  186. {
  187. enemy.flyHeight = enemy.transform.position.y;
  188. }
  189. enemy.Init();
  190. enemy.SetSortingOrder(enemy.baseSortingOrder + enemyDic[gameMapTile.id].Count);
  191. break;
  192. }
  193. enemyObj.transform.position = pos;
  194. return enemyObj;
  195. }
  196. public void OnEnemyRecycle(Enemy enemy)
  197. {
  198. if (!enemyDic.ContainsKey(enemy.id))
  199. {
  200. return;
  201. }
  202. enemyDic[enemy.id].Remove(enemy);
  203. for (int i = 0; i < enemyDic[enemy.id].Count; i++)
  204. {
  205. enemyDic[enemy.id][i].SetSortingOrder(enemy.baseSortingOrder + i);
  206. }
  207. }
  208. public Enemy GetMinDisOtherEnemy(Enemy self)
  209. {
  210. Enemy minDisEnemy = null;
  211. foreach (var item in enemyDic)
  212. {
  213. for (int i = 0; i < item.Value.Count; i++)
  214. {
  215. if (item.Value[i] != self && !item.Value[i].isDie && item.Value[i].gameObject.activeInHierarchy)
  216. {
  217. if (!minDisEnemy || (minDisEnemy.transform.position - self.transform.position).magnitude
  218. > (item.Value[i].transform.position - self.transform.position).magnitude)
  219. {
  220. minDisEnemy = item.Value[i];
  221. }
  222. }
  223. }
  224. }
  225. return minDisEnemy;
  226. }
  227. }