/
game.c
231 lines (192 loc) · 5.01 KB
/
game.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
#include "psionrl.h"
TCOD_console_t *map_layer = NULL;
TCOD_console_t *psion_layer = NULL;
/* Attempts to move, processes a turn */
void attempt_move(map_t* m, int newx, int newy) {
if (is_walkable(m, newx, newy)) {
move_player(newx, newy);
}
}
/* The main game loop */
void play(void) {
int playing = 1;
int x, y, vpx, vpy;
/* A messy way to do the map, works for now */
map_t *map = map_new(50, 50);
generate_cave(map);
map_fov_build(map);
/* Set up the offscreen consoles */
map_layer = TCOD_console_new(ui_viewport_width, ui_viewport_height);
psion_layer = TCOD_console_new(ui_viewport_width, ui_viewport_height);
TCOD_console_set_key_color(psion_layer, C_KEY);
bgcolor(psion_layer, C_KEY);
/* Set up the player */
init_player();
rename_player("Gu the Cabeboy");
blink_player(map);
/* Initialize the inventory */
/* Enter the main game loop! */
while (playing) {
/* Prepare the screen for drawing */
clear(NULL);
/* Calculate the position of the viewport */
vpx = player->x - ui_viewport_width/2 > 0 ?
player->x - ui_viewport_width/2 : 0;
vpy = player->y - ui_viewport_height/2 > 0 ?
player->y - ui_viewport_height/2 : 0;
if ((vpx + ui_viewport_width) > map->width) {
vpx = map->width - ui_viewport_width;
}
if ((vpy + ui_viewport_height) > map->height) {
vpy = map->height - ui_viewport_height;
}
if (ui_viewport_width > map->width) {
vpx = 0;
}
if (ui_viewport_height > map->height) {
vpy = 0;
}
/* Calculate the field of view */
map_fov_do(map, player->x, player->y);
/* Only display the part of the map inside the viewport */
for (y = 0; y < ui_viewport_height; y++) {
for (x = 0; x < ui_viewport_width; x++) {
if (((vpy+y) < map->height) && ((vpx+x) < map->width)) {
tile_t *tile = tile_at(map, vpx+x, vpy+y);
if (TCOD_map_is_in_fov(map->fov, vpx+x, vpy+y)) {
fgcolor(map_layer, tile->fg_lit);
}
else {
fgcolor(map_layer, tile->fg_dark);
}
putch(map_layer, x, y, tile->glyph);
}
}
}
TCOD_console_blit(map_layer, 0, 0, ui_viewport_width,
ui_viewport_height, NULL, ui_viewport_x,
ui_viewport_y, 255);
/* Draw the player and status */
fgcolor(NULL, C_WHITE);
putch(NULL, ui_viewport_x+player->x-vpx, ui_viewport_y+player->y-vpy, '@');
fgcolor(NULL, C_MSG);
putstr(NULL, 1, 23, player->name);
/* Redraw the screen */
update();
/* Handle keypress */
TCOD_key_t k = getkey();
if (k.c == 0) {
switch (k.vk) {
/* Move around */
case TCODK_KP7: /* up left */
attempt_move(map, player->x-1, player->y-1);
break;
case TCODK_KP8: /* up */
case TCODK_UP:
attempt_move(map, player->x, player->y-1);
break;
case TCODK_KP9: /* up right */
attempt_move(map, player->x+1, player->y-1);
break;
case TCODK_KP1: /* down left */
attempt_move(map, player->x-1, player->y+1);
break;
case TCODK_KP2: /* down */
case TCODK_DOWN:
attempt_move(map, player->x, player->y+1);
break;
case TCODK_KP3: /* down right */
attempt_move(map, player->x+1, player->y+1);
break;
case TCODK_KP4: /* left */
case TCODK_LEFT:
attempt_move(map, player->x-1, player->y);
break;
case TCODK_KP6: /* right */
case TCODK_RIGHT:
attempt_move(map, player->x+1, player->y);
break;
/* View character summary */
case TCODK_TAB:
character();
break;
default:
printf("%d %d\n", TCODK_TAB, k.vk);
break;
}
}
else {
switch (k.c) {
/* Quit the game */
case 'Q':
playing = quit();
break;
/* Open up the inventory */
case 'i':
inventory();
break;
/* View character summary */
case 'c':
character();
break;
/* Use scan ability */
case 's':
psion_scan(map);
break;
default:
break;
}
}
}
/* Clean up, the game is over */
map_destroy(map);
}
bool quit(void) {
fgcolor(NULL, C_MSG);
putstr(NULL, 1, 1, "Really quit? [yn]");
update();
return (choice("yn") == 'n');
}
void inventory(void) {
clear(NULL);
fgcolor(NULL, C_WHITE);
putstr(NULL, 1, 1, "Inventory");
/*
item_t *item = inv->head;
if (item == NULL) {
fgcolor(NULL, C_GREY);
putstr(NULL, 1, 3, "Empty");
}
else {
int y = 3;
while (item != NULL) {
putstr(NULL, 1, y++, item->name);
item = item->next;
}
}
*/
fgcolor(NULL, C_GREY);
putstr(NULL, 1, 23, "[? for help]");
update();
getkey();
}
void character(void) {
int line = 3;
clear(NULL);
/* Show statistics */
fgcolor(NULL, C_WHITE);
putstrf(NULL, 1, 1, "%s", player->name);
fgcolor(NULL, C_LIGHT_GREY);
putstrf(NULL, 1, line++, "Level: %d", 1);
putstrf(NULL, 1, line++, "Energy: %d/%d", 5, 5);
putstrf(NULL, 1, line++, "Focus: %d", 3);
/* Show intrinsics */
line++;
fgcolor(NULL, C_WHITE);
putstrf(NULL, 1, line++, "Intrinsics");
fgcolor(NULL, C_LIGHT_GREY);
line++;
putstrf(NULL, 1, line++, "Cold resistant");
update();
getkey();
}