CombatHeroMoveState.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using Animancer;
  2. using Core.Event.Event;
  3. using Fort23.Core;
  4. using OfficeOpenXml.FormulaParsing.Excel.Functions.Logical;
  5. using UnityEngine;
  6. using UnityEngine.AI;
  7. namespace GameLogic.Combat.Hero.State
  8. {
  9. public class CombatHeroMoveState : CombatHeroStateBasic
  10. {
  11. protected Vector3 lasetTaregtPoint;
  12. protected Vector3 lastMovePoint;
  13. protected HeroPlayStateInfoBasic _transitionAsset;
  14. protected bool isStop;
  15. public CombatHeroMoveState(CombatHeroEntity combatHeroEntity) : base(combatHeroEntity)
  16. {
  17. }
  18. protected override void ProEnter()
  19. {
  20. EventManager.Instance.AddEventListener(CustomEventType.RefreshFull,RefreshFull);
  21. combatHeroEntity.CombatAIBasic.NavMeshAgent.Warp(combatHeroEntity.dotPos);
  22. Start();
  23. }
  24. protected void Start()
  25. {
  26. lasetTaregtPoint = new Vector3(999999, 99999, 99999);
  27. _transitionAsset = combatHeroEntity.combatHeroAnimtion.Play("run");
  28. combatHeroEntity.CombatAIBasic.NavMeshAgent.updateRotation = false;
  29. if ( combatHeroEntity.CombatAIBasic.NavMeshAgent.isOnNavMesh == false)
  30. {
  31. Debug.LogError("对象错误");
  32. }
  33. combatHeroEntity.CombatAIBasic.NavMeshAgent.isStopped = true;
  34. // combatHeroEntity.CombatAIBasic.NavMeshAgent.obstacleAvoidanceType =
  35. // ObstacleAvoidanceType.HighQualityObstacleAvoidance;
  36. combatHeroEntity.CombatAIBasic.NavMeshAgent.avoidancePriority = 40;
  37. }
  38. protected void RefreshFull(IEventData ieEventData)
  39. {
  40. RefreshFullEventData data = (RefreshFullEventData)ieEventData;
  41. if (data.isFullShow&&!isStop)
  42. {
  43. isStop = true;
  44. combatHeroEntity.CombatAIBasic.NavMeshAgent.isStopped = true;
  45. combatHeroEntity.CombatAIBasic.NavMeshAgent.velocity = Vector3.zero;
  46. combatHeroEntity.CombatAIBasic.NavMeshAgent.avoidancePriority = 50;
  47. }
  48. else if(isStop)
  49. {
  50. Start();
  51. isStop = false;
  52. }
  53. }
  54. protected override void ProExit()
  55. {
  56. EventManager.Instance.RemoveEventListener(CustomEventType.RefreshFull,RefreshFull);
  57. combatHeroEntity.CombatAIBasic.NavMeshAgent.isStopped = true;
  58. combatHeroEntity.CombatAIBasic.NavMeshAgent.velocity = Vector3.zero;
  59. combatHeroEntity.CombatAIBasic.NavMeshAgent.avoidancePriority = 50;
  60. // combatHeroEntity.CombatAIBasic.NavMeshAgent.acceleration = 0;
  61. }
  62. protected void SetNewPath(Vector3 taregtPos)
  63. {
  64. lastMovePoint = combatHeroEntity.dotPos;
  65. lasetTaregtPoint = taregtPos;
  66. combatHeroEntity.CombatAIBasic.NavMeshAgent.isStopped = false;
  67. // combatHeroEntity.CombatAIBasic.NavMeshAgent.acceleration = 8;
  68. combatHeroEntity.CombatAIBasic.NavMeshAgent.SetDestination(taregtPos);
  69. }
  70. protected override void ProUpdate(float t)
  71. {
  72. if (combatHeroEntity.CombatAIBasic.currFocusTarget == null)
  73. {
  74. combatHeroEntity.CombatAIBasic.ChangeState(CombatHeroStateType.idle);
  75. return;
  76. }
  77. Vector3 targetPos = combatHeroEntity.CombatAIBasic.currFocusTarget.dotPos;
  78. Vector3 myPos = combatHeroEntity.dotPos;
  79. float dis = combatHeroEntity.CurrCombatHeroInfo.maxDisTo;
  80. if (combatHeroEntity.CombatAIBasic.currFocusTarget is BannerHero)
  81. {
  82. dis = 1;
  83. }
  84. if (Vector3.SqrMagnitude(targetPos - myPos) >
  85. dis)
  86. {
  87. if (Vector3.SqrMagnitude(lasetTaregtPoint - targetPos) > 1)
  88. {
  89. SetNewPath(targetPos);
  90. }
  91. }
  92. else
  93. {
  94. combatHeroEntity.CombatAIBasic.ChangeState(CombatHeroStateType.idle);
  95. return;
  96. }
  97. if (_transitionAsset != null)
  98. {
  99. float v = combatHeroEntity.CombatAIBasic.NavMeshAgent.velocity.sqrMagnitude;
  100. float v2 = v / (combatHeroEntity.IsEnemy?9:36);
  101. _transitionAsset.SetSpeed(Mathf.Clamp(v2, 0.2f, 1.5f));
  102. }
  103. if ( combatHeroEntity.CombatAIBasic.NavMeshAgent.isOnNavMesh&&!combatHeroEntity.CombatAIBasic.NavMeshAgent.isStopped)
  104. {
  105. Vector3 nextPos = myPos;
  106. Vector3 p = nextPos - lastMovePoint;
  107. lastMovePoint = myPos;
  108. if (p.sqrMagnitude > 0.00001)
  109. {
  110. combatHeroEntity.combatHeroGameObject.rotation = Quaternion.Lerp(
  111. combatHeroEntity.combatHeroGameObject.rotation, Quaternion.LookRotation(p.normalized), 0.5f);
  112. }
  113. }
  114. }
  115. }
  116. }