-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
155 lines (135 loc) · 3.55 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
#include "game.h"
#include "entity.h"
#include "map.h"
#include "constants.h" // remove when implement messages
#include <curses.h>
#include <stdlib.h>
#include <string.h>
/* - STATIC GLOBAL VARIABLES - */
static struct global_game_state {
entity *player;
tile_map *map;
int input;
int step_count;
char message[TERMINAL_COLUMNS * RESERVED_ROWS];
} game_state = { NULL, NULL, '\0', 0, {'\0'} };
/* - HELPER FUNCTION PROTOTYPES - */
// - init_game() helpers
int init_curses(void); // Sets up curses
int init_game_state(void); // Sets up the initial game state
// - game_loop() helpers
int get_input(void); // Get player input
int update_game_state(void); // Update the game state
int draw_screen(void); // Draw the screen
// - clean_up() helpers
int clean_game_state(void); // Frees all game state memory.
// - helper helpers
void print_game_message(void); // Temporary error output function
int set_game_message(const char *); // Sets game_state.message
/* - FUNCTION DEFINITIONS - */
int init_game(void) {
if (!init_curses() ||
!init_game_state() || // make_player and make_map happen here
!generate_map_rooms(game_state.map)) {
return 0;
}
draw_screen();
return 1;
}
int game_loop(void) {
while (game_state.input != 'Q') {
get_input();
if (!update_game_state()) return 0;
draw_screen();
}
return 1;
}
int clean_up(void) {
clean_game_state();
endwin();
return 1;
}
/* - HELPER FUNCTION DEFINITIONS - */
// - init_game() helpers
int init_curses(void) {
initscr();
resize_term(TERMINAL_ROWS, TERMINAL_COLUMNS);
noecho();
curs_set(0);
raw();
return 1;
}
int init_game_state(void) {
game_state.player = make_entity(30, 15, '@');
game_state.map = make_tile_map();
set_game_message("Use WASD to move around.");
if (!game_state.player || !game_state.map) {
clean_game_state();
return 0;
}
return 1;
}
// - game_loop() helpers
int get_input(void) {
napms(50);
flushinp();
game_state.input = getch();
return 1;
}
int update_game_state(void) {
int dx = 0;
int dy = 0;
set_game_message(++game_state.step_count > 100 ? "Press Q to exit :)" : "");
switch(game_state.input) {
case KEY_LEFT:
case 'a':
dx = -1;
break;
case KEY_RIGHT:
case 'd':
dx = 1;
break;
case KEY_DOWN:
case 's':
dy = 1;
break;
case KEY_UP:
case 'w':
dy = -1;
break;
case 'q':
game_state.input = 'Q';
case 'Q':
return 0;
default:
break;
}
if (map_collision(entity_x_position(game_state.player) + dx,
entity_y_position(game_state.player) + dy,
game_state.map))
set_game_message("I can't go through walls...");
else
move_entity(game_state.player, dx, dy);
return 1;
}
int draw_screen(void) {
clear();
draw_tile_map(game_state.map);
draw_entity(game_state.player);
print_game_message();
refresh();
return 1;
}
// - clean_up() helpers
int clean_game_state(void) {
if (game_state.player) free(game_state.player);
if (game_state.map) free(game_state.map);
return 1;
}
// - helper helpers
void print_game_message(void) {
mvprintw(MESSAGE_Y, MESSAGE_X, game_state.message);
}
int set_game_message(const char *str) {
strncpy(game_state.message, str, RESERVED_ROWS * TERMINAL_COLUMNS);
}