-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame_bb.cpp
381 lines (328 loc) · 9.97 KB
/
game_bb.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
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
#include "game_bb.h"
#define HERO_LEVEL 45
#define HERO_OFFSET 4
#define HERO_HEIGHT 10
#define BONUS_OFFSET 5
#define BRIDGE_LEVEL 54
#define PLATFORM_LEVEL 55
#define PLATFORM_MIN_DISTANCE 5
#define PLATFORM_MIN_WIDTH 5
#define PLATFORM_MAX_WIDTH 15
#define SCROLL_STEP 2
#define SCROLL_MULTIPLIER 3
#define BONUS_LIMIT 50
#define BONUS_CLOSE 1
#define BONUS_FAT 2
#define platform_end(platform) (platform.x + platform.width - 1)
#define HILLS 9
const uint8_t bg_hills[][2] = {{0, 35}, {72, 20}, {96, 30}, {110, 26},
{127, 35}, {199, 20}, {223, 30}, {237, 26}, {254, 35}};
struct platform_t {
int8_t x;
int8_t width;
// TODO: There is only one bridge shown at a time. We can get rid of this
int8_t bridge;
};
struct data_t {
uint8_t gameOn;
uint32_t score;
uint32_t hiScore;
uint8_t bonus;
struct platform_t platformCurr;
struct platform_t platformNext;
int8_t bg;
uint8_t bonus_next;
uint8_t bonus_type;
};
static Arduboy2 *gr;
const uint8_t spriteMap[][5] PROGMEM = {
{0b00110, 0b01111, 0b11110, 0b01111, 0b00110},
{0b01110, 0b10001, 0b10101, 0b10001, 0b01110},
{0b11000, 0b11000, 0b11101, 0b11000, 0b11000}
};
static struct data_t *data;
static uint8_t bridge;
static uint8_t bonus_promote;
static void platform_random(struct platform_t *platform) {
uint8_t width = (bonus_promote & BONUS_FAT) ? 15 : random(PLATFORM_MIN_WIDTH, PLATFORM_MAX_WIDTH);
platform->x = platform_end(data->platformNext) - platform_end(data->platformCurr) +
((bonus_promote & BONUS_CLOSE) ? PLATFORM_MIN_DISTANCE :
random(PLATFORM_MIN_DISTANCE, BRIDGE_LEVEL - width));
platform->width = width;
bonus_promote = 0;
}
static void display_platform(struct platform_t platform, uint8_t color) {
gr->fillRect(platform.x, PLATFORM_LEVEL, platform.width, 9, color);
gr->drawPixel(platform.x + platform.width / 2, PLATFORM_LEVEL+2, BLACK);
if(platform.bridge && platform.x > 0) {
gr->drawFastHLine(0,
BRIDGE_LEVEL,
platform.x + platform.bridge, color);
}
}
static void display_hero(int8_t state, int8_t x, int8_t y, uint8_t color) {
int8_t foot = y + 8;
if(state & 4) {
y += HERO_HEIGHT;
foot = y;
gr->drawPixel(x, foot, color);
gr->drawPixel(x + 2, foot, color);
}
gr->drawBitmap(x, y + 2, gameBBLogo, 3, 7, color);
if(state & 1) {
gr->drawPixel(x + 2, foot, BLACK);
gr->drawPixel(x + 3, foot, color);
}
if(state & 2) {
gr->drawPixel(x, foot, BLACK);
gr->drawPixel(x - 1, foot, color);
}
}
static void display_background(void) {
gr->clear();
// The hills
data->bg = data->bg % 128;
for(int i = 0;++i < HILLS;) {
gr->drawLine(bg_hills[i - 1][0] - data->bg, bg_hills[i - 1][1],
bg_hills[i][0] - data->bg, bg_hills[i][1]);
}
// The score
gr->drawBitmap(64+29, 1, cupBmp, 8, 8, WHITE);
gr->fillRect(65, 2, 28, 5, BLACK);
drawNumber(gr, 65, 2, data->score, WHITE, 7);
gr->fillRect(102, 2, 26, 5, BLACK);
drawNumber(gr, 102, 2, data->hiScore, WHITE, 0);
for(int i = 0;i < ceil((float)data->bonus / BONUS_OFFSET);i++) {
gr->drawBitmap(1 + i * (BONUS_OFFSET + 1), 1, spriteMap[0], (i + 1) * BONUS_OFFSET > data->bonus ? data->bonus % BONUS_OFFSET : BONUS_OFFSET, BONUS_OFFSET, WHITE);
}
if(data->bonus_next > 0) {
gr->drawBitmap(data->bonus_next, PLATFORM_LEVEL + 2,
spriteMap[data->bonus_type], BONUS_OFFSET, BONUS_OFFSET, WHITE);
}
if(bridge) {
gr->drawFastHLine(platform_end(data->platformCurr),
BRIDGE_LEVEL, bridge, WHITE);
}
display_platform(data->platformCurr, WHITE);
display_platform(data->platformNext, WHITE);
}
static void platform_new(void) {
struct platform_t platformFuture;
int8_t distance;
platform_random(&platformFuture);
platformFuture.bridge = 0;
data->bonus_type = 0;
data->bonus_next = 0;
distance = floor(platform_end(data->platformCurr) / SCROLL_STEP) * SCROLL_STEP;
platformFuture.x += distance * SCROLL_MULTIPLIER;
while(distance >= SCROLL_STEP) {
if(! gr->nextFrame()) continue;
data->bg++;
data->platformCurr.x -= SCROLL_STEP;
data->platformNext.x -= SCROLL_STEP;
platformFuture.x -= SCROLL_STEP * SCROLL_MULTIPLIER;
distance -= SCROLL_STEP;
display_background();
display_platform(platformFuture, WHITE);
display_hero(0, platform_end(data->platformNext) - HERO_OFFSET, HERO_LEVEL, WHITE);
gr->display();
}
data->platformNext.bridge =
platform_end(data->platformCurr) + bridge - data->platformNext.x;
data->platformNext.x -= platform_end(data->platformCurr);
data->platformCurr = data->platformNext;
data->platformNext = platformFuture;
data->platformNext.bridge = 0;
bridge = 0;
distance = data->platformNext.x - platform_end(data->platformCurr);
if(distance > BONUS_OFFSET + 3 && random(2) == 0) {
data->bonus_next = platform_end(data->platformCurr) + 2 + random(distance - BONUS_OFFSET - 2);
data->bonus_type = random(3);
}
}
static void hero_fall(int8_t x, int8_t y) {
while(y < HEIGHT) {
if (!gr->nextFrame()) {
continue;
}
display_background();
display_hero(4 | (y % 3), x, y, WHITE);
gr->display();
gr->idle();
y++;
}
}
static void hero_score(int8_t up) {
data->score += up;
if(data->score > data->hiScore) {
data->hiScore = data->score;
}
}
static int8_t hero_walk(int8_t from, int8_t to) {
const int8_t bridge_end = platform_end(data->platformCurr) + bridge;
const int8_t bridge_over = bridge_end - 1 > platform_end(data->platformNext);
int8_t hero_state = 0;
int8_t claim_bonus = 0;
int8_t i = from;
if(! bridge_over) {
to = min(to, platform_end(data->platformNext) - HERO_OFFSET);
}
while(i < to) {
if (!gr->nextFrame()) {
continue;
}
gr->pollButtons();
if(buttonPressed(gr) && i + HERO_OFFSET + HERO_OFFSET <= data->platformNext.x) {
hero_state = hero_state ? 0 : 4;
}
display_background();
display_hero((i % 3) | hero_state, i, HERO_LEVEL, WHITE);
gr->display();
// Game over?
// TODO: simplify this condition
if((i + HERO_OFFSET >= bridge_end && (data->platformNext.x >= bridge_end || bridge_over)) ||
(hero_state && i + HERO_OFFSET + HERO_OFFSET > data->platformNext.x)) {
data->gameOn = 0;
hero_fall(i + HERO_OFFSET + (hero_state ? -1 : 1),
HERO_LEVEL - (hero_state ? 0 : HERO_HEIGHT));
return 0;
}
// Pick bonus
if(hero_state && i + HERO_OFFSET >= data->bonus_next && i <= data->bonus_next + BONUS_OFFSET) {
bonus_promote = data->bonus_type;
data->bonus_next = 0;
data->bonus_type = 0;
claim_bonus = 1;
}
gr->idle();
i++;
}
return claim_bonus;
}
static void rise_bridge(int8_t bridge_x) {
bridge = 0;
while(! buttonReleased(gr)) {
if (!gr->nextFrame()) {
continue;
}
gr->pollButtons();
bridge += bridge < BRIDGE_LEVEL;
gr->drawFastVLine(bridge_x,
BRIDGE_LEVEL - bridge + 1, bridge, WHITE);
gr->display();
gr->idle();
}
gr->drawFastVLine(bridge_x,
BRIDGE_LEVEL - bridge + 1, bridge, BLACK);
}
static void place_bridge(int8_t bridge_x) {
uint8_t bridge_short = (data->platformNext.x >= platform_end(data->platformCurr) + bridge);
for(float d = 0;d < PI/(bridge_short ? 1 : 2);) {
int8_t x, y;
if (!gr->nextFrame()) {
continue;
}
y = BRIDGE_LEVEL + sin(d + PI + PI / 2) * bridge;
x = bridge_x + cos(d + PI + PI / 2) * bridge;
gr->drawLine(bridge_x, BRIDGE_LEVEL, x, y, WHITE);
gr->display();
gr->drawLine(bridge_x, BRIDGE_LEVEL, x, y, BLACK);
d += PI / 15;
gr->idle();
}
bridge = bridge_short ? 0 : bridge;
}
static void bonus_up(int8_t claim_bonus) {
if(! claim_bonus) return;
int8_t y = HERO_LEVEL - 2;
int8_t hero_pos = platform_end(data->platformNext) - HERO_OFFSET;
while(! buttonPressed(gr) && y > 0) {
if (!gr->nextFrame()) {
continue;
}
gr->pollButtons();
display_background();
gr->drawFastHLine(hero_pos - 4, y + 2, 3);
gr->drawFastVLine(hero_pos - 3, y + 1, 3);
drawNumber(gr, hero_pos, y, claim_bonus, WHITE, 0);
display_hero(0, hero_pos, HERO_LEVEL, WHITE);
y -= (HERO_LEVEL - y) / 2;
gr->display();
gr->idle();
}
}
static void build_bridge(void) {
int8_t claim_bonus;
int8_t bridge_x = platform_end(data->platformCurr);
rise_bridge(bridge_x);
place_bridge(bridge_x);
// Walk over the bridge
claim_bonus = hero_walk(bridge_x - HERO_OFFSET,
max(platform_end(data->platformNext) - HERO_OFFSET,
bridge_x + bridge));
if(!claim_bonus) {
data->bonus_next = 0;
data->bonus_type = 0;
}
if(data->gameOn) {
claim_bonus += bridge_x + bridge - 1 == data->platformNext.x + data->platformNext.width / 2;
hero_score(claim_bonus + 1);
bonus_up(claim_bonus);
claim_bonus -= !!bonus_promote;
if(claim_bonus) {
data->bonus = min(data->bonus + claim_bonus, BONUS_LIMIT);
}
}
}
static void game_new(void) {
data->gameOn = 1;
data->score = 0;
data->bonus = 0;
data->bonus_next = 0;
data->bonus_type = 0;
data->platformCurr.x = -19;
data->platformCurr.width = 36;
data->platformCurr.bridge = 0;
data->platformNext.x = 32;
data->platformNext.width = 0;
data->platformNext.bridge = 0;
platform_random(&data->platformNext);
}
static void game_on(void) {
bridge = 0;
while(data->gameOn && ! gr->justPressed(B_BUTTON)) {
if (!gr->nextFrame()) {
continue;
}
gr->pollButtons();
display_background();
display_hero(0, platform_end(data->platformCurr) - HERO_OFFSET,
HERO_LEVEL, WHITE);
if(buttonPressed(gr) && ! gr->justPressed(B_BUTTON)) {
build_bridge();
if(data->gameOn) {
// Place new platform
platform_new();
} else if(data->bonus >= BONUS_OFFSET) {
data->bonus -= BONUS_OFFSET;
data->gameOn = 1;
bridge = 0;
}
}
gr->display();
gr->idle();
}
}
void gameBB(Arduboy2 *sgr, uint8_t *gdat, uint8_t menu, uint8_t *gameOn, uint32_t *score, uint32_t *hiScore) {
gr = sgr;
data = (struct data_t *)gdat;
if (menu == MENU_NEW) {
game_new();
}
if (menu != MENU_EXIT) {
game_on();
}
*gameOn = data->gameOn;
*score = data->score;
*hiScore = data->hiScore;
}