CombatHeroMoveState.cs 5.0 KB

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