-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
314 lines (247 loc) · 6.49 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
#include <gb/gb.h>
#include <stdbool.h>
#include <stdlib.h>
#include "tiles/pool.h"
#include "tiles/ocean.h"
#include "tiles/sprites.h"
#include "tiles/text.h"
#include "maps/map.h"
#include "maps/text.h"
#include "data/data.h"
#include "sounds/sounds.h"
#include "movement/movement.h"
#include "movement/parse.h"
#include "internal/internal.h"
void init();
void check_input();
void update_switches();
void check_game_state();
void restart_game();
void player_hit(UINT16 time);
void spawn_enemies();
void inactive_enemies();
GameState game_state;
Large player;
Enemy* enemies;
UINT8 enemy_count;
Obstacle *obstacle;
UINT8 obstacle_count;
// keep track of sprite ids here, they should increment sequentially
UINT8 dog_id[] = {0, 1, 2, 3};
UINT8 bowl_id[] = {4, 5, 6};
UINT8 next_enemy_id = 7;
// consts for game
const UINT8 tile_size = 8; // px
const UINT8 player_size = 16; // px
void main() {
init();
while(1) {
check_input(); // Check for user input (and act on it)
inactive_enemies();
spawn_enemies();
update_switches(); // Make sure the SHOW_SPRITES and SHOW_BKG switches are on each loop
check_game_state();
wait_vbl_done(); // Wait until VBLANK to avoid corrupting memory, waits 1 frame
if(game_state.game_over) { break; }
}
restart_game();
}
void init() {
game_state.game_over = false;
game_state.off_frame = false;
game_state.sys_time_i = UINT16_MAX - I_FRAMES;
game_state.sys_time_bs = UINT16_MAX - BS_FRAMES;
game_state.scrolled = 0;
game_state.level = POOL_LEVEL;
DISPLAY_ON;
changeLevel(POOL_LEVEL);
set_sprite_data(0, 20, spriteTiles);
init_large(&player, dog_id, 16, 16);
render_large(&player, dog_id);
// dog bowls
Small s; // no need to save the bowls in an array
init_small(&s, 0x09, 20, 144);
render_small(&s, bowl_id[0]);
init_small(&s, 0x09, 20+10, 144);
render_small(&s, bowl_id[1]);
init_small(&s, 0x09, 20+20, 144);
render_small(&s, bowl_id[2]);
enemies = read_enemy(game_state.level, &enemy_count);
init_sound();
init_hp();
}
void update_switches() {
HIDE_WIN;
SHOW_SPRITES;
SHOW_BKG;
}
void check_input() {
game_state.joypad = joypad();
UINT8 x_mod = 0;
UINT8 y_mod = 0;
if (game_state.joypad & J_A) {
}
if (game_state.joypad & J_B) {
}
// UP
if (game_state.joypad & J_UP) {
y_mod = y_mod - 1;
}
// DOWN
if (game_state.joypad & J_DOWN) {
y_mod++;
}
// LEFT
if (game_state.joypad & J_LEFT) {
x_mod = x_mod - 1;
}
// RIGHT
if (game_state.joypad & J_RIGHT) {
x_mod++;
}
// on the off_frame, put some gravity on the player
if (game_state.off_frame) {
y_mod++;
}
UINT8 temp_x = player.x + x_mod;
UINT8 temp_y = player.y + y_mod;
UINT16 time = sys_time;
if (player_collision_with_enemies(temp_x, temp_y, player_size, enemies, enemy_count)) {
play_sound(boundary_hit);
player_hit(time);
// quick fix, need to calculate the actual collision
// (so the player doesn't get stuck in the enemy).
// we would want the player to move in the opposite direction to collision
temp_x = temp_x - x_mod;
// return
}
// screen boundaries
if (player_collision_with_screen(temp_x, temp_y, player_size, tile_size))
{
if (is_grace_period_over(BS_FRAMES, time, game_state.sys_time_bs)) {
play_sound(boundary_hit);
game_state.sys_time_bs = sys_time;
}
return;
}
if (scroll(player.x + x_mod, x_mod, 0, &game_state.scrolled)) {
move_large(&player, temp_x - x_mod, temp_y);
for (UINT8 i = 0; i < enemy_count; i++){
Enemy *enemy = &enemies[i];
if (has_spawned(enemy) == 0){ continue; }
UINT8 enemy_x;
UINT8 enemy_y;
if (enemy->sprite_size == 0) {
enemy_x = enemy->small.x;
enemy_y = enemy->small.y;
}
else{
enemy_x = enemy->large.x;
enemy_y = enemy->large.y;
}
move_enemy(enemy, enemy_x - x_mod, enemy_y);
}
}
else {
move_large(&player, temp_x, temp_y);
}
for (UINT8 i = 0; i < enemy_count; i++) {
Enemy *enemy = &enemies[i];
if (has_spawned(enemy) == 0){ continue; }
move_enemy_preset(enemy, game_state.off_frame);
}
}
// check if invincibility frames ran out, and take damage if yes
void player_hit(UINT16 time){
if (is_grace_period_over(I_FRAMES, time, game_state.sys_time_i)) {
// invinibility ran out, player takes damage
dec_hp();
hide_sprite(bowl_id[get_hp()]);
play_sound(boundary_hit);
game_state.sys_time_i = time;
}
}
void check_game_state(){
game_state.off_frame = !game_state.off_frame;
if (is_player_dead()) {
game_state.game_over = true;
play_sound(death);
delay(1000);
SHOW_WIN;
set_win_data(11, 8, textTiles);
// TODO: make this not hard coded lmao
set_win_tile_xy(3, 9, 0x13); //
set_win_tile_xy(4, 9, 0x0B); // G
set_win_tile_xy(5, 9, 0x0C); // A
set_win_tile_xy(6, 9, 0x0D); // M
set_win_tile_xy(7, 9, 0x0E); // E
set_win_tile_xy(8, 9, 0x13); //
set_win_tile_xy(9, 9, 0x11); // O
set_win_tile_xy(10, 9, 0x10); // V
set_win_tile_xy(11, 9, 0x0E); // E
set_win_tile_xy(12, 9, 0x12); // R
delay(1000);
}
if (checkLevelChange(&(game_state.level), &(game_state.scrolled))) {
enemies = read_enemy(game_state.level, &enemy_count);
delay(1000);
}
}
void spawn_enemies(){
UINT8 id_extra = 0;
for (UINT8 i = 0; i < enemy_count; i++){
Enemy *enemy = &enemies[i];
if (has_spawned(enemy) == 1){ continue; }
UINT16 enemy_x;
UINT8 id[4] = { 0, 0, 0, 0 };
if (enemy->sprite_size == 0) {
enemy_x = enemy->small.x;
id[0] = 7 + i + id_extra;
}
else{
enemy_x = enemy->large.x;
id[0] = 7 + i + id_extra;
id[1] = 7 + i + 1 + id_extra;
id[2] = 7 + i + 2 + id_extra;
id[3] = 7 + i + 3 + id_extra;
id_extra += 3;
}
if (game_state.scrolled + 160 >= enemy_x) {
render_enemy(enemy, id);
// render_enemy(enemy, get_next_enemy_id(enemy, &next_enemy_id));
}
}
}
void inactive_enemies(){
for (UINT8 i = 0; i < enemy_count; i++) {
Enemy *enemy = &enemies[i];
if (is_active(enemy) == 1 && is_dead(enemy) == 1){ continue; }
UINT16 enemy_x = enemy->small.x;
UINT8 id = enemy->small.id;
// UINT8 id[4] = {0, 0, 0, 0};
// if (enemy->sprite_size == 0) {
// enemy_x = enemy->small.x;
// id[0] = enemy->small.id;
// }
// else{
// enemy_x = enemy->large.x;
// id[0] = enemy->large.id[0];
// id[1] = enemy->large.id[1];
// id[2] = enemy->large.id[2];
// id[3] = enemy->large.id[3];
// }
if (game_state.scrolled < enemy_x && enemy_x < game_state.scrolled + 160) {
continue;
}
// hide_enemy(enemy);
}
}
// lazy way to restart game, will reload the rom
void restart_game(){
while (1) {
if (joypad())
{
reset();
}
}
}