From 21e186f75b504d832d9c7bef0456edd7d5d3155e Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 8 Sep 2021 10:52:35 +0800 Subject: +behavior design --- .../Runtime/Composites/Selector.cs | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Assets/ThirdParty/Behavior Designer/Runtime/Composites/Selector.cs (limited to 'Assets/ThirdParty/Behavior Designer/Runtime/Composites/Selector.cs') diff --git a/Assets/ThirdParty/Behavior Designer/Runtime/Composites/Selector.cs b/Assets/ThirdParty/Behavior Designer/Runtime/Composites/Selector.cs new file mode 100644 index 00000000..e85b1ca5 --- /dev/null +++ b/Assets/ThirdParty/Behavior Designer/Runtime/Composites/Selector.cs @@ -0,0 +1,46 @@ +namespace BehaviorDesigner.Runtime.Tasks +{ + [TaskDescription("The selector task is similar to an \"or\" operation. It will return success as soon as one of its child tasks return success. " + + "If a child task returns failure then it will sequentially run the next task. If no child task returns success then it will return failure.")] + [HelpURL("http://www.opsive.com/assets/BehaviorDesigner/documentation.php?id=26")] + [TaskIcon("{SkinColor}SelectorIcon.png")] + public class Selector : Composite + { + // The index of the child that is currently running or is about to run. + private int currentChildIndex = 0; + // The task status of the last child ran. + private TaskStatus executionStatus = TaskStatus.Inactive; + + public override int CurrentChildIndex() + { + return currentChildIndex; + } + + public override bool CanExecute() + { + // We can continue to execuate as long as we have children that haven't been executed and no child has returned success. + return currentChildIndex < children.Count && executionStatus != TaskStatus.Success; + } + + public override void OnChildExecuted(TaskStatus childStatus) + { + // Increase the child index and update the execution status after a child has finished running. + currentChildIndex++; + executionStatus = childStatus; + } + + public override void OnConditionalAbort(int childIndex) + { + // Set the current child index to the index that caused the abort + currentChildIndex = childIndex; + executionStatus = TaskStatus.Inactive; + } + + public override void OnEnd() + { + // All of the children have run. Reset the variables back to their starting values. + executionStatus = TaskStatus.Inactive; + currentChildIndex = 0; + } + } +} \ No newline at end of file -- cgit v1.1-26-g67d0