Sequence.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. namespace BehaviorDesigner.Runtime.Tasks
  2. {
  3. [TaskDescription("The sequence task is similar to an \"and\" operation. It will return failure as soon as one of its child tasks return failure. " +
  4. "If a child task returns success then it will sequentially run the next task. If all child tasks return success then it will return success.")]
  5. [TaskIcon("{SkinColor}SequenceIcon.png")]
  6. public class Sequence : Composite
  7. {
  8. // The index of the child that is currently running or is about to run.
  9. private int currentChildIndex = 0;
  10. // The task status of the last child ran.
  11. private TaskStatus executionStatus = TaskStatus.Inactive;
  12. public override int CurrentChildIndex()
  13. {
  14. return currentChildIndex;
  15. }
  16. public override bool CanExecute()
  17. {
  18. // We can continue to execuate as long as we have children that haven't been executed and no child has returned failure.
  19. return currentChildIndex < children.Count && executionStatus != TaskStatus.Failure;
  20. }
  21. public override void OnChildExecuted(TaskStatus childStatus)
  22. {
  23. // Increase the child index and update the execution status after a child has finished running.
  24. currentChildIndex++;
  25. executionStatus = childStatus;
  26. }
  27. public override void OnConditionalAbort(int childIndex)
  28. {
  29. // Set the current child index to the index that caused the abort
  30. currentChildIndex = childIndex;
  31. executionStatus = TaskStatus.Inactive;
  32. }
  33. public override void OnEnd()
  34. {
  35. // All of the children have run. Reset the variables back to their starting values.
  36. executionStatus = TaskStatus.Inactive;
  37. currentChildIndex = 0;
  38. }
  39. }
  40. }