ParallelSelector.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. namespace BehaviorDesigner.Runtime.Tasks
  2. {
  3. [TaskDescription("Similar to the selector task, the parallel selector task will return success as soon as a child task returns success. " +
  4. "The difference is that the parallel task will run all of its children tasks simultaneously versus running each task one at a time. " +
  5. "If one tasks returns success the parallel selector task will end all of the child tasks and return success. " +
  6. "If every child task returns failure then the parallel selector task will return failure.")]
  7. [TaskIcon("{SkinColor}ParallelSelectorIcon.png")]
  8. public class ParallelSelector : Composite
  9. {
  10. // The index of the child that is currently running or is about to run.
  11. private int currentChildIndex;
  12. // The task status of every child task.
  13. private TaskStatus[] executionStatus;
  14. public override void OnAwake()
  15. {
  16. // Create a new task status array that will hold the execution status of all of the children tasks.
  17. executionStatus = new TaskStatus[children.Count];
  18. }
  19. public override void OnChildStarted(int childIndex)
  20. {
  21. // One of the children has started to run. Increment the child index and set the current task status of that child to running.
  22. currentChildIndex++;
  23. executionStatus[childIndex] = TaskStatus.Running;
  24. }
  25. public override bool CanRunParallelChildren()
  26. {
  27. // This task can run parallel children.
  28. return true;
  29. }
  30. public override int CurrentChildIndex()
  31. {
  32. return currentChildIndex;
  33. }
  34. public override bool CanExecute()
  35. {
  36. // We can continue executing if we have more children that haven't been started yet.
  37. return currentChildIndex < children.Count;
  38. }
  39. public override void OnChildExecuted(int childIndex, TaskStatus childStatus)
  40. {
  41. // One of the children has finished running. Set the task status.
  42. executionStatus[childIndex] = childStatus;
  43. }
  44. public override void OnConditionalAbort(int childIndex)
  45. {
  46. // Start from the beginning on an abort
  47. currentChildIndex = 0;
  48. for (int i = 0; i < executionStatus.Length; ++i) {
  49. executionStatus[i] = TaskStatus.Inactive;
  50. }
  51. }
  52. public override TaskStatus OverrideStatus(TaskStatus status)
  53. {
  54. // Assume all of the children have finished executing. Loop through the execution status of every child and check to see if any tasks are currently running
  55. // or succeeded. If a task is still running then all of the children are not done executing and the parallel selector task should continue to return a task status of running.
  56. // If a task succeeded then return success. The Behavior Manager will stop all of the children tasks. If no child task is running or has succeeded then the parallel selector
  57. // task failed and it will return failure.
  58. bool childrenComplete = true;
  59. for (int i = 0; i < executionStatus.Length; ++i) {
  60. if (executionStatus[i] == TaskStatus.Running) {
  61. childrenComplete = false;
  62. } else if (executionStatus[i] == TaskStatus.Success) {
  63. return TaskStatus.Success;
  64. }
  65. }
  66. return (childrenComplete ? TaskStatus.Failure : TaskStatus.Running);
  67. }
  68. public override void OnEnd()
  69. {
  70. // Reset the execution status and the child index back to their starting values.
  71. for (int i = 0; i < executionStatus.Length; ++i) {
  72. executionStatus[i] = TaskStatus.Inactive;
  73. }
  74. currentChildIndex = 0;
  75. }
  76. }
  77. }