Parallel.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. namespace BehaviorDesigner.Runtime.Tasks
  2. {
  3. [TaskDescription("Similar to the sequence task, the parallel task will run each child task until a child task returns failure. " +
  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. "Like the sequence class, the parallel task will return success once all of its children tasks have return success. " +
  6. "If one tasks returns failure the parallel task will end all of the child tasks and return failure.")]
  7. [TaskIcon("{SkinColor}ParallelIcon.png")]
  8. public class Parallel : 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 TaskStatus OverrideStatus(TaskStatus status)
  45. {
  46. // 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
  47. // or failed. If a task is still running then all of the children are not done executing and the parallel task should continue to return a task status of running.
  48. // If a task failed then return failure. The Behavior Manager will stop all of the children tasks. If no child task is running or has failed then the parallel
  49. // task succeeded and it will return success.
  50. bool childrenComplete = true;
  51. for (int i = 0; i < executionStatus.Length; ++i) {
  52. if (executionStatus[i] == TaskStatus.Running) {
  53. childrenComplete = false;
  54. } else if (executionStatus[i] == TaskStatus.Failure) {
  55. return TaskStatus.Failure;
  56. }
  57. }
  58. return (childrenComplete ? TaskStatus.Success : TaskStatus.Running);
  59. }
  60. public override void OnConditionalAbort(int childIndex)
  61. {
  62. // Start from the beginning on an abort
  63. currentChildIndex = 0;
  64. for (int i = 0; i < executionStatus.Length; ++i) {
  65. executionStatus[i] = TaskStatus.Inactive;
  66. }
  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. }