/
modules.cpp
250 lines (219 loc) · 6.83 KB
/
modules.cpp
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
#include "modules.h"
#include "gamelevel.h"
// === Echo ===
void Echo::reset(GameLevel& context, const byte subX) {
if(context.frameCount() % ECHO_CYCLE == 0) {
for(byte i = 0; i < ECHO_VERT_RESO; ++i) {
intensities[i] = 0;
}
acceptFlag = true;
}
else {
acceptFlag = false;
}
submarineX = subX;
}
void Echo::add(const int left, const char top, const char bottom) {
if(!acceptFlag) { return; }
const int dist = left - submarineX;
if(dist < 0) { return; }
const byte b = top / ECHO_GRID_SIZE;
const byte e = bottom / ECHO_GRID_SIZE;
byte newInte = (byte)((FIELD_WIDTH - dist) / ECHO_HORI_RESO) + 3; // +3 for display soon
for(byte i = b; i <= e; ++i) {
if(newInte > intensities[i]) {
intensities[i] = newInte; // update
}
}
}
void Echo::draw(GameLevel& context) const {
if(
context.frameCount() / 3 % 2 == 0 || // tearing
context.frameCount() % ECHO_CYCLE < ECHO_CYCLE / 2 // sustain: 30 frames
) {
return;
}
for(byte i = 0; i < ECHO_VERT_RESO; ++i) {
for(byte j = 0; j < intensities[i]; ++j) {
context.core.fillRect(SCREEN_WIDTH - (j - 1) * ECHO_GRID_SIZE, i * ECHO_GRID_SIZE, ECHO_GRID_SIZE - 1, ECHO_GRID_SIZE - 1, 1);
}
}
}
// === Platoons ===
void Platoons::initialize() {
for(byte i = 0; i < PLATOON_MAX; ++i) {
status[i] = 0;
}
}
void Platoons::set(const char y, const byte type) {
for(byte i = 0; i < PLATOON_MAX; ++i) {
if(!inUse(i)) {
spawnYs[i] = y;
types[i] = type;
timers[i] = 0;
status[i] = PLATOON_USING_MASK;
return;
}
}
}
void Platoons::spawn(GameLevel& context) {
const byte INTERVALS[] = {10, 16, 16};
for(byte i = 0; i < PLATOON_MAX; ++i) {
if(!inUse(i)) { continue; }
if(
timers[i] % INTERVALS[types[i]] == 0 &&
timers[i] < PLATOON_CONSISTS * INTERVALS[types[i]] // limitation PLATOON_CONSISTS
) {
// type
const byte isOdd = timers[i] / INTERVALS[types[i]] % 2 == 1; // 0123 -> 1234
byte spawnType;
if(types[i] == PLATOON_ZIG_FILE) {
spawnType = isOdd ? SENEMY_ZIG_FIRE : SENEMY_ZIG_NOFIRE;
}
else {
spawnType = isOdd ? SENEMY_TRI_FIRE : SENEMY_TRI_NOFIRE;
}
// y
char y = spawnYs[i];
if(types[i] == PLATOON_TRI_SHOAL) {
y += random(20) - 10;
}
// spawn
if(context.spawnSmallEnemy(y, (i << 4) | spawnType)) {
status[i] = 0; // reset platoon
}
}
if(timers[i] < 0xff) {
++timers[i];
}
}
}
bool Platoons::checkBonus(const byte idx, bool killed) {
if(!inUse(idx)) { return false; }
// count
++status[idx];
if(killed) { status[idx] += (1 << 3); }
// check
if((status[idx] & PLATOON_COUNT_MASK) >= PLATOON_CONSISTS) {
if(((status[idx] >> 3) & PLATOON_COUNT_MASK) >= PLATOON_CONSISTS) {
status[idx] = 0;
return true;
}
status[idx] = 0;
}
return false;
}
// === Generator ===
void Generator::initialize() {
difficulty = 0;
zone = 0;
waveCount = WAVES_IN_ZONE - 1;
waveIndex = 0;
progCount = 0;
delayTimer = 0;
dispTimer = 0;
}
void Generator::spawn(GameLevel& context) {
#ifdef DEBUG
if(context.core.pressed(BTN_B)) {
if(context.core.pushed(BTN_U)) { difficulty += 10; }
if(context.core.pushed(BTN_D)) { difficulty -= 10; }
}
#endif
if(dispTimer > 0) { --dispTimer; }
if(delayTimer > 0) { --delayTimer; return; } // if delaying, skip generating step
const byte* waves[] = { // generating scripts
#ifdef DEBUG
waveTest,
#else
waveEmpty, // 0
#endif
waveBeginner0, waveBeginner1, waveRandomLines, waveZigTop, waveZigBottom, // easy 1-5
waveTriTri, waveSandwich, waveCatapult, waveSuperPlatoon, // normal 6-9
waveBigWall, waveJamming, waveCamouflage0, waveCamouflage1, waveArrows // hard 10-14
};
static const byte DIFFICULTY_GROUP[] = {5, 4, 5};
//static const byte WAVE_PATTERN_MAX = 15;
byte inst; // current instruction (or operand)
inst = pgm_read_byte(waves[waveIndex] + progCount); // fetch
while(inst != INST_END_WAVE && delayTimer <= 0) {
// spawn
if((inst & INST_SPAWN_MASK) != 0) {
const byte type = inst & INST_TYPE_MASK;
// get next operand
++progCount;
inst = pgm_read_byte(waves[waveIndex] + progCount);
const byte rand = inst & INST_RAND_MASK;
if(rand == INST_RAND_ERROR) { break; } // invalid operand
byte y = inst & INST_Y_MASK;
if (rand == INST_RAND_WIDE ) { y = random(4, 57); }
else if(rand == INST_RAND_NARROW) { y += random(10) - 5; }
switch(type) {
case SPAWN_BIG: context.spawnBigEnemy (y); break;
case SPAWN_ZIG_SINGLE: context.spawnSmallEnemy(y-5, SENEMY_ZIG_FIRE ); break;
case SPAWN_ZIG_FILE: context.platoons.set (y-5, PLATOON_ZIG_FILE ); break;
case SPAWN_TRI_SINGLE: context.spawnSmallEnemy(y , SENEMY_TRI_FIRE ); break;
case SPAWN_TRI_LINE: context.platoons.set (y , PLATOON_TRI_LINE ); break;
case SPAWN_TRI_SHOAL: context.platoons.set (y , PLATOON_TRI_SHOAL); break;
default: break;
}
}
// delay
else {
delayTimer = inst;
}
// get next instruction
++progCount;
inst = pgm_read_byte(waves[waveIndex] + progCount);
}
// next wave
static const byte ZONE_DISP_FRAMES = 90;
static const byte DIFFICULTY_INCR = 20;
static const byte DIFFICULTY_DECR = 10;
if(inst == INST_END_WAVE && delayTimer <= 0) {
++waveCount;
progCount = 0;
// difficulty limit
byte randNum;
byte randMin = 1; // except zero
if(getDifficulty() < 10) {
randNum = DIFFICULTY_GROUP[0];
}
else if(getDifficulty() < 50) {
randNum = DIFFICULTY_GROUP[0] + DIFFICULTY_GROUP[1];
}
else if(getDifficulty() < 70) {
randNum = DIFFICULTY_GROUP[0] + DIFFICULTY_GROUP[1] + DIFFICULTY_GROUP[2];
}
else{
randNum = DIFFICULTY_GROUP[1] + DIFFICULTY_GROUP[2];
randMin += DIFFICULTY_GROUP[0];
}
waveIndex = random(randNum) + randMin;
// difficulty up
if(difficulty < DIFFICULTY_CAP + DIFFICULTY_DECR) {
difficulty += DIFFICULTY_INCR / WAVES_IN_ZONE;
}
// next zone
if(waveCount >= WAVES_IN_ZONE) {
waveCount = 0;
dispTimer = ZONE_DISP_FRAMES; // disp zone and score
++zone;
// difficulty down
if(difficulty < DIFFICULTY_CAP + DIFFICULTY_DECR && zone > 1) {
difficulty -= DIFFICULTY_DECR;
}
}
}
}
void Generator::draw(GameLevel& context) const {
if(dispTimer > 0 && context.frameCount() / 5 % 2 == 0) {
char text[12];
sprintf(text, "Score %05d", context.getScore());
context.core.setCursor(SCREEN_WIDTH / 2 - 6 * 11 / 2, 0);
context.core.print(text);
sprintf(text, "ZONE%3d", zone);
context.core.setCursor(SCREEN_WIDTH / 2 - 6 * 7 / 2, 10);
context.core.print(text);
}
}