blob: 214644fb74f915df8abdce4f91fe82dd29d6c346 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
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();
if (variables != null && synchronizeVariables.Value) {
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;
}
}
}
|