-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmap.c
426 lines (370 loc) · 12.7 KB
/
map.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
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include "map.h"
/*
[OFFSET CALCULATOR FUNCTIONS]
You might want to modify this functions if you are using different assets
In short, this functions help us to find which asset to draw
*/
static void get_map_offset(Map * map);
static Point get_wall_offset_assets(Map * map, int i, int j);
static Point get_floor_offset_assets(Map * map, int i, int j);
static Point get_hole_offset_assets(Map * map, int i, int j);
static const int offset = 16;
static bool tile_collision(Point player, Point tile_coord);
Map create_map(char * path, uint8_t type){
Map map;
memset(&map, 0, sizeof(Map));
FILE * f = fopen(path, "r");
if(!f){
game_abort("Can't load map file : %s", path);
}
game_log("Using %s to load the map", path);
fscanf(f, "%d %d", &map.row, &map.col);
char ch;
// Map Array
map.map = (uint8_t **) malloc(map.row * sizeof(uint8_t *));
for(int i=0; i<map.row; i++){
map.map[i] = (uint8_t *) malloc(map.col * sizeof(uint8_t));
}
// Map Offset Resource Pack Array
map.offset_assets = (Point **) malloc(map.row * sizeof(Point *));
for(int i=0; i<map.row; i++){
map.offset_assets[i] = (Point *) malloc(map.col * sizeof(Point));
}
int coin_counter = 0;
// Scan the map to the array
int door_counter = 0;
for(int i = 0; i < map.row; i++){
for(int j = 0; j < map.col; j++){
fscanf(f, " %c", &ch);
switch(ch)
{
case '#': // Case Wall
map.map[i][j] = WALL;
break;
case '.': // Case Floor
map.map[i][j] = FLOOR;
break;
case 'P': // Spawn Point
map.map[i][j] = FLOOR;
map.Spawn = (Point){i, j};
break;
case 'S': // Slime Enemy
map.map[i][j] = FLOOR;
map.EnemyCode[map.EnemySpawnSize] = ch;
map.EnemySpawn[map.EnemySpawnSize++] = (Point){i, j};
break;
case 'C': // Coins
map.map[i][j] = COIN;
coin_counter++;
break;
case '_': // Nothing
map.map[i][j] = HOLE;
break;
default: // If not determined, set it as black
map.map[i][j] = NOTHING;
break;
}
}
}
map.assets = al_load_bitmap("Assets/map_packets.png");
if(!map.assets){
game_abort("Can't load map assets");
}
map.coin_assets = al_load_bitmap("Assets/coins.png");
if (!map.coin_assets) {
game_abort("Can't load coin assets");
}
// load the offset for each tiles type
get_map_offset(&map);
map.coin_audio = al_load_sample("Assets/audio/coins.mp3");
if(!map.coin_audio){
game_abort("Can't load coin audio");
}
fclose(f);
return map;
}
void draw_map(Map * map, Point cam){
// Draw map based on the camera point coordinate
al_clear_to_color(al_map_rgb(24, 20, 37));
for(int i=0; i<map->row; i++){
for(int j=0; j<map->col; j++){
int dy = i * TILE_SIZE - cam.y; // destiny y axis
int dx = j * TILE_SIZE - cam.x; // destiny x axis
Point offset_asset = map->offset_assets[i][j];
al_draw_scaled_bitmap(map->assets, // image
offset_asset.x, offset_asset.y, 16, 16, // source x, source y, width, height
dx, dy, TILE_SIZE, TILE_SIZE, // destiny x, destiny y, destiny width, destiny height
0 // flag : set 0
);
switch(map->map[i][j]){
case COIN:
al_draw_scaled_bitmap(map->coin_assets,
0, 0, 16, 16,
dx, dy, TILE_SIZE, TILE_SIZE,
0);
break;
default:
break;
}
#ifdef DRAW_HITBOX
al_draw_rectangle(
dx, dy, dx + TILE_SIZE, dy + TILE_SIZE,
al_map_rgb(0, 255, 0), 1
);
#endif
}
}
}
void update_map(Map * map, Point player_coord, int* total_coins){
/*
Hint: To check if it's collide with object in map, you can use tile_collision function
e.g. to update the coins if you touch it
*/
}
void destroy_map(Map * map){
for(int i = 0; i < map->row; i++){
free(map->map[i]);
free(map->offset_assets[i]);
}
free(map->map);
free(map->offset_assets);
al_destroy_bitmap(map->assets);
al_destroy_bitmap(map->coin_assets);
al_destroy_sample(map->coin_audio);
}
bool isWalkable(BLOCK_TYPE block){
if(block == FLOOR || block == COIN) return true;
return false;
}
/*
DON'T CHANGE CODE BELOW UNLESS YOU ARE UNDERSTAND WELL
OR WANT TO CHANGE TO DIFFERENT ASSETS
*/
static bool isWall(Map * map, int i, int j);
//static bool isFloorHole(Map * map, int i, int j);
static bool isFloor(Map * map, int i, int j);
static bool isNothing(Map * map, int i, int j);
static Point get_floor_offset_assets(Map * map, int i, int j){
bool up = isWall(map, i-1, j);
bool left = isWall(map, i, j-1);
bool right = isWall(map, i, j+1);
bool top_left = isWall(map, i-1, j-1);
bool top_right = isWall(map, i-1, j+1);
if(up && left && right){
return (Point){ offset * 12, offset * 3 };
}
if(up && left){
return (Point){ offset * 11, 0 };
}
if(up && right){
return (Point){ offset * 13, 0 };
}
if(left){
if(top_left)
return (Point){ offset * 11, offset * 1 };
else
return (Point){ offset * 15, offset * 1 };
}
if(right){
if(top_right)
return (Point){ offset * 13, offset * 1 };
else
return (Point){ offset * 14, offset * 1 };
}
if(up){
return (Point){ offset * 12, 0 };
}
if(top_left){
return (Point){ offset * 11, offset * 3 };
}
if(top_right){
return (Point){ offset * 13, offset * 3 };
}
return (Point){12 * offset, 4 * offset};
}
// Calculate the offset from the source assets
static Point get_wall_offset_assets(Map * map, int i, int j){
bool up = isWall(map, i-1, j);
bool down = isWall(map, i+1, j);
bool left = isWall(map, i, j-1);
bool right = isWall(map, i, j+1);
if(up && down && left && right){
return (Point){ 3 * offset, 5 * offset };
}
if(up && down && right){
bool left_floor = isFloor(map, i, j - 1);
bool right_down = isWall(map, i + 1, j + 1);
bool right_up = isWall(map, i - 1, j + 1);
if (right_down && right_up)
return (Point) { 2 * offset, 5 * offset };
if (left_floor) {
return (Point) { 1 * offset, 3 * offset };
}
else {
return (Point) { 4 * offset, 5 * offset };
}
}
if (up && down && left) {
bool right_floor = isFloor(map, i, j + 1);
bool left_down = isWall(map, i + 1, j - 1);
bool left_up = isWall(map, i - 1, j - 1);
if (left_down && left_up)
return (Point) { 4 * offset, 5 * offset };
if (right_floor) {
return (Point) { 1 * offset, 3 * offset };
}
else {
return (Point) { 2 * offset, 5 * offset };
}
}
if(down && right && left){
bool down_right_wall = isWall(map, i + 1, j + 1);
bool down_left_wall = isWall(map, i + 1, j - 1);
bool down_right = isFloor(map, i+1, j+1);
bool down_left = isFloor(map, i+1, j-1);
if(down_right_wall && down_left_wall)
return (Point) { 3 * offset, 4 * offset };
if((down_right ^ down_left) == 0)
return (Point){ 8 * offset, 5 * offset };
if(down_right)
return (Point){ 4 * offset, 4 * offset };
if(down_left)
return (Point){ 2 * offset, 4 * offset };
}
if(left && right){
if(isFloor(map, i+1, j))
return (Point){ 7 * offset, 5 * offset};
else
return (Point){ 7 * offset, 7 * offset};
}
if(down && up){
bool left_ = isFloor(map, i, j-1);
bool right_ = isFloor(map, i, j+1);
if((left_ ^ right_) == 0)
return (Point){ 1 * offset, 3 * offset};
if(left_)
return (Point){ 2 * offset, 5 * offset};
return (Point){ 4 * offset, 5 * offset};
}
if(down && right){
if(isFloor(map, i+1, j+1))
return (Point){ 4 * offset, 5 * offset };
else
return (Point){ 2 * offset, 4 * offset };
}
if(down && left){
if (isFloor(map, i, j + 1) && isFloor(map, i + 1, j - 1))
return (Point) { 1 * offset, 2 * offset };
else if(isFloor(map, i+1, j-1))
return (Point){ 2 * offset, 5 * offset };
else
return (Point){ 4 * offset, 4 * offset};
}
if(up && right){
if(isFloor(map, i+1, j))
return (Point){ 2 * offset, 6 * offset};
else
return (Point){ 5 * offset, 6 * offset};
}
if(up && left){
if(isFloor(map, i+1, j))
return (Point){ 4 * offset, 6 * offset};
else
return (Point){ 6 * offset, 6 * offset};
}
if(left || right){
if(isFloor(map, i+1, j))
return (Point){ 7 * offset, 5 * offset};
else
return (Point){ 7 * offset, 7 * offset};
}
if(down){
return (Point){ 1 * offset, 2 * offset};
}
if(up){
return (Point){ 1 * offset, 4 * offset};
}
return (Point){ 0, 0 };
}
static Point get_hole_offset_assets(Map * map, int i, int j){
bool up = isNothing(map, i-1, j);
bool U_Floor = isFloor(map, i-1, j);
if(up){
return (Point){ 0, 0 };
}
if(U_Floor){
bool UL = isFloor(map, i-1, j-1);
bool UR = isFloor(map, i-1, j+1);
if(UL && UR) return (Point){ offset * 13, offset * 11 };
if(UL) return (Point){ offset * 14, offset * 11 };
if(UR) return (Point){ offset * 12, offset * 11 };
return (Point){ offset * 10, offset * 12 };
}
return (Point){ 0, 0 };
}
static Point get_nothing_offset_assets(Map * map, int i, int j){
bool U_Floor = isFloor(map, i-1, j);
if(U_Floor){
bool UL = isFloor(map, i-1, j-1);
bool UR = isFloor(map, i-1, j+1);
if(UL && UR) return (Point){ offset * 13, offset * 11 };
if(UL) return (Point){ offset * 14, offset * 11 };
if(UR) return (Point){ offset * 12, offset * 11 };
return (Point){ offset * 10, offset * 12 };
}
return (Point){ 0, 0 };
}
static void get_map_offset(Map * map){
// Calculate once to use it later on when draw() function called
for(int i = 0; i < map->row; i++){
for(int j = 0; j < map->col; j++){
switch(map->map[i][j]){
case WALL:
map->offset_assets[i][j] = get_wall_offset_assets(map, i, j);
break;
case FLOOR:
case COIN:
map->offset_assets[i][j] = get_floor_offset_assets(map, i, j);
break;
case HOLE:
map->offset_assets[i][j] = get_hole_offset_assets(map, i, j);
break;
case NOTHING:
default:
map->offset_assets[i][j] = (Point){ 0, 0 };
break;
}
}
}
}
static bool isWall(Map * map, int i, int j){
if(i < 0 || j < 0 || i >= map->row || j >= map->col) return false;
if(map->map[i][j] == WALL) return true;
return false;
}
static bool isFloor(Map * map, int i, int j){
if(i < 0 || j < 0 || i >= map->row || j >= map->col) return false;
if(map->map[i][j] == FLOOR) return true;
return false;
}
static bool isNothing(Map * map, int i, int j){
if(i < 0 || j < 0 || i >= map->row || j >= map->col) return true;
if(map->map[i][j] == NOTHING || map->map[i][j] == HOLE) return true;
return false;
}
/*
Collision
*/
static bool tile_collision(Point player, Point tile_coord){
if (player.x < tile_coord.x + TILE_SIZE &&
player.x + TILE_SIZE > tile_coord.x &&
player.y < tile_coord.y + TILE_SIZE &&
player.y + TILE_SIZE > tile_coord.y) {
return true; // Rectangles collide
} else {
return false; // Rectangles do not collide
}
}