forked from Press-Play-On-Tape/LodeRunner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLodeRunner_EnemyMovements.ino
executable file
·587 lines (346 loc) · 17.1 KB
/
LodeRunner_EnemyMovements.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
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
#include "Arduboy2Ext.h"
#include <ArduboyTones.h>
#include "sounds.h"
// ------------------------------------------------------------------------------------------
// Clear the 'Future Position' values for each enemy prior to mving them ..
//
// The future positions are stored against the enemy to be able to plan the movement of all
// enemies
// ------------------------------------------------------------------------------------------
void clearEnemyMovementPositions(Enemy *enemies) {
for (uint8_t x = 0; x < NUMBER_OF_ENEMIES; x++) {
Enemy &enemy = enemies[x];
enemy.setXFuturePosition(0);
enemy.setYFuturePosition(0);
}
}
// ------------------------------------------------------------------------------------------
// Move an enemy ..
// ------------------------------------------------------------------------------------------
void enemyMovements(Enemy *enemy) {
uint8_t enemyX = enemy->getX() / GRID_SIZE;
uint8_t enemyY = enemy->getY() / GRID_SIZE;
LevelElement current = level.getLevelData(enemyX, enemyY);
LevelElement down = level.getLevelData(enemyX, enemyY + 1);
// Check to see if the enemy has touched gold!
if (current == LevelElement::Gold && enemy->getGoldCountdown() == 0) {
if (random(0, ENEMY_GOLD_PICKUP_THRESHOLD) == 0) {
enemy->setGoldCountdown(random(ENEMY_GOLD_HOLD_MINIMUM, ENEMY_GOLD_HOLD_MAXIMUM));
level.setLevelData(enemyX, enemyY, LevelElement::Blank);
}
}
// Move enemy ..
const auto stance = enemy->getStance();
// If the enemy is being reborn, return without moving ..
if (stance >= PlayerStance::Rebirth_1) return;
// If the enemy is falling, then continue falling ..
if (stance == PlayerStance::Falling) {
if (current < LevelElement::Brick_1 && current > LevelElement::Brick_Close_4 && canContinueToFall_Enemy(down)) return;
}
if (enemy->getX() % GRID_SIZE == 0 && enemy->getY() % GRID_SIZE == 0) {
bool hasMoved = false;
// If the enemy is in a hole, then attemt to wiggle out ..
if (enemy->getEscapeHole() > EscapeHole::None) {
// If the enemy has gold, then make it available to pickup ..
if (enemy->getGoldCountdown() > 0 && level.getLevelData(enemyX, enemyY - 1) == LevelElement::Blank) {
enemy->setGoldCountdown(0);
level.setLevelData(enemyX, enemyY - 1, LevelElement::Gold);
}
enemy->setYDelta(0);
enemy->setEscapeHole(static_cast<EscapeHole>(static_cast<uint8_t>(enemy->getEscapeHole()) - 1));
}
// Otherwise move the enemy towards the player ..
else {
Direction direction = Direction::Up;
LevelElement up = level.getLevelData(enemyX, enemyY - 1);
LevelElement right = level.getLevelData(enemyX + 1, enemyY);
LevelElement rightDown = level.getLevelData(enemyX + 1, enemyY + 1);
LevelElement leftDown = level.getLevelData(enemyX - 1, enemyY + 1);
LevelElement left = level.getLevelData(enemyX - 1, enemyY);
// LevelElement down = level.getLevelData(enemyX, enemyY + 1);
int16_t xDiff = enemy->getX() - (player.getX() - level.getXOffset());
int16_t yDiff = enemy->getY() - (player.getY() - level.getYOffset());
if (enemy->getDirectionCountdown() > 0) {
// Can the enemy move in the preferred direction ?
bool preferredHasMoved = attemptToMove(enemy, enemyX, enemyY, enemy->getPreferredDirection(), current, up, right, rightDown, down, leftDown, left, false);
enemy->decrementDirectionCountdown();
if (preferredHasMoved) return;
direction = enemy->getDirection();
}
else {
direction = getDirection_16Directions(xDiff, yDiff);
enemy->setPreferredDirection(getDirection_4Directions(direction));
}
// Drop the gold?
if (enemy->getGoldCountdown() > 0 && isSolid(down) && current == LevelElement::Blank) {
enemy->decrementGoldCountdown();
if (enemy->getGoldCountdown() == 0) {
level.setLevelData(enemyX, enemyY, LevelElement::Gold);
}
}
Direction direction1 = direction;
Direction direction2 = direction;
// Can the enemy move in the first direction ?
if (attemptToMove(enemy, enemyX, enemyY, direction, current, up, right, rightDown, down, leftDown, left, true)) return;
// If not, try to move in the closest possible direction to the preferred ..
for (uint8_t x = 0; x < 8; x++) {
if (!hasMoved) {
direction1--;
hasMoved = attemptToMove(enemy, enemyX, enemyY, direction1, current, up, right, rightDown, down, leftDown, left, true);
if (hasMoved) break;
}
if (!hasMoved) {
direction2++;
hasMoved = attemptToMove(enemy, enemyX, enemyY, direction2, current, up, right, rightDown, down, leftDown, left, true);
if (hasMoved) break;
}
}
// If no movement and we are falling, check to see if we have hit the bottom ..
if (enemy->getStance() == PlayerStance::Falling) {
if (canBeStoodOn_Enemy(down)) {
enemy->setYDelta(0);
}
}
}
}
}
// ------------------------------------------------------------------------------------------
// Can the *enemy move into the nominated cell or is it occupied by others ?
// ------------------------------------------------------------------------------------------
boolean isOccupiedByAnotherEnemy(Enemy *enemies, Enemy *enemy, int8_t xDelta, int8_t yDelta) {
for (uint8_t x = 0; x < NUMBER_OF_ENEMIES; x++) {
const Enemy & testEnemy = enemies[x];
if (!testEnemy.getEnabled() || testEnemy.getId() == enemy->getId()) continue;
const int16_t targetX = static_cast<int16_t>(testEnemy.getX() + testEnemy.getXFuturePosition());
const int16_t targetY = static_cast<int16_t>(testEnemy.getY() + testEnemy.getYFuturePosition());
const Rect testRect = { targetX, targetY, GRID_SIZE, GRID_SIZE };
const int16_t enemyX = static_cast<int16_t>(enemy->getX()) + xDelta;
const int16_t enemyY = static_cast<int16_t>(enemy->getY()) + yDelta;
const Rect enemyRect = { enemyX, enemyY, GRID_SIZE, GRID_SIZE };
if (arduboy.collide(testRect, enemyRect)) return true;
}
return false;
}
// ------------------------------------------------------------------------------------------
// Attempt to move in the nominated direction ..
// ------------------------------------------------------------------------------------------
boolean attemptToMove(Enemy *enemy, uint8_t enemyX, uint8_t enemyY,
Direction direction,
LevelElement current, LevelElement up,
LevelElement right, LevelElement rightDown, LevelElement down,
LevelElement leftDown, LevelElement left, bool randomMoves) {
switch (direction) {
case Direction::LeftUp ... Direction::LeftUp2:
case Direction::Up ... Direction::RightUp:
{
bool canBeOccupied = canBeOccupiedBasic_Enemy(up);
bool occupiedByAnotherEnemy = !isOccupiedByAnotherEnemy(enemies, enemy, 0, -GRID_SIZE);
if ( current == LevelElement::Ladder && canBeOccupied && occupiedByAnotherEnemy) {
updateEnemyStance(enemy, PlayerStance::Climbing_Up1, PlayerStance::Climbing_Up2);
move(enemy, 0, -2, randomMoves);
return true;
}
}
break;
case Direction::RightUp2 ... Direction::RightDown1:
{
bool notOccupiedByAnotherEnemy_Right = !isOccupiedByAnotherEnemy(enemies, enemy, GRID_SIZE, 0);
bool canBeFallenInto_Down = canBeFallenInto_Enemy(down, enemies, enemyX, enemyY + 1);
bool canBeFallenInto_RightDown = canBeFallenInto_Enemy(rightDown, enemies, enemyX + 1, enemyY + 1);
bool canBeOccupied_Right = canBeOccupied_Enemy(right);
bool canBeStoodOn_RightDown = canBeStoodOn_Enemy(rightDown);
if (right == LevelElement::Rail && notOccupiedByAnotherEnemy_Right) {
updateEnemyStance(enemy, PlayerStance::Swinging_Right1, PlayerStance::Swinging_Right4);
move(enemy, 2, 0, randomMoves);
return true;
}
else if (current == LevelElement::Rail && canBeOccupied_Right && canBeStoodOn_RightDown && notOccupiedByAnotherEnemy_Right) {
updateEnemyStance(enemy, PlayerStance::Running_Right1, PlayerStance::Running_Right4);
move(enemy, 2, 0, randomMoves);
return true;
}
else if (current == LevelElement::Ladder && canBeOccupied_Right && notOccupiedByAnotherEnemy_Right) {
updateEnemyStance(enemy, PlayerStance::Climbing_Up1, PlayerStance::Climbing_Up2);
move(enemy, 2, 0, randomMoves);
return true;
}
else if (canBeFallenInto_Down) {
enemy->setStance(PlayerStance::Falling);
move(enemy, 0, 2, randomMoves);
// If we have fallen into a brick hole, start counting ..
switch (down) {
case LevelElement::Brick_1 ... LevelElement::Brick_Close_4:
enemy->setEscapeHole(EscapeHole::WaitMax);
sound.tones(enemyFallsIn);
break;
default: break;
}
return true;
}
else if (canBeStoodOn_RightDown && canBeOccupied_Right && notOccupiedByAnotherEnemy_Right) {
updateEnemyStance(enemy, PlayerStance::Running_Right1, PlayerStance::Running_Right4);
move(enemy, 2, 0, randomMoves);
return true;
}
else if (canBeOccupied_Right && canBeFallenInto_RightDown && notOccupiedByAnotherEnemy_Right) {
updateEnemyStance(enemy, PlayerStance::Running_Right1, PlayerStance::Running_Right4);
move(enemy, 2, 0, randomMoves);
return true;
}
}
break;
case Direction::RightDown ... Direction::LeftDown:
{
bool notOccupiedByAnotherEnemy_Right = !isOccupiedByAnotherEnemy(enemies, enemy, 0, GRID_SIZE);
bool canBeFallenInto_Down = canBeFallenInto_Enemy(down, enemies, enemyX, enemyY + 1);
if (down == LevelElement::Ladder && notOccupiedByAnotherEnemy_Right) {
updateEnemyStance(enemy, PlayerStance::Climbing_Down2, PlayerStance::Climbing_Down1);
move(enemy, 0, 2, randomMoves);
return true;
}
else if (canBeFallenInto_Down && notOccupiedByAnotherEnemy_Right) {
enemy->setStance(PlayerStance::Falling);
move(enemy, 0, 2, randomMoves);
// If we have fallen into a brick hole, start counting ..
switch (down) {
case LevelElement::Brick_1 ... LevelElement::Brick_Close_4:
enemy->setEscapeHole(EscapeHole::WaitMax);
break;
default: break;
}
return true;
}
}
break;
case Direction::LeftDown2 ... Direction::LeftUp1:
{
bool notOccupiedByAnotherEnemy_Left = !isOccupiedByAnotherEnemy(enemies, enemy, -GRID_SIZE, 0);
bool canBeFallenInto_Down = canBeFallenInto_Enemy(down, enemies, enemyX, enemyY + 1);
bool canBeFallenInto_LeftDown = canBeFallenInto_Enemy(leftDown, enemies, enemyX - 1, enemyY + 1);
bool canBeOccupied_Left = canBeOccupied_Enemy(left);
bool canBeStoodOn_LeftDown = canBeStoodOn_Enemy(leftDown);
if (left == LevelElement::Rail && notOccupiedByAnotherEnemy_Left) {
updateEnemyStance(enemy, PlayerStance::Swinging_Left4, PlayerStance::Swinging_Left1);
move(enemy, -2, 0, randomMoves);
return true;
}
else if (current == LevelElement::Rail && canBeOccupied_Left && canBeStoodOn_LeftDown && notOccupiedByAnotherEnemy_Left) {
updateEnemyStance(enemy, PlayerStance::Running_Left4, PlayerStance::Running_Left1);
move(enemy, -2, 0, randomMoves);
return true;
}
else if (current == LevelElement::Ladder && canBeOccupied_Left && notOccupiedByAnotherEnemy_Left) {
updateEnemyStance(enemy, PlayerStance::Climbing_Up1, PlayerStance::Climbing_Up2);
move(enemy, -2, 0, randomMoves);
return true;
}
else if (canBeFallenInto_Down) {
enemy->setStance(PlayerStance::Falling);
move(enemy, 0, 2, randomMoves);
// If we have fallen into a brick hole, start counting ..
switch (down) {
case LevelElement::Brick_1 ... LevelElement::Brick_Close_4:
enemy->setEscapeHole(EscapeHole::WaitMax);
sound.tones(enemyFallsIn);
break;
default: break;
}
return true;
}
else if (canBeStoodOn_LeftDown && canBeOccupied_Left && notOccupiedByAnotherEnemy_Left) {
updateEnemyStance(enemy, PlayerStance::Running_Left4, PlayerStance::Running_Left1);
move(enemy, -2, 0, randomMoves);
return true;
}
else if (canBeOccupied_Left && canBeFallenInto_LeftDown && notOccupiedByAnotherEnemy_Left) {
updateEnemyStance(enemy, PlayerStance::Swinging_Left4, PlayerStance::Swinging_Left1);
move(enemy, -2, 0, randomMoves);
return true;
}
}
break;
default: break;
}
return false;
}
// ------------------------------------------------------------------------------------------
// If a player has just exitted a hole then try to move away from it ..
// ------------------------------------------------------------------------------------------
void setDirectionAfterHoleEscape(Enemy *enemy) {
uint8_t enemyX = enemy->getX() / GRID_SIZE;
uint8_t enemyY = enemy->getY() / GRID_SIZE;
LevelElement left = level.getLevelData(enemyX - 1, enemyY);
LevelElement leftDown = level.getLevelData(enemyX - 1, enemyY + 1);
LevelElement right = level.getLevelData(enemyX + 1, enemyY);
LevelElement rightDown = level.getLevelData(enemyX + 1, enemyY + 1);
if (static_cast<int16_t>(enemy->getX()) > (static_cast<int16_t>(player.getX()) - level.getXOffset())) {
if (canBeOccupied_Enemy(left) && canBeStoodOn_Enemy(leftDown)) {
enemy->setStance(PlayerStance::Running_Left1);
enemy->setXDelta(-2);
}
else {
enemy->setStance(PlayerStance::Running_Right1);
enemy->setXDelta(2);
}
}
else {
if (canBeOccupied_Enemy(right) && canBeStoodOn_Enemy(rightDown)) {
enemy->setStance(PlayerStance::Running_Right1);
enemy->setXDelta(2);
}
else {
enemy->setStance(PlayerStance::Running_Left1);
enemy->setXDelta(-2);
}
}
}
// ------------------------------------------------------------------------------------------
// Move the enemy .. if the randomMoves flag is set, then pick a distance to travel.
// ------------------------------------------------------------------------------------------
void move(Enemy *enemy, int8_t x, int8_t y, bool randomMoves) {
enemy->setXDelta(x);
enemy->setYDelta(y);
enemy->setXFuturePosition(HALF_GRID_SIZE * x);
enemy->setYFuturePosition(HALF_GRID_SIZE * y);
if (x < 0) { enemy->setDirection(Direction::Left); }
if (x > 0) { enemy->setDirection(Direction::Right); }
if (y < 0) { enemy->setDirection(Direction::Up); }
if (y > 0) { enemy->setDirection(Direction::Down); }
if (randomMoves && enemy->getDirectionCountdown() == 0) {
// If we are about to climb up a ladder, then we need to climb to the top !
if (enemy->getDirection() == Direction::Up) {
enemy->setPreferredDirection(Direction::Up);
const uint8_t enemyX = enemy->getX() / GRID_SIZE;
uint8_t enemyY = (enemy->getY() / GRID_SIZE) - 1;
uint8_t howHigh = 0;
while (true) {
LevelElement up = level.getLevelData(enemyX, enemyY);
if (up == LevelElement::Ladder) {
LevelElement upLeft = level.getLevelData(enemyX - 1, enemyY);
LevelElement upRight = level.getLevelData(enemyX + 1, enemyY);
LevelElement left = level.getLevelData(enemyX - 1, enemyY);
LevelElement right = level.getLevelData(enemyX + 1, enemyY);
howHigh++;
// Have we reached a spot on the ladder that we can move left or right ?
if (canBeClimbedOn(upLeft) || canBeClimbedOn(upRight)) break;
if ((canBeOccupiedBasic_Enemy(upLeft) && canBeStoodOnBasic_Enemy(left)) ||
(canBeOccupiedBasic_Enemy(upRight) && canBeStoodOnBasic_Enemy(right))) break;
enemyY--;
}
else {
break;
}
}
enemy->setDirectionCountdown(howHigh);
}
else {
enemy->setDirectionCountdown(random(0, 6));
}
}
}
// ------------------------------------------------------------------------------------------
// Update enemy stance if appropriate ..
// ------------------------------------------------------------------------------------------
void updateEnemyStance(Enemy *enemy, PlayerStance lowerRange, PlayerStance upperRange) {
if (enemy->getStance() < lowerRange || enemy->getStance() > upperRange) enemy->setStance(lowerRange);
}