-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.chai
More file actions
148 lines (120 loc) · 2.82 KB
/
example.chai
File metadata and controls
148 lines (120 loc) · 2.82 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// dummy mock class represents an Actor
class Actor
{
// c-tor
def Actor()
{
this.saidHello = false;
this.rotating = false;
this.lookAroundCounter = 0;
this.lookAroundLimiter = 5;
this.stoppedCounter = 0;
this.rotateCounter = 0;
}
// methods
def sayHello()
{
print("Hello!\n");
this.saidHello = true;
}
def canLookAround()
{
if(this.lookAroundCounter >= this.lookAroundLimiter)
{
return false;
}
++this.lookAroundCounter;
return true;
}
def isStopped()
{
++this.stoppedCounter;
if(this.stoppedCounter % 3 == 0) // 2/3 of checks will fail
{
return true;
}
return false;
}
def stop()
{
print("Stop.");
}
def isStillRotating()
{
if(this.rotating == false)
{
return false;
}
++this.rotateCounter;
if(this.rotateCounter % 5 == 0) // 4/5 of checks will confirm that actor is rotating
{
return false;
}
return true;
}
def rotate(degrees)
{
if(this.rotating == true)
{
return;
}
this.rotating = true;
print("I'll turn ${degrees} degrees!\n");
}
// class' data
var saidHello;
var rotating;
var lookAroundCounter;
var lookAroundLimiter;
var stoppedCounter;
var rotateCounter;
}
global hero = Actor();
BT.AddSelector(); // root
BT.AddSequence(); // sayHello
BT.AddSequence(); // lookAround
// set active node: first (zero-based indexing) child of root:
BT.SetAtAbsolutely(0);
BT.AddInvert();
// go to first child of invert decorator node:
BT.SetAtRelatively(0);
// saidHello condition node:
BT.AddCondition(fun()
{
return hero.saidHello;
});
// get back to the 'sayHello' sequence:
BT.SetAtAbsolutely(0);
// add say "Hello" action node:
BT.AddAction(fun()
{
hero.sayHello();
return StateSuccess;
});
// set as active 'lookAround' sequence:
BT.SetAtAbsolutely(1);
// canLookAround condition node:
BT.AddCondition(fun()
{
return hero.canLookAround();
});
// Stop action node:
BT.AddAction(fun()
{
if(hero.isStopped() == true)
{
return StateSuccess;
}
hero.stop();
return StateRunning;
});
// Rotate360 action:
BT.AddAction(fun()
{
hero.rotate(360);
if(hero.isStillRotating() == true)
{
return StateRunning;
}
return StateSuccess;
});