-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoardController.hpp
441 lines (365 loc) · 12.6 KB
/
BoardController.hpp
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
Copyright 2019 Zach Vonler
This file is part of LEDbyrinth.
LEDbyrinth is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LEDbyrinth is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LEDbyrinth. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef _BOARDCONTROLLER_HPP_
#define _BOARDCONTROLLER_HPP_
#include "Accelerometer.hpp"
#include "Levels.hpp"
#include "PictureFrame.hpp"
struct Ball {
int xInt() {
return int(x);
}
int yInt() {
return int(y);
}
float x = 0.0;
float y = 0.0;
float v_x = 0.0;
float v_y = 0.0;
const float radius = 0.1;
};
#define EPSILON 0.000001
class BoardController {
public:
BoardController(Adafruit_NeoMatrix& matrix, Accelerometer& accel, LevelSet& levels)
: _matrix(matrix), _accel(accel), _levels(levels),
_levelIndex(1),
_lastUpdateTm(0),
_trapCount(0),
_palette(_matrix)
{
}
void setLevel(byte index) {
_levelIndex = index;
reset();
}
uint32_t nextUpdateTm() {
return _lastUpdateTm + PERIOD;
}
void redrawBoard() {
_matrix.clear();
for (int i = 0; i < _matrix.width(); ++i) {
for (int j = 0; j < _matrix.height(); ++j) {
_matrix.drawPixel(i, j, _palette.colorForCell(level()->cellAt(i, j)));
}
}
_matrix.show();
}
void drawBall() {
_matrix.drawPixel(_ball.xInt(), _ball.yInt(), _palette.ballColor());
_matrix.show();
}
void reset() {
redrawBoard();
_ball.v_x = 0.0;
_ball.v_y = 0.0;
_ball.x = level()->startX;
_ball.y = level()->startY;
drawBall();
_lastUpdateTm = millis();
}
// Returns true if anything on the board changed, false otherwise
bool update() {
if (millis() < nextUpdateTm()) {
return false;
}
int startingX = _ball.xInt();
int startingY = _ball.yInt();
updateBallPosition();
updateBallVelocity();
// If the ball's position has moved across cell boundaries, update the board
bool changed = false;
if ((_ball.xInt() != startingX || _ball.yInt() != startingY)) {
changed = true;
_matrix.drawPixel(startingX, startingY, _palette.colorForCell(level()->cellAt(startingX, startingY)));
_matrix.show();
byte newCell = level()->cellAt(_ball.xInt(), _ball.yInt());
if (newCell >= Cell_PointerBase) {
_matrix.drawPixel(startingX, startingY, _palette.colorForCell(level()->cellAt(startingX, startingY)));
byte destination = newCell - Cell_PointerBase;
byte row = destination / _matrix.width();
byte col = destination % _matrix.width();
animateTeleport(_ball.xInt(), _ball.yInt(), col, row);
_ball.x = float(col);
_ball.y = float(row);
_ball.v_x = 0.0;
_ball.v_y = 0.0;
drawBall();
} else if (newCell >= Cell_WarpBase) {
animateExit();
_levelIndex = newCell - Cell_WarpBase;
reset();
} else {
switch (newCell) {
case Cell_Exit:
animateExit();
_trapCount = 0; // Reset the easter egg
_levelIndex = max(1, (_levelIndex + 1) % _levels.count); // Prevent exiting to level 0 from the last
reset();
break;
case Cell_Trap:
// Easter egg - level 0 is accessible from level 1
if (_levelIndex == 1 && ++_trapCount >= 10) {
_trapCount = 0;
animateExit();
_levelIndex = 0;
reset();
} else {
animateTrap();
reset();
}
break;
case Cell_Empty: // Move the ball
drawBall();
}
}
}
_lastUpdateTm = millis();
return changed;
}
void animateExit() {
uint32_t innerColor = _palette.colorForCell(Cell_Exit);
uint32_t outerColor = _palette.ballColor();
for (int j = 0; j < 6; j++) {
for (int i = 0; i < 22; i += 2) {
_matrix.fillCircle(_ball.xInt(), _ball.yInt(), i, outerColor);
for (int k = i - 1; k >= 0; --k) {
uint32_t color = (k % 2) == (i % 2) ? outerColor : innerColor;
_matrix.drawCircle(_ball.xInt(), _ball.yInt(), k, color);
}
_matrix.show();
delay(20);
}
uint32_t temp = outerColor;
outerColor = innerColor;
innerColor = temp;
}
}
void drawRectCentered(Adafruit_NeoMatrix& gfx, int x, int y, int width, int height, uint16_t color) {
int leftX = x - width / 2;
int topY = y - height / 2;
gfx.drawRect(leftX, topY, width, height, color);
}
void animateTrap() {
uint32_t innerColor = _palette.colorForCell(Cell_Trap);
uint32_t outerColor = _palette.white;
for (int j = 0; j < 6; j++) {
for (int i = 1; i < 8; i += 2) {
drawRectCentered(_matrix, _ball.xInt(), _ball.yInt(), i, i, outerColor);
for (int k = i - 1; k >= 0; --k) {
uint32_t color = (k % 2) == (i % 2) ? outerColor : innerColor;
drawRectCentered(_matrix, _ball.xInt(), _ball.yInt(), k, k, color);
}
_matrix.show();
delay(20);
}
uint32_t temp = outerColor;
outerColor = innerColor;
innerColor = temp;
}
}
void animateTeleport(int startX, int startY, int endX, int endY) {
uint32_t innerColor = _palette.colorForCell(Cell_PointerBase);
uint32_t outerColor = _palette.ballColor();
for (int i = 0; i <= 10; i += 2) {
_matrix.fillCircle(startX, startY, i, outerColor);
for (int k = i - 1; k >= 0; --k) {
uint32_t color = (k % 2) == (i % 2) ? outerColor : innerColor;
_matrix.drawCircle(startX, startY, k, color);
}
_matrix.show();
delay(100);
}
redrawBoard();
delay(100);
for (int i = 10; i > 0; i -= 2) {
_matrix.fillCircle(endX, endY, i, innerColor);
for (int k = i - 1; k >= 0; --k) {
uint32_t color = (k % 2) == (i % 2) ? innerColor : outerColor;
_matrix.drawCircle(endX, endY, k, color);
}
_matrix.show();
delay(100);
redrawBoard();
}
}
struct ColorPalette {
ColorPalette(Adafruit_NeoMatrix& matrix) {
orange = matrix.Color(90, 45, 0);
green = matrix.Color(0, 80, 0);
white = matrix.Color(60, 60, 60);
red = matrix.Color(80, 0, 0);
purple = matrix.Color(90, 0, 90);
off = matrix.Color(0, 0, 0);
blue = matrix.Color(0, 0, 80);
cyan = matrix.Color(0, 45, 90);
}
uint32_t ballColor() {
return blue;
}
uint32_t colorForCell(byte cell) {
if (cell >= Cell_PointerBase) return orange;
if (cell > Cell_WarpBase) return green;
switch (cell) {
case Cell_WarpBase : return purple;
case Cell_Wall : return white;
case Cell_Wall_Purple : return purple;
case Cell_Trap : return red;
case Cell_Wall_Cyan: return cyan;
case Cell_Exit : return green;
case Cell_Empty :
default : return off;
}
}
uint32_t orange;
uint32_t green;
uint32_t white;
uint32_t red;
uint32_t purple;
uint32_t off;
uint32_t blue;
uint32_t cyan;
};
bool validLocation(int x, int y) {
byte potential = level()->cellAt(x, y);
if (potential >= Cell_WarpBase) { // Covers warps and pointers
return true;
} else {
switch (potential) {
case Cell_Empty:
case Cell_Trap:
case Cell_Exit:
return true;
case Cell_Wall:
case Cell_Wall_Purple:
case Cell_Wall_Cyan:
default: // Unknown cells treated like walls
return false;
}
}
}
void updateBallPosition() {
// Apply the ball's velocity to its position if possible
float potentialX = _ball.x + _ball.v_x;
float potentialY = _ball.y + _ball.v_y;
if (potentialX < 0) {
potentialX = 0.0 + _ball.radius;
} else if (int(potentialX) >= _matrix.width()) {
potentialX = _matrix.width() - _ball.radius;
}
if (potentialY < 0) {
potentialY = 0.0 + _ball.radius;
} else if (int(potentialY) >= _matrix.height()) {
potentialY = _matrix.height() - _ball.radius;
}
// If not changing cells, no need to check interference
if (int(potentialX) == _ball.xInt() && int(potentialY) == _ball.yInt()) {
_ball.x = potentialX;
_ball.y = potentialY;
return;
}
if (int(potentialX) != _ball.xInt() && int(potentialY) != _ball.yInt()) {
// Both X & Y position potentially changing
byte pathAValid = validLocation(_ball.xInt(), int(potentialY));
byte pathBValid = validLocation(int(potentialX), _ball.yInt());
if (validLocation(int(potentialX), int(potentialY))) {
if (pathAValid && pathBValid) {
_ball.x = potentialX;
_ball.y = potentialY;
} else if (pathAValid) {
_ball.y = potentialY;
} else if (pathBValid) {
_ball.x = potentialX;
}
}
} else if (int(potentialY) != _ball.yInt()) {
// If potential destination is not a valid location,
// place the ball as close as it can get.
if (validLocation(_ball.xInt(), int(potentialY))) {
_ball.y = potentialY;
} else if (potentialY > _ball.y) {
_ball.y = float(int(potentialY)) - _ball.radius;
} else {
_ball.y = float(int(potentialY + 1)) + _ball.radius;
}
_ball.x = potentialX;
} else if (int(potentialX) != _ball.xInt()) {
// If potential destination is not a valid location,
// place the ball as close as it can get.
if (validLocation(int(potentialX), _ball.yInt())) {
_ball.x = potentialX;
} else if (potentialX > _ball.x) {
_ball.x = float(int(potentialX)) - _ball.radius;
} else {
_ball.x = float(int(potentialX + 1)) + _ball.radius;
}
_ball.y = potentialY;
}
}
const float deadZone = 0.08;
const float velocityDecay = 0.75;
void updateBallVelocity() {
float xAccel = -1.0 * _accel.readXAxis();
if (fabs(xAccel) < deadZone) {
_ball.v_x *= velocityDecay;
} else if (xAccel >= 0) {
// If accelerating against a wall or edge, set velocity to zero
if (_ball.x + _ball.radius >= float(_matrix.width()) || !validLocation(int(_ball.x + _ball.radius + EPSILON), _ball.yInt())) {
_ball.v_x = 0.0;
} else {
_ball.v_x += xAccel * PERIOD / 1000.0;
}
} else {
if (_ball.x - _ball.radius - EPSILON <= 0.0 || !validLocation(int(_ball.x - _ball.radius - EPSILON), _ball.yInt())) {
_ball.v_x = 0.0;
} else {
_ball.v_x += xAccel * PERIOD / 1000.0;
}
}
_ball.v_x = max(-1.0, min(1.0, _ball.v_x));
float yAccel = -1.0 * _accel.readYAxis();
if (fabs(yAccel) < deadZone) {
_ball.v_y *= velocityDecay;
} else if (yAccel >= 0) {
// If accelerating against a wall or edge, set velocity to zero
if (_ball.y + _ball.radius >= float(_matrix.height()) || !validLocation(_ball.xInt(), int(_ball.y + _ball.radius + EPSILON))) {
_ball.v_y = 0.0;
} else {
_ball.v_y += yAccel * PERIOD / 1000.0;
}
} else {
if (_ball.y - _ball.radius - EPSILON <= 0.0 || !validLocation(_ball.xInt(), int(_ball.y - _ball.radius - EPSILON))) {
_ball.v_y = 0.0;
} else {
_ball.v_y += yAccel * PERIOD / 1000.0;
}
}
_ball.v_y = max(-1.0, min(1.0, _ball.v_y));
}
protected:
Adafruit_NeoMatrix& _matrix;
Accelerometer& _accel;
LevelSet& _levels;
byte _levelIndex;
long _lastUpdateTm;
Ball _ball;
byte _trapCount;
ColorPalette _palette;
const long PERIOD = 5; // millis
Level* level() {
return &_levels.levels[_levelIndex];
}
};
#endif