From 97da432c35b8c7aaf9dd2c39e2aa4b1f55f36065 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 27 Jan 2021 16:15:06 +0800 Subject: +behaviour designer --- .../Runtime/Composites/PrioritySelector.cs | 72 ++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Client/Assets/Behavior Designer/Runtime/Composites/PrioritySelector.cs (limited to 'Client/Assets/Behavior Designer/Runtime/Composites/PrioritySelector.cs') diff --git a/Client/Assets/Behavior Designer/Runtime/Composites/PrioritySelector.cs b/Client/Assets/Behavior Designer/Runtime/Composites/PrioritySelector.cs new file mode 100644 index 00000000..5f2dac27 --- /dev/null +++ b/Client/Assets/Behavior Designer/Runtime/Composites/PrioritySelector.cs @@ -0,0 +1,72 @@ +using System.Collections.Generic; + +namespace BehaviorDesigner.Runtime.Tasks +{ + [TaskDescription("Similar to the selector task, the priority selector task will return success as soon as a child task returns success. " + + "Instead of running the tasks sequentially from left to right within the tree, the priority selector will ask the task what its priority is to determine the order. " + + "The higher priority tasks have a higher chance at being run first.")] + [HelpURL("http://www.opsive.com/assets/BehaviorDesigner/documentation.php?id=29")] + [TaskIcon("{SkinColor}PrioritySelectorIcon.png")] + public class PrioritySelector : Composite + { + // The index of the child that is currently running or is about to run. + private int currentChildIndex = 0; + // The task status of every child task. + private TaskStatus executionStatus = TaskStatus.Inactive; + // The order to run its children in. + private List childrenExecutionOrder = new List(); + + public override void OnStart() + { + // Make sure the list is empty before we add child indexes to it. + childrenExecutionOrder.Clear(); + + // Loop through each child task and determine its priority. The higher the priority the lower it goes within the list. The task with the highest + // priority will be first in the list and will be executed first. + for (int i = 0; i < children.Count; ++i) { + float priority = children[i].GetPriority(); + int insertIndex = childrenExecutionOrder.Count; + for (int j = 0; j < childrenExecutionOrder.Count; ++j) { + if (children[childrenExecutionOrder[j]].GetPriority() < priority) { + insertIndex = j; + break; + } + } + childrenExecutionOrder.Insert(insertIndex, i); + } + } + + public override int CurrentChildIndex() + { + // Use the execution order list in order to determine the current child index. + return childrenExecutionOrder[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