/
shooter.h
140 lines (113 loc) · 3.12 KB
/
shooter.h
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
void shooter() {
static Ball missile;
const float enemyAcceleration = 0.0036;
const uint16_t enemySpawnChanceDefault = 16383; // out of 65535
const uint8_t enemyVelocityDefault = 0.125;
uint8_t enemySpawnChance = enemySpawnChanceDefault;
uint8_t enemyVelocity = enemyVelocityDefault;
static Ball enemy;
static bool initialized = false;
static uint16_t enemiesHit = 0;
static uint8_t enemyColorCount = 3;
if (!initialized || modeInit) {
initialized = true;
modeInit = false;
missile.position = -1;
enemy.position = NUM_LEDS;
enemiesHit = 0;
}
fadeToBlackBy(leds, NUM_LEDS, 20);
// launch new missile?
if (missile.position < 0) {
for (uint8_t i = 0; i < buttonCount; i++) {
if (buttonChanged[i]) {
if (buttons[i].fell()) {
missile.position = 0;
missile.velocity = 1.0;
missile.color = buttonColors[i];
break;
}
}
}
}
// update enemy
if (enemy.position > 0 && enemy.position < NUM_LEDS) {
if (enemy.exploding < 1) {
enemy.position -= enemy.velocity;
enemy.velocity += enemyAcceleration;
}
if (enemy.exploding > 0) {
enemy.exploding++;
if (enemy.exploding > 16) {
enemy.exploding = 0;
enemy.position = NUM_LEDS;
}
}
}
else if (random16() <= enemySpawnChance) {
// spawn a new enemy
enemy.position = NUM_LEDS - 1;
enemy.color = buttonColors[random8(0, enemyColorCount)];
enemy.velocity = enemyVelocity;
}
// update missile
if (missile.position >= 0 && missile.position < NUM_LEDS) {
missile.position += missile.velocity;
}
if (missile.position >= NUM_LEDS) {
missile.position = -1;
}
// check for collisions
// enemy hit base?
if (enemy.position <= 0) {
// player explodes
while (leds[0] != CRGB(CRGB::White)) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] += CRGB(2, 2, 2);
}
LEDS.show();
}
while (leds[0] != CRGB(CRGB::Black)) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] -= CRGB(2, 2, 2);
}
LEDS.show();
}
// game resets
enemy.position = NUM_LEDS;
missile.position = -1;
enemiesHit = 0;
enemySpawnChance = enemySpawnChanceDefault;
enemyVelocity = enemyVelocityDefault;
return;
}
// missile hit enemy?
if (enemy.color == missile.color && missile.position >= enemy.position) {
// enemy explodes
enemy.exploding = 1;
enemy.velocity = 0;
missile.position = -1;
// increase enemy spawn chance
if (enemySpawnChance < 65535) {
enemySpawnChance++;
}
// increase enemy velocity
enemyVelocity += 0.1;
enemiesHit++;
if (enemyColorCount < buttonCount && enemiesHit % 10 == 0)
enemyColorCount++;
}
// draw enemy
if (enemy.position > 0 && enemy.position < NUM_LEDS) {
if (enemy.exploding < 1) {
leds[(uint8_t) enemy.position] += enemy.color;
}
else {
leds[(uint8_t) enemy.position] += CHSV(0, 0, 255);
}
}
// draw missile
if (missile.position >= 0 && missile.position < NUM_LEDS) {
leds[(uint8_t) missile.position] += missile.color;
}
}