EnemyCreater.cs 8.7 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 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. Debug.Log(createCharacter[i].spawnTime.id);
  101. StartCreateEnemy(createCharacter[i]);
  102. }
  103. }
  104. }
  105. public async void StartCreateEnemy(CreaterControl createrControl)
  106. {
  107. SpawnTime spawnTime = createrControl.spawnTime;
  108. for (int i = 0; i < spawnTime.num; i++)
  109. {
  110. if (!instance)
  111. {
  112. return;
  113. }
  114. GameMapLayer gameMapLayer = createrControl.mapLayer;
  115. for(int j= 0; j < createrControl.pos.Count; j++)
  116. {
  117. CreateEnemy(createrControl.tile, createrControl.pos[j], gameMapLayer.Hp, gameMapLayer.moveSpeed, gameMapLayer.attack);
  118. }
  119. if(spawnTime.num == 1)
  120. {
  121. return;
  122. }
  123. float TimeInterval = (float)(spawnTime.endTime - spawnTime.startTime) / (spawnTime.num - 1);
  124. await Task.Delay((int)(TimeInterval * 1000));
  125. }
  126. }
  127. public GameObject CreateEnemy(GameMapTile gameMapTile, Vector3 pos, float hpRatio, float moveSpeedRatio, float attackRatio, bool active = true)
  128. {
  129. string enemyStr = $"Prefab/{gameMapTile.type}/{gameMapTile.ch}";
  130. GameObject enemyObj = Util.Instantiate(enemyStr, pos, active: active);
  131. Parameter parameter = gameMapTile.parameter;
  132. AttackInfo attackInfo;
  133. switch (gameMapTile.type)
  134. {
  135. case GameMapTile.Type.Tower:
  136. EnemyTower enemyTower = enemyObj.GetComponent<EnemyTower>();
  137. Tower tower = enemyObj.GetComponent<Tower>();
  138. if (enemyTower != null)
  139. {
  140. enemyTower.id = gameMapTile.id;
  141. enemyTower.totalHp = (int)(parameter.HP * hpRatio);
  142. enemyTower.hp = enemyTower.totalHp;
  143. attackInfo = enemyTower.Attack_summonInfos[0];
  144. attackInfo.damage = (int)(parameter.Attack_summon * attackRatio);
  145. enemyTower.Attack_summonInfos[0] = attackInfo;
  146. enemyTower.Init();
  147. }
  148. if (tower != null)
  149. {
  150. tower.id = gameMapTile.id;
  151. tower.totalHp = (int)(parameter.HP * hpRatio);
  152. tower.hp = tower.totalHp;
  153. attackInfo = tower.Attack_summonInfos[0];
  154. attackInfo.damage = (int)(parameter.Attack_summon * attackRatio);
  155. tower.Attack_summonInfos[0] = attackInfo;
  156. tower.Init();
  157. }
  158. if (!buildingDic.ContainsKey(gameMapTile.id))
  159. {
  160. buildingDic.Add(gameMapTile.id, new List<GameObject>());
  161. }
  162. buildingDic[gameMapTile.id].Add(enemyObj);
  163. break;
  164. case GameMapTile.Type.Enemy:
  165. Enemy enemy = enemyObj.GetComponent<Enemy>();
  166. enemy.id = gameMapTile.id;
  167. if (!enemyDic.ContainsKey(gameMapTile.id))
  168. {
  169. enemyDic.Add(gameMapTile.id, new List<Enemy>());
  170. }
  171. enemyDic[gameMapTile.id].Add(enemy);
  172. enemy.totalHp = (int)(parameter.HP * hpRatio);
  173. enemy.hp = enemy.totalHp;
  174. enemy.minMoveSpeed = parameter.MinMoveSpeed * moveSpeedRatio;
  175. enemy.maxMoveSpeed = parameter.MaxMoveSpeed * moveSpeedRatio;
  176. attackInfo = enemy.Attack_summonInfos[0];
  177. attackInfo.damage = (int)(parameter.Attack_summon * attackRatio);
  178. enemy.Attack_summonInfos[0] = attackInfo;
  179. attackInfo = enemy.Attack_marchInfos[0];
  180. attackInfo.damage = (int)(parameter.Attack_march * attackRatio);
  181. enemy.Attack_marchInfos[0] = attackInfo;
  182. if (enemy.canFly)
  183. {
  184. enemy.flyHeight = enemy.transform.position.y;
  185. }
  186. enemy.Init();
  187. enemy.SetSortingOrder(enemy.baseSortingOrder + enemyDic[gameMapTile.id].Count);
  188. break;
  189. }
  190. enemyObj.transform.position = pos;
  191. return enemyObj;
  192. }
  193. public void OnEnemyRecycle(Enemy enemy)
  194. {
  195. if (!enemyDic.ContainsKey(enemy.id))
  196. {
  197. return;
  198. }
  199. enemyDic[enemy.id].Remove(enemy);
  200. for (int i = 0; i < enemyDic[enemy.id].Count; i++)
  201. {
  202. enemyDic[enemy.id][i].SetSortingOrder(enemy.baseSortingOrder + i);
  203. }
  204. }
  205. public Enemy GetMinDisOtherEnemy(Enemy self)
  206. {
  207. Enemy minDisEnemy = null;
  208. foreach (var item in enemyDic)
  209. {
  210. for (int i = 0; i < item.Value.Count; i++)
  211. {
  212. if (item.Value[i] != self && !item.Value[i].isDie && item.Value[i].gameObject.activeInHierarchy)
  213. {
  214. if (!minDisEnemy || (minDisEnemy.transform.position - self.transform.position).magnitude
  215. > (item.Value[i].transform.position - self.transform.position).magnitude)
  216. {
  217. minDisEnemy = item.Value[i];
  218. }
  219. }
  220. }
  221. }
  222. return minDisEnemy;
  223. }
  224. }