-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
522 lines (468 loc) · 9.4 KB
/
game.cpp
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include "game.h"
#include "defines.h"
#include "tileset.h"
#include "draw.h"
#include "title.h"
#include "levels.h"
uint8_t underlevel[MAP_FULL_SIZE];
extern uint8_t editorlevel[];
uint8_t level[MAP_FULL_SIZE];
uint8_t current_level = 0;
bool finished = false;
bool has_player = true;
uint8_t rules[TILE_COUNT];
void updateRules();
bool isSubject(uint8_t tile);
bool isStatus(uint8_t tile);
uint8_t getStatus(uint8_t tile);
void updateGame();
void victory();
void moveDown();
void moveUp();
void moveLeft();
void moveRight();
uint8_t getSubject(uint8_t tile);
bool isPush(uint8_t tile);
bool isStop(uint8_t tile);
bool isYou(uint8_t tile);
bool isWin(uint8_t tile);
bool isSink(uint8_t tile);
bool isKill(uint8_t tile);
bool isSwap(uint8_t tile);
bool isWord(uint8_t tile);
void moveImpl(uint8_t i, int8_t delta, uint8_t min, uint8_t max);
void convertTile(uint8_t from, uint8_t to);
void initLevel(uint8_t id)
{
if (id == level_count)
{
for (uint8_t i = 0; i < MAP_FULL_SIZE; i++)
{
level[i] = editorlevel[i];
}
}
else
{
for (uint8_t i = 0; i < MAP_FULL_SIZE; i++)
{
level[i] = levels[id][i];
}
}
}
void updateGame()
{
if (!has_player)
{
if ((gb.frameCount >> 2) % 2)
{
gb.lights.fill(RED);
}
else
{
gb.lights.clear();
}
}
if (gb.buttons.pressed(BUTTON_DOWN))
{
moveDown();
}
else if (gb.buttons.pressed(BUTTON_UP))
{
moveUp();
}
else if (gb.buttons.pressed(BUTTON_LEFT))
{
moveLeft();
}
else if (gb.buttons.pressed(BUTTON_RIGHT))
{
moveRight();
}
else if (gb.buttons.pressed(BUTTON_B))
{
startLevel(current_level);
return;
}
else if (gb.buttons.pressed(BUTTON_MENU))
{
gotoScreen(LEVEL_SELECT_SCREEN);
return;
}
else
{
return;
}
updateRules();
for (int i = 0; i < MAP_FULL_SIZE; i++)
{
if (isYou(level[i]) && (isWin(underlevel[i]) || isWin(level[i])))
{
current_level++;
finished = true;
}
else if (isSink(underlevel[i]))
{
level[i] = EMPTY;
underlevel[i] = EMPTY;
}
else if (isKill(underlevel[i]))
{
level[i] = underlevel[i];
underlevel[i] = EMPTY;
}
}
}
void victory()
{
memset(buffer1, 0x00, ROW_SIZE);
drawText(60, 4, "VICTORY", buffer1);
drawScreenSlice(56, 16, buffer1);
waitForPreviousDraw();
for (int i = 0; i < 10; i++)
{
gb.lights.fill(YELLOW);
delay(100);
gb.lights.clear();
delay(100);
}
if (current_level == level_count)
{
gotoScreen(MENU_SCREEN);
}
else if (current_level == level_count_sd)
{
gotoScreen(EDITOR_SCREEN);
}
else
{
startLevel(current_level);
}
finished = false;
}
void initRules()
{
rules[EMPTY] = 0;
rules[W_BABA] = IS_PUSH;
rules[W_FLAG] = IS_PUSH;
rules[W_ROCK] = IS_PUSH;
rules[W_IS] = IS_PUSH;
rules[W_WIN] = IS_PUSH;
rules[W_PUSH] = IS_PUSH;
rules[W_WALL] = IS_PUSH;
rules[BABA] = 0;
rules[FLAG] = 0;
rules[ROCK] = 0;
rules[WALL] = 0;
rules[W_STOP] = IS_PUSH;
rules[W_YOU] = IS_PUSH;
rules[W_GOOP] = IS_PUSH;
rules[GOOP] = 0;
rules[W_SINK] = IS_PUSH;
rules[W_LAVA] = IS_PUSH;
rules[LAVA] = 0;
rules[W_KILL] = IS_PUSH;
rules[W_LOVE] = IS_PUSH;
rules[LOVE] = 0;
rules[W_SWAP] = 0;
rules[W_EMPTY]= IS_PUSH;
for (uint8_t i = 0; i < MAP_FULL_SIZE; i++)
{
underlevel[i] = EMPTY;
}
}
void updateRules()
{
rules[BABA] = 0;
rules[FLAG] = 0;
rules[ROCK] = 0;
rules[WALL] = 0;
rules[GOOP] = 0;
rules[LAVA] = 0;
rules[LOVE] = 0;
for (int i = 0; i < MAP_FULL_SIZE; i++)
{
if (level[i] == W_IS)
{
uint8_t x = i % MAP_WIDTH;
uint8_t y = i / MAP_WIDTH;
if (x > 0 && x < MAP_WIDTH - 1)
{
uint8_t subject = level[i - 1];
uint8_t status = level[i + 1];
if (isSubject(subject))
{
if (isStatus(status))
{
rules[ getSubject(subject) ] |= getStatus(status);
}
else if (isSubject(status))
{
convertTile(getSubject(subject), getSubject(status));
}
}
}
if (y > 0 && y < GAME_SLICES - 1)
{
uint8_t subject = level[i - MAP_WIDTH];
uint8_t status = level[i + MAP_WIDTH];
if (isSubject(subject))
{
if (isStatus(status))
{
rules[ getSubject(subject) ] |= getStatus(status);
}
else if (isSubject(status))
{
convertTile(getSubject(subject), getSubject(status));
}
}
}
}
}
}
bool isSubject(uint8_t tile)
{
switch (tile)
{
case W_BABA:
case W_FLAG:
case W_ROCK:
case W_WALL:
case W_GOOP:
case W_LAVA:
case W_LOVE:
case W_EMPTY:
return true;
}
return false;
}
bool isStatus(uint8_t tile)
{
switch (tile)
{
case W_PUSH:
case W_STOP:
case W_WIN:
case W_YOU:
case W_SINK:
case W_KILL:
case W_SWAP:
return true;
}
return false;
}
uint8_t getStatus(uint8_t tile)
{
switch (tile)
{
case W_PUSH: return IS_PUSH;
case W_STOP: return IS_STOP;
case W_WIN: return IS_WIN;
case W_YOU: return IS_YOU;
case W_SINK: return IS_SINK;
case W_KILL: return IS_KILL;
case W_SWAP: return IS_SWAP;
}
return 0;
}
uint8_t getSubject(uint8_t tile)
{
switch (tile)
{
case W_BABA: return BABA;
case W_WALL: return WALL;
case W_ROCK: return ROCK;
case W_FLAG: return FLAG;
case W_GOOP: return GOOP;
case W_LAVA: return LAVA;
case W_LOVE: return LOVE;
case W_EMPTY:return EMPTY;
}
return 0;
}
void moveDown()
{
for (int8_t i = MAP_WIDTH * (GAME_SLICES - 1); i >= 0; i--)
{
if (isYou(level[i]))
{
moveImpl(i, MAP_WIDTH, 0, MAP_FULL_SIZE);
}
}
}
void moveUp()
{
for (uint8_t i = MAP_WIDTH; i < MAP_FULL_SIZE; i++)
{
if (isYou(level[i]))
{
moveImpl(i, -MAP_WIDTH, 0, MAP_FULL_SIZE);
}
}
}
void moveLeft()
{
for (uint8_t i = 0; i < MAP_FULL_SIZE; i++)
{
if (isYou(level[i]) && (i % MAP_WIDTH) > 0)
{
uint8_t row_start = (i / MAP_WIDTH) * MAP_WIDTH;
moveImpl(i, -1, row_start, row_start + MAP_WIDTH);
}
}
}
void moveRight()
{
for (uint8_t k = 0; k < MAP_FULL_SIZE; k++)
{
uint8_t i = MAP_FULL_SIZE - k - 1;
if (isYou(level[i]) && (i % MAP_WIDTH) < MAP_WIDTH - 1)
{
uint8_t row_start = (i / MAP_WIDTH) * MAP_WIDTH;
moveImpl(i, 1, row_start, row_start + MAP_WIDTH);
}
}
}
void moveImpl(uint8_t i, int8_t delta, uint8_t min, uint8_t max)
{
uint8_t firstEmpty = 0xff;
for (uint8_t yy = i + delta; yy >= min && yy < max; yy += delta)
{
if (isStop(level[yy]))
{
break;
}
else if (isSwap(level[yy]))
{
uint8_t saved = level[yy];
level[yy] = level[yy - delta];
level[yy - delta] = saved;
break;
}
if (!isPush(level[yy]) && !isYou(level[yy]))
{
firstEmpty = yy;
break;
}
}
if (firstEmpty < 0xff)
{
for (uint8_t c = firstEmpty; c != i; c -= delta)
{
if (!isPush(level[c]) && !isYou(level[c]))
{
underlevel[c] = level[c];
}
level[c] = level[c - delta];
level[c - delta] = underlevel[c - delta];
underlevel[c - delta] = EMPTY;
}
}
}
bool isPush(uint8_t tile)
{
return (rules[tile] & IS_PUSH);
}
bool isStop(uint8_t tile)
{
return (rules[tile] & IS_STOP);
}
bool isYou(uint8_t tile)
{
return (rules[tile] & IS_YOU);
}
bool isWin(uint8_t tile)
{
return (rules[tile] & IS_WIN);
}
bool isSink(uint8_t tile)
{
return (rules[tile] & IS_SINK);
}
bool isKill(uint8_t tile)
{
return (rules[tile] & IS_KILL);
}
bool isSwap(uint8_t tile)
{
return (rules[tile] & IS_SWAP);
}
bool isWord(uint8_t tile)
{
return isSubject(tile) || isStatus(tile) || tile == W_IS;
}
#define SLICE_HEIGHT 12
void startLevel(uint8_t id)
{
gotoScreen(GAME_SCREEN);
gb.lights.clear();
has_player = true;
current_level = id;
initLevel(id);
initRules();
updateRules();
}
void gameTick()
{
updateGame();
if (CURRENT_SCREEN != GAME_SCREEN)
{
return;
}
if (!has_player)
{
return;
}
for (uint8_t sliceIndex = 0; sliceIndex < GAME_SLICES; sliceIndex++)
{
uint16_t * buffer = sliceIndex % 2 == 0 ? buffer1 : buffer2;
for (int16_t s = 0; s < MAP_WIDTH;s++)
{
const uint16_t * sprBegin = &tileset[ level[sliceIndex * MAP_WIDTH + s] * TILE_SIZE * TILE_SIZE ];
for (int y = 0; y < TILE_SIZE; y++)
{
memcpy(&buffer[SCREEN_WIDTH * y + s * TILE_SIZE + 2], sprBegin + y * TILE_SIZE, TILE_SIZE*2);
}
}
if (sliceIndex != 0) waitForPreviousDraw();
drawScreenSlice(sliceIndex * SLICE_HEIGHT, SLICE_HEIGHT, buffer);
}
uint16_t * buffer_black = GAME_SLICES % 2 == 0 ? buffer1 : buffer2;
memset(buffer_black, 0x00, SCREEN_WIDTH * 16);
waitForPreviousDraw();
drawScreenSlice(120, 8, buffer_black);
waitForPreviousDraw();
if (finished)
{
victory();
}
else if (has_player)
{
has_player = false;
for (uint8_t i = 0; i < MAP_FULL_SIZE; i++)
{
if (isYou(level[i]))
{
has_player = true;
break;
}
}
}
if (!has_player)
{
memset(buffer1, 0x00, ROW_SIZE);
drawText(32, 4, "B: RESTART LEVEL", buffer1);
drawScreenSlice(56, 16, buffer1);
waitForPreviousDraw();
}
}
void convertTile(uint8_t from, uint8_t to)
{
for (uint8_t k = 0; k < MAP_FULL_SIZE; k++)
{
if (level[k] == from)
{
level[k] = to;
}
}
}