-
Notifications
You must be signed in to change notification settings - Fork 97
/
BTTree.cs
67 lines (53 loc) · 1.31 KB
/
BTTree.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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using BT;
// How to use:
// 1. Initiate values in the database for the children to use.
// 2. Initiate BT _root
// 3. Some actions & preconditions that will be used later
// 4. Add children nodes
// 5. Activate the _root, including the children nodes' initialization
public abstract class BTTree : MonoBehaviour {
protected BTNode _root = null;
[HideInInspector]
public Database database;
[HideInInspector]
public bool isRunning = true;
public const string RESET = "Rest";
private static int _resetId;
void Awake () {
Init();
_root.Activate(database);
}
void Update () {
if (!isRunning) return;
if (database.GetData<bool>(RESET)) {
Reset();
database.SetData<bool>(RESET, false);
}
// Iterate the BT tree now!
if (_root.Evaluate()) {
_root.Tick();
}
}
void OnDestroy () {
if (_root != null) {
_root.Clear();
}
}
// Need to be called at the initialization code in the children.
protected virtual void Init () {
database = GetComponent<Database>();
if (database == null) {
database = gameObject.AddComponent<Database>();
}
_resetId = database.GetDataId(RESET);
database.SetData<bool>(_resetId, false);
}
protected void Reset () {
if (_root != null) {
_root.Clear();
}
}
}