using BehaviorDesigner.Runtime.Tasks.EnemyAI;
using UnityEngine;
using UnityEngine.AI;
using UnityGameFramework.Runtime;
namespace BehaviorDesigner.Runtime.Tasks.Movement
{
[TaskDescription("ToTarget using the Unity NavMesh.")]
[TaskCategory("YLSJ")]
[HelpURL("http://www.opsive.com/assets/BehaviorDesigner/Movement/documentation.php?id=9")]
[TaskIcon("Assets/Behavior Designer Movement/Editor/Icons/{SkinColor}WanderIcon.png")]
public class ActionMoveToTarget : NavMeshMovementBase
{
[Tooltip("终点目标Object")]
public SharedTransform targetEndObj;
///
/// 跟随的目标
///
public SharedTransform target;
public SharedTransform targetObject;
[Tooltip("检测目标的层级")]
public LayerMask objectLayerMask;
public SharedFloat viewDistance = 10;
// 是否移动中
private bool bMove = false;
private Vector3 movePos = Vector3.zero;
///
/// 出生最大距离
///
private float liveAlertDistance = 0;
private BehaviorTree behaviorTree = null;
///
/// 路径点顺序/倒叙
///
private bool bPointAdd = true;
///
/// 是否赶往出生点
///
private bool bToLive = false;
private Role role = null;
public override void OnAwake()
{
base.OnAwake();
behaviorTree = gameObject.GetComponent();
}
public override void OnStart()
{
targetEndObj.Value = GameObject.Find("AIManager/EndPoint").transform;
base.OnStart();
role = GetComponent();
liveAlertDistance = (float)behaviorTree.GetVariable("liveAlertDistance").GetValue();
if (m_NavMeshAgent.component.isOnNavMesh)
{
m_NavMeshAgent.component.isStopped = false;
}
movePos = targetEndObj.Value.position;
SetDestination(movePos);
}
public override TaskStatus OnUpdate()
{
Transform objectFound = null;
target.Value = null;
//var hitColliders = Physics.OverlapSphere(transform.position, viewDistance.Value, objectLayerMask);
//if (hitColliders != null)
//{
// for (int i = 0; i < hitColliders.Length; ++i)
// {
// if (hitColliders[i].gameObject.activeSelf == false)
// {
// continue;
// }
// Role role = hitColliders[i].gameObject.GetComponent();
// if (role == null || role.isDie)
// {
// continue;
// }
// target.Value = role.transform;
// }
//}
//if (target != null && target.Value != null)
//{
// float sqrDistance = (target.Value.position - transform.position).sqrMagnitude;
// if (sqrDistance < viewDistance.Value)
// {
// //SetDestination(target.Value.position);
// targetObject.Value = target.Value;
// return TaskStatus.Success;
// }
//}
SetDestination(movePos);
if (Vector3.Distance(transform.position, targetEndObj.Value.position) < 1.5f)
{
EventComponent eventCmpt = GameEntry.GetComponent();
eventCmpt.FireNow(this, new DefenseEventEscapeNum());
RoleManager.Instance.DeleteRole(role);
}
//else
//{
// SetDestination(targetEndObj.Value.position);
//}
return TaskStatus.Running;
}
public override void OnEnd()
{
base.OnEnd();
}
protected bool SetDestination(Vector3 destination)
{
NavMeshHit hit;
bool has = NavMesh.SamplePosition(destination, out hit, 5, NavMesh.AllAreas);
if (has && m_NavMeshAgent.component.isOnNavMesh)
{
m_NavMeshAgent.component.SetDestination(hit.position);
}
return true;
}
// Reset the public variables
public override void OnReset()
{
}
}
}