EnemyCreater.cs 8.5 KB

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