-
Notifications
You must be signed in to change notification settings - Fork 16
/
Decorator.cs
94 lines (86 loc) · 2.58 KB
/
Decorator.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MBT
{
public abstract class Decorator : Node, IParentNode, IChildrenNode
{
private Node[] stackState = new Node[0];
public override void AddChild(Node node)
{
// Allow only one children
if (this.children.Count > 0)
{
Node child = this.children[0];
if (child == node) {
return;
}
child.parent.RemoveChild(child);
this.children.Clear();
}
// Remove parent in case there is one already
if (node.parent != null) {
node.parent.RemoveChild(node);
}
this.children.Add(node);
node.parent = this;
}
protected Node GetChild()
{
if (children.Count > 0) {
return children[0];
}
return null;
}
public override void RemoveChild(Node node)
{
if (children.Contains(node))
{
children.Remove(node);
node.parent = null;
}
}
/// <summary>
/// Copy and store current state of execution stack if it was not saved before.
/// </summary>
protected void StoreBTState()
{
// TODO: GetStack generates garbage when array is copied - this can be improved
if (stackState.Length == 0)
{
stackState = behaviourTree.GetStack();
}
}
protected void DisposeBTState()
{
stackState = new Node[0];
}
internal Node[] GetStoredBTState()
{
return stackState;
}
/// <summary>
/// Helper method used to abort nodes in valid case
/// </summary>
/// <param name="abort">Abort type</param>
protected void TryAbort(Abort abort)
{
switch (abort)
{
case Abort.Self:
if (status == Status.Running) {
behaviourTree.Interrupt(this);
}
break;
case Abort.LowerPriority:
if (status == Status.Success || status == Status.Failure) {
behaviourTree.Interrupt(this);
}
break;
case Abort.Both:
behaviourTree.Interrupt(this);
break;
}
}
}
}