using System.Collections; using System.Collections.Generic; using UnityEngine; using NodeCanvas.Framework; using ParadoxNotion.Design; using KairoEngine.Core; namespace KairoEngine.CharacterSystem { public enum MoveSpeed { walk, run } [Category("KairoEngine")] public class CharacterMoveToTargetTaskAction : ActionTask { [BlackboardOnly, RequiredField] public BBParameter character; [BlackboardOnly, RequiredField] public BBParameter target; [RequiredField] public BBParameter attackDistance = 1f; public MoveSpeed moveSpeed = MoveSpeed.walk; public BBParameter lostTargetTimer = 1f; private float counter = 0f; private Transform _target; private Vector3 pos; private bool success = true; private bool done = false; protected override string info { get { return "Move character to target"; } } protected override void OnExecute() { _target = target.value; IssueCommand(); counter = 0f; } protected override void OnUpdate() { if(target.value == null && !done) { counter += Time.deltaTime; if(counter > lostTargetTimer.value) { EndAction(false); } } else counter = 0f; float distance = Vector3.Distance(character.value.transform.position, _target.position); if(distance < attackDistance.value) EndAction(true); float distanceFromTarget = Vector3.Distance(_target.position, pos); if(distanceFromTarget > 1f && !done) { GenericEvents.StopListening(character.value.unique_name + "-ArrivedInPositionAction", End); IssueCommand(); done = false; } } protected override void OnStop() { GenericEvents.StopListening(character.value.unique_name + "-ArrivedInPositionAction", End); ICommand stopCommand = new CharacterStopMoveCommand(character.value); CommandInvoker.AddCommand(stopCommand); } private void End() { EndAction(success); } private void IssueCommand() { float translationSpeed = 1.25f; float animationSpeed = 1f; if(moveSpeed == MoveSpeed.run) { translationSpeed = 3.5f; animationSpeed = 2f; } pos = _target.position; GenericEvents.StartListening(character.value.unique_name + "-ArrivedInPositionAction", End); ICommand movecommand = new CharacterMoveToPositionCommand(character.value, pos, translationSpeed, animationSpeed, attackDistance.value); CommandInvoker.AddCommand(movecommand); } } }