using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAPI;
namespace BossRoom.Server
{
public class ChaseAction : Action
{
private NetworkedObject m_Target;
private ServerCharacterMovement m_Movement;
private Vector3 m_CurrentTargetPos;
public ChaseAction(ServerCharacter parent, ref ActionRequestData data, int level) : base(parent, ref data, level)
{
}
///
/// Called when the Action starts actually playing (which may be after it is created, because of queueing).
///
/// false if the action decided it doesn't want to run after all, true otherwise.
public override bool Start()
{
if(m_Data.TargetIds == null || m_Data.TargetIds.Length == 0 || !MLAPI.Spawning.SpawnManager.SpawnedObjects.ContainsKey(m_Data.TargetIds[0]) )
{
Debug.Log("Failed to start ChaseAction. The target entity wasn't submitted or doesn't exist anymore" );
return false;
}
m_Target = MLAPI.Spawning.SpawnManager.SpawnedObjects[m_Data.TargetIds[0]];
m_Movement = m_Parent.GetComponent();
m_Movement.SetMovementTarget(m_Target.transform.position);
m_CurrentTargetPos = m_Target.transform.position;
return true;
}
///
/// Called each frame while the action is running.
///
/// true to keep running, false to stop. The Action will stop by default when its duration expires, if it has a duration set.
public override bool Update()
{
float dist_to_target = (m_Parent.transform.position - m_Target.transform.position).magnitude;
if( m_Data.Amount > dist_to_target )
{
//we made it! we're done.
Cancel();
return false;
}
float target_moved = (m_Target.transform.position - m_CurrentTargetPos).magnitude;
if( m_Data.Amount < target_moved )
{
//target has moved past our range tolerance. Must repath.
this.m_Movement.SetMovementTarget(m_Target.transform.position);
m_CurrentTargetPos = m_Target.transform.position;
}
return true;
}
public override void Cancel()
{
if( m_Movement != null )
{
m_Movement.CancelMove();
}
}
}
}