forked from Critters/TWANG
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TWANG.ino
792 lines (709 loc) · 21.6 KB
/
TWANG.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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
// Required libs
#include "FastLED.h"
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"
#include "toneAC.h"
#include "iSin.h"
#include "RunningMedian.h"
// Included libs
#include "Enemy.h"
#include "Particle.h"
#include "Spawner.h"
#include "Lava.h"
#include "Boss.h"
#include "Conveyor.h"
// MPU
MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;
// LED setup
#define NUM_LEDS 300
#define DATA_PIN 3
#define CLOCK_PIN 4
#define LED_COLOR_ORDER BGR//GBR
#define BRIGHTNESS 150
#define DIRECTION 1 // 0 = right to left, 1 = left to right
#define MIN_REDRAW_INTERVAL 16 // Min redraw interval (ms) 33 = 30fps / 16 = 63fps
#define USE_GRAVITY 1 // 0/1 use gravity (LED strip going up wall)
#define BEND_POINT 550 // 0/1000 point at which the LED strip goes up the wall
// Life LED setup with 7-LED WS2812 disc
#define LIFE_NUM_LEDS 7
#define LIFE_DATA_PIN 5
#define LIFE_CLOCK_PIN 6
#define LIFE_LED_COLOR_ORDER BGR//GBR
// GAME
long previousMillis = 0; // Time of the last redraw
int levelNumber = 0;
long lastInputTime = 0;
#define TIMEOUT 30000
#define LEVEL_COUNT 9
#define MAX_VOLUME 10
iSin isin = iSin();
// JOYSTICK
#define JOYSTICK_ORIENTATION 1 // 0, 1 or 2 to set the angle of the joystick
#define JOYSTICK_DIRECTION 1 // 0/1 to flip joystick direction
#define ATTACK_THRESHOLD 30000 // The threshold that triggers an attack
#define JOYSTICK_DEADZONE 5 // Angle to ignore
int joystickTilt = 0; // Stores the angle of the joystick
int joystickWobble = 0; // Stores the max amount of acceleration (wobble)
// WOBBLE ATTACK
#define ATTACK_WIDTH 70 // Width of the wobble attack, world is 1000 wide
#define ATTACK_DURATION 500 // Duration of a wobble attack (ms)
long attackMillis = 0; // Time the attack started
bool attacking = 0; // Is the attack in progress?
#define BOSS_WIDTH 40
// PLAYER
#define MAX_PLAYER_SPEED 10 // Max move speed of the player
char* stage; // what stage the game is at (PLAY/DEAD/WIN/GAMEOVER)
long stageStartTime; // Stores the time the stage changed for stages that are time based
int playerPosition; // Stores the player position
int playerPositionModifier; // +/- adjustment to player position
bool playerAlive;
long killTime;
#define INITIAL_LIVES 6 // Initial number of lives for the game.
int lives = INITIAL_LIVES;
// POOLS
int lifeLEDs[3] = {52, 50, 40};
Enemy enemyPool[10] = {
Enemy(), Enemy(), Enemy(), Enemy(), Enemy(), Enemy(), Enemy(), Enemy(), Enemy(), Enemy()
};
int const enemyCount = 10;
Particle particlePool[40] = {
Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle()
};
int const particleCount = 40;
Spawner spawnPool[2] = {
Spawner(), Spawner()
};
int const spawnCount = 2;
Lava lavaPool[4] = {
Lava(), Lava(), Lava(), Lava()
};
int const lavaCount = 4;
Conveyor conveyorPool[2] = {
Conveyor(), Conveyor()
};
int const conveyorCount = 2;
Boss boss = Boss();
// Game strip
CRGB leds[NUM_LEDS];
// Life LEDs disc
CRGB life_leds_disc[LIFE_NUM_LEDS];
RunningMedian MPUAngleSamples = RunningMedian(5);
RunningMedian MPUWobbleSamples = RunningMedian(5);
void setup() {
Serial.begin(9600);
while (!Serial);
// MPU
Wire.begin();
accelgyro.initialize();
// Fast LED
//FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, LED_COLOR_ORDER>(leds, NUM_LEDS);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); // For Neopixel Strip
// Life LEDs
//FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, LED_COLOR_ORDER>(leds, NUM_LEDS);
FastLED.addLeds<NEOPIXEL, LIFE_DATA_PIN>(life_leds_disc, LIFE_NUM_LEDS); // For Life LED disc
FastLED.setBrightness(BRIGHTNESS);
FastLED.setDither(1);
// for ye olde three single LEDs setup
for (int i = 0; i < 3; i++) {
pinMode(lifeLEDs[i], OUTPUT);
digitalWrite(lifeLEDs[i], HIGH);
}
loadLevel();
}
void loop() {
long mm = millis();
int brightness = 0;
if (stage == "PLAY") {
if (attacking) {
SFXattacking();
} else {
SFXtilt(joystickTilt);
}
} else if (stage == "DEAD") {
updateLives();
SFXdead();
}
if (mm - previousMillis >= MIN_REDRAW_INTERVAL) {
getInput();
long frameTimer = mm;
previousMillis = mm;
if (abs(joystickTilt) > JOYSTICK_DEADZONE) {
lastInputTime = mm;
if (stage == "SCREENSAVER") {
levelNumber = -1;
stageStartTime = mm;
stage = "WIN";
}
} else {
if (lastInputTime + TIMEOUT < mm) {
stage = "SCREENSAVER";
}
}
if (stage == "SCREENSAVER") {
screenSaverTick();
} else if (stage == "PLAY") {
// PLAYING
if (attacking && attackMillis + ATTACK_DURATION < mm) attacking = 0;
// If not attacking, check if they should be
if (!attacking && joystickWobble > ATTACK_THRESHOLD) {
attackMillis = mm;
attacking = 1;
}
// If still not attacking, move!
playerPosition += playerPositionModifier;
if (!attacking) {
int moveAmount = (joystickTilt / 6.0);
if (DIRECTION) moveAmount = -moveAmount;
moveAmount = constrain(moveAmount, -MAX_PLAYER_SPEED, MAX_PLAYER_SPEED);
playerPosition -= moveAmount;
if (playerPosition < 0) playerPosition = 0;
if (playerPosition >= 1000 && !boss.Alive()) {
// Reached exit!
levelComplete();
return;
}
}
if (inLava(playerPosition)) {
die();
}
// Ticks and draw calls
FastLED.clear();
tickConveyors();
tickSpawners();
tickBoss();
tickLava();
tickEnemies();
drawPlayer();
drawAttack();
drawExit();
updateLives();
} else if (stage == "DEAD") {
// DEAD
FastLED.clear();
updateLives();
if (!tickParticles()) {
loadLevel();
}
} else if (stage == "WIN") {
// LEVEL COMPLETE
FastLED.clear();
if (stageStartTime + 500 > mm) {
int n = max(map(((mm - stageStartTime)), 0, 500, NUM_LEDS, 0), 0);
for (int i = NUM_LEDS; i >= n; i--) {
brightness = 255;
leds[i] = CRGB(0, brightness, 0);
}
SFXwin();
} else if (stageStartTime + 1000 > mm) {
int n = max(map(((mm - stageStartTime)), 500, 1000, NUM_LEDS, 0), 0);
for (int i = 0; i < n; i++) {
brightness = 255;
leds[i] = CRGB(0, brightness, 0);
}
SFXwin();
} else if (stageStartTime + 1200 > mm) {
leds[0] = CRGB(0, 255, 0);
} else {
nextLevel();
}
} else if (stage == "COMPLETE") {
FastLED.clear();
SFXcomplete();
if (stageStartTime + 500 > mm) {
int n = max(map(((mm - stageStartTime)), 0, 500, NUM_LEDS, 0), 0);
for (int i = NUM_LEDS; i >= n; i--) {
brightness = (sin(((i * 10) + mm) / 500.0) + 1) * 255;
leds[i].setHSV(brightness, 255, 50);
}
} else if (stageStartTime + 5000 > mm) {
// main LED strip
for (int i = NUM_LEDS; i >= 0; i--) {
brightness = (sin(((i * 10) + mm) / 500.0) + 1) * 255;
leds[i].setHSV(brightness, 255, 50);
}
// live LED disc
for (int i = LIFE_NUM_LEDS; i >= 0; i--) {
brightness = (sin(((i * 10) + mm/4) / 500.0) + 1) * 255;
life_leds_disc[i].setHSV(brightness, 255, 50);
}
} else if (stageStartTime + 5500 > mm) {
int n = max(map(((mm - stageStartTime)), 5000, 5500, NUM_LEDS, 0), 0);
for (int i = 0; i < n; i++) {
brightness = (sin(((i * 10) + mm) / 500.0) + 1) * 255;
leds[i].setHSV(brightness, 255, 50);
}
} else {
nextLevel();
}
} else if (stage == "GAMEOVER") {
// GAME OVER!
FastLED.clear();
stageStartTime = 0;
}
//updateLives();
Serial.print(millis() - mm);
Serial.print(" - ");
FastLED.show();
Serial.println(millis() - mm);
}
}
// ---------------------------------
// ------------ LEVELS -------------
// ---------------------------------
void loadLevel() {
updateLives();
cleanupLevel();
playerPosition = 0;
playerAlive = 1;
switch (levelNumber) {
case 0:
// Left or right?
playerPosition = 200;
spawnEnemy(1, 0, 0, 0);
break;
case 1:
// Slow moving enemy
spawnEnemy(900, 0, 1, 0);
break;
case 2:
// Spawning enemies at exit every 2 seconds
spawnPool[0].Spawn(1000, 3000, 2, 0, 0);
break;
case 3:
// Lava intro
spawnLava(400, 490, 2000, 2000, 0, "OFF");
spawnPool[0].Spawn(1000, 5500, 3, 0, 0);
break;
case 4:
// Sin enemy
spawnEnemy(700, 1, 7, 275);
spawnEnemy(500, 1, 5, 250);
break;
case 5:
// Conveyor
spawnConveyor(100, 600, -1);
spawnEnemy(800, 0, 0, 0);
break;
case 6:
// Conveyor of enemies
spawnConveyor(50, 1000, 1);
spawnEnemy(300, 0, 0, 0);
spawnEnemy(400, 0, 0, 0);
spawnEnemy(500, 0, 0, 0);
spawnEnemy(600, 0, 0, 0);
spawnEnemy(700, 0, 0, 0);
spawnEnemy(800, 0, 0, 0);
spawnEnemy(900, 0, 0, 0);
break;
case 7:
// Lava run
spawnLava(195, 300, 2000, 2000, 0, "OFF");
spawnLava(350, 455, 2000, 2000, 0, "OFF");
spawnLava(510, 610, 2000, 2000, 0, "OFF");
spawnLava(660, 760, 2000, 2000, 0, "OFF");
spawnPool[0].Spawn(1000, 3800, 4, 0, 0);
break;
case 8:
// Sin enemy #2
spawnEnemy(700, 1, 7, 275);
spawnEnemy(500, 1, 5, 250);
spawnPool[0].Spawn(1000, 5500, 4, 0, 3000);
spawnPool[1].Spawn(0, 5500, 5, 1, 10000);
spawnConveyor(100, 900, -1);
break;
case 9:
// Boss
spawnBoss();
break;
}
stageStartTime = millis();
stage = "PLAY";
}
void spawnBoss() {
boss.Spawn();
moveBoss();
}
void moveBoss() {
int spawnSpeed = 2500;
if (boss._lives == 2) spawnSpeed = 2000;
if (boss._lives == 1) spawnSpeed = 1500;
spawnPool[0].Spawn(boss._pos, spawnSpeed, 3, 0, 0);
spawnPool[1].Spawn(boss._pos, spawnSpeed, 3, 1, 0);
}
void spawnEnemy(int pos, int dir, int sp, int wobble) {
for (int e = 0; e < enemyCount; e++) {
if (!enemyPool[e].Alive()) {
enemyPool[e].Spawn(pos, dir, sp, wobble);
enemyPool[e].playerSide = pos > playerPosition ? 1 : -1;
return;
}
}
}
void spawnLava(int left, int right, int ontime, int offtime, int offset, char* state) {
for (int i = 0; i < lavaCount; i++) {
if (!lavaPool[i].Alive()) {
lavaPool[i].Spawn(left, right, ontime, offtime, offset, state);
return;
}
}
}
void spawnConveyor(int startPoint, int endPoint, int dir) {
for (int i = 0; i < conveyorCount; i++) {
if (!conveyorPool[i]._alive) {
conveyorPool[i].Spawn(startPoint, endPoint, dir);
return;
}
}
}
void cleanupLevel() {
for (int i = 0; i < enemyCount; i++) {
enemyPool[i].Kill();
}
for (int i = 0; i < particleCount; i++) {
particlePool[i].Kill();
}
for (int i = 0; i < spawnCount; i++) {
spawnPool[i].Kill();
}
for (int i = 0; i < lavaCount; i++) {
lavaPool[i].Kill();
}
for (int i = 0; i < conveyorCount; i++) {
conveyorPool[i].Kill();
}
boss.Kill();
}
void levelComplete() {
stageStartTime = millis();
stage = "WIN";
if (levelNumber == LEVEL_COUNT) stage = "COMPLETE";
//lives = INITIAL_LIVES;
updateLives();
}
void nextLevel() {
levelNumber ++;
if (levelNumber > LEVEL_COUNT) levelNumber = 0;
loadLevel();
}
void gameOver() {
levelNumber = 0;
loadLevel();
}
void die() {
playerAlive = 0;
if (levelNumber > 0) lives --;
updateLives();
if (lives == 0) {
levelNumber = 0;
lives = INITIAL_LIVES;
}
for (int p = 0; p < particleCount; p++) {
particlePool[p].Spawn(playerPosition);
}
stageStartTime = millis();
stage = "DEAD";
killTime = millis();
}
// ----------------------------------
// -------- TICKS & RENDERS ---------
// ----------------------------------
void tickEnemies() {
for (int i = 0; i < enemyCount; i++) {
if (enemyPool[i].Alive()) {
enemyPool[i].Tick();
// Hit attack?
if (attacking) {
if (enemyPool[i]._pos > playerPosition - (ATTACK_WIDTH / 2) && enemyPool[i]._pos < playerPosition + (ATTACK_WIDTH / 2)) {
enemyPool[i].Kill();
SFXkill();
}
}
if (inLava(enemyPool[i]._pos)) {
enemyPool[i].Kill();
SFXkill();
}
// Draw (if still alive)
if (enemyPool[i].Alive()) {
leds[getLED(enemyPool[i]._pos)] = CRGB(255, 0, 0);
}
// Hit player?
if (
(enemyPool[i].playerSide == 1 && enemyPool[i]._pos <= playerPosition) ||
(enemyPool[i].playerSide == -1 && enemyPool[i]._pos >= playerPosition)
) {
die();
return;
}
}
}
}
void tickBoss() {
// DRAW
if (boss.Alive()) {
boss._ticks ++;
for (int i = getLED(boss._pos - BOSS_WIDTH / 2); i <= getLED(boss._pos + BOSS_WIDTH / 2); i++) {
leds[i] = CRGB::DarkRed;
leds[i] %= 100;
}
// CHECK COLLISION
if (getLED(playerPosition) > getLED(boss._pos - BOSS_WIDTH / 2) && getLED(playerPosition) < getLED(boss._pos + BOSS_WIDTH)) {
die();
return;
}
// CHECK FOR ATTACK
if (attacking) {
if (
(getLED(playerPosition + (ATTACK_WIDTH / 2)) >= getLED(boss._pos - BOSS_WIDTH / 2) && getLED(playerPosition + (ATTACK_WIDTH / 2)) <= getLED(boss._pos + BOSS_WIDTH / 2)) ||
(getLED(playerPosition - (ATTACK_WIDTH / 2)) <= getLED(boss._pos + BOSS_WIDTH / 2) && getLED(playerPosition - (ATTACK_WIDTH / 2)) >= getLED(boss._pos - BOSS_WIDTH / 2))
) {
boss.Hit();
if (boss.Alive()) {
moveBoss();
} else {
spawnPool[0].Kill();
spawnPool[1].Kill();
}
}
}
}
}
void drawPlayer() {
leds[getLED(playerPosition)] = CRGB(0, 255, 0);
}
void drawExit() {
if (!boss.Alive()) {
leds[NUM_LEDS - 1] = CRGB(0, 0, 255);
}
}
void tickSpawners() {
long mm = millis();
for (int s = 0; s < spawnCount; s++) {
if (spawnPool[s].Alive() && spawnPool[s]._activate < mm) {
if (spawnPool[s]._lastSpawned + spawnPool[s]._rate < mm || spawnPool[s]._lastSpawned == 0) {
spawnEnemy(spawnPool[s]._pos, spawnPool[s]._dir, spawnPool[s]._sp, 0);
spawnPool[s]._lastSpawned = mm;
}
}
}
}
void tickLava() {
int A, B, p, i, brightness, flicker;
long mm = millis();
Lava LP;
for (i = 0; i < lavaCount; i++) {
flicker = random8(5);
LP = lavaPool[i];
if (LP.Alive()) {
A = getLED(LP._left);
B = getLED(LP._right);
if (LP._state == "OFF") {
if (LP._lastOn + LP._offtime < mm) {
LP._state = "ON";
LP._lastOn = mm;
}
for (p = A; p <= B; p++) {
leds[p] = CRGB(3 + flicker, (3 + flicker) / 1.5, 0);
}
} else if (LP._state == "ON") {
if (LP._lastOn + LP._ontime < mm) {
LP._state = "OFF";
LP._lastOn = mm;
}
for (p = A; p <= B; p++) {
leds[p] = CRGB(150 + flicker, 100 + flicker, 0);
}
}
}
lavaPool[i] = LP;
}
}
bool tickParticles() {
bool stillActive = false;
for (int p = 0; p < particleCount; p++) {
if (particlePool[p].Alive()) {
particlePool[p].Tick(USE_GRAVITY);
leds[getLED(particlePool[p]._pos)] += CRGB(particlePool[p]._power, 0, 0);
stillActive = true;
}
}
return stillActive;
}
void tickConveyors() {
int b, dir, n, i, ss, ee, led;
long m = 10000 + millis();
playerPositionModifier = 0;
for (i = 0; i < conveyorCount; i++) {
if (conveyorPool[i]._alive) {
dir = conveyorPool[i]._dir;
ss = getLED(conveyorPool[i]._startPoint);
ee = getLED(conveyorPool[i]._endPoint);
for (led = ss; led < ee; led++) {
b = 5;
n = (-led + (m / 100)) % 5;
if (dir == -1) n = (led + (m / 100)) % 5;
b = (5 - n) / 2.0;
if (b > 0) leds[led] = CRGB(0, 0, b);
}
if (playerPosition > conveyorPool[i]._startPoint && playerPosition < conveyorPool[i]._endPoint) {
if (dir == -1) {
playerPositionModifier = -(MAX_PLAYER_SPEED - 4);
} else {
playerPositionModifier = (MAX_PLAYER_SPEED - 4);
}
}
}
}
}
void drawAttack() {
if (!attacking) return;
int n = map(millis() - attackMillis, 0, ATTACK_DURATION, 100, 5);
for (int i = getLED(playerPosition - (ATTACK_WIDTH / 2)) + 1; i <= getLED(playerPosition + (ATTACK_WIDTH / 2)) - 1; i++) {
leds[i] = CRGB(0, 0, n);
}
if (n > 90) {
n = 255;
leds[getLED(playerPosition)] = CRGB(255, 255, 255);
} else {
n = 0;
leds[getLED(playerPosition)] = CRGB(0, 255, 0);
}
leds[getLED(playerPosition - (ATTACK_WIDTH / 2))] = CRGB(n, n, 255);
leds[getLED(playerPosition + (ATTACK_WIDTH / 2))] = CRGB(n, n, 255);
}
int getLED(int pos) {
// The world is 1000 pixels wide, this converts world units into an LED number
return constrain((int)map(pos, 0, 1000, 0, NUM_LEDS - 1), 0, NUM_LEDS - 1);
}
bool inLava(int pos) {
// Returns if the player is in active lava
int i;
Lava LP;
for (i = 0; i < lavaCount; i++) {
LP = lavaPool[i];
if (LP.Alive() && LP._state == "ON") {
if (LP._left < pos && LP._right > pos) return true;
}
}
return false;
}
void updateLives() {
// Updates the life LEDs to show how many lives the player has left
for (int i = 0; i < 3; i++) {
digitalWrite(lifeLEDs[i], lives > i ? HIGH : LOW);
}
life_leds_disc[0] = CRGB( 0 , 0, 0); // Make the live LEDs disc center LED dark
for (int j = 1; j < LIFE_NUM_LEDS; j++) {
// Show the remaining lives as green dots, the lost lives as red dots.
life_leds_disc[j] = CRGB(lives + 1 <= j ? 42 : 0 , lives + 1 > j ? 42 : 0, 0);
}
}
// ---------------------------------
// --------- SCREENSAVER -----------
// ---------------------------------
void screenSaverTick() {
int n, b, c, i;
long mm = millis();
int mode = (mm / 30000) % 2;
for (i = 0; i < NUM_LEDS; i++) {
leds[i].nscale8(250);
}
for (i = 0; i < LIFE_NUM_LEDS; i++) {
life_leds_disc[i].nscale8(250);
}
c = 90 + ((sin(mm / 8000.00) + 1) * 130);
if (mode == 0) {
// Marching green <> orange and all of the rainbow
n = (mm / 250) % 10;
b = 10 + ((sin(mm / 500.00) + 1) * 20.00);
//c = 20 + ((sin(mm / 5000.00) + 1) * 33);
// main game strip
for (i = 0; i < NUM_LEDS; i++) {
if (i % 10 == n) {
leds[i] = CHSV( c, 255, 150);
}
}
// Live LEDs disc
for (i = 1; i < LIFE_NUM_LEDS ; i++) {
if (i % 10 == n) {
life_leds_disc[i] = CHSV( c, 255, 150);
}
}
} else if (mode == 1) {
// Random flashes
randomSeed(mm);
// main game strip
for (i = 0; i < NUM_LEDS; i++) {
if (random8(200) == 0) {
leds[i] = CHSV( c, 255, 100);
}
}
// Live LEDs disc
for (i = 0; i < LIFE_NUM_LEDS; i++) {
if (random8(200) == 0) {
life_leds_disc[i] = CHSV( c, 255, 100);
}
}
}
}
// ---------------------------------
// ----------- JOYSTICK ------------
// ---------------------------------
void getInput() {
// This is responsible for the player movement speed and attacking.
// You can replace it with anything you want that passes a -90>+90 value to joystickTilt
// and any value to joystickWobble that is greater than ATTACK_THRESHOLD (defined at start)
// For example you could use 3 momentery buttons:
// if(digitalRead(leftButtonPinNumber) == HIGH) joystickTilt = -90;
// if(digitalRead(rightButtonPinNumber) == HIGH) joystickTilt = 90;
// if(digitalRead(attackButtonPinNumber) == HIGH) joystickWobble = ATTACK_THRESHOLD;
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
int a = (JOYSTICK_ORIENTATION == 0 ? ax : (JOYSTICK_ORIENTATION == 1 ? ay : az)) / 166;
int g = (JOYSTICK_ORIENTATION == 0 ? gx : (JOYSTICK_ORIENTATION == 1 ? gy : gz));
if (abs(a) < JOYSTICK_DEADZONE) a = 0;
if (a > 0) a -= JOYSTICK_DEADZONE;
if (a < 0) a += JOYSTICK_DEADZONE;
MPUAngleSamples.add(a);
MPUWobbleSamples.add(g);
joystickTilt = MPUAngleSamples.getMedian();
if (JOYSTICK_DIRECTION == 1) {
joystickTilt = 0 - joystickTilt;
}
joystickWobble = abs(MPUWobbleSamples.getHighest());
}
// ---------------------------------
// -------------- SFX --------------
// ---------------------------------
void SFXtilt(int amount) {
int f = map(abs(amount), 0, 90, 80, 900) + random8(100);
if (playerPositionModifier < 0) f -= 500;
if (playerPositionModifier > 0) f += 200;
toneAC(f, min(min(abs(amount) / 9, 5), MAX_VOLUME));
}
void SFXattacking() {
int freq = map(sin(millis() / 2.0) * 1000.0, -1000, 1000, 500, 600);
if (random8(5) == 0) {
freq *= 3;
}
toneAC(freq, MAX_VOLUME);
}
void SFXdead() {
int freq = max(1000 - (millis() - killTime), 10);
freq += random8(200);
int vol = max(10 - (millis() - killTime) / 200, 0);
toneAC(freq, MAX_VOLUME);
}
void SFXkill() {
toneAC(2000, MAX_VOLUME, 1000, true);
}
void SFXwin() {
int freq = (millis() - stageStartTime) / 3.0;
freq += map(sin(millis() / 20.0) * 1000.0, -1000, 1000, 0, 20);
int vol = 10;//max(10 - (millis()-stageStartTime)/200, 0);
toneAC(freq, MAX_VOLUME);
}
void SFXcomplete() {
noToneAC();
}