-
Notifications
You must be signed in to change notification settings - Fork 0
/
BehaviourTreeBase.cs
104 lines (85 loc) · 2.4 KB
/
BehaviourTreeBase.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
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
public enum NodeStatus
{
FAILURE,
SUCCESS,
RUNNING
}
public abstract class BehaviourState
{
}
public abstract class Node
{
public bool starting = true;
protected bool debug = false;
public int ticks = 0;
public static List<string> debugTypeBlacklist = new List<string>() { "Selector", "Sequence", "Repeater", "Inverter", "Succeeder" };
public virtual NodeStatus Update(BehaviourState State)
{
NodeStatus ret = OnUpdate(State);
if (debug && !debugTypeBlacklist.Contains(GetType().Name))
{
string result = "Unknown";
switch (ret)
{
case NodeStatus.SUCCESS:
result = "Success";
break;
case NodeStatus.FAILURE:
result = "Failure";
break;
case NodeStatus.RUNNING:
result = "Running";
break;
}
Debug.Log("Behaving: " + GetType().Name + " - " + result);
}
ticks++;
starting = false;
if (ret != NodeStatus.RUNNING)
Reset();
return ret;
}
public abstract NodeStatus OnUpdate(BehaviourState state);
public void Reset()
{
starting = true;
ticks = 0;
OnReset();
}
public abstract void OnReset();
}
public abstract class Composite : Node
{
protected List<Node> children = new List<Node>();
public string compositeName;
public Composite(string name, params Node[] nodes)
{
compositeName = name;
children.AddRange(nodes);
}
public override NodeStatus Update(BehaviourState state)
{
bool shouldLog = debug && ticks == 0 ? true : false;
if (shouldLog)
Debug.Log("Running behaviour list: " + compositeName);
NodeStatus ret = base.Update(state);
if (debug && ret != NodeStatus.RUNNING)
Debug.Log("Behaviour list " + compositeName + " returned: " + ret.ToString());
return ret;
}
}
public abstract class Leaf : Node
{
}
public abstract class Decorator : Node
{
protected Node child;
public Decorator (Node node)
{
child = node;
}
}