summaryrefslogtreecommitdiff
path: root/Client/Assets/Behavior Designer/Runtime/Actions/StartBehaviorTree.cs
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-01-27 16:15:06 +0800
committerchai <chaifix@163.com>2021-01-27 16:15:06 +0800
commit97da432c35b8c7aaf9dd2c39e2aa4b1f55f36065 (patch)
treed9b1db5908a3a030c529e230386fe01062923b09 /Client/Assets/Behavior Designer/Runtime/Actions/StartBehaviorTree.cs
parent1fe4ffba72f56ccc6a89d1896142425c666887d4 (diff)
+behaviour designer
Diffstat (limited to 'Client/Assets/Behavior Designer/Runtime/Actions/StartBehaviorTree.cs')
-rw-r--r--Client/Assets/Behavior Designer/Runtime/Actions/StartBehaviorTree.cs90
1 files changed, 90 insertions, 0 deletions
diff --git a/Client/Assets/Behavior Designer/Runtime/Actions/StartBehaviorTree.cs b/Client/Assets/Behavior Designer/Runtime/Actions/StartBehaviorTree.cs
new file mode 100644
index 00000000..e817713a
--- /dev/null
+++ b/Client/Assets/Behavior Designer/Runtime/Actions/StartBehaviorTree.cs
@@ -0,0 +1,90 @@
+using UnityEngine;
+
+namespace BehaviorDesigner.Runtime.Tasks
+{
+ [TaskDescription("Start a new behavior tree and return success after it has been started.")]
+ [HelpURL("http://www.opsive.com/assets/BehaviorDesigner/documentation.php?id=20")]
+ [TaskIcon("{SkinColor}StartBehaviorTreeIcon.png")]
+ public class StartBehaviorTree : Action
+ {
+ [Tooltip("The GameObject of the behavior tree that should be started. If null use the current behavior")]
+ public SharedGameObject behaviorGameObject;
+ [Tooltip("The group of the behavior tree that should be started")]
+ public SharedInt group;
+ [Tooltip("Should this task wait for the behavior tree to complete?")]
+ public SharedBool waitForCompletion = false;
+ [Tooltip("Should the variables be synchronized?")]
+ public SharedBool synchronizeVariables;
+
+ private bool behaviorComplete;
+ private Behavior behavior;
+
+ public override void OnStart()
+ {
+ var behaviorTrees = GetDefaultGameObject(behaviorGameObject.Value).GetComponents<Behavior>();
+ if (behaviorTrees.Length == 1) {
+ behavior = behaviorTrees[0];
+ } else if (behaviorTrees.Length > 1) {
+ for (int i = 0; i < behaviorTrees.Length; ++i) {
+ if (behaviorTrees[i].Group == group.Value) {
+ behavior = behaviorTrees[i];
+ break;
+ }
+ }
+ // If the group can't be found then use the first behavior tree
+ if (behavior == null) {
+ behavior = behaviorTrees[0];
+ }
+ }
+
+ if (behavior != null) {
+ var variables = Owner.GetAllVariables();
+ for (int i = 0; i < variables.Count; ++i) {
+ behavior.SetVariable(variables[i].Name, variables[i]);
+ }
+
+ behavior.EnableBehavior();
+
+ if (waitForCompletion.Value) {
+ behaviorComplete = false;
+ behavior.OnBehaviorEnd += BehaviorEnded;
+ }
+ }
+ }
+
+ public override TaskStatus OnUpdate()
+ {
+ if (behavior == null) {
+ return TaskStatus.Failure;
+ }
+
+ // Return a status of running if we are waiting for the behavior tree to end and it hasn't ended yet
+ if (waitForCompletion.Value && !behaviorComplete) {
+ return TaskStatus.Running;
+ }
+
+ return TaskStatus.Success;
+ }
+
+ private void BehaviorEnded()
+ {
+ behaviorComplete = true;
+ }
+
+ public override void OnEnd()
+ {
+ if (behavior != null && waitForCompletion.Value) {
+ behavior.OnBehaviorEnd -= BehaviorEnded;
+ }
+ }
+
+ public override void OnReset()
+ {
+ // Reset the properties back to their original values.
+ behaviorGameObject = null;
+ group = 0;
+ waitForCompletion = false;
+ synchronizeVariables = false;
+ }
+ }
+} \ No newline at end of file