-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstroids.ino
562 lines (497 loc) · 11.4 KB
/
stroids.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
#include <SPI.h>
#include <EEPROM.h>
#include "Game.h"
#include "Art.h"
GameCore gb;
// State
enum class State : char
{
Title = 0,
Menu = 1,
Game = 2,
Pause = 3,
End = 4
};
State state = State::Title;
bool debugShow = false;
unsigned long highscore = 0;
unsigned long score = 0;
int lives = 4;
#define SPACEFRICTION 0.975
#define playerAcceleration 0.1
#define playerSpeedCap 2
#define playerSize 4
float playerX, playerY;
float playerXSpeed, playerYSpeed; // vec2d :D
float playerDir;
byte playerBlink = 0;
#define starNumber 32
byte starx[starNumber];
byte stary[starNumber];
#define stroidNumber 32
float stroidSpeed = 0.5;
float stroidX[stroidNumber];
float stroidY[stroidNumber];
byte stroidSize[stroidNumber];
float stroidDir[stroidNumber];
bool stroidActive[stroidNumber];
#define shotNumber 8
#define shotSpeed 4
int shotX[shotNumber];
int shotY[shotNumber];
float shotDir[shotNumber];
bool shotActive[shotNumber];
// Declare Functions
void stateIntro(void);
void initMenu(void);
void stateMenu(void);
void backInit(void);
void backDraw(void);
void asteroidInit(void);
void asteroidStep(void);
void asteroidDraw(void);
void playerInit(void);
void playerReset(void);
void playerStep(void);
void playerCollision(void);
void playerDraw(void);
void shotInit(void);
void shotDraw(void);
bool shotAdd(int x, int y, float dir);
void shotAsteroid(void);
void hudDraw(void);
void stateGame(void);
void stateGameDraw(void);
void statePause(void);
void setup(void);
void loop(void);
// Intro
void stateIntro(void)
{
gb.clearDisplay();
gb.setCursor(46, 28);
gb.print(strArduboy);
gb.display();
// gb.tunes.tone(987, 160);
delay(160);
// gb.tunes.tone(1318, 400);
delay(2000);
initMenu(); state = State::Menu;
}
// Menu
struct MenuType {
byte selected;
byte screen;
}; MenuType menu;
void initMenu(void)
{
menu.selected = 0;
menu.screen = 0;
backInit();
asteroidInit();
}
void stateMenu(void)
{
// Simple placeholder for the moment
if (gb.pushed(BTN_A))
{
state = State::Game;
playerInit();
asteroidInit();
shotInit();
}
if (gb.pushed(BTN_B))
{
debugShow = !debugShow;
}
gb.fillScreen(0);
asteroidStep();
backDraw();
asteroidDraw();
gb.drawBitmapSlow(0, 0, sprLogoMask, 0);
gb.drawBitmapSlow(0, 0, sprLogo, 1);
// Top score
char text[24];
gb.setCursor(4, 56);
sprintf(text, strHighscore, highscore); // sprintf is weird.
gb.print(text);
if (debugShow)
{
gb.setCursor(0, 48);
gb.print(strDebug);
}
gb.display();
}
// Background
void backInit(void)
{
for(byte i = 0; i < starNumber; i++)
{
starx[i] = random(128);
stary[i] = random(64);
};
}
void backDraw(void)
{
for(byte i = 0; i < starNumber; i++)
{
gb.drawPixel(starx[i], stary[i], 1);
};
}
// 'Stroids
void asteroidInit(void)
{
const int stroidStartNumber = 3; // How many to start each round with
byte i;
for(i = 0; i < stroidNumber; i++) //
{
stroidActive[i] = false;
}
for (i = 0; i < stroidStartNumber; i++)
{
stroidActive[i] = true;
stroidX[i] = random(1) * 127;
stroidY[i] = random(1) * 63;
stroidDir[i] = random(2 * pi);
// stroidSpeed[i] = 0.5;
stroidSize[i] = 6;
}
}
void asteroidStep(void)
{
// Loops 'stroids and adds speed / direction to position
byte numActive = 0; // Counts how many are active
for (byte i = 0; i < stroidNumber; i++)
{
if (stroidActive[i])
{
stroidX[i] += gb.lengthdirX(stroidSpeed, stroidDir[i]);
stroidY[i] += gb.lengthdirY(stroidSpeed, stroidDir[i]);
// Loop offscreen
if (stroidX[i] > 127) stroidX[i] = 0;
if (stroidX[i] < 0) stroidX[i] = 127;
if (stroidY[i] > 63) stroidY[i] = 0;
if (stroidY[i] < 0) stroidY[i] = 63;
numActive++;
}
}
if (numActive == 0) // If all asteroids are gone, start next round
{
asteroidInit();
}
}
void asteroidDraw(void)
{
for (byte i = 0; i < stroidNumber; i++)
{
if (stroidActive[i])
gb.drawCircle(stroidX[i], stroidY[i], stroidSize[i], 1); // Todo : make actual asteroid graphics
}
}
// Player
void playerInit(void)
{
score = 0;
lives = 5;
playerReset();
}
void playerReset(void)
{
playerX = 63;
playerY = 31;
playerXSpeed = 0;
playerYSpeed = 0;
playerDir = pi * 1.5;
playerBlink = 120; // 2 seconds of invuln.
}
void playerStep(void)
{
if (gb.pressed(BTN_L))
{
playerDir -= 0.15;
}
if (gb.pressed(BTN_R))
{
playerDir += 0.15;
}
if (gb.pressed(BTN_U))
{
playerXSpeed += gb.lengthdirX(playerAcceleration, playerDir);
playerYSpeed += gb.lengthdirY(playerAcceleration, playerDir);
if (playerXSpeed > 0)
playerXSpeed = min(playerXSpeed, playerSpeedCap);
if (playerYSpeed > 0)
playerYSpeed = min(playerYSpeed, playerSpeedCap);
if (playerXSpeed < 0)
playerXSpeed = max(playerXSpeed, - playerSpeedCap);
if (playerYSpeed < 0)
playerYSpeed = max(playerYSpeed, - playerSpeedCap);
}
else
{
if (abs(playerXSpeed) > 0) { playerXSpeed *= SPACEFRICTION; }
if (abs(playerYSpeed) > 0) { playerYSpeed *= SPACEFRICTION; }
}
if (gb.pushed(BTN_D))
{
// teleport to random position.
playerX = random(128);
playerY = random(64);
}
playerX += playerXSpeed;
playerY += playerYSpeed;
// Modulo is for losers
if (playerX > 127) playerX = 0;
if (playerX < 0) playerX = 127;
if (playerY > 63) playerY = 0;
if (playerY < 0) playerY = 63;
// Shoots
if (gb.pushed(BTN_B))
{
shotAdd(playerX, playerY, playerDir);
}
// Collision / invincible checks
if (playerBlink > 0)
{
playerBlink--;
}
else
{
playerCollision();
}
// Collisions
if (lives == 0)
{
state = State::Menu; // To - do : go to gameover screen
if (score > highscore)
{
highscore = score;
}
}
}
void playerCollision(void)
{
// Collision is based on a single pixel at the players midpoint.Should make up for the difficulty from the small playfield.
for(byte i = 0; i < stroidNumber; i++)
{
if ((stroidActive[i] == true) && (gb.Distance(playerX, playerY, stroidX[i], stroidY[i]) < stroidSize[i]))
{
lives--;
playerReset();
return;
}
}
}
void playerDraw(void)
{
if(((playerBlink / 12) & 1) == 0) // If visible on this frame
{
int x1, y1, x2, y2, x3, y3;
// reminder that the arduino library uses radians ! Not degrees ! You idiot ! You wasted HOURS working that out!
x1 = playerX + gb.lengthdirX(playerSize, playerDir); // Tip
y1 = playerY + gb.lengthdirY(playerSize, playerDir);
x2 = playerX + gb.lengthdirX(playerSize,(playerDir + pi) + (0.3 * pi)); // Left(?) wing
y2 = playerY + gb.lengthdirY(playerSize,(playerDir + pi) + (0.3 * pi));
x3 = playerX + gb.lengthdirX(playerSize,(playerDir + pi) - (0.3 * pi)); // Right(?) wing
y3 = playerY + gb.lengthdirY(playerSize,(playerDir + pi) - (0.3 * pi));
gb.drawLine(x1, y1, x2, y2, 1); // Tip to wings
gb.drawLine(x1, y1, x3, y3, 1);
gb.drawLine(x2, y2, playerX, playerY, 1); // Tips to center
gb.drawLine(x3, y3, playerX, playerY, 1);
}
}
// Shooting
void shotInit(void)
{
for(byte i = 0; i < shotNumber; i++)
{
shotX[i] = 0;
shotY[i] = 0;
shotDir[i] = 0;
shotActive[i] = false;
}
}
void shotStep(void)
{
// loops shots, updates positions based on speed and direction.
for (byte i = 0; i < shotNumber; i++)
{
if (shotActive[i])
{
shotX[i] += gb.lengthdirX(shotSpeed, shotDir[i]);
shotY[i] += gb.lengthdirY(shotSpeed, shotDir[i]);
// Delete if out of bounds
if ((shotX[i] < 0) or (shotY[i] <0) or (shotX[i]> 128) or (shotY[i] > 64))
{
shotActive[i] = false;
}
}
}
}
void shotDraw(void)
{
for(byte i = 0; i < shotNumber; i++)
{
if (shotActive[i])
{
gb.drawPixel(shotX[i], shotY[i], 1);
}
}
}
bool shotAdd(int x, int y, float dir)
{
// loops shots and inserts into the first free space it can find.
for(byte i = 0; i < shotNumber; i++)
{
if (shotActive[i] == false)
{
shotX[i] = x;
shotY[i] = y;
shotDir[i] = dir;
shotActive[i] = true;
return true;
}
}
return false;
}
void shotAsteroid(void)
{
// Loops every active shot and every active asteroid and checks for incircle collisions
for(byte s = 0; s < shotNumber; s++)
{
if (shotActive[s] == true)
{
for(byte a = 0; a < stroidNumber; a++)
{
if ((stroidActive[a] == true) && ((gb.Distance(shotX[s], shotY[s], stroidX[a], stroidY[a])) <= stroidSize[a]))
{
shotActive[s] = false;
stroidActive[a] = false;
score += stroidSize[a] * 100; // Todo : better score calculation
int msize = stroidSize[a] - 2; // Chunk sizes
if (msize != 0)
{
bool done = false;
byte num = 2; // Makes 2 chunks
float dir = shotDir[s] + (0.5 * pi);
byte m = 0;
while(! done) // Loops searching for free asteroid slots
{
if(stroidActive[m] == false)
{
int newDir = dir + (num * pi);
stroidActive[m] = true;
stroidX[m] = stroidX[a] + gb.lengthdirX(msize, newDir);
stroidY[m] = stroidY[a] + gb.lengthdirY(msize, newDir);
stroidSize[m] = msize;
// stroidSpeed[m] = stroidSpeed[a];
stroidDir[m] = newDir;
num--;
}
m++;
if ((num == 0) or (m > 32)) // Break if satisfied or cannot continue
{
done = true;
}
}
}
}
}
}
}
}
void hudDraw(void)
{
gb.fillRect(128 - (min(lives - 1, 8) * 8), 56, 128, 64, 0); // Background for lives counter
for(byte i = 0; i <= (min(lives - 1, 8)); i++)
{
gb.drawBitmap(128 - (i * 8), 56, sprLives, 1);
}
/*
char hud[10];
gb.printf(" % s", hud, score);
*/
char text[8];
if(debugShow == true)
{
gb.setCursor(0, 0);
sprintf(text, strCPU, gb.cpuLoad());
gb.print(text);
}
gb.setCursor(0, 56);
sprintf(text, strScore, score); // sprintf is weird.
gb.print(text);
}
// Game
void stateGame(void)
{
if (gb.pushed(BTN_A))
{
state = State::Pause;
}
shotStep();
playerStep();
asteroidStep();
shotAsteroid();
stateGameDraw();
gb.display();
}
void stateGameDraw(void)
{
gb.fillScreen(0);
backDraw();
asteroidDraw();
playerDraw();
shotDraw();
hudDraw();
}
// Pause
void statePause(void)
{
if (gb.pushed(BTN_A))
{
state = State::Game;
}
stateGameDraw();
if((gb.frameCount() / 12) % 2) // Flicker the text
{
gb.setCursor(44, 28); // Seems to be about the middle of the screen
gb.print(strPaused);
}
gb.display();
}
// KillScreen
void stateEnd(void)
{
gb.fillScreen(0);
asteroidStep();
backDraw();
asteroidDraw();
gb.fillRect(8, 8, 120, 56, 0);
gb.drawLine(8, 8, 120, 8, 1);
gb.drawLine(8, 56, 120, 56, 1);
gb.drawLine(8, 8, 8, 56, 1);
gb.drawLine(120, 8, 120, 56, 1);
gb.display();
}
void endInit(void)
{
}
// The good bits
void setup(void)
{
gb.setup();
}
void loop(void)
{
if(! gb.nextFrame()) { return; }
gb.updateInput();
switch(state)
{
case State::Title: { stateIntro(); } break;
case State::Menu: { stateMenu(); } break;
case State::Game: { stateGame(); } break;
case State::Pause: { statePause(); } break;
case State::End: { stateEnd(); } break;
}
}