forked from odaki/TWANG
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TWANG.ino
1242 lines (1083 loc) · 38 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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
TWANG
Based on original code by Critters/TWANG
https://github.com/Critters/TWANG
Additional contributions from:
- bdring (serial menu, more levels)
- dskw (more levels)
- fablab-muenchen (screensaver rework)
- chess9876543210 (more screensavers)
Ported to the Arduino Nano by nuess0r
CONNECTION for Arduino Nano:
Pins D2 LED data pin
Pins D3 LED clock pin
Pins D5 & 6 & 7 Life LEDs
Pins D9 & 10 Loudspeaker
Pins A4 Gyroscope SDA
Pins A5 Gyroscope SCL
*/
#define VERSION "2018-11-18"
// Required libs
#include "FastLED.h"
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"
#include "iSin.h"
#include "RunningMedian.h"
#include <stdint.h> // uint8_t type variables
// Included libs
#include "Enemy.h"
#include "Particle.h"
#include "Spawner.h"
#include "Lava.h"
#include "Boss.h"
#include "Conveyor.h"
#include "settings.h"
// what type of sound device ....pick one
#define USE_TONEAC
#if defined(USE_TONEAC)
#include "toneAC.h"
#define twangInitTone() do {} while(0)
#define twangPlayTone(freq, vol) toneAC(freq, vol)
#define twangPlayToneLen(freq, vol, len) toneAC(freq, vol, len, true)
#define twangStopTone() noToneAC()
#else
#define twangInitTone() do {} while(0)
#define twangPlayTone(freq, vol) do {} while(0)
#define twangPlayToneLen(freq, vol, len) do {} while(0)
#define twangStopTone() do {} while(0)
#endif
// GAME
long previousMillis = 0; // Time of the last redraw
uint8_t levelNumber = 0;
iSin isin = iSin();
int8_t joystickTilt = 0; // Stores the angle of the joystick
int joystickWobble = 0; // Stores the max amount of wobble
// WOBBLE ATTACK
uint8_t attack_width = DEFAULT_ATTACK_WIDTH;
long attackMillis = 0; // Time the attack started
bool attacking = 0; // Is the attack in progress?
enum stages {
STARTUP,
PLAY,
WIN,
DEAD,
SCREENSAVER,
BOSS_KILLED,
GAMEOVER
} stage;
int score;
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;
uint8_t lives;
bool lastLevel = false;
#ifdef USE_LIFELEDS
#define LIFE_LEDS 3
const PROGMEM int lifeLEDs[LIFE_LEDS] = {7, 6, 5}; // these numbers are Arduino GPIO numbers...this is not used in the B. Dring enclosure design
#endif
// POOLS
#define ENEMY_COUNT 10
Enemy enemyPool[ENEMY_COUNT] = {
Enemy(), Enemy(), Enemy(), Enemy(), Enemy(), Enemy(), Enemy(), Enemy(), Enemy(), Enemy()
};
#if defined(ARDUINO_AVR_MEGA2560)
// Arduino Mega 2560
#define PARTICLE_COUNT 40
Particle particlePool[PARTICLE_COUNT] = {
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()
};
#elif defined(ARDUINO_AVR_NANO)
#define PARTICLE_COUNT 24
Particle particlePool[PARTICLE_COUNT] = {
Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(),
Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(), Particle(),
Particle(), Particle(), Particle(), Particle()
};
#else
#error "Please define size of particle pool for your board."
#endif
#define SPAWN_COUNT 2
Spawner spawnPool[SPAWN_COUNT] = {
Spawner(), Spawner()
};
#define LAVA_COUNT 4
Lava lavaPool[LAVA_COUNT] = {
Lava(), Lava(), Lava(), Lava()
};
#define CONVEYOR_COUNT 2
Conveyor conveyorPool[CONVEYOR_COUNT] = {
Conveyor(), Conveyor()
};
Boss boss = Boss();
// MPU
MPU6050 accelgyro;
CRGB leds[MAX_LEDS]; // this is set to the max, but the actual number used is set in FastLED.addLeds below
RunningMedian<int,5> MPUAngleSamples;
RunningMedian<int,5> MPUWobbleSamples;
void setup() {
Serial.begin(115200);
settings_eeprom_read();
showSetupInfo();
// MPU
Wire.begin();
accelgyro.initialize();
// initialize sound device (if necessary)
twangInitTone();
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, user_settings.led_count);
FastLED.setBrightness(user_settings.led_brightness);
FastLED.setDither(1);
stage = STARTUP;
}
void loop() {
long mm = millis();
while(Serial.available()) { // see if there are someone is trying to edit settings via serial port
processSerial(Serial.read());
}
if (mm - previousMillis >= MIN_REDRAW_INTERVAL) {
getInput();
long frameTimer = mm;
previousMillis = mm;
if(abs(joystickTilt) > user_settings.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 == STARTUP){
if (stageStartTime+STARTUP_FADE_DUR > mm)
tickStartup(mm);
else
{
SFXcomplete();
levelNumber = 0;
loadLevel();
}
}else if(stage == PLAY){
// PLAYING
if(attacking && attackMillis+ATTACK_DURATION < mm)
attacking = 0;
if (attacking)
SFXattacking();
// If not attacking, check if they should be
if(!attacking && joystickWobble > user_settings.attack_threshold){
attackMillis = mm;
attacking = 1;
}
// If still not attacking, move!
playerPosition += playerPositionModifier;
if(!attacking){
SFXtilt(joystickTilt);
int8_t moveAmount = (joystickTilt/6.0);
if(user_settings.player_direction) moveAmount = -moveAmount;
moveAmount = constrain(moveAmount, -MAX_PLAYER_SPEED, MAX_PLAYER_SPEED);
playerPosition -= moveAmount;
if(playerPosition < 0) playerPosition = 0;
// stop player from leaving if boss is alive
if (boss.Alive() && playerPosition >= VIRTUAL_WORLD_COUNT) // move player back
playerPosition = VIRTUAL_WORLD_COUNT - 1;
if(playerPosition >= VIRTUAL_WORLD_COUNT && !boss.Alive()) {
// Reached exit!
levelComplete();
return;
}
}
if(inLava(playerPosition)){
die();
}
// Ticks and draw calls
FastLED.clear();
tickConveyors();
tickSpawners();
tickBoss();
tickLava();
tickEnemies();
drawPlayer();
drawAttack();
drawExit();
}else if(stage == DEAD){
// DEAD
SFXdead();
FastLED.clear();
tickDie(mm);
if(!tickParticles()){
if(lives == 0){
gameOver();
} else {
loadLevel();
}
}
}else if(stage == WIN){
// LEVEL COMPLETE
tickWin(mm);
}else if(stage == BOSS_KILLED){
tickBossKilled(mm);
} else if (stage == GAMEOVER) {
if (stageStartTime+GAMEOVER_FADE_DURATION > mm)
{
tickGameover(mm);
}
else
{
FastLED.clear();
levelNumber = 0;
lives = user_settings.lives_per_level;
loadLevel();
}
}
FastLED.show();
}
}
// ---------------------------------
// ------------ LEVELS -------------
// ---------------------------------
void loadLevel(){
// leave these alone
FastLED.setBrightness(user_settings.led_brightness);
updateLives();
cleanupLevel();
playerAlive = 1;
lastLevel = false; // this gets changed on the boss level
/// Defaults...OK to change the following items in the levels below
attack_width = DEFAULT_ATTACK_WIDTH;
playerPosition = 0;
/* ==== Level Editing Guide ===============
Level creation is done by adding to or editing the switch statement below
You can add as many levels as you want but you must have a "case"
for each level. The case numbers must start at 0 and go up without skipping any numbers.
Don't edit case 0 or the last (boss) level. They are special cases and editing them could
break the code.
TWANG uses a virtual 1000 LED grid (World). It will then scale that number to your strip, so if you
want something in the middle of your strip use the number 500. Consider the size of your strip
when adding features. All time values are specified in milliseconds (1/1000 of a second)
You can add any of the following features.
Enemies: You add up to 10 enemies with the spawnEnemy(...) functions.
spawnEnemy(position, direction, speed, wobble);
position: Where the enemy starts
direction: If it moves, what direction does it go 0=down, 1=away
speed: How fast does it move. Typically 1 to 4.
wobble: 0=regular movement, 1=bouncing back and forth use the speed value
to set the length of the wobble.
Spawn Pools: This generates and endless source of new enemies. 2 pools max
spawnPool[index].Spawn(position, rate, speed, direction, activate);
index: You can have up to 2 pools, use an index of 0 for the first and 1 for the second.
position: The location the enemies with be generated from.
rate: The time in milliseconds between each new enemy
speed: How fast they move. Typically 1 to 4.
direction: Directions they go 0=down, 1=away
activate: The delay in milliseconds before the first enemy
Lava: You can create 4 pools of lava.
spawnLava(left, right, ontime, offtime, offset, state);
left: the lower end of the lava pool
right: the upper end of the lava pool
ontime: How long the lave stays on.
offset: the delay before the first switch
state: does it start on or off_dir
Conveyor: You can create 2 conveyors.
spawnConveyor(startPoint, endPoint, speed)
startPoint: The close end of the conveyor
endPoint: The far end of the conveyor
speed: positive = away, negative = towards you (must be less than +/- player speed)
===== Other things you can adjust per level ================
Player Start position:
playerPosition = xxx;
The size of the TWANG attack
attack_width = xxx;
*/
switch(levelNumber){
case 0: // basic introduction
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, Lava::OFF);
spawnEnemy(350, 0, 1, 0);
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, -6);
spawnEnemy(800, 0, 0, 0);
break;
case 6:
// Drainage
spawnConveyor(100, 600, 1);
spawnConveyor(600, 1000, -1);
spawnEnemy(600, 0, 0, 0);
spawnPool[0].Spawn(1000, 5500, 3, 0, 0);
break;
case 7:
// Sin enemy swarm
spawnEnemy(700, 1, 7, 275);
spawnEnemy(500, 1, 5, 250);
spawnEnemy(600, 1, 7, 200);
spawnEnemy(800, 1, 5, 350);
spawnEnemy(400, 1, 7, 150);
spawnEnemy(450, 1, 5, 400);
break;
case 8:
// Sin enemy #2 practice (slow conveyor)
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, -4);
break;
case 9:
// Conveyor of enemies
spawnConveyor(50, 1000, 6);
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 10:
// Lava run
spawnLava(195, 300, 2000, 2000, 0, Lava::OFF);
spawnLava(400, 500, 2000, 2000, 0, Lava::OFF);
spawnLava(600, 700, 2000, 2000, 0, Lava::OFF);
spawnPool[0].Spawn(1000, 3800, 4, 0, 0);
break;
case 11:
// Sin enemy #2 (fast conveyor)
spawnEnemy(800, 1, 7, 275);
spawnEnemy(700, 1, 7, 275);
spawnEnemy(500, 1, 5, 250);
spawnPool[0].Spawn(1000, 3000, 4, 0, 3000);
spawnPool[1].Spawn(0, 5500, 5, 1, 10000);
spawnConveyor(100, 900, -6);
break;
case 12:
// less lava, more enemies
spawnLava(350, 455, 2000, 2000, 0, false);
spawnLava(660, 760, 2000, 2000, 0, false);
spawnPool[0].Spawn(1000, 3800, 4, 0, 270);
spawnEnemy(800, 0, 0, 0);
break;
case 13:
// pushed towards lava
spawnConveyor(100, 800, 1);
spawnLava(800, 850, 1000, 2000, 0, false);
spawnPool[0].Spawn(1000, 2000, 4, 0, 0);
break;
case 14:
// quick lava
spawnPool[0].Spawn(0, 2300, 6, 1, 7000);
spawnLava(200, 400, 1000, 2000, 0, false);
spawnLava(600, 800, 1000, 2000, 0, false);
spawnPool[1].Spawn(1000, 2500, 6, 0, 1000);
break;
case 15: // spawn train;
spawnPool[0].Spawn(900, 1300, 2, 0, 0);
break;
case 16: // spawn train skinny attack width;
attack_width = 32;
spawnPool[0].Spawn(900, 1800, 2, 0, 0);
break;
case 17: // evil fast split spawner
spawnPool[0].Spawn(550, 1500, 2, 0, 0);
spawnPool[1].Spawn(550, 1500, 2, 1, 0);
break;
case 18: // split spawner with exit blocking lava
spawnPool[0].Spawn(500, 1200, 2, 0, 0);
spawnPool[1].Spawn(500, 1200, 2, 1, 0);
spawnLava(900, 950, 2200, 800, 2000, Lava::OFF);
break;
case 19: // (don't edit last level)
// Boss this should always be the last level
spawnBoss();
break;
}
stageStartTime = millis();
stage = PLAY;
}
void spawnBoss(){
lastLevel = true;
boss.Spawn();
moveBoss();
}
void moveBoss(){
int spawnSpeed = 1800;
if(boss._lives == 2) spawnSpeed = 1600;
if(boss._lives == 1) spawnSpeed = 1000;
spawnPool[0].Spawn(boss._pos, spawnSpeed, 3, 0, 0);
spawnPool[1].Spawn(boss._pos, spawnSpeed, 3, 1, 0);
}
/* ======================== spawn Functions =====================================
The following spawn functions add items to pools by looking for an inactive
item in the pool. You can only add as many as the ..._COUNT. Additional attempts
to add will be ignored.
==============================================================================
*/
void spawnEnemy(int pos, bool dir, uint8_t speed, uint16_t wobble){
for(uint8_t e = 0; e<ENEMY_COUNT; e++){ // look for one that is not alive for a place to add one
if(!enemyPool[e].Alive()){
enemyPool[e].Spawn(pos, dir, speed, wobble);
enemyPool[e].playerSide = pos > playerPosition?1:-1;
return;
}
}
}
void spawnLava(int left, int right, int ontime, int offtime, int offset, bool state){
for(uint8_t i = 0; i<LAVA_COUNT; i++){
if(!lavaPool[i].Alive()){
lavaPool[i].Spawn(left, right, ontime, offtime, offset, state);
return;
}
}
}
void spawnConveyor(int startPoint, int endPoint, uint8_t speed){
for(uint8_t i = 0; i<CONVEYOR_COUNT; i++){
if(!conveyorPool[i]._alive){
conveyorPool[i].Spawn(startPoint, endPoint, speed);
return;
}
}
}
void cleanupLevel(){
for(uint8_t i = 0; i<ENEMY_COUNT; i++){
enemyPool[i].Kill();
}
for(uint8_t i = 0; i<PARTICLE_COUNT; i++){
particlePool[i].Kill();
}
for(uint8_t i = 0; i<SPAWN_COUNT; i++){
spawnPool[i].Kill();
}
for(uint8_t i = 0; i<LAVA_COUNT; i++){
lavaPool[i].Kill();
}
for(uint8_t i = 0; i<CONVEYOR_COUNT; i++){
conveyorPool[i].Kill();
}
boss.Kill();
}
void levelComplete(){
stageStartTime = millis();
stage = WIN;
//if(levelNumber == LEVEL_COUNT){
if (lastLevel) {
stage = BOSS_KILLED;
}
if (levelNumber != 0) // no points for the first level
{
score = score + (lives * 10); //
}
}
void nextLevel(){
levelNumber ++;
if(lastLevel)
levelNumber = 0;
lives = user_settings.lives_per_level;
loadLevel();
}
void gameOver(){
levelNumber = 0;
loadLevel();
}
void die(){
playerAlive = 0;
if(levelNumber > 0)
lives--;
if(lives == 0){
stage = GAMEOVER;
stageStartTime = millis();
}
else
{
for(uint8_t p = 0; p < PARTICLE_COUNT; p++){
particlePool[p].Spawn(playerPosition);
}
stageStartTime = millis();
stage = DEAD;
}
killTime = millis();
}
// ----------------------------------
// -------- TICKS & RENDERS ---------
// ----------------------------------
void tickStartup(long mm)
{
FastLED.clear();
if(stageStartTime+STARTUP_WIPEUP_DUR > mm) // fill to the top with green
{
int n = min(map(((mm-stageStartTime)), 0, STARTUP_WIPEUP_DUR, 0, user_settings.led_count), user_settings.led_count); // fill from top to bottom
for(int i = 0; i<= n; i++){
leds[i] = CRGB(0, 255, 0);
}
}
else if(stageStartTime+STARTUP_SPARKLE_DUR > mm) // sparkle the full green bar
{
for(int i = 0; i< user_settings.led_count; i++){
if(random8(30) < 28)
leds[i] = CRGB(0, 255, 0); // most are green
else {
int flicker = random8(250);
leds[i] = CRGB(flicker, 150, flicker); // some flicker brighter
}
}
}
else if (stageStartTime+STARTUP_FADE_DUR > mm) // fade it out to bottom
{
int n = max(map(((mm-stageStartTime)), STARTUP_SPARKLE_DUR, STARTUP_FADE_DUR, 0, user_settings.led_count), 0); // fill from top to bottom
int brightness = max(map(((mm-stageStartTime)), STARTUP_SPARKLE_DUR, STARTUP_FADE_DUR, 255, 0), 0);
for(int i = n; i< user_settings.led_count; i++){
leds[i] = CRGB(0, brightness, 0);
}
}
SFXFreqSweepWarble(STARTUP_FADE_DUR, millis()-stageStartTime, 40, 400, 20);
}
void tickEnemies(){
for(uint8_t i = 0; i<ENEMY_COUNT; 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[user_settings.led_count-1] = CRGB(0, 0, 255);
}
}
void tickSpawners(){
long mm = millis();
for(uint8_t s = 0; s<SPAWN_COUNT; 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]._speed, 0);
spawnPool[s]._lastSpawned = mm;
}
}
}
}
void tickLava(){
int A, B, p;
uint8_t i, brightness, flicker;
long mm = millis();
const uint8_t lava_off_brightness = WS2812_LAVA_OFF_BRIGHTNESS;
Lava LP;
for(i = 0; i<LAVA_COUNT; i++){
LP = lavaPool[i];
if(LP.Alive()){
A = getLED(LP._left);
B = getLED(LP._right);
if(LP._state == Lava::OFF){
if(LP._lastOn + LP._offtime < mm){
LP._state = Lava::ON;
LP._lastOn = mm;
}
for(p = A; p<= B; p++){
flicker = random8(lava_off_brightness);
leds[p] = CRGB(lava_off_brightness+flicker, (lava_off_brightness+flicker)/1.5, 0);
}
}else if(LP._state == Lava::ON){
if(LP._lastOn + LP._ontime < mm){
LP._state = Lava::OFF;
LP._lastOn = mm;
}
for(p = A; p<= B; p++){
if(random8(30) < 29)
leds[p] = CRGB(150, 0, 0);
else
leds[p] = CRGB(180, 100, 0);
}
}
}
lavaPool[i] = LP;
}
}
bool tickParticles(){
bool stillActive = false;
uint8_t brightness;
for(uint8_t p = 0; p < PARTICLE_COUNT; p++){
if(particlePool[p].Alive()){
particlePool[p].Tick(user_settings.gravity, user_settings.bend_point);
if (particlePool[p]._power < 5)
{
brightness = (5 - particlePool[p]._power) * 10;
leds[getLED(particlePool[p]._pos)] += CRGB(brightness, brightness/2, brightness/2);\
}
else
leds[getLED(particlePool[p]._pos)] += CRGB(particlePool[p]._power, 0, 0);\
stillActive = true;
}
}
return stillActive;
}
void tickConveyors(){
int b, n, ss, ee, led;
uint8_t speed, i;
long m = 10000+millis();
playerPositionModifier = 0;
const uint8_t conveyor_brightness = WS2812_CONVEYOR_BRIGHTNES;
uint8_t levels = 5; // brightness levels in conveyor
for(i = 0; i<CONVEYOR_COUNT; i++){
if(conveyorPool[i]._alive){
speed = constrain(conveyorPool[i]._speed, -MAX_PLAYER_SPEED+1, MAX_PLAYER_SPEED-1);
ss = getLED(conveyorPool[i]._startPoint);
ee = getLED(conveyorPool[i]._endPoint);
for(led = ss; led<ee; led++){
n = (-led + (m/100)) % levels;
if(speed < 0)
n = (led + (m/100)) % levels;
b = map(n, 5, 0, 0, conveyor_brightness);
if(b > 0)
leds[led] = CRGB(0, 0, b);
}
if(playerPosition > conveyorPool[i]._startPoint && playerPosition < conveyorPool[i]._endPoint){
playerPositionModifier = speed;
}
}
}
}
void tickBossKilled(long mm) // boss funeral
{
static uint8_t gHue = 0;
FastLED.setBrightness(255); // super bright!
uint8_t brightness = 0;
FastLED.clear();
if(stageStartTime+6500 > mm){
gHue++;
fill_rainbow( leds, user_settings.led_count, gHue, 7); // FastLED's built in rainbow
if( random8() < 200) { // add glitter
leds[ random16(user_settings.led_count) ] += CRGB::White;
}
SFXbosskilled();
}else if(stageStartTime+7000 > mm){
int n = max(map(((mm-stageStartTime)), 5000, 5500, user_settings.led_count, 0), 0);
for(int i = 0; i< n; i++){
brightness = (sin(((i*10)+mm)/500.0)+1)*255;
leds[i].setHSV(brightness, 255, 50);
}
SFXcomplete();
}else{
nextLevel();
}
}
void tickDie(long mm) { // a short bright explosion...particles persist after it.
const int duration = 200; // milliseconds
const int width = 10; // half width of the explosion
if(stageStartTime+duration > mm) {// Spread red from player position up and down the width
uint8_t brightness = map((mm-stageStartTime), 0, duration, 255, 50); // this allows a fade from white to red
// fill up
int n = max(map(((mm-stageStartTime)), 0, duration, getLED(playerPosition), getLED(playerPosition)+width), 0);
for(int i = getLED(playerPosition); i<= n; i++){
leds[i] = CRGB(255, brightness, brightness);
}
// fill to down
n = max(map(((mm-stageStartTime)), 0, duration, getLED(playerPosition), getLED(playerPosition)-width), 0);
for(int i = getLED(playerPosition); i>= n; i--){
leds[i] = CRGB(255, brightness, brightness);
}
}
}
void tickGameover(long mm) {
uint8_t brightness = 0;
FastLED.clear();
if(stageStartTime+GAMEOVER_SPREAD_DURATION > mm) // Spread red from player position to top and bottom
{
// fill to top
int n = max(map(((mm-stageStartTime)), 0, GAMEOVER_SPREAD_DURATION, getLED(playerPosition), user_settings.led_count), 0);
for(int i = getLED(playerPosition); i<= n; i++){
leds[i] = CRGB(255, 0, 0);
}
// fill to bottom
n = max(map(((mm-stageStartTime)), 0, GAMEOVER_SPREAD_DURATION, getLED(playerPosition), 0), 0);
for(int i = getLED(playerPosition); i>= n; i--){
leds[i] = CRGB(255, 0, 0);
}
SFXgameover();
}
else if(stageStartTime+GAMEOVER_FADE_DURATION > mm) // fade down to bottom and fade brightness
{
int n = max(map(((mm-stageStartTime)), GAMEOVER_FADE_DURATION, GAMEOVER_SPREAD_DURATION, 0, user_settings.led_count), 0);
brightness = map(((mm-stageStartTime)), GAMEOVER_SPREAD_DURATION, GAMEOVER_FADE_DURATION, 255, 0);
for(int i = 0; i<= n; i++){
leds[i] = CRGB(brightness, 0, 0);
}
SFXcomplete();
}
}
void tickWin(long mm) {
FastLED.clear();
if(stageStartTime+WIN_FILL_DURATION > mm){
int n = max(map(((mm-stageStartTime)), 0, WIN_FILL_DURATION, user_settings.led_count, 0), 0); // fill from top to bottom
for(int i = user_settings.led_count; i>= n; i--){
leds[i] = CRGB(0, 255, 0);
}
SFXwin();
}else if(stageStartTime+WIN_CLEAR_DURATION > mm){
int n = max(map(((mm-stageStartTime)), WIN_FILL_DURATION, WIN_CLEAR_DURATION, user_settings.led_count, 0), 0); // clear from top to bottom
for(int i = 0; i< n; i++){
leds[i] = CRGB(0, 255, 0);
}
SFXwin();
}else if(stageStartTime+WIN_OFF_DURATION > mm){ // wait a while with leds off
leds[0] = CRGB(0, 255, 0);
}else{
nextLevel();
}
}
void drawLives()
{
// show how many lives are left by drawing a short line of green leds for each life
SFXcomplete(); // stop any sounds
FastLED.clear();
int pos = 0;
for (uint8_t i = 0; i < lives; i++)
{
for (uint8_t j=0; j<4; j++)
{
leds[pos++] = CRGB(0, 255, 0);
FastLED.show();
}
leds[pos++] = CRGB(0, 0, 0);
}
FastLED.show();
delay(1000);
FastLED.clear();
}
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, VIRTUAL_WORLD_COUNT, 0, user_settings.led_count-1), 0, user_settings.led_count-1);
}
bool inLava(int pos){
// Returns if the player is in active lava
int i;
Lava LP;
for(i = 0; i<LAVA_COUNT; i++){
LP = lavaPool[i];
if(LP.Alive() && LP._state == Lava::ON){
if(LP._left < pos && LP._right > pos) return true;
}
}
return false;
}
void initLifeLEDs(){
#ifdef USE_LIFELEDS
// Life LEDs
for(uint8_t i = 0; i<LIFE_LEDS; i++){
pinMode(pgm_read_byte_near(lifeLEDs[i]), OUTPUT);
digitalWrite(pgm_read_byte_near(lifeLEDs[i]), HIGH);
}
#endif
}
void updateLives(){
#ifdef USE_LIFELEDS
// Updates the life LEDs to show how many lives the player has left
for(uint8_t i = 0; i<LIFE_LEDS; i++){
digitalWrite(pgm_read_byte_near(lifeLEDs[i]), lives>i?HIGH:LOW);
}
#endif
drawLives();
}