This repository has been archived by the owner on Jan 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
494 lines (409 loc) · 13.6 KB
/
main.c
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
// FIXME: Switch example back to arcade tileset - readme specifies that and it's PD, so harder to mess up if someone wanted to get angry!
#include "main.h"
#include "constants.h"
#include "sprite.h"
#include "title.h"
#include "pause.h"
#include "game_config.h"
#include "title_config.h"
#include "scroll_anim.h"
#include <gb/gb.h>
#include <rand.h>
UBYTE temp1, temp2, temp3, temp4, temp5, temp6, i, j;
UBYTE playerWorldPos, playerX, playerY, btns, oldBtns, playerXVel, playerYVel, gameState, playerVelocityLock, cycleCounter;
UBYTE playerHealth, playerMoney;
UBYTE buffer[32U];
UBYTE playerInvulnTime;
UINT16 temp16, temp16b, playerWorldTileStart;
UBYTE* bankedCurrentMap;
UBYTE* bankedNextMap;
UBYTE* * * currentMapSprites; // Triple pointer, so intense!!
UBYTE* tempPointer;
UBYTE lockScrollToBottom;
UBYTE worldState[64U];
UBYTE bitLookup[6U];
UBYTE currentMap[0x80];
UBYTE nextMap[0x80];
struct SPRITE mapSprites[6];
enum SPRITE_DIRECTION playerDirection;
void init_vars() {
gameState = GAME_STATE_RUNNING;
playerWorldPos = 0U;
btns = oldBtns = 0U;
playerXVel = playerYVel = 0U;
playerX = playerY = 32U;
playerHealth = STARTING_HEALTH;
playerMoney = STARTING_MONEY;
playerVelocityLock = 0U;
playerInvulnTime = 0u;
lockScrollToBottom = 0U;
// TODO: Convert this to a const array, or document why it isn't one.
bitLookup[0] = 1U;
bitLookup[1] = 2U;
bitLookup[2] = 4U;
bitLookup[3] = 8U;
bitLookup[4] = 16U;
bitLookup[5] = 32U;
// Doesn't matter for first run, but if we restart (game over, etc) we don't want to leave things in a funky state.
for (i = 0; i != 64; i++)
worldState[i] = 0U;
}
// Used in a vblank method to make sure
void set_xscroll_zero() {
SCX_REG = 0;
}
// Call during vblank to keep the scroll at the stop position for the lower screen, so we can redraw the screen
// without weird artifacts
void vbl() {
if (lockScrollToBottom == SCROLL_DIRECTION_RIGHT) {
move_bkg(SCREEN_WIDTH, SCREEN_HEIGHT);
} else if (lockScrollToBottom == SCROLL_DIRECTION_LEFT) {
move_bkg(LEFT_SCROLL_STOP, SCREEN_HEIGHT);
}
}
void load_map() {
SWITCH_ROM_MBC1(BANK_MAP);
bankedCurrentMap = MAP;
currentMapSprites = MAP_SPRITES;
playerWorldTileStart = (UINT16)playerWorldPos * (UINT16)MAP_TILE_SIZE;
temp1 = 0;
for (i = 0U; i != MAP_TILES_DOWN; i++) {
for (j = 0U; j != MAP_TILES_ACROSS; j++) {
buffer[j*2U] = bankedCurrentMap[playerWorldTileStart + j] << 2U;
buffer[j*2U+1U] = buffer[j*2U]+1U;
currentMap[temp1] = bankedCurrentMap[playerWorldTileStart + j];
temp1++;
}
temp1 += 6; // 6 bytes of padding per row to simplify math. May find a use for this later.
set_bkg_tiles(0U, i << 1U, 20U, 1U, buffer);
for (j = 0U; j != MAP_TILES_ACROSS*2; j++) {
buffer[j]+=2;
}
set_bkg_tiles(0U, i*2U+1U, 20U, 1U, buffer);
playerWorldTileStart += MAP_TILES_ACROSS;
}
tempPointer = currentMapSprites[playerWorldPos];
temp1 = 0x00; // Generic data
temp2 = 0U; // Position
// Not added to the sprite file, because this depends on map data in a separate bank, and we can only have one bank loaded at a time.
// There are ways to do this - could have map lookups in the primary file, but that would get more confusing and weird.
while(temp2 != MAX_SPRITES) {
temp1 = tempPointer++[0];
// 255 indicates the end of the array. Bail out!
if (temp1 == 255U)
break;
// If this sprite exists in worldState, move on.
temp3 = worldState[playerWorldPos];
temp4 = bitLookup[temp2];
if ((temp3 & temp4) == 0U) {
// Type is really the id of the sprite... could do with improving this...
mapSprites[temp2].type = tempPointer++[0];
mapSprites[temp2].direction = SPRITE_DIRECTION_STOP;
SWITCH_ROM_MBC1(BANK_SPRITES);
load_sprite(); // WARNING: load_sprite destroys temp1 -- the data in it is GONE past this point.
SWITCH_ROM_MBC1(BANK_MAP);
} else {
// Clean up anything left behind.
for (i = 0U; i < 4U; i++)
move_sprite(WORLD_SPRITE_START + (temp2 << 2U) + i, SPRITE_OFFSCREEN, SPRITE_OFFSCREEN);
// Advance the pointer...
tempPointer++;
mapSprites[temp2].type = SPRITE_TYPE_NONE;
}
temp2++;
}
SWITCH_ROM_MBC1(BANK_SPRITES);
clear_sprites_from_temp2();
// Leave this as it was - we depend on it elsewhere.
playerWorldTileStart = (UINT16)playerWorldPos * (UINT16)MAP_TILE_SIZE;
}
void load_next_map(UINT16 id) {
UINT16 tempid = id * (UINT16)MAP_TILE_SIZE;
SWITCH_ROM_MBC1(BANK_MAP);
bankedNextMap = MAP;
temp1 = 0;
for (i = 0U; i != MAP_TILES_DOWN; i++) {
for (j = 0U; j != MAP_TILES_ACROSS; j++) {
nextMap[temp1] = bankedNextMap[tempid + j];
temp1++;
}
temp1 += 6; // 6 bytes of padding per row to simplify math. May find a use for this later.
tempid += MAP_TILES_ACROSS;
}
}
UBYTE test_collision(UBYTE x, UBYTE y) {
// This offsets us by one tile to get us in line with 0-7= tile 0, 8-f = tile 1, etc...
x -= (8U - SPRITE_LEFT_BUFFER);
y += SPRITE_TOP_BUFFER; // Add a buffer to the top of our sprite - we made it < 16px tall
temp3 = currentMap[((y & 0xf0) - 16) + (x>>4)];
if (temp3 >= FIRST_DAMAGE_TILE) {
temp3 = COLLISION_TYPE_DAMAGE;
} else if (temp3 >= FIRST_SOLID_TILE) {
temp3 = COLLISION_TYPE_SOLID;
} else {
temp3 = COLLISION_TYPE_NONE;
}
return temp3;
}
void init_screen() {
disable_interrupts();
DISPLAY_OFF;
SWITCH_ROM_MBC1(BANK_GRAPHICS);
// REMEMBER: Graphics are set up with weird overlaps - we could be smart about this, but for now we limit you to 128 tiles in each section.
set_bkg_data(0U, 0, TILES);
set_sprite_data(0U, 128U, SPRITES); // This is also why we limit ourselves to 128 sprites.
load_map();
scroll_bkg(0U, 0U);
SPRITES_8x8;
// Initialize main character sprite to something sane.
for (i = 0; i != 4; i++)
set_sprite_tile(i, i);
SHOW_BKG;
SHOW_SPRITES;
move_win(0, 128);
SHOW_WIN;
// Always, ALWAYS force the window to stay at 0 to avoid glitchy movement.
STAT_REG = 0x45;
LYC_REG = SCREEN_HEIGHT;
set_interrupts(VBL_IFLAG | LCD_IFLAG);
add_LCD(set_xscroll_zero);
add_VBL(vbl);
DISPLAY_ON;
enable_interrupts();
}
void update_health() {
for (i = 0; i < playerHealth; i++)
buffer[i] = WINDOW_TILE_HEALTH_FULL;
for (; i < MAX_HEALTH; i++)
buffer[i] = WINDOW_TILE_HEALTH_EMPTY;
set_win_tiles(1U, 0U, MAX_HEALTH, 1U, buffer);
}
void update_money() {
buffer[0] = WINDOW_TILE_MONEY;
buffer[1] = WINDOW_TILE_NUMERIC_0 + (playerMoney / 10);
buffer[2] = WINDOW_TILE_NUMERIC_0 + (playerMoney % 10);
set_win_tiles(17U, 1U, 3U, 1U, buffer);
}
void damage_player(UBYTE amount) {
if (amount >= playerHealth) {
gameState = GAME_STATE_GAME_OVER;
playerHealth = 0;
} else {
playerHealth -= amount;
}
update_health();
if (playerXVel == 0U && playerYVel == 0) {
// If the player wasn't "moving" give a sane velocity swap
playerYVel = PLAYER_MOVE_DISTANCE;
} else {
// Otherwise reverse whatever they were doing.
playerXVel = 0U-playerXVel;
playerYVel = 0U-playerYVel;
}
playerVelocityLock = DAMAGE_COLLISION_LOCK_TIME;
playerInvulnTime = DAMAGE_COLLISION_INVULN_TIME;
}
// NOTE: If we need kernel space, this could be moved to a separate bank as long as show_pause() and load_map were
// turned into separate gameStates called in the nmi method, rather than calling them directly here.
void handle_input() {
oldBtns = btns;
btns = joypad();
if (btns & J_START && !(oldBtns & J_START)) {
SWITCH_ROM_MBC1(BANK_PAUSE);
show_pause();
}
if (!playerVelocityLock) {
// General player movement code.
playerXVel = playerYVel = 0;
if (btns & J_UP) {
playerYVel = -PLAYER_MOVE_DISTANCE;
playerDirection = SPRITE_DIRECTION_UP;
} else if (btns & J_DOWN) {
playerYVel = PLAYER_MOVE_DISTANCE;
playerDirection = SPRITE_DIRECTION_DOWN;
}
if (btns & J_LEFT) {
playerXVel = -PLAYER_MOVE_DISTANCE;
playerDirection = SPRITE_DIRECTION_LEFT;
} else if (btns & J_RIGHT) {
playerXVel = PLAYER_MOVE_DISTANCE;
playerDirection = SPRITE_DIRECTION_RIGHT;
}
}
temp1 = playerX + playerXVel;
temp2 = playerY + playerYVel;
if (playerXVel != 0) {
if (temp1 + HALF_SPRITE_WIDTH >= SCREEN_WIDTH) {
// If we've been hurt, don't transition yet; just prevent movement...
if (playerInvulnTime) {
temp1 = playerX;
} else {
playerX = 8U + PLAYER_MOVE_DISTANCE;
playerWorldPos++;
load_next_map(playerWorldPos);
SWITCH_ROM_MBC1(BANK_SCROLL_ANIM);
do_scroll_anim(SCROLL_DIRECTION_RIGHT);
lockScrollToBottom = SCROLL_DIRECTION_RIGHT;
load_map();
lockScrollToBottom = 0;
move_bkg(0, 0);
return;
}
} else if (temp1 <= 8U) {
// If we've been hurt, don't transition yet; just prevent movement...
if (playerInvulnTime) {
temp1 = playerX;
} else {
playerX = SCREEN_WIDTH - SPRITE_WIDTH - PLAYER_MOVE_DISTANCE;
playerWorldPos--;
load_next_map(playerWorldPos);
SWITCH_ROM_MBC1(BANK_SCROLL_ANIM);
do_scroll_anim(SCROLL_DIRECTION_LEFT);
lockScrollToBottom = SCROLL_DIRECTION_LEFT;
load_map();
lockScrollToBottom = 0;
move_bkg(0, 0);
return;
}
} else {
if (playerXVel == PLAYER_MOVE_DISTANCE) {
if (test_collision(temp1 + SPRITE_WIDTH, temp2) || test_collision(temp1 + SPRITE_WIDTH, temp2 + SPRITE_HEIGHT)) {
if (temp3 == COLLISION_TYPE_DAMAGE && !playerInvulnTime) {
damage_player(1U);
} else {
temp1 = playerX;
}
}
} else {
if (test_collision(temp1-1U, temp2) || test_collision(temp1-1U, temp2 + SPRITE_HEIGHT)) {
if (temp3 == COLLISION_TYPE_DAMAGE && !playerInvulnTime) {
damage_player(1U);
} else {
temp1 = playerX;
}
}
}
}
}
if (playerYVel != 0) {
if (temp2 + HALF_SPRITE_HEIGHT >= SCREEN_HEIGHT) {
// If we've been hurt, don't transition yet; just prevent movement...
if (playerInvulnTime) {
temp2 = playerY;
} else {
playerY = SPRITE_HEIGHT + PLAYER_MOVE_DISTANCE;
playerWorldPos += WORLD_ROW_HEIGHT;
load_next_map(playerWorldPos);
SWITCH_ROM_MBC1(BANK_SCROLL_ANIM);
do_scroll_anim(SCROLL_DIRECTION_DOWN);
load_map();
move_bkg(0, 0);
return;
}
} else if (temp2 <= 12U) {
// If we've been hurt, don't transition yet; just prevent movement...
if (playerInvulnTime) {
temp2 = playerY;
} else {
playerY = (SCREEN_HEIGHT - 4) - PLAYER_MOVE_DISTANCE;
playerWorldPos -= WORLD_ROW_HEIGHT;
load_next_map(playerWorldPos);
SWITCH_ROM_MBC1(BANK_SCROLL_ANIM);
do_scroll_anim(SCROLL_DIRECTION_UP);
load_map();
move_bkg(0, 0);
return;
}
} else {
if (playerYVel <= PLAYER_MOVE_DISTANCE) {
if (test_collision(temp1, temp2 + SPRITE_HEIGHT) || test_collision(temp1 + SPRITE_WIDTH, temp2 + SPRITE_HEIGHT)) {
if (temp3 == COLLISION_TYPE_DAMAGE && !playerInvulnTime) {
damage_player(1U);
} else {
temp2 = playerY;
}
}
} else {
if (test_collision(temp1, temp2) || test_collision(temp1 + SPRITE_WIDTH, temp2)) {
if (temp3 == COLLISION_TYPE_DAMAGE && !playerInvulnTime) {
damage_player(1U);
} else {
temp2 = playerY;
}
}
}
}
}
playerX = temp1;
playerY = temp2;
for (i = 0U; i < 4U; i++) {
draw_sprite_anim_state();
// This little bit of trickery flashes the sprite if the player is in the invulnerability period after getting hurt.
if (!playerInvulnTime || playerInvulnTime & 0x04) {
move_sprite(i, playerX + (i%2U)*8U, playerY + (i/2U)*8U);
} else {
move_sprite(i, SPRITE_OFFSCREEN, SPRITE_OFFSCREEN);
}
}
if (playerVelocityLock > 0)
--playerVelocityLock;
if (playerInvulnTime > 0)
--playerInvulnTime;
}
void draw_sprite_anim_state() {
#if MAIN_CHARACTER_SPRITE_TYPE == MAIN_CHARACTER_SPRITE_SINGLE
// This code will only be used if your game only has a single static sprite for the main character.
if (playerXVel + playerYVel != 0U)
set_sprite_tile(i, i);
#elif MAIN_CHARACTER_SPRITE_TYPE == MAIN_CHARACTER_SPRITE_DIRECTIONAL
// This code will only be used if your game has a multi-directional non-animated main character sprite.
if (playerXVel + playerYVel != 0U)
set_sprite_tile(i, ((playerDirection-1U)<<2U) + i);
#elif MAIN_CHARACTER_SPRITE_TYPE == MAIN_CHARACTER_SPRITE_ANIMATED_DIRECTIONAL
// This code will only be used if your game has a multi-directional animated main character sprite.
if (playerXVel + playerYVel != 0U)
set_sprite_tile(i, (((sys_time & PLAYER_ANIM_INTERVAL) >> PLAYER_ANIM_SHIFT)<<2U) + ((playerDirection-1U)<<3U) + i);
#else
#error "Unknown main character sprite type. Check the definition for MAIN_CHARACTER_SPRITE_TYPE"
#endif
}
void main() {
// Wrap the entirety of our main in a loop, so when the user exits, we can put ourselves back into a "like-new" state.
// Just set gameState to something, check in the inner for loop, and break if it's time to reboot.
while (1) {
init_vars();
SWITCH_ROM_MBC1(BANK_TITLE);
show_title();
init_screen();
update_health();
update_money();
initrand(sys_time);
// Game loop. Does all the things.
while(1) {
if (gameState == GAME_STATE_RUNNING) {
++cycleCounter;
SWITCH_ROM_MBC1(BANK_MAP);
handle_input();
SWITCH_ROM_MBC1(BANK_SPRITES);
move_sprites();
if (!playerVelocityLock) {
SWITCH_ROM_MBC1(BANK_SPRITES);
test_sprite_collision();
}
} else if (gameState == GAME_STATE_PAUSED) {
SWITCH_ROM_MBC1(BANK_PAUSE);
do_pause();
} else if (gameState == GAME_STATE_GAME_OVER) {
SWITCH_ROM_MBC1(BANK_TITLE);
show_game_over();
break; // Break out of the game loop and start this whole mess all over again...
} else if (gameState == GAME_STATE_WINNER) {
SWITCH_ROM_MBC1(BANK_TITLE);
show_winner_screen();
break; // Break out of the game loop and the let them play again.
}
// Limit us to not-batnose-crazy speeds
wait_vbl_done();
}
}
}