-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.c
179 lines (155 loc) · 5.1 KB
/
controller.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
#include "controller.h"
#include "utils.h"
#include "tron.h"
int Controller_get_height(const Controller* self)
{
return self->game->model->height;
}
int Controller_get_width(const Controller* self)
{
return self->game->model->width;
}
int Controller_get_player_count(const Controller* self)
{
return self->game->model->num_players;
}
Player* Controller_get_player(const Controller* self, const int index)
{
return &self->game->model->players[index];
}
int Controller_get_wall_count(const Controller* self)
{
return self->game->model->num_walls;
}
Wall* Controller_get_wall(const Controller* self, const int index)
{
return &self->game->model->walls[index];
}
GameState Controller_get_state(const Controller* self)
{
return self->game->model->state;
}
void Controller_set_state(const Controller* self, const GameState state)
{
const GameState old_state = self->game->model->state;
if (old_state == state)
{
debug_log("Game already in this state");
return;
}
if (old_state == GAME_STATE_GAME_OVER && state == GAME_STATE_MENU)
{
Model_reset(self->game->model);
}
else if ((old_state == GAME_STATE_MENU || old_state == GAME_STATE_GAME_OVER) && state == GAME_STATE_PLAYING)
{
Model_start(self->game->model);
}
else if (old_state == GAME_STATE_PLAYING && state == GAME_STATE_MENU)
{
Model_cancel(self->game->model);
}
else if (old_state == GAME_STATE_PLAYING && state == GAME_STATE_GAME_OVER)
{
Model_game_over(self->game->model);
}
else debug_log("Invalid state transition");
}
void Controller_play(const Controller* self, const int width, const int height)
{
if (self->game->model->state == GAME_STATE_PLAYING)
{
debug_log("Game already playing");
return;
}
self->game->model->width = width;
self->game->model->height = height;
Controller_set_state(self, GAME_STATE_PLAYING);
debug_logf("New game state: %d", Controller_get_state(self));
}
void Controller_cancel(const Controller* self)
{
if (self->game->model->state == GAME_STATE_MENU)
{
debug_log("Game already stopped");
return;
}
Controller_set_state(self, GAME_STATE_MENU);
debug_logf("New game state: %d", Controller_get_state(self));
}
void Controller_game_over(const Controller* self)
{
if (self->game->model->state == GAME_STATE_GAME_OVER)
{
debug_log("Game already over");
return;
}
Controller_set_state(self, GAME_STATE_GAME_OVER);
debug_logf("New game state: %d", Controller_get_state(self));
}
void Controller_new_player(Controller* self)
{
Model_add_player(self->game->model, 0, 0, DIRECTION_UP);
}
void Controller_remove_player(Controller* self, const int index)
{
Model_remove_player(self->game->model, index);
}
void Controller_update(Controller* self)
{
if (self->game->model->state != GAME_STATE_PLAYING) return;
for (int i = 0; i < self->game->model->num_players; i++)
{
Player* player = Controller_get_player(self, i);
if (player->state != PLAYER_STATE_ALIVE) continue;
Model_move_player(self->game->model, i, 1);
}
for (int i = 0; i < self->game->model->num_players; i++)
{
Player* player = Controller_get_player(self, i);
const PlayerState state = player->state;
if (state == PLAYER_STATE_DEAD) continue;
Model_calculate_player_state(self->game->model, i);
}
Wall wall;
int distance;
for (int i = 0; i < self->game->model->num_players; i++)
{
Player* player = Controller_get_player(self, i);
if (player->state == PLAYER_STATE_TO_DEATH)
{
player->state = PLAYER_STATE_DEAD;
Direction opposite = Model_get_opposite_direction(player->direction);
Direction current = player->direction;
int dx, dy;
Model_get_relative_direction(opposite, &dx, &dy);
if (Model_try_hit_raycast(self->game->model, player->x + dx, player->y + dy, opposite, &wall, &distance))
Model_add_wall(self->game->model, player->x, player->y, current, distance, i);
}
else if (player->state == PLAYER_STATE_ALIVE)
player->score++;
}
int sum_player_alive = 0;
for (int i = 0; i < self->game->model->num_players; i++)
{
Player* player = Controller_get_player(self, i);
if (player->state == PLAYER_STATE_ALIVE)
sum_player_alive++;
}
if (sum_player_alive <= 1)
Controller_set_state(self, GAME_STATE_GAME_OVER);
}
void Controller_get_player_wall(const Controller* self, const int index, Wall* output, int* distance)
{
Model_get_player_wall(self->game->model, index, output, distance);
}
bool Controller_out_of_bounds(const Controller* self, const int x, const int y)
{
return Model_out_of_bounds(self->game->model, x, y);
}
void Controller_move_player(Controller* self, const int index, const Direction direction)
{
Player* player = Controller_get_player(self, index);
if (player->state != PLAYER_STATE_ALIVE) return;
Model_change_direction(self->game->model, index, direction);
}