forked from MinaPecheux/UnityTutorials-RTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.cs
More file actions
87 lines (73 loc) · 2.06 KB
/
Copy pathNode.cs
File metadata and controls
87 lines (73 loc) · 2.06 KB
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
using System.Collections.Generic;
namespace BehaviorTree
{
public enum NodeState
{
RUNNING,
SUCCESS,
FAILURE
}
public class Node
{
protected NodeState _state;
public NodeState State { get => _state; }
private Node _parent;
protected List<Node> children = new List<Node>();
private Dictionary<string, object> _dataContext = new Dictionary<string, object>();
public Node()
{
_parent = null;
}
public Node(List<Node> children) : this()
{
SetChildren(children);
}
public virtual NodeState Evaluate() => NodeState.FAILURE;
public void SetChildren(List<Node> children)
{
foreach (Node c in children)
Attach(c);
}
public void Attach(Node child)
{
children.Add(child);
child._parent = this;
}
public void Detach(Node child)
{
children.Remove(child);
child._parent = null;
}
public object GetData(string key)
{
object val = null;
if (_dataContext.TryGetValue(key, out val))
return val;
Node node = _parent;
if (node != null)
val = node.GetData(key);
return val;
}
public bool ClearData(string key)
{
bool cleared = false;
if (_dataContext.ContainsKey(key))
{
_dataContext.Remove(key);
return true;
}
Node node = _parent;
if (node != null)
cleared = node.ClearData(key);
return cleared;
}
public void SetData(string key, object value)
{
_dataContext[key] = value;
}
public Node Parent { get => _parent; }
public List<Node> Children { get => children; }
public bool HasChildren { get => children.Count > 0; }
public virtual bool IsFlowNode => false;
}
}