-
Notifications
You must be signed in to change notification settings - Fork 1
/
DungeonCrawl.ino
210 lines (196 loc) · 4.68 KB
/
DungeonCrawl.ino
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "State.h"
#define PATH_COLOR dim(BLUE, 64)
#define AVATAR_COLOR GREEN
#define WALL_COLOR RED
#define FOG_COLOR WHITE
#define RESET_COLOR MAGENTA
enum commandStates {NONE, AVATAR, MOVE, PATH, WALL, WALL_REVEALED, RESET}; //add a treasure tile?
bool revealed = false;
byte heading = 0;
Timer timer;
STATE_DEC(avatarS);
STATE_DEC(avatarMovingS);
STATE_DEC(pathS);
STATE_DEC(wallS);
STATE_DEC(resetBroadcastS);
STATE_DEC(resetIgnoreS);
STATE_DEC(resetS);
STATE_DEF(setupS,
{ //entry
revealed = false;
randomize();
},
{ //loop
if(startState() == START_STATE_WE_ARE_ROOT) {
changeState(avatarS::state);
} else {
changeState(random(2) == 0 ? wallS::state : pathS::state);
}
}
)
STATE_DEF(avatarS,
{ //entry
setValueSentOnAllFaces(AVATAR);
setColor(AVATAR_COLOR);
revealed = true; //will become revealed path after adventurer leaves
},
{ //loop
FOREACH_FACE(f) {
if (!isValueReceivedOnFaceExpired(f)) {
switch(getLastValueReceivedOnFace(f)) {
case MOVE: //avatar is being pulled to neighbor revert to path
changeState(pathS::state);
break;
}
}
}
}
)
STATE_DEF(avatarMovingS,
{ //entry
setValueSentOnFace(MOVE, heading); //tell neighbor avatar is moving here
setColorOnFace(AVATAR_COLOR, heading);
setColorOnFace(AVATAR_COLOR, (heading + 1) % 6);
setColorOnFace(AVATAR_COLOR, (heading + 5) % 6);
},
{ //loop
bool doneMoving = true;
FOREACH_FACE(f) {
if (!isValueReceivedOnFaceExpired(f)) {
switch(getLastValueReceivedOnFace(f)) {
case AVATAR: //wait for all neighbors to be not AVATARs
doneMoving = false;
break;
case RESET:
changeState(resetBroadcastS::state);
break;
}
}
}
if (doneMoving) { //after avatar is confirmed to be here then transition to actual Avatar state
changeState(avatarS::state);
}
if (buttonLongPressed()) {
changeState(resetBroadcastS::state);
}
}
)
STATE_DEF(pathS,
{ //entry
setValueSentOnAllFaces(PATH);
setColor(revealed ? PATH_COLOR : FOG_COLOR);
},
{ //loop
if (buttonSingleClicked()) {
FOREACH_FACE(f) { //check if avatar is on neighbor
if (!isValueReceivedOnFaceExpired(f)) {
switch(getLastValueReceivedOnFace(f)) {
case AVATAR: //reveal and pull avatar
revealed = true;
heading = f;
changeState(avatarMovingS::state);
break;
}
}
}
}
FOREACH_FACE(f) {
if (!isValueReceivedOnFaceExpired(f)) {
switch(getLastValueReceivedOnFace(f)) {
case RESET:
changeState(resetBroadcastS::state);
break;
}
}
}
if (buttonLongPressed()) {
changeState(resetBroadcastS::state);
}
}
)
STATE_DEF(wallS,
{ //entry
setValueSentOnAllFaces(revealed ? WALL_REVEALED : WALL);
setColor(revealed ? WALL_COLOR : FOG_COLOR);
},
{ //loop
if (buttonSingleClicked()) {
FOREACH_FACE(f) { //check if avatar is on neighbor
if (!isValueReceivedOnFaceExpired(f)) {
switch(getLastValueReceivedOnFace(f)) {
case AVATAR: //reveal and don't pull avatar
revealed = true;
heading = f;
changeState(wallS::state);
break;
}
}
}
}
FOREACH_FACE(f) {
if (!isValueReceivedOnFaceExpired(f)) {
switch(getLastValueReceivedOnFace(f)) {
case RESET:
changeState(resetBroadcastS::state);
break;
}
}
}
if (buttonLongPressed()) {
changeState(resetBroadcastS::state);
}
}
)
//broadcast reset for a bit
STATE_DEF(resetBroadcastS,
{ //entry
setValueSentOnAllFaces(RESET);
setColor(RESET_COLOR);
timer.set(512);
},
{ //loop
if (timer.isExpired()) {
changeState(resetIgnoreS::state);
}
}
)
//stop broadcasting reset after a bit, then ignore the reset broadcast
STATE_DEF(resetIgnoreS,
{ //entry
setValueSentOnAllFaces(NONE);
setColor(dim(RESET_COLOR, 128));
timer.set(512);
},
{ //loop
if (timer.isExpired()) {
changeState(resetS::state);
}
if (buttonLongPressed()) {
changeState(resetBroadcastS::state);
}
}
)
//ignore reset wave for a bit more, then reinitialize
STATE_DEF(resetS,
{ //entry
timer.set(512);
},
{ //loop
if (timer.isExpired()) {
changeState(setupS::state);
}
if (buttonLongPressed()) {
changeState(resetBroadcastS::state);
}
}
)
void setup() {
changeState(setupS::state);
}
void loop() {
stateFn();
}
/*
* 3422, 700
* /
*/