forked from mikelaurence/peabrain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Peabrain.cs
61 lines (46 loc) · 1.52 KB
/
Peabrain.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
using UnityEngine;
using System;
using System.Collections;
public class Peabrain : PeabrainNode {
public enum Status {
Success, Continue, Failure, Exception
}
public Actionable actor;
public object lastDelegateResult;
public PeabrainNode rootNode;
public PeabrainNode currentNode;
public ArrayList currentFilters = new ArrayList();
public Peabrain(Actionable actor) {
this.actor = actor;
}
public void Think() {
// Run filters
if (currentFilters.Count > 0) {
for (int f = 0; f < currentFilters.Count; f++) {
if (((PeabrainFilter) currentFilters[f]).Filter()) {
//PeabrainNode filterParent = ((PeabrainNode) currentFilters[f]).parent;
PeabrainFilter filter = (PeabrainFilter) currentFilters[f];
// Remove this and all later filters from stack
currentFilters.RemoveRange(f, currentFilters.Count - f);
filter.OnFilter();
// Move action to node just above filter
//filterParent.OnChildFailure();
}
}
}
if (currentNode == null)
currentNode = rootNode;
currentNode.Execute();
}
public Type ActorType() {
return actor.GetType();
}
public void LoadFromJSON(string jsonString) {
Hashtable hash = (Hashtable) JSON.JsonDecode(jsonString);
children = new PeabrainNode[]{ NodeFromJSONHash(hash, this) };
rootNode = children[0];
}
public override void OnChildSuccess() { currentNode = rootNode; }
public override void OnChildFailure() { currentNode = rootNode; }
public override void OnChildException() { currentNode = rootNode; }
}