123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using BehaviorDesigner.Runtime;
- using BehaviorDesigner.Runtime.Tasks;
- namespace BehaviorDesigner.Runtime.Tasks
- {
- /// <summary>
- /// 行为 攻击
- /// </summary>
- public class ActionAttack : Action
- {
- [Tooltip("攻击目标")]
- public SharedTransform target;
- [BehaviorDesigner.Runtime.Tasks.Tooltip("攻击条件")]
- public AttackCondition attackCondition = new AttackCondition();
- [BehaviorDesigner.Runtime.Tasks.Tooltip("animator的攻击触发器开关")]
- public SharedString attackTriggerName = "Attack1";
- [BehaviorDesigner.Runtime.Tasks.Tooltip("攻击条件成功后,等待时间多少时间离开此状态")]
- public SharedFloat waitTime = 2;
- [BehaviorDesigner.Runtime.Tasks.Tooltip("是否随机攻击")]
- public bool randomAttack = false;
- [BehaviorDesigner.Runtime.Tasks.Tooltip("失败是否自动继续执行后面逻辑")]
- public bool failAutoNext = false;
- [BehaviorDesigner.Runtime.Tasks.Tooltip("随机攻击攻击的触发器开关集合,随机攻击开启后会在列表内随机使用其中一个攻击,randomAttack为false时无效")]
- public List<string> randomAttackTriggerNames = new List<string>();
- /// <summary>
- /// 开始时间
- /// </summary>
- private float startTime;
- /// <summary>
- /// 动画控制器
- /// </summary>
- private Animator animator = null;
- /// <summary>
- /// 攻击条件是否达成
- /// </summary>
- private bool attackConditionSuccess = false;
- private Role role = null;
- public override void OnAwake()
- {
- animator = gameObject.GetComponentInChildren<Animator>();
- role = gameObject.GetComponent<Role>();
- }
- public override void OnStart()
- {
- startTime = Time.time;
- role.attacking = true;
- if (target.Value == null)
- {
- attackConditionSuccess = false;
- return;
- }
- switch (attackCondition.condition)
- {
- case eAttackCondition.None:
- {
- attackConditionSuccess = true;
- }
- break;
- case eAttackCondition.Distance:
- {
- float dis = Vector3.Distance(transform.position, target.Value.transform.position);
- if (dis >= attackCondition.conditionMinValue && dis < attackCondition.conditionMaxValue)
- {
- attackConditionSuccess = true;
- }
- else
- {
- attackConditionSuccess = false;
- return;
- }
- }
- break;
- case eAttackCondition.HpPercentage:
- {
- Role selfRole = transform.GetComponent<Role>();
- float hpPerc = selfRole.mData.HP_Final / selfRole.mData.MaxHP_Final;
- if (hpPerc >= attackCondition.conditionMinValue && hpPerc < attackCondition.conditionMaxValue)
- {
- attackConditionSuccess = true;
- }
- else
- {
- attackConditionSuccess = false;
- return;
- }
- }
- break;
- }
- // 随机攻击
- if (randomAttack)
- {
- int randomVal = Random.Range(0, randomAttackTriggerNames.Count);
- animator.SetBool(randomAttackTriggerNames[randomVal], true);
- }
- else
- {
- animator.SetBool(attackTriggerName.Value, true);
- }
- // 朝向目标攻击
- Vector3 targetPos = target.Value.position;
- targetPos.y = transform.position.y;
- transform.LookAt(targetPos);
- //// 缓慢旋转
- //Vector3 dir = target.Value.position - transform.position;
- //dir.Normalize();
- //transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(dir), 60 * Time.deltaTime);
- }
- public override TaskStatus OnUpdate()
- {
- if (attackConditionSuccess == false)
- {
- if (failAutoNext)
- {
- return TaskStatus.Success;
- }
- else
- {
- return TaskStatus.Failure;
- }
- }
- if (startTime + waitTime.Value < Time.time)
- {
- return TaskStatus.Success;
- }
- return TaskStatus.Running;
- }
- /// <summary>
- /// 结束
- /// </summary>
- public override void OnEnd()
- {
- role.attacking = false;
- }
- }
- /// <summary>
- /// 攻击条件
- /// </summary>
- public enum eAttackCondition
- {
- /// <summary>
- /// 无
- /// </summary>
- None = 0,
- /// <summary>
- /// 距离(实际距离)
- /// </summary>
- Distance = 1,
- /// <summary>
- /// 血量百分百(0~1)
- /// </summary>
- HpPercentage = 2,
- }
- /// <summary>
- /// 攻击条件
- /// </summary>
- public class AttackCondition
- {
- [BehaviorDesigner.Runtime.Tasks.Tooltip("攻击条件类型")]
- public eAttackCondition condition = eAttackCondition.None;
- [BehaviorDesigner.Runtime.Tasks.Tooltip("攻击条件最小值")]
- public float conditionMinValue = 0;
- [BehaviorDesigner.Runtime.Tasks.Tooltip("攻击条件最大值")]
- public float conditionMaxValue = 0;
- }
- }
|