-
Notifications
You must be signed in to change notification settings - Fork 23
/
Selector.cs
47 lines (39 loc) · 1.34 KB
/
Selector.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
namespace WUG.BehaviorTreeVisualizer
{
public class Selector : Composite
{
int currentChild = 0;
/// <summary>
/// Runs each child until one of them returns status Success. Will return failure if no child runs successfully.
/// </summary>
/// <param name="name">Friendly name - displayed on the Behavior Tool Debugger UI if set</param>
/// <param name="childNodes">Children node(s) to run</param>
public Selector(string name, params Node[] childNodes) : base(name, childNodes) { }
protected override void OnReset()
{
currentChild = 0;
for (int i = 0; i < ChildNodes.Count; i++)
{
(ChildNodes[i] as Node).Reset();
}
}
protected override NodeStatus OnRun()
{
if (currentChild >= ChildNodes.Count)
{
return NodeStatus.Failure;
}
NodeStatus nodeStatus = (ChildNodes[currentChild]as Node).Run();
switch (nodeStatus)
{
case NodeStatus.Failure:
currentChild++;
break;
case NodeStatus.Success:
return NodeStatus.Success;
}
//in progress
return NodeStatus.Running;
}
}
}