-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBehaviorTree.cs
182 lines (144 loc) · 4.6 KB
/
BehaviorTree.cs
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections.Generic;
namespace BehaviorTree
{
/// <summary>
/// The entry class for behaviour tree.
/// </summary>
public class Tree : ISharedVariableContainer
{
public Tree()
{
}
public Tree(string name, Node root = null) {
m_Name = name;
m_RootNode = root;
m_SharedVariables = new Dictionary<string, SharedVariable>();
m_RestartWhenComplete = false;
}
public Tree(Tree other)
{
m_Name = other.m_Name;
m_SharedVariables = new Dictionary<string, SharedVariable>();
foreach (var pair in other.m_SharedVariables)
{
m_SharedVariables.Add(pair.Key, pair.Value.Clone());
}
m_RestartWhenComplete = other.m_RestartWhenComplete;
}
/// <summary>
/// Update the time of the behaviour tree.
/// </summary>
public BehaviorTreeStatus Tick()
{
if (m_RestartWhenComplete &&
(m_RootNode.ExecutionStatus == BehaviorTreeStatus.Success || m_RootNode.ExecutionStatus == BehaviorTreeStatus.Failure))
{
m_RootNode.Restart();
}
return m_RootNode.Tick();
}
public void SetExternalSource(object agent, object world)
{
if (m_RootNode == null)
{
throw new InvalidOperationException("Root Node is empty!!!");
}
m_RootNode.SetExternalSource(agent, world);
}
/// <summary>
/// Awake is used to initialize any variables or state before this tree start.
/// Awake is called only once during the lifetime of this tree.
/// </summary>
public virtual void Awake()
{
m_RootNode.Awake();
}
public virtual Tree Clone(object agent, object world)
{
var tree = new Tree(this);
//m_SharedVariables["Self"].SetValue(agent);
tree.m_RootNode = m_RootNode.Clone(agent, world, tree);
return tree;
}
public virtual Tree Clone()
{
var tree = new Tree(this);
tree.m_RootNode = m_RootNode.Clone(tree);
return tree;
}
/// <summary>
/// Restart this behavior tree. This make this tree can be reevaluted.
/// </summary>
public void Restart()
{
m_RootNode.Restart();
}
/// <summary>
/// The execution status of this tree.
/// </summary>
public BehaviorTreeStatus ExecutionStatus
{
get => m_RootNode.ExecutionStatus;
}
public void SetRootNode(Node root)
{
m_RootNode = root;
}
public Node GetRootNode()
{
return m_RootNode;
}
public void SetVariable(SharedVariable variable)
{
string name = variable.Name;
if (m_SharedVariables == null)
{
m_SharedVariables = new Dictionary<string, SharedVariable>();
}
if (m_SharedVariables.ContainsKey(name))
{
m_SharedVariables[name] = variable;
}
else
{
m_SharedVariables.Add(name, variable);
}
}
public void SetVariable(string name, SharedVariable variable)
{
variable.Name = name;
if (m_SharedVariables.ContainsKey(name))
{
m_SharedVariables[name] = variable;
}
else
{
m_SharedVariables.Add(name, variable);
}
}
public SharedVariable GetVariable(string name)
{
if (m_SharedVariables.ContainsKey(name))
{
return m_SharedVariables[name];
}
throw new ArgumentException("Variable " + name + " dos't exist!");
}
public bool HasVariable(string name)
{
return m_SharedVariables.ContainsKey(name);
}
public bool RestartWhenComplete
{
get=> m_RestartWhenComplete;
set => m_RestartWhenComplete = value;
}
[SerializeField]
private string m_Name;
[SerializeField]
private Node m_RootNode;
private Dictionary<string, SharedVariable> m_SharedVariables;
private bool m_RestartWhenComplete;
}
}