123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- 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<CharacterController> character;
- [BlackboardOnly, RequiredField] public BBParameter<Transform> target;
- [RequiredField] public BBParameter<float> attackDistance = 1f;
- public MoveSpeed moveSpeed = MoveSpeed.walk;
- public BBParameter<float> 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);
-
- }
- }
- }
|