ActionAttack.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using BehaviorDesigner.Runtime;
  5. using BehaviorDesigner.Runtime.Tasks;
  6. namespace BehaviorDesigner.Runtime.Tasks
  7. {
  8. /// <summary>
  9. /// 行为 攻击
  10. /// </summary>
  11. public class ActionAttack : Action
  12. {
  13. [Tooltip("攻击目标")]
  14. public SharedTransform target;
  15. [BehaviorDesigner.Runtime.Tasks.Tooltip("攻击条件")]
  16. public AttackCondition attackCondition = new AttackCondition();
  17. [BehaviorDesigner.Runtime.Tasks.Tooltip("animator的攻击触发器开关")]
  18. public SharedString attackTriggerName = "Attack1";
  19. [BehaviorDesigner.Runtime.Tasks.Tooltip("攻击条件成功后,等待时间多少时间离开此状态")]
  20. public SharedFloat waitTime = 2;
  21. [BehaviorDesigner.Runtime.Tasks.Tooltip("是否随机攻击")]
  22. public bool randomAttack = false;
  23. [BehaviorDesigner.Runtime.Tasks.Tooltip("失败是否自动继续执行后面逻辑")]
  24. public bool failAutoNext = false;
  25. [BehaviorDesigner.Runtime.Tasks.Tooltip("随机攻击攻击的触发器开关集合,随机攻击开启后会在列表内随机使用其中一个攻击,randomAttack为false时无效")]
  26. public List<string> randomAttackTriggerNames = new List<string>();
  27. /// <summary>
  28. /// 开始时间
  29. /// </summary>
  30. private float startTime;
  31. /// <summary>
  32. /// 动画控制器
  33. /// </summary>
  34. private Animator animator = null;
  35. /// <summary>
  36. /// 攻击条件是否达成
  37. /// </summary>
  38. private bool attackConditionSuccess = false;
  39. private Role role = null;
  40. public override void OnAwake()
  41. {
  42. animator = gameObject.GetComponentInChildren<Animator>();
  43. role = gameObject.GetComponent<Role>();
  44. }
  45. public override void OnStart()
  46. {
  47. startTime = Time.time;
  48. role.attacking = true;
  49. if (target.Value == null)
  50. {
  51. attackConditionSuccess = false;
  52. return;
  53. }
  54. switch (attackCondition.condition)
  55. {
  56. case eAttackCondition.None:
  57. {
  58. attackConditionSuccess = true;
  59. }
  60. break;
  61. case eAttackCondition.Distance:
  62. {
  63. float dis = Vector3.Distance(transform.position, target.Value.transform.position);
  64. if (dis >= attackCondition.conditionMinValue && dis < attackCondition.conditionMaxValue)
  65. {
  66. attackConditionSuccess = true;
  67. }
  68. else
  69. {
  70. attackConditionSuccess = false;
  71. return;
  72. }
  73. }
  74. break;
  75. case eAttackCondition.HpPercentage:
  76. {
  77. Role selfRole = transform.GetComponent<Role>();
  78. float hpPerc = selfRole.mData.HP_Final / selfRole.mData.MaxHP_Final;
  79. if (hpPerc >= attackCondition.conditionMinValue && hpPerc < attackCondition.conditionMaxValue)
  80. {
  81. attackConditionSuccess = true;
  82. }
  83. else
  84. {
  85. attackConditionSuccess = false;
  86. return;
  87. }
  88. }
  89. break;
  90. }
  91. // 随机攻击
  92. if (randomAttack)
  93. {
  94. int randomVal = Random.Range(0, randomAttackTriggerNames.Count);
  95. animator.SetBool(randomAttackTriggerNames[randomVal], true);
  96. }
  97. else
  98. {
  99. animator.SetBool(attackTriggerName.Value, true);
  100. }
  101. // 朝向目标攻击
  102. Vector3 targetPos = target.Value.position;
  103. targetPos.y = transform.position.y;
  104. transform.LookAt(targetPos);
  105. //// 缓慢旋转
  106. //Vector3 dir = target.Value.position - transform.position;
  107. //dir.Normalize();
  108. //transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(dir), 60 * Time.deltaTime);
  109. }
  110. public override TaskStatus OnUpdate()
  111. {
  112. if (attackConditionSuccess == false)
  113. {
  114. if (failAutoNext)
  115. {
  116. return TaskStatus.Success;
  117. }
  118. else
  119. {
  120. return TaskStatus.Failure;
  121. }
  122. }
  123. if (startTime + waitTime.Value < Time.time)
  124. {
  125. return TaskStatus.Success;
  126. }
  127. return TaskStatus.Running;
  128. }
  129. /// <summary>
  130. /// 结束
  131. /// </summary>
  132. public override void OnEnd()
  133. {
  134. role.attacking = false;
  135. }
  136. }
  137. /// <summary>
  138. /// 攻击条件
  139. /// </summary>
  140. public enum eAttackCondition
  141. {
  142. /// <summary>
  143. /// 无
  144. /// </summary>
  145. None = 0,
  146. /// <summary>
  147. /// 距离(实际距离)
  148. /// </summary>
  149. Distance = 1,
  150. /// <summary>
  151. /// 血量百分百(0~1)
  152. /// </summary>
  153. HpPercentage = 2,
  154. }
  155. /// <summary>
  156. /// 攻击条件
  157. /// </summary>
  158. public class AttackCondition
  159. {
  160. [BehaviorDesigner.Runtime.Tasks.Tooltip("攻击条件类型")]
  161. public eAttackCondition condition = eAttackCondition.None;
  162. [BehaviorDesigner.Runtime.Tasks.Tooltip("攻击条件最小值")]
  163. public float conditionMinValue = 0;
  164. [BehaviorDesigner.Runtime.Tasks.Tooltip("攻击条件最大值")]
  165. public float conditionMaxValue = 0;
  166. }
  167. }