forked from Press-Play-On-Tape/Choplifter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.ino
447 lines (283 loc) · 11.9 KB
/
Utils.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
#include "src/utils/Arduboy2Ext.h"
#include "Enums.h"
#include "Images.h"
/* ----------------------------------------------------------------------------
* A better absolute!
*/
template<typename T> T absT(const T & v) {
return (v < 0) ? -v : v;
}
AbsHelicopterStance absHelicopterStance(HelicopterStance helicopterStance) {
return (AbsHelicopterStance)absT((int8_t)helicopterStance);
}
/* ----------------------------------------------------------------------------
* Draw a horizontal dotted line.
*
* So much nicer than a solid line!
*/
void drawHorizontalDottedLine(int x1, int x2, int y) {
for (int z = x1; z <= x2; z+=2) {
arduboy.drawPixel(z, y, WHITE);
}
}
/* ----------------------------------------------------------------------------
* Slow the helicopter down or speed it up expotentially ..
*/
int8_t calcSpeed(int8_t initValue, bool increase) {
switch (initValue) {
case 8:
if (!increase) return 4;
case 4:
if (increase) return 4;
if (!increase) return 2;
case 2:
if (increase) return 4;
if (!increase) return 1;
case 1:
if (increase) return 2;
if (!increase) return 0;
case 0:
if (increase) return 1;
if (!increase) return -1;
case -1:
if (increase) return 0;
if (!increase) return -2;
case -2:
if (increase) return -1;
if (!increase) return -4;
case -4:
if (increase) return -2;
if (!increase) return -4;
case -8:
if (increase) return -4;
default: return 0;
}
}
/* ----------------------------------------------------------------------------
* Draw the helicopter ..
*/
void drawHelicopter(int8_t x, int8_t y, HelicopterStance image) {
uint8_t index = (uint8_t)absHelicopterStance(image);
bool mirror = ((int8_t)image < 0);
arduboy.drawCompressedMirror(x, y, helicopter[((index - 1) * 6) + 1], BLACK, mirror);
arduboy.drawCompressedMirror(x, y, helicopter[(index - 1) * 6], WHITE, mirror);
arduboy.drawCompressedMirror(x, y, helicopter[((index - 1) * 6) + (frame) + 2], WHITE, mirror);
}
/* ----------------------------------------------------------------------------
* Reset the helicopter ready for the next sortie ..
*/
void resetSortie() {
heli.stance = HelicopterStance::Left_Level;
heli.xDelta = 0;
heli.yDelta = 0;
heli.xInc = DELTA_X_DO_NOTHING;
heli.xPos = 0;
heli.yPos = 40;
heli.prevTurn = PREV_TURN_FROM_LEFT;
heli.hits = 0;
heli.countDown = HELICOPTER_COUNT_DOWN_INACTIVE;
tankBulletExplosion.xPos = BULLET_INACTIVE_X_VALUE;
playerBulletExplosion.xPos = BULLET_INACTIVE_X_VALUE;
backgroundXOffset = 0;
playerStack.clear();
}
/* ----------------------------------------------------------------------------
* Reset the all variables ready for the next game ..
*/
void resetGame() {
resetSortie();
heli.xPos = 160;
dead = 0;
inHelicopter = 0;
safe = 0;
introduction_count = 0;
sortieNumber = 1;
dormitories[0] = { DormitoryState::Open, DORMITORY_SPACING, 0 };
dormitories[1] = { DormitoryState::Intact, (DORMITORY_SPACING * 2), 0 };
dormitories[2] = { DormitoryState::Intact, (DORMITORY_SPACING * 3), 0 };
dormitories[3] = { DormitoryState::Intact, (DORMITORY_SPACING * 4), 0 };
tanks[0] = {TankState::Stationary, TurrentDirection::Left_Low, 5, (1 * (int16_t)DORMITORY_SPACING) + (int16_t)random(-DORMITORY_SPACING_HALF, DORMITORY_SPACING_HALF), (1 * (int16_t)DORMITORY_SPACING), true, 0};
tanks[1] = {TankState::Stationary, TurrentDirection::Left_Low, 5, (2 * (int16_t)DORMITORY_SPACING) + (int16_t)random(-DORMITORY_SPACING_HALF, DORMITORY_SPACING_HALF), (2 * (int16_t)DORMITORY_SPACING), true, 0};
tanks[2] = {TankState::Stationary, TurrentDirection::Left_Low, 5, (3 * (int16_t)DORMITORY_SPACING) + (int16_t)random(-DORMITORY_SPACING_HALF, DORMITORY_SPACING_HALF), (3 * (int16_t)DORMITORY_SPACING), true, 0};
tanks[3] = {TankState::Stationary, TurrentDirection::Left_Low, 5, (4 * (int16_t)DORMITORY_SPACING) + (int16_t)random(-DORMITORY_SPACING_HALF, DORMITORY_SPACING_HALF), (4 * (int16_t)DORMITORY_SPACING), true, 0};
tanks[4] = {TankState::Stationary, TurrentDirection::Left_Low, 5, (5 * (int16_t)DORMITORY_SPACING) + (int16_t)random(-DORMITORY_SPACING_HALF, DORMITORY_SPACING_HALF), (5 * (int16_t)DORMITORY_SPACING), true, 0};
for (uint8_t i = 0; i < NUMBER_OF_HOSTAGES; i++) {
hostages[i] = { (i < 16 ? HostageStance::Leaving_Dorm : HostageStance::In_Dorm), (uint8_t)(((i % 16) * 15) + 10), dormitories[i / 16].xPos };
}
for (uint8_t i = 0; i < NUMBER_OF_PLAYER_BULLETS; i++) {
playerBullets[i] = { BULLET_INACTIVE_X_VALUE, 0, 0, 0, 0 };
}
for (uint8_t i = 0; i < NUMBER_OF_TANK_BULLETS; i++) {
tankBullets[i] = { BULLET_INACTIVE_X_VALUE, 0, 0, 0, 0 };
}
gameState = GameState::Introduction;
}
/* ----------------------------------------------------------------------------
* Has the nominated bullet hot anything ?
*/
void bulletHit(Bullet *bullet, BulletExplosion *explosion, bool playerBullet) {
// Check to see if a tank bullet hit the helicopter (it can be at any height) ..
{
Rect hitZone = {50, 0, 0, 0};
if (!playerBullet) {
switch (absHelicopterStance(heli.stance)) {
case AbsHelicopterStance::Side_Level ... AbsHelicopterStance::Side_Incline_2:
case AbsHelicopterStance::Side_Reverse_Incline_1 ... AbsHelicopterStance::Side_Reverse_Incline_5:
case AbsHelicopterStance::Side_Incline_Rotate_3:
hitZone.x = 50;
hitZone.y = heli.yPos + 5;
hitZone.width = 28;
hitZone.height = 12;
break;
case AbsHelicopterStance::Side_Incline_3 ... AbsHelicopterStance::Side_Incline_Max:
hitZone.x = 50;
hitZone.y = heli.yPos + 7;
hitZone.width = 28;
hitZone.height = 12;
break;
case AbsHelicopterStance::Side_Level_Rotate_1 ... AbsHelicopterStance::Side_Front_Incline_2:
case AbsHelicopterStance::Side_Incline_Rotate_1 ... AbsHelicopterStance::Side_Incline_Rotate_2:
hitZone.x = 57;
hitZone.y = heli.yPos + 5;
hitZone.width = 14;
hitZone.height = 13;
break;
}
Point pt = { 64 + bullet->xPos - heli.xPos, bullet->yPos };
if (arduboy.collide(pt, hitZone)) {
explosion->xPos = bullet->xPos;
explosion->yPos = hitZone.y - 4;
explosion->explosionType = ExplosionType::Both_Medium;
bullet->xPos = BULLET_INACTIVE_X_VALUE;
heli.hits++;
if (heli.hits == HELICOPTER_BULLET_NUMBER_OF_HITS) {
sound.tones(exploding);
explosion->explosionType = ExplosionType::Large_1;
if (heli.countDown == HELICOPTER_COUNT_DOWN_INACTIVE) {
heli.countDown++;
sound.tones(exploding);
}
// Kill any hostages that were in the helicopter ..
dead = dead + inHelicopter;
inHelicopter = 0;
for (int i = 0; i < NUMBER_OF_HOSTAGES; i++) {
Hostage *hostage = &hostages[i];
if (hostage->stance == HostageStance::In_Helicopter) hostage->stance = HostageStance::Dead;
}
}
}
}
}
// Check to see if we hit anything ..
switch (bullet->yPos) {
case 40 ... 46: // Hitting a dormitory?
for (uint8_t i = 0; i < NUMBER_OF_DORMITORIES; i++) {
Dormitory *dormitory = &dormitories[i];
uint16_t diff = absT(dormitory->xPos - bullet->xPos);
if (dormitory->state == DormitoryState::Intact && diff < 16) {
dormitory->numberOfHits++;
explosion->xPos = bullet->xPos;
explosion->yPos = 36 + (diff / 3);
explosion->explosionType = ExplosionType::Medium;
if (dormitory->numberOfHits == DORMITORY_HITS_REQUIRED) {
sound.tones(exploding);
dormitory->state = DormitoryState::Open;
explosion->xPos = bullet->xPos;
explosion->yPos = 36 + (diff / 3);
explosion->explosionType = ExplosionType::Large_1;
// Release the hostages ..
uint8_t count = 0;
for (uint8_t i = 0; i < NUMBER_OF_HOSTAGES; i++) {
Hostage *hostage = &hostages[i];
if (hostage->stance == HostageStance::In_Dorm) {
hostage->stance = HostageStance::Leaving_Dorm;
hostage->xPos = dormitory->xPos + 16;
hostage->countDown = (count * 15) + 10;
count++;
if (count == DORMITORY_HOSTAGE_CAPACITY) { break; }
}
}
}
bullet->xPos = BULLET_INACTIVE_X_VALUE;
}
}
break;
case 47 ... 52: // Hitting a hostage or the tank?
{
bool hit = false;
for (uint8_t i = 0; i < NUMBER_OF_HOSTAGES; i++) {
Hostage *hostage = &hostages[i];
uint16_t diff = hostage->xPos - bullet->xPos;
if (hostage->stance >= HostageStance::Running_Left_1 &&
hostage->stance <= HostageStance::Waving_22 &&
diff < 3) {
sound.tones(kill_hostage);
explosion->xPos = bullet->xPos;
explosion->yPos = 52;
explosion->explosionType = ExplosionType::Medium;
bullet->xPos = BULLET_INACTIVE_X_VALUE;
hostage->stance = HostageStance::Dying_2;
dead++;
if (dead + safe == NUMBER_OF_HOSTAGES) {
heli.countDown = HELICOPTER_END_OF_GAME_START;
}
hit = true;
break;
}
}
if (!hit && playerBullet) {
for (uint8_t i = 0; i < NUMBER_OF_TANKS; i++) {
Tank *tank = &tanks[i];
uint16_t diff = absT(tank->xPos - bullet->xPos);
if (bullet->startYPos > TANK_BULLET_MIN_Y_VALUE && diff < 16) { // Bullets that hit tank must be fired from down low ..
explosion->xPos = bullet->xPos;
explosion->yPos = (diff < 8 ? 46 : 51);
explosion->explosionType = ExplosionType::Medium;
bullet->xPos = BULLET_INACTIVE_X_VALUE;
tank->numberOfHits++;
if (tank->numberOfHits == (level == LEVEL_EASY ? TANK_BULLET_NUMBER_OF_HITS_EASY : TANK_BULLET_NUMBER_OF_HITS_HARD) ) {
sound.tones(exploding);
explosion->yPos = 50;
explosion->explosionType = ExplosionType::Large_1;
tank->state = TankState::Dead3;
}
}
}
}
}
break;
case 61 ... 74: // Hitting the ground ..
sound.tones(miss);
explosion->xPos = bullet->xPos;
explosion->yPos = 60;
explosion->explosionType = ExplosionType::Small;
bullet->xPos = BULLET_INACTIVE_X_VALUE;
break;
}
}
/* ----------------------------------------------------------------------------
* Turn sound off and commit to the EEPROM.
*/
void turnSoundOff() {
arduboy.audio.off();
arduboy.audio.saveOnOff();
}
/* ----------------------------------------------------------------------------
* Turn sound on and commit to the EEPROM.
*/
void turnSoundOn() {
arduboy.audio.on();
arduboy.audio.saveOnOff();
}
/* ----------------------------------------------------------------------------
* Helper function for using image lookup arrays
*/
const uint8_t * imageArrayLookup(const uint8_t * const * const array) {
return reinterpret_cast<const uint8_t *>(pgm_read_word(array));
}
/* ----------------------------------------------------------------------------
* Alternate helper function for using image lookup arrays
*/
template< typename IntT > const uint8_t * imageArrayLookup(const uint8_t * const * const array, IntT index) {
return reinterpret_cast<const uint8_t *>(pgm_read_word(&array[index]));
}