EnemyManager.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace GameWorld
  5. {
  6. public class EnemyManager
  7. {
  8. public EnemyType GetEnemyType(string name)
  9. {
  10. return EnemyGenerator.GetEnemyType(name);
  11. }
  12. public GameObject Generate(string name, Vector2? pos = null, bool withEffect = true, bool enemyPoint = true)
  13. {
  14. return this.Generator.GenerateEnemy(name, pos, withEffect, enemyPoint);
  15. }
  16. public GameObject Generate(EnemyType type, Vector2? pos = null, bool withEffect = true, bool enemyPoint = true)
  17. {
  18. return this.Generator.GenerateEnemy(type, pos, withEffect, enemyPoint);
  19. }
  20. private EnemyGenerator Generator
  21. {
  22. get
  23. {
  24. return Singleton<EnemyGenerator>.Instance;
  25. }
  26. }
  27. public List<EnemyAttribute> EnemyAttributes
  28. {
  29. get
  30. {
  31. return this._enemyAttributes;
  32. }
  33. }
  34. public int Count
  35. {
  36. get
  37. {
  38. return this._enemyAttributes.Count;
  39. }
  40. }
  41. public GameObject First
  42. {
  43. get
  44. {
  45. return (this._enemyAttributes.Count <= 0 || !(this._enemyAttributes[0] != null)) ? null : this._enemyAttributes[0].gameObject;
  46. }
  47. }
  48. private List<GameObject> GetXNearestRangeEnemy(float middle, float range)
  49. {
  50. List<GameObject> list = new List<GameObject>();
  51. for (int i = 0; i < this._enemyAttributes.Count; i++)
  52. {
  53. if (MathfX.isInMiddleRange(this._enemyAttributes[i].transform.position.x, middle, range))
  54. {
  55. list.Add(this._enemyAttributes[i].gameObject);
  56. }
  57. }
  58. return list;
  59. }
  60. public bool HasNearEnemy(float xPos, float range)
  61. {
  62. for (int i = 0; i < this._enemyAttributes.Count; i++)
  63. {
  64. if (MathfX.isInMiddleRange(this._enemyAttributes[i].transform.position.x, xPos, range))
  65. {
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71. public bool HasEnemyInRect(Rect rect)
  72. {
  73. for (int i = 0; i < this._enemyAttributes.Count; i++)
  74. {
  75. if (rect.Contains(this._enemyAttributes[i].transform.position))
  76. {
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82. public GameObject GetNearestRangeEnemys(Vector2 pos, float range)
  83. {
  84. GameObject result = null;
  85. List<GameObject> xnearestRangeEnemy = this.GetXNearestRangeEnemy(pos.x, range);
  86. float num = range;
  87. for (int i = 0; i < xnearestRangeEnemy.Count; i++)
  88. {
  89. float num2 = Vector2.Distance(pos, xnearestRangeEnemy[i].transform.position);
  90. if (num2 < num && num2 < range)
  91. {
  92. result = xnearestRangeEnemy[i];
  93. num = num2;
  94. }
  95. }
  96. return result;
  97. }
  98. public GameObject GetNearestRangeEnemyWithDir(Vector2 pos, float rectX, float rectY, int dir, bool isNeedAlive, bool isOnGround)
  99. {
  100. GameObject result = null;
  101. List<GameObject> xnearestRangeEnemy = this.GetXNearestRangeEnemy(pos.x, rectX + rectY);
  102. float num = rectX + rectY;
  103. for (int i = 0; i < xnearestRangeEnemy.Count; i++)
  104. {
  105. GameObject gameObject = xnearestRangeEnemy[i];
  106. bool flag = InputSetting.JudgeDir(pos, gameObject.transform.position) == dir;
  107. bool flag2 = MathfX.isInMiddleRange(gameObject.transform.position.x, pos.x, rectX) && MathfX.isInMiddleRange(gameObject.transform.position.y, pos.y, rectY);
  108. EnemyAttribute component = gameObject.GetComponent<EnemyAttribute>();
  109. bool flag3 = component != null && !component.isDead;
  110. bool flag4 = component != null && component.isOnGround;
  111. if (flag2 && flag && flag3 == isNeedAlive && flag4 == isOnGround)
  112. {
  113. float num2 = Vector2.Distance(pos, gameObject.transform.position);
  114. if (num2 < num)
  115. {
  116. result = gameObject;
  117. num = num2;
  118. }
  119. }
  120. }
  121. return result;
  122. }
  123. public bool HasXNearestRangeEnemy(float middle, float diameter)
  124. {
  125. for (int i = 0; i < this._enemyAttributes.Count; i++)
  126. {
  127. if (MathfX.isInMiddleRange(this._enemyAttributes[i].transform.position.x, middle, diameter))
  128. {
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. public Vector3? GetFarestEnemyPosition(Vector3 pivot, Rect? rect = null)
  135. {
  136. float num = 0f;
  137. int num2 = -1;
  138. for (int i = 0; i < this.EnemyAttributes.Count; i++)
  139. {
  140. EnemyAttribute enemyAttribute = this.EnemyAttributes[i];
  141. if (Vector3.Distance(enemyAttribute.transform.position, pivot) > num)
  142. {
  143. if (rect != null)
  144. {
  145. if (Mathf.Abs(enemyAttribute.transform.position.x - rect.Value.center.x) < rect.Value.width / 2f && Mathf.Abs(enemyAttribute.transform.position.y - rect.Value.center.y) < rect.Value.height / 2f)
  146. {
  147. num = Vector3.Distance(enemyAttribute.transform.position, pivot);
  148. num2 = i;
  149. }
  150. }
  151. else
  152. {
  153. num = Vector3.Distance(enemyAttribute.transform.position, pivot);
  154. num2 = i;
  155. }
  156. }
  157. }
  158. return (num2 != -1) ? new Vector3?(this.EnemyAttributes[num2].transform.position) : null;
  159. }
  160. public Vector3 GetAverageEnemyPosition(Vector3 player, float distance = 10f)
  161. {
  162. int num = 0;
  163. Vector3 vector = Vector3.zero;
  164. for (int i = 0; i < this._enemyAttributes.Count; i++)
  165. {
  166. if (Vector3.Distance(player, this._enemyAttributes[i].transform.position) < distance)
  167. {
  168. vector += this._enemyAttributes[i].transform.position;
  169. num++;
  170. }
  171. }
  172. if (num != 0)
  173. {
  174. vector /= (float)num;
  175. }
  176. return vector;
  177. }
  178. public GameObject GetEnemyById(string id)
  179. {
  180. for (int i = 0; i < this._enemyAttributes.Count; i++)
  181. {
  182. if (this._enemyAttributes[i].id == id)
  183. {
  184. return this._enemyAttributes[i].gameObject;
  185. }
  186. }
  187. return null;
  188. }
  189. public GameObject GetEnemyByType(EnemyType type)
  190. {
  191. for (int i = 0; i < this._enemyAttributes.Count; i++)
  192. {
  193. if (this._enemyAttributes[i].baseData.enemyType == type)
  194. {
  195. return this._enemyAttributes[i].gameObject;
  196. }
  197. }
  198. return null;
  199. }
  200. public List<GameObject> GetEnemysByType(EnemyType type)
  201. {
  202. List<GameObject> list = new List<GameObject>();
  203. for (int i = 0; i < this._enemyAttributes.Count; i++)
  204. {
  205. if (this._enemyAttributes[i].baseData.enemyType == type)
  206. {
  207. list.Add(this._enemyAttributes[i].gameObject);
  208. }
  209. }
  210. return list;
  211. }
  212. public int GetEnemyCountByType(EnemyType type)
  213. {
  214. int num = 0;
  215. for (int i = 0; i < this._enemyAttributes.Count; i++)
  216. {
  217. if (this._enemyAttributes[i].baseData.enemyType == type)
  218. {
  219. num++;
  220. }
  221. }
  222. return num;
  223. }
  224. public bool CanBeExecutedEnemyExist()
  225. {
  226. bool flag = false;
  227. for (int i = 0; i < R.Enemy.Count; i++)
  228. {
  229. flag |= R.Enemy.EnemyAttributes[i].CurrentCanBeExecute;
  230. }
  231. return flag;
  232. }
  233. public void AddEnemy(EnemyAttribute enemyAttribute)
  234. {
  235. if (enemyAttribute != null)
  236. {
  237. this._enemyAttributes.Add(enemyAttribute);
  238. }
  239. }
  240. public void RemoveEnemy(EnemyAttribute enemyAttribute)
  241. {
  242. if (enemyAttribute != null)
  243. {
  244. this._enemyAttributes.Remove(enemyAttribute);
  245. }
  246. }
  247. public void KillAllEnemy()
  248. {
  249. if (this._enemyAttributes != null)
  250. {
  251. for (int i = 0; i < this._enemyAttributes.Count; i++)
  252. {
  253. this._enemyAttributes[i].currentHp = 0;
  254. }
  255. }
  256. }
  257. public void Clear()
  258. {
  259. this.Boss = null;
  260. this._enemyAttributes.Clear();
  261. }
  262. private readonly List<EnemyAttribute> _enemyAttributes = new List<EnemyAttribute>();
  263. public GameObject Boss;
  264. }
  265. }