CharacterMoveToPositionTaskAction.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using NodeCanvas.Framework;
  5. using ParadoxNotion.Design;
  6. using KairoEngine.Core;
  7. namespace KairoEngine.CharacterSystem
  8. {
  9. [Category("KairoEngine")]
  10. public class CharacterMoveToPositionTaskAction : ActionTask
  11. {
  12. [BlackboardOnly, RequiredField] public BBParameter<CharacterController> character;
  13. [BlackboardOnly, RequiredField] public BBParameter<Vector3> position;
  14. [RequiredField] public BBParameter<float> stoppingDistance = 1f;
  15. public MoveSpeed moveSpeed = MoveSpeed.walk;
  16. protected override string info {
  17. get { return "Move character to position"; }
  18. }
  19. protected override void OnExecute()
  20. {
  21. float translationSpeed = 1.25f;
  22. float animationSpeed = 1f;
  23. if(moveSpeed == MoveSpeed.run)
  24. {
  25. translationSpeed = 3.5f;
  26. animationSpeed = 2f;
  27. }
  28. GenericEvents.StartListening(character.value.unique_name + "-ArrivedInPositionAction", End);
  29. ICommand movecommand = new CharacterMoveToPositionCommand(character.value, position.value, translationSpeed, animationSpeed, stoppingDistance.value);
  30. CommandInvoker.AddCommand(movecommand);
  31. }
  32. protected override void OnUpdate()
  33. {
  34. float distance = Vector3.Distance(character.value.transform.position, position.value);
  35. if(distance < stoppingDistance.value) End();
  36. }
  37. protected override void OnStop()
  38. {
  39. End();
  40. }
  41. private void End()
  42. {
  43. GenericEvents.StopListening(character.value.unique_name + "-ArrivedInPositionAction", End);
  44. ICommand stopCommand = new CharacterStopMoveCommand(character.value);
  45. CommandInvoker.AddCommand(stopCommand);
  46. EndAction(true);
  47. }
  48. }
  49. }