82 lines
2.6 KiB
C#
82 lines
2.6 KiB
C#
using System.Collections;
|
|
using RPGCore.Movement.ObjectModules.UnitMovement.Events;
|
|
using RPGCore.ObjectModules.ActionObjectModule;
|
|
using RPGCore.ObjectModules.EventObjectModule;
|
|
using RPGCore.StatusEffect.ObjectModules.StatusObjectModule;
|
|
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
|
|
namespace RPGCore.Movement.ObjectModules.UnitMovement.Actions
|
|
{
|
|
public class PointMoveAction : BaseActionParallel
|
|
{
|
|
private readonly Vector3 _destination;
|
|
|
|
private UnitMovementModule _unitMovement;
|
|
|
|
public PointMoveAction(Vector3 destination)
|
|
{
|
|
_destination = destination;
|
|
}
|
|
|
|
public override void CanDoIt()
|
|
{
|
|
Check(
|
|
unit.GetComponent<StatusModule>().IsControllable(),
|
|
UnitIsBusyMessage);
|
|
}
|
|
|
|
protected override void OnDoIt()
|
|
{
|
|
_unitMovement = unit.GetComponent<UnitMovementModule>();
|
|
|
|
var moveStartEvent = new MoveStartEvent{ unit = unit };
|
|
|
|
unit.events.InvokeBefore(moveStartEvent);
|
|
Check(!moveStartEvent.isPrevented, ActionWasPreventedMessage);
|
|
unit.events.InvokeAfter(moveStartEvent);
|
|
|
|
unit.StartCoroutine(MovePoint_Coroutine());
|
|
}
|
|
|
|
private IEnumerator MovePoint_Coroutine()
|
|
{
|
|
var agent = unit.GetComponent<NavMeshAgent>();
|
|
agent.SetDestination(_destination);
|
|
|
|
var lastPosition = agent.transform.position;
|
|
|
|
while (state == ActionState.Running)
|
|
{
|
|
var speed = agent.speed = _unitMovement.moveMaxSpeed;
|
|
if (agent.remainingDistance <= speed * Time.fixedDeltaTime)
|
|
{
|
|
EndIt();
|
|
yield break;
|
|
};
|
|
|
|
yield return new WaitForFixedUpdate();
|
|
// unit.GetModule<EventModule>().Invoke(new MoveEvent
|
|
// {
|
|
// unit = unit,
|
|
// lastPosition = lastPosition,
|
|
// currentPosition = unit.transform.position
|
|
// });
|
|
//
|
|
// lastPosition = unit.transform.position;
|
|
}
|
|
}
|
|
|
|
protected override void OnEndIt()
|
|
{
|
|
unit.GetComponent<NavMeshAgent>().isStopped = true;
|
|
unit.GetComponent<NavMeshAgent>().enabled = false;
|
|
unit.events.Invoke(new MoveEndEvent{ unit = unit });
|
|
}
|
|
|
|
protected override void OnCancelIt()
|
|
{
|
|
OnEndIt();
|
|
}
|
|
}
|
|
} |