-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
511 lines (452 loc) · 11.3 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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#include <gb/gb.h>
#include <stdio.h>
#include "tiles/title_screen/letters.c"
#include "maps/title_screen/title_map.c"
#include "tiles/follow_the_train/follow_the_train_data.c"
#include "maps/follow_the_train/follow_the_train_map.c"
#include "huGEDriver/huGEDriver.h"
#include "maps/SampleMap.h"
#include "tiles/map/SampleTiles.h"
// #include "tiles/map/SampleTiles2.h"
#include "tiles/player/PlayerTile.h"
#include "animation/player_ani.h"
#include "mecanics/physics.h"
#include "constants.h"
typedef void (*nullfunc_t)(void);
#define camera_max_y ((sample_mapHeight - 18) * 8)
#define camera_max_x ((sample_mapWidth - 20) * 8)
#define MIN(A, B) ((A) < (B) ? (A) : (B))
#define NBSFRAMES 0x02 /* Nb frames for the sprite */
extern const hUGESong_t song_Data;
uint8_t joy;
// current and old positions of the camera in pixels
uint16_t camera_x, camera_y, old_camera_x, old_camera_y;
// current and old position of the map in tiles
uint8_t map_pos_x, map_pos_y, old_map_pos_x, old_map_pos_y;
// redraw flag, indicates that camera position was changed
uint8_t redraw;
static uint8_t time = 0; /* Global "time" value (counter) */
fixed sposx, sposy; /* Sprite position (fixed point) */
fixed fsposx, fsposy; /* Future Sprite position (fixed point) */
// fixed Last_sposx, Last_sposy; /* Last sprite position (fixed point) */
int16_t sspx, sspy; /* Sprite speed (fixed point) */
uint8_t sframe = 0; /* Current frame of the sprite */
fixed player_x_speed;
uint8_t player_animation = PLAYER_IDLE;
uint8_t number_farmes = 0x01;
uint8_t sframe_loop = 1;
uint8_t sprite_tile = 0;
void animate_player(uint8_t *animation);
void place_sprite();
void colison();
// In game variables
uint8_t start_screen_retries = 3;
/* Colison phisics */
/* Set sprite tiles */
void player_sprite(uint8_t *animation)
{
uint8_t s;
s = sframe << 1;
if (animation == PLAYER_RIGHT)
{
if (walls_down_check(10, 10) == 0)
{
set_sprite_tile(0, player_jump_right[s]);
number_farmes = 0x05;
sframe = 0;
sframe_loop = 0;
}
else
{
set_sprite_tile(0, player_right[s]);
number_farmes = 0x03;
sframe_loop = 1;
}
}
if (animation == PLAYER_LEFT)
{
if (walls_down_check(10, 10) == 0)
{
set_sprite_tile(0, player_jump_left[s]);
number_farmes = 0x05;
sframe = 0;
sframe_loop = 0;
}
else
{
set_sprite_tile(0, player_left[s]);
number_farmes = 0x03;
sframe_loop = 1;
}
}
if (animation == PLAYER_IDLE)
{
set_sprite_tile(0, player_idle[s]);
number_farmes = 0x01;
sframe = 0;
sframe_loop = 1;
}
// set_sprite_tile(1, player_right[s + 1]);
}
void animate_player(uint8_t *animation)
{
if ((time & 0x07) == 0)
{
sframe++;
if (sframe == number_farmes && sframe_loop == 1)
sframe = 0;
player_sprite(animation);
}
}
void place_sprite()
{
//speed limit
if (sspx > MAX_X_SPEED)
{
sspx = MAX_X_SPEED;
}
if (sspx < -MAX_X_SPEED)
{
sspx = -MAX_X_SPEED;
}
if (sspy > MAX_X_SPEED)
{
sspy = MAX_Y_SPEED;
}
if (sspy < -MAX_Y_SPEED)
{
sspy = -MAX_Y_SPEED;
}
uint16_t fx, fy;
fx = sposx.w + sspx;
fy = sposy.w + sspy;
// printf("%d ", sspy);
if (sspx > 0)
{
if (walls_right(10, 10) == 0)
{
if (fx / 256 < (20 - 7) * 8)
{
sposx.w += sspx;
}
else if (camera_x < camera_max_x)
{
camera_x++;
redraw = TRUE;
}
else if (fx / 256 < 20 * 8)
{
sposx.w += sspx;
}
}
// Kill check
if (walls_right(21, 11) == 1)
{
// showTitleScreen();
levelOne();
}
}
else if (sspx < 0)
{
if (walls_left(10, 10) == 0)
{
if (fx / 256 > 7 * 8)
{
sposx.w += sspx;
}
else if (camera_x > 0)
{
camera_x--;
redraw = TRUE;
}
else if (fx / 256 > 8)
{
sposx.w += sspx;
}
}
// Kill check
if (walls_left(21, 11) == 1)
{
// showTitleScreen();
levelOne();
}
}
if (walls_down_check(10, 9) == 0)
{
sspy = sspy + GRAVITY;
// sposy.w += sspy;
// printf("%d", sspy);
}
if (sspy > 0)
{
// DOWN Collision Check
if (walls_down(10, 9) == 0)
{
sposy.w += sspy;
}
if (walls_down(10, 9) == 1)
{
sspy = 0;
}
// Kill check
// if (walls_down(20, 11) == 1)
// {
// // showTitleScreen();
// levelOne();
// }
}
else if (sspy < 0)
{
if (walls_up(10, 10) == 0)
{
// printf("%d ", sspy);
sposy.w += sspy;
}
if (walls_up(10, 10) == 1)
{
sspy = 0;
}
//kill check
if (walls_up(21, 11) == 1)
{
// showTitleScreen();
levelOne();
}
}
if (walls_down(20, 11) == 1)
{
// showTitleScreen();
levelOne();
}
if (
camera_x > 40 && camera_x < 43 ||
camera_x > 50 && camera_x < 52 ||
camera_x > 60 && camera_x < 62 ||
camera_x > 60 && camera_x < 62 ||
camera_x > 66 && camera_x < 68 ||
camera_x > 70 && camera_x < 80 ||
camera_x > 90 && camera_x < 100 ||
camera_x > 105 && camera_x < 108 ||
camera_x > 110 && camera_x < 116 ||
camera_x > 120 && camera_x < 140 ||
camera_x > 145 && camera_x < 180 ||
camera_x > 250 && camera_x < 253 ||
camera_x > 260 && camera_x < 268 ||
camera_x > 350 && camera_x < 400 ||
camera_x > 410 && camera_x < 470 ||
camera_x > 500 && camera_x < 1000
)
{
if (sprite_tile < 10)
{
sprite_tile = 10;
set_bkg_data(0, 32u, tiles[1]);
}
}
else
{
if (sprite_tile >= 10)
{
sprite_tile = 0;
set_bkg_data(0, 30u, tiles);
}
}
move_sprite(0, sposx.b.h, sposy.b.h);
// move_sprite(1, sposx.b.h + 8, sposy.b.h);
/* friction */
sspx = sspx / 2;
}
void set_camera()
{
// update hardware scroll position
SCY_REG = camera_y;
SCX_REG = camera_x;
// up or down
map_pos_y = (uint8_t)(camera_y >> 3u);
if (map_pos_y != old_map_pos_y)
{
if (camera_y < old_camera_y)
{
set_bkg_submap(map_pos_x, map_pos_y, MIN(21u, sample_mapWidth - map_pos_x), 1, sample_map, sample_mapWidth);
}
else
{
if ((sample_mapHeight - 18u) > map_pos_y)
set_bkg_submap(map_pos_x, map_pos_y + 18u, MIN(21u, sample_mapWidth - map_pos_x), 1, sample_map, sample_mapWidth);
}
old_map_pos_y = map_pos_y;
}
// left or right
map_pos_x = (uint8_t)(camera_x >> 3u);
if (map_pos_x != old_map_pos_x)
{
if (camera_x < old_camera_x)
{
set_bkg_submap(map_pos_x, map_pos_y, 1, MIN(19u, sample_mapHeight - map_pos_y), sample_map, sample_mapWidth);
}
else
{
if ((sample_mapWidth - 20u) > map_pos_x)
set_bkg_submap(map_pos_x + 20u, map_pos_y, 1, MIN(19u, sample_mapHeight - map_pos_y), sample_map, sample_mapWidth);
}
old_map_pos_x = map_pos_x;
}
// set old camera position to current camera position
old_camera_x = camera_x, old_camera_y = camera_y;
}
void levelOne(void)
{
DISPLAY_OFF;
SHOW_BKG;
disable_interrupts();
LCDC_REG = LCDCF_OFF | LCDCF_BG8800 | LCDCF_BG9800 | LCDCF_OBJON | LCDCF_BGON;
/*
* LCD = Off
* BG Chr = 0x8800
* BG Bank = 0x9800
* OBJ = 8x8
* OBJ = On
* BG = On
*/
// Loads sprites to memory
set_sprite_data(0x00, 11u, player);
set_sprite_prop(0, 0x00);
// set_sprite_prop(1, 0x00);
sframe = 0;
sposx.w = 0x1000;
sposy.w = 0x1000;
sspx = 0x0000;
sspy = 0x0050;
player_sprite(PLAYER_RIGHT);
place_sprite();
player_x_speed.w = 200;
set_bkg_data(0, 30u, tiles);
map_pos_x = map_pos_y = 0;
old_map_pos_x = old_map_pos_y = 255;
set_bkg_submap(map_pos_x, map_pos_y, 20, 18, sample_map, sample_mapWidth);
DISPLAY_ON;
camera_x = camera_y = 0;
old_camera_x = camera_x;
old_camera_y = camera_y;
DISPLAY_ON;
enable_interrupts();
redraw = FALSE;
SCX_REG = camera_x;
SCY_REG = camera_y;
while (TRUE)
{
joy = joypad();
time++;
// printf("%d", joy);
animate_player(player_animation);
place_sprite();
player_animation = PLAYER_IDLE;
// Jump
if (joy & J_B)
{
if (walls_down_check(10, 10) == 1)
{
// printf("%d", walls_down_check(10, 10));
// sposy.w = sposy.w - 200;
sspy = -300;
}
}
// up or down
// if (joy & J_UP)
// {
// if (camera_y)
// {
// camera_y--;
// redraw = TRUE;
// }
// }
// else if (joy & J_DOWN)
// {
// if (camera_y < camera_max_y)
// {
// camera_y++;
// redraw = TRUE;
// }
// }
// left or right
if (joy & J_LEFT)
{
player_animation = PLAYER_LEFT;
sspx = sspx - player_x_speed.w;
}
else if (joy & J_RIGHT)
{
player_animation = PLAYER_RIGHT;
sspx = sspx + player_x_speed.w;
}
if (redraw)
{
wait_vbl_done();
set_camera();
redraw = FALSE;
}
else
{
wait_vbl_done();
}
}
}
void showTitleScreen()
{
__critical
{
hUGE_init(&song_Data);
add_VBL(hUGE_dosound);
}
set_bkg_data(0, 11, letters);
set_bkg_tiles(0, 0, titleMapWidth, titleMapHeight, titleMap);
SHOW_BKG;
DISPLAY_ON;
while (1)
{
wait_vbl_done();
scroll_bkg(1, 1);
delay(100);
check_player_start_activity();
}
}
void show_all_you_had_to_do_was_press_the_start_button_screen()
{
move_bkg(0, 0);
set_bkg_data(0, 121, follow_the_train_data);
set_bkg_tiles(0, 0, 20, 18, follow_the_train_map);
SHOW_BKG;
delay(3000);
((nullfunc_t)0x3000)();
}
void check_player_start_activity()
{
if (start_screen_retries > 0)
{
joy = joypad();
if (joy != 0)
{
if (joy != J_START)
{
show_all_you_had_to_do_was_press_the_start_button_screen();
}
else
{
start_screen_retries--;
}
}
}
else
{
levelOne();
}
}
void main(void)
{
LCDC_REG = 0xD1;
BGP_REG = 0b11100100;
NR52_REG = 0x80;
NR51_REG = 0xFF;
NR50_REG = 0x77;
showTitleScreen();
}