CombatAIBasic.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Common.Utility.CombatEvent;
  5. using Core.State;
  6. using Fort23.Core;
  7. using GameLogic.Combat.CombatTool;
  8. using GameLogic.Combat.Hero;
  9. using GameLogic.Combat.Hero.State;
  10. using OfficeOpenXml.FormulaParsing.Excel.Functions.Logical;
  11. using UnityEngine;
  12. using UnityEngine.AI;
  13. public class CombatAIBasic : IDisposable
  14. {
  15. public StateControl stateControl;
  16. public NavMeshAgent NavMeshAgent;
  17. public NavMeshObstacle NavMeshObstacle;
  18. protected CombatHeroEntity _combatHeroEntity;
  19. protected float _r = 5;
  20. /// <summary>
  21. /// 当前聚焦的目标
  22. /// </summary>
  23. public CombatHeroEntity currFocusTarget;
  24. private BetterList<GameObject> _areaList = new BetterList<GameObject>();
  25. public void Init(CombatHeroEntity combatHeroEntity, NavMeshAgent navMeshAgent)
  26. {
  27. CombatEventManager.Instance.AddEventListener(CombatEventType.AlertTrigger, AlertTrigger);
  28. _combatHeroEntity = combatHeroEntity;
  29. stateControl = new StateControl();
  30. NavMeshAgent = navMeshAgent;
  31. // NavMeshAgent.enabled = false;
  32. // NavMeshObstacle= navMeshAgent.gameObject.GetComponent<NavMeshObstacle>();
  33. // if (NavMeshObstacle == null)
  34. // {
  35. // NavMeshObstacle= navMeshAgent.gameObject.AddComponent<NavMeshObstacle>();
  36. // NavMeshObstacle.shape= NavMeshObstacleShape.Capsule;
  37. // NavMeshObstacle.radius = NavMeshAgent.radius-0.1f;
  38. // NavMeshObstacle.carving = true;
  39. // }
  40. ProInitState();
  41. ProInit();
  42. }
  43. public GameObject[] AreaList
  44. {
  45. get { return _areaList.ToArray(); }
  46. }
  47. protected virtual void ProInit()
  48. {
  49. }
  50. protected void AlertTrigger(IEventData iEventData)
  51. {
  52. AlertTriggerEventData alertTriggerEventData = iEventData as AlertTriggerEventData;
  53. if (alertTriggerEventData.combatHeroEntity == _combatHeroEntity)
  54. {
  55. if (alertTriggerEventData.isTrigger && !_areaList.Contains(alertTriggerEventData.triggerObject))
  56. {
  57. _areaList.Add(alertTriggerEventData.triggerObject);
  58. }
  59. else if (!alertTriggerEventData.isTrigger)
  60. {
  61. _areaList.Remove(alertTriggerEventData.triggerObject);
  62. }
  63. }
  64. }
  65. protected void ProInitState()
  66. {
  67. stateControl.AddState(CombatHeroStateType.idle, new CombatHeroIdleState(_combatHeroEntity));
  68. stateControl.AddState(CombatHeroStateType.move, new CombatHeroMoveState(_combatHeroEntity));
  69. stateControl.AddState(CombatHeroStateType.att, new CombatHeroAttState(_combatHeroEntity));
  70. stateControl.AddState(CombatHeroStateType.dile, new CombatHeroDieState(_combatHeroEntity));
  71. stateControl.AddState(CombatHeroStateType.rolling, new CombatHeroRollingState(_combatHeroEntity));
  72. }
  73. public void ChangeState(string name)
  74. {
  75. stateControl.ChangeState(name);
  76. }
  77. public void Update(float t)
  78. {
  79. for (int i = 0; i < _areaList.Count; i++)
  80. {
  81. if (!_areaList[i].activeSelf)
  82. {
  83. _areaList.RemoveAt(i);
  84. i--;
  85. }
  86. }
  87. CombatHeroEntity[] allHero =
  88. CombatController.currActiveCombat.CombatHeroController.GetHero(!_combatHeroEntity.IsEnemy);
  89. FindMinDixtance(allHero, out currFocusTarget);
  90. stateControl.Update(t);
  91. if (currFocusTarget != null)
  92. {
  93. if (!stateControl.CurrStateName.Equals(CombatHeroStateType.move))
  94. {
  95. Vector3 nextPos = currFocusTarget.dotPos;
  96. Vector3 p = nextPos - _combatHeroEntity.dotPos;
  97. if (p.sqrMagnitude > 0.00001)
  98. {
  99. _combatHeroEntity.combatHeroGameObject.rotation = Quaternion.Lerp(
  100. _combatHeroEntity.combatHeroGameObject.rotation, Quaternion.LookRotation(p.normalized), 0.5f);
  101. }
  102. }
  103. }
  104. ProUpdate();
  105. }
  106. protected virtual void ProUpdate()
  107. {
  108. }
  109. protected void FindMinDixtance(CombatHeroEntity[] allHero, out CombatHeroEntity minDistanceHero)
  110. {
  111. minDistanceHero = null;
  112. if (allHero == null)
  113. {
  114. return;
  115. }
  116. float minDistance = float.MaxValue;
  117. for (int i = 0; i < allHero.Length; i++)
  118. {
  119. CombatHeroEntity hero = allHero[i];
  120. float distance = Vector3.Distance(hero.combatHeroGameObject.position,
  121. _combatHeroEntity.combatHeroGameObject.position);
  122. if (distance < minDistance)
  123. {
  124. if (!_combatHeroEntity.IsEnemy || distance < _combatHeroEntity.CurrCombatHeroInfo.maxDis + 3)
  125. {
  126. minDistance = distance;
  127. minDistanceHero = hero;
  128. }
  129. }
  130. }
  131. }
  132. public void Dispose()
  133. {
  134. ProDispose();
  135. _combatHeroEntity = null;
  136. stateControl.Dispose();
  137. stateControl = null;
  138. }
  139. protected virtual void ProDispose()
  140. {
  141. CombatEventManager.Instance.RemoveEventListener(CombatEventType.AlertTrigger, AlertTrigger);
  142. }
  143. }