/
tilemap.c
100 lines (82 loc) · 2.46 KB
/
tilemap.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
#include <stdio.h>
#include <ncurses.h>
#include "tilemap.h"
#include "colorpairs.h"
struct tile create_tile(char *name, char *description, int x, int y,
walkable_t walkable, char character, int color_pair) {
struct tile tile;
tile.name = name;
tile.description = description;
tile.x = x;
tile.y = y;
tile.character = character;
tile.walkable = walkable;
tile.color_pair = color_pair;
return tile;
}
struct tile create_water_tile(int x, int y) {
return create_tile("Water", "It's water. Swim swim.", x, y, UNWALKABLE, '~',
WHITE_ON_BLUE);
}
struct tile create_bridge_tile(int x, int y) {
return create_tile("Bridge",
"It's a bridge. You use it to walk over things.", x, y,
WALKABLE, '=', BLACK_ON_YELLOW);
}
struct tile create_grass_tile(int x, int y) {
return create_tile("Grass", "Little blades of plant.", x, y, WALKABLE, '.',
WHITE_ON_GREEN);
}
struct tile create_wall_tile(int x, int y) {
return create_tile("Wall",
"Walls. Always keeping you out of places. Fuck 'em.", x,
y, UNWALKABLE, '#', WHITE_ON_WHITE);
}
struct tile create_soul_tree_tile(int x, int y) {
return create_tile("Soul Tree", "The Soul Tree is fucking huge!", x, y,
UNWALKABLE, '#', GREEN_ON_YELLOW);
}
struct tilemap create_tilemap(char *map_location) {
FILE *map_file;
char char_buffer;
map_file = fopen(map_location, "r");
struct tilemap tilemap;
int x=0, y=0;
char_buffer = getc(map_file);
while (char_buffer != EOF) {
if (char_buffer == '.') {
tilemap.matrix[y][x] = create_grass_tile(x, y);
} else if (char_buffer == '~') {
tilemap.matrix[y][x] = create_water_tile(x, y);
} else if (char_buffer == '=') {
tilemap.matrix[y][x] = create_bridge_tile(x, y);
} else if (char_buffer == '#') {
tilemap.matrix[y][x] = create_wall_tile(x, y);
} else if (char_buffer == 'T') {
tilemap.matrix[y][x] = create_soul_tree_tile(x, y);
}
x++;
char_buffer = getc(map_file);
if (x == 50) {
char_buffer = getc(map_file);
y++;
x = 0;
}
}
fclose(map_file);
return tilemap;
}
struct tile get_tile(struct tilemap tilemap, int x, int y) {
return tilemap.matrix[y][x];
}
void print_map(struct tilemap tilemap) {
char map_string[751];
int x, y;
for (y = 0; y < 15; y++) {
for (x = 0; x < 50; x++) {
map_string[(y*50)+x] = tilemap.matrix[y][x].character;
}
}
map_string[750] = 0;
printf("%s", map_string);
}