-
Notifications
You must be signed in to change notification settings - Fork 0
/
level_setup.c
201 lines (166 loc) · 4.33 KB
/
level_setup.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
// Mark 3:5
#include <gb/gb.h>
#include <stdio.h>
#include <stdlib.h>
#include "Player.c"
#include "Assets/MazeMap.c"
#include "Configurations/transitions.c"
#include "Assets/MazeSprites.c"
#include "music.c"
/** LEVEL ATTRIBUTES **/
struct LevelFeatures {
UINT16 key_loc;
UINT16 door_loc;
UINT16 key_coord[2];
UINT16 door_coord[2];
};
struct LevelFeatures level_1;
struct LevelFeatures level_2;
struct LevelFeatures level_3;
unsigned char *levels[] = { maze_map, maze_map_two, maze_map_three };
UINT8 level_num = 0;
const char reset_block[1] = {0x00};
UBYTE haskey;
/** For debugging **/
UBYTE debug = 0;
/** PLAYER ATTRIBUTES **/
const int P1 = 0;
// Player coodinates
struct PlayerLocation {
UINT8 x;
UINT8 y;
};
struct PlayerLocation playerlocation;
/** LEVEL ATTRIBUTES **/
// Generates the level specified
// in the parameter.
void generate(UINT8 level_num)
{
set_bkg_data(0, 4, maze_sprites);
set_bkg_tiles(0, 0, 20, 18, levels[level_num]);
}
// Generates a random level.
void generate_level()
{
generate(level_num++);
}
// Sets the key and door locations.
void set_level_attributes()
{
// Level 1 attributes
level_1.key_loc = 74;
level_1.door_loc = 4;
level_1.key_coord[0] = 14;
level_1.key_coord[1] = 3;
level_1.door_coord[0] = 4;
level_1.door_coord[1] = 0;
// Level 2 attributes
level_2.key_loc = 107;
level_2.door_loc = 339;
level_2.key_coord[0] = 7;
level_2.key_coord[1] = 5;
level_2.door_coord[0] = 19;
level_2.door_coord[1] = 16;
// Level 3 attributes
level_3.key_loc = 189;
level_3.door_loc = 358;
level_3.key_coord[0] = 9;
level_3.key_coord[1] = 9;
level_3.door_coord[0] = 18;
level_3.door_coord[1] = 18;
}
void level_complete()
{
if (level_num < 3)
{
fadeout();
HIDE_SPRITES;
printf("\n \n \n \n \n \n \n \n \n LEVEL COMPLETED!");
fadein();
waitpad(J_START);
fadeout();
SHOW_SPRITES;
generate_level();
reset_player(P1, &playerlocation);
fadein();
}
else
{
fadeout();
HIDE_SPRITES;
printf("\n \n \n \n \n \n \n \n \n YOU COMPLETED THE GAME!");
fadein();
}
}
// Determines the boundaries where the player can move.
UBYTE allow_player_move(UINT8 player_X, UINT8 player_Y, struct LevelFeatures *lvl, UINT8 index)
{
UINT16 idx_TOP_LEFT_X, idx_TOP_LEFT_Y, idx_TILE_TOP_LEFT;
UBYTE result;
// How many sprites(8-bits) down is the player?
idx_TOP_LEFT_X = (player_X - 8) / 8;
idx_TOP_LEFT_Y = (player_Y - 16) / 8;
// 20 sprites per row
idx_TILE_TOP_LEFT = 20 * idx_TOP_LEFT_Y + idx_TOP_LEFT_X;
if (debug)
{
printf("%u %u\n", (UINT16) player_X, (UINT16) player_Y);
printf("%u %u %u\n", (UINT16) idx_TOP_LEFT_X, (UINT16) idx_TOP_LEFT_Y, (UINT16) idx_TILE_TOP_LEFT);
}
// Checks if we are on a blank block.
result = levels[index][idx_TILE_TOP_LEFT] == reset_block[0];
if (idx_TILE_TOP_LEFT==lvl->key_loc)
{
// collect the key
if (haskey == 0)
play_sound();
set_bkg_tiles(lvl->key_coord[0], lvl->key_coord[1], 1, 1, reset_block[0]);
haskey = 1;
result = 1;
}
else if (idx_TILE_TOP_LEFT==lvl->door_loc && haskey)
{
play_sound();
set_bkg_tiles(lvl->door_coord[0], lvl->door_coord[1], 1, 1, reset_block);
level_complete();
haskey = 0;
}
return result;
}
// Controls of the player
void player_control(UINT8 player_id, struct PlayerLocation *playerLoc, const struct LevelFeatures *lvl)
{
if (joypad() & J_LEFT)
{
if (allow_player_move(playerLoc->x - 8, playerLoc->y, lvl, level_num - 1))
{
playerLoc->x -= 8;
animate_sprite(player_id, -8, 0);
}
}
else if (joypad() & J_RIGHT)
{
if (allow_player_move(playerLoc->x + 8, playerLoc->y, lvl, level_num - 1))
{
playerLoc->x += 8;
animate_sprite(player_id, 8, 0);
}
}
else if (joypad() & J_UP)
{
if (allow_player_move(playerLoc->x, playerLoc->y - 8, lvl, level_num - 1))
{
playerLoc->y -= 8;
animate_sprite(player_id, 0, -8);
}
}
else if (joypad() & J_DOWN)
{
if (allow_player_move(playerLoc->x, playerLoc->y + 8, lvl, level_num - 1))
{
playerLoc->y += 8;
animate_sprite(player_id, 0, 8);
}
}
performant_delay(5);
}