AIManager.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityGameFramework.Runtime;
  5. using GameFramework.Event;
  6. using BattleDefine;
  7. /// <summary>
  8. /// 描述:
  9. /// 作者:WJ
  10. /// </summary>
  11. public class AIManager : MonoBehaviour
  12. {
  13. /// <summary>
  14. /// 单键
  15. /// </summary>
  16. private static AIManager m_Instance;
  17. /// <summary>
  18. /// 刷怪点MO
  19. /// </summary>
  20. private Dictionary<int, WJBirthPointMo> mPointMos = new Dictionary<int, WJBirthPointMo>();
  21. /// <summary>
  22. /// 近战攻击角色
  23. /// </summary>
  24. private List<Role> mNearAttackRoles = new List<Role>();
  25. /// <summary>
  26. /// 远程攻击角色
  27. /// </summary>
  28. private List<Role> mFarAttackRoles = new List<Role>();
  29. private List<SceneEventAreaLockLogic> mAreaLocks = new List<SceneEventAreaLockLogic>();
  30. /// <summary>
  31. /// 当前所在刷怪点索引
  32. /// </summary>
  33. public int mCurPoint = 0;
  34. public Transform playerPoint;
  35. /// <summary>
  36. /// 检测朝玩家移动
  37. /// </summary>
  38. private bool mCheckMoveToPlayer = true;
  39. /// <summary>
  40. /// 单键
  41. /// </summary>
  42. public static AIManager Instance
  43. {
  44. get
  45. {
  46. if (m_Instance == null)
  47. {
  48. m_Instance = GameObject.Find("AIManager").AddComponent<AIManager>();
  49. }
  50. return m_Instance;
  51. }
  52. }
  53. /// <summary>
  54. /// 初始化
  55. /// </summary>
  56. public void Init()
  57. {
  58. mPointMos.Clear();
  59. // 关卡数据
  60. sm_gate gateMo = sm_gate.GetMoById(GameDateManager.Instance.GateID);
  61. // 出生点
  62. playerPoint = this.transform.Find("Area1/0");
  63. // 刷怪点数据
  64. List<sm_gatelevel> levels = gateMo.GetMonsterLevels();
  65. for (int i = 0; i < levels.Count; i++)
  66. {
  67. Transform tran = transform.Find("Area1/" + (i + 1).ToString());
  68. if (tran)
  69. {
  70. WJBirthPointMo pointMO = new WJBirthPointMo();
  71. pointMO.point = i + 1;
  72. pointMO.pointTran = tran;
  73. pointMO.monsters = levels[i].GetMonsterList();
  74. pointMO.bossID = levels[i].boss1;
  75. if(tran.CompareTag("NoRandom"))
  76. {
  77. pointMO.randomPos = false;
  78. }
  79. mPointMos.Add(i + 1, pointMO);
  80. }
  81. }
  82. // 创建怪物
  83. switch((EBattleSceneType)int.Parse(gateMo.battleType))
  84. {
  85. case EBattleSceneType.EBattleType_Default:
  86. while (CreateNextPointMonster(Vector3.zero))
  87. {
  88. }
  89. break;
  90. case EBattleSceneType.EBattleType_Boss:
  91. CreatPointMonster(1, Vector3.zero);
  92. break;
  93. case EBattleSceneType.EBattleType_Guide:
  94. CreatPointMonster(1, Vector3.zero);
  95. break;
  96. case EBattleSceneType.EBattleType_Arena:
  97. break;
  98. case EBattleSceneType.EBattleType_Safe:
  99. break;
  100. }
  101. }
  102. public Transform GetPlayerCreatePoint()
  103. {
  104. return GameObject.Find("AIManager/Area1/0").transform;
  105. }
  106. public Transform GetPointMOByIndex(int index)
  107. {
  108. return playerPoint;
  109. }
  110. /// <summary>
  111. /// 创建怪物点
  112. /// </summary>
  113. public bool CreatPointMonster(int point, Vector3 livePos)
  114. {
  115. if (mPointMos.ContainsKey(point))
  116. {
  117. mCurPoint = point;
  118. // 怪物
  119. for (int i = 0; i < mPointMos[point].monsters.Count; i++)
  120. {
  121. if (livePos == Vector3.zero)
  122. {
  123. livePos = mPointMos[point].pointTran.position;
  124. }
  125. float radius = mPointMos[point].randomDis;
  126. // 在出生位置小范围随机
  127. if (mPointMos[point].randomPos)
  128. {
  129. livePos += new Vector3(Random.Range(-radius, radius), 0, Random.Range(-radius, radius));
  130. }
  131. //if (mPointMos[point].monsters[i].heroId == mPointMos[point].bossID)
  132. //{
  133. // RoleManager.Instance.CreateMonster(point, mPointMos[point].monsters[i], null, livePos, livePos, radius, true);
  134. //}
  135. //else
  136. //{
  137. // RoleManager.Instance.CreateMonster(point, mPointMos[point].monsters[i], null, livePos, livePos, radius, false);
  138. //}
  139. }
  140. EventComponent nowEvent = GameEntry.GetComponent<EventComponent>();
  141. nowEvent.FireNow(this, new NoviceEventSpawnerIndex(point));
  142. mCheckMoveToPlayer = true;
  143. return true;
  144. }
  145. return false;
  146. }
  147. /// <summary>
  148. /// 获取场景所有怪物数量
  149. /// </summary>
  150. /// <returns></returns>
  151. public int GetMonsterMaxNumber()
  152. {
  153. int nodeIndex = 1;
  154. int maxNumber = 0;
  155. foreach (var item in mPointMos)
  156. {
  157. maxNumber += GetPointMonsterNumber(nodeIndex);
  158. }
  159. return maxNumber;
  160. }
  161. /// <summary>
  162. /// 获取单一出生点 怪物数量
  163. /// </summary>
  164. /// <param name="point"></param>
  165. /// <returns></returns>
  166. public int GetPointMonsterNumber(int point)
  167. {
  168. int number = 0;
  169. if (mPointMos.ContainsKey(point))
  170. {
  171. number += mPointMos[point].monsters.Count;
  172. }
  173. return number;
  174. }
  175. /// <summary>
  176. /// 创建下一个怪物点
  177. /// </summary>
  178. /// <returns>是否创建成功</returns>
  179. public bool CreateNextPointMonster(Vector3 livePos)
  180. {
  181. mCurPoint++;
  182. if (mPointMos.ContainsKey(mCurPoint))
  183. {
  184. // 解除区域限制
  185. RemoveAreaLockCmpt(mCurPoint);
  186. return CreatPointMonster(mCurPoint, livePos);
  187. }
  188. return false;
  189. }
  190. /// <summary>
  191. /// 添加禁止区域
  192. /// </summary>
  193. /// <param name="al"></param>
  194. public void AddAreaLockCmpt(SceneEventAreaLockLogic al)
  195. {
  196. mAreaLocks.Add(al);
  197. }
  198. public SceneEventAreaLockLogic GetAreaLockCmpt(int id)
  199. {
  200. foreach(var item in mAreaLocks)
  201. {
  202. if(item.areaIndex == id)
  203. {
  204. return item;
  205. }
  206. }
  207. return null;
  208. }
  209. /// <summary>
  210. /// 移除禁止区域
  211. /// </summary>
  212. /// <param name="index"></param>
  213. public void RemoveAreaLockCmpt(int index)
  214. {
  215. foreach(var item in mAreaLocks)
  216. {
  217. if(item.areaIndex == index)
  218. {
  219. item.AreaUnlock();
  220. mAreaLocks.Remove(item);
  221. UI_TipsWindow.InitFloatWaringDialog("前方结界屏障已消失,可以通行!");
  222. break;
  223. }
  224. }
  225. }
  226. }
  227. /// <summary>
  228. /// 怪物出生点MO
  229. /// </summary>
  230. public class WJBirthPointMo
  231. {
  232. /// <summary>
  233. /// 怪物
  234. /// </summary>
  235. public List<sm_hero> monsters;
  236. /// <summary>
  237. /// BOSS ID
  238. /// </summary>
  239. public int bossID = 0;
  240. /// <summary>
  241. /// 出生点
  242. /// </summary>
  243. public int point = 0;
  244. /// <summary>
  245. /// 是否小范围随机
  246. /// </summary>
  247. public bool randomPos = true;
  248. /// <summary>
  249. /// 随机的距离
  250. /// </summary>
  251. public float randomDis = 5.0f;
  252. /// <summary>
  253. /// 点坐标
  254. /// </summary>
  255. public Transform pointTran = null;
  256. }