using System; using System.Collections; using RPGCore.Movement.ObjectModules.UnitMovement.Events; using RPGCore.ObjectModules.ActionObjectModule; using UnityEngine; namespace RPGCore.Movement.ObjectModules.UnitMovement.Actions { public class DirectionMoveAction : BaseAction { private readonly Func _directionGetter; private UnitMovementModule _unitMovement; public DirectionMoveAction(Func directionGetter) { _directionGetter = directionGetter; } public override void CanDoIt() { Check( unit.GetComponent().isControllable, UnitIsBusyMessage); } protected override void OnDoIt() { _unitMovement = unit.GetComponent(); var moveStartEvent = new MoveStartEvent{ unit = unit }; unit.events.InvokeBefore(moveStartEvent); Check(!moveStartEvent.isPrevented, ActionWasPreventedMessage); unit.events.InvokeAfter(moveStartEvent); unit.StartCoroutine(MoveCoroutine()); } private IEnumerator MoveCoroutine() { while (state == ActionState.Running) { _unitMovement.moveInput = _directionGetter(); yield return new WaitForFixedUpdate(); } } protected override void OnEndIt() { _unitMovement.moveInput = Vector2.zero; _unitMovement.isSprinting = false; unit.events.Invoke(new MoveEndEvent{ unit = unit }); } protected override void OnCancelIt() { OnEndIt(); } } }