root / proj / src / proj_func.c @ 340
History | View | Annotate | Download (13.9 KB)
1 |
#include <lcom/lcf.h> |
---|---|
2 |
|
3 |
#include "proj_func.h" |
4 |
|
5 |
#include "interrupts_func.h" |
6 |
#include "graph.h" |
7 |
#include "keyboard.h" |
8 |
#include "errors.h" |
9 |
#include "proj_macros.h" |
10 |
#include "utils.h" |
11 |
#include "ent.h" |
12 |
#include "fast_math.h" |
13 |
#include "rectangle.h" |
14 |
#include "font.h" |
15 |
|
16 |
#include <math.h> |
17 |
|
18 |
int cleanup(void) { |
19 |
int r = SUCCESS;
|
20 |
if ((r = unsubscribe_all()))
|
21 |
printf("%s: failed to unsubscribe drivers.\n", __func__);
|
22 |
if ((r = graph_cleanup()))
|
23 |
printf("%s: graph cleanup failed\n", __func__);
|
24 |
|
25 |
return r;
|
26 |
} |
27 |
|
28 |
static keys_t key_presses;
|
29 |
|
30 |
host_info_t* host_info_ctor(gunner_t *host, gunner_t *remote) { |
31 |
host_info_t *ret = (host_info_t*)malloc(sizeof(host_info_t));
|
32 |
if (ret == NULL) return ret; |
33 |
|
34 |
ret->host_x = (int16_t)gunner_get_x (host); |
35 |
ret->host_y = (int16_t)gunner_get_y (host); |
36 |
ret->host_angle = (int16_t)gunner_get_angle (host); |
37 |
ret->host_health = (int16_t)gunner_get_health (host); |
38 |
ret->host_current_health = (int16_t)gunner_get_curr_health(host); |
39 |
|
40 |
// remote
|
41 |
ret->remote_x = (int16_t)gunner_get_x (remote); |
42 |
ret->remote_y = (int16_t)gunner_get_y (remote); |
43 |
ret->remote_angle = (int16_t)gunner_get_angle (remote); |
44 |
ret->remote_health = (int16_t)gunner_get_health (remote); |
45 |
ret->remote_current_health = (int16_t)gunner_get_curr_health(remote); |
46 |
|
47 |
ret->no_bullets = 0;
|
48 |
|
49 |
return ret;
|
50 |
} |
51 |
|
52 |
void host_info_dtor(host_info_t *p) {
|
53 |
if (p==NULL) return; |
54 |
|
55 |
if ((p->bullets_x) != NULL){ free(p->bullets_x); p->bullets_x = NULL; } |
56 |
|
57 |
if ((p->bullets_y) != NULL){ free(p->bullets_y); p->bullets_y = NULL; } |
58 |
|
59 |
if ((p->bullets_vx) != NULL){ free(p->bullets_vx); p->bullets_vx = NULL; } |
60 |
|
61 |
if ((p->bullets_vy) != NULL){ free(p->bullets_vy); p->bullets_vy = NULL; } |
62 |
|
63 |
if ((p->bullets_shooter) != NULL){ free(p->bullets_shooter); p->bullets_shooter = NULL; } |
64 |
|
65 |
free(p); |
66 |
} |
67 |
|
68 |
remote_info_t* remote_info_ctor(void) {
|
69 |
remote_info_t *ret = (remote_info_t*)malloc(sizeof(remote_info_t));
|
70 |
if (ret == NULL) return ret; |
71 |
|
72 |
memset(&(ret->remote_keys_pressed), 0, sizeof(keys_t)); |
73 |
|
74 |
ret->remote_angle = 0;
|
75 |
|
76 |
return ret;
|
77 |
} |
78 |
|
79 |
void remote_info_dtor(remote_info_t *p) {
|
80 |
if (p==NULL) return; |
81 |
free(p); |
82 |
} |
83 |
|
84 |
bullet_info_t* bullet_info_ctor(void) {
|
85 |
bullet_info_t *ret = (bullet_info_t*)malloc(sizeof(bullet_info_t));
|
86 |
if (ret == NULL) return ret; |
87 |
|
88 |
ret->new_bullet = false;
|
89 |
|
90 |
return ret;
|
91 |
} |
92 |
|
93 |
void bullet_info_dtor(bullet_info_t *p) {
|
94 |
if (p == NULL) return; |
95 |
free(p); |
96 |
} |
97 |
|
98 |
void update_key_presses(void) { |
99 |
if (keyboard_get_size() == 1) { |
100 |
switch(keyboard_get_scancode()[0]) { |
101 |
case W_MAKE_CODE : key_presses.w_pressed = 1; break; |
102 |
case W_BREAK_CODE : key_presses.w_pressed = 0; break; |
103 |
case A_MAKE_CODE : key_presses.a_pressed = 1; break; |
104 |
case A_BREAK_CODE : key_presses.a_pressed = 0; break; |
105 |
case S_MAKE_CODE : key_presses.s_pressed = 1; break; |
106 |
case S_BREAK_CODE : key_presses.s_pressed = 0; break; |
107 |
case D_MAKE_CODE : key_presses.d_pressed = 1; break; |
108 |
case D_BREAK_CODE : key_presses.d_pressed = 0; break; |
109 |
case CTRL_MAKE_CODE : key_presses.ctrl_pressed = 1; break; |
110 |
case CTRL_BREAK_CODE : key_presses.ctrl_pressed = 0; break; |
111 |
case PLUS_MAKE_CODE : key_presses.plus_pressed = 1; update_scale(); break; |
112 |
case PLUS_BREAK_CODE : key_presses.plus_pressed = 0; update_scale(); break; |
113 |
case MINUS_MAKE_CODE : key_presses.minus_pressed = 1; update_scale(); break; |
114 |
case MINUS_BREAK_CODE : key_presses.minus_pressed = 0; update_scale(); break; |
115 |
} |
116 |
} |
117 |
} |
118 |
|
119 |
void update_movement(map_t *map, gunner_t *p, keys_t *keys, list_t *shooter_list) {
|
120 |
/** Player movement */{
|
121 |
int ver_mov = keys->s_pressed - keys->w_pressed;
|
122 |
int hor_mov = keys->d_pressed - keys->a_pressed;
|
123 |
double x = gunner_get_x(p);
|
124 |
double y = gunner_get_y(p);
|
125 |
|
126 |
gunner_set_pos(p, x + SHOOTER_SPEED * hor_mov, y); |
127 |
if (map_collides_gunner(map, p)) gunner_set_pos(p, x, y);
|
128 |
else {
|
129 |
list_node_t *it = list_begin(shooter_list); |
130 |
while (it != list_end(shooter_list)) {
|
131 |
gunner_t *p2 = *(gunner_t**)list_node_val(it); |
132 |
if (p != p2 && gunner_collides_gunner(p, p2)) {
|
133 |
gunner_set_pos(p, x, y); |
134 |
break;
|
135 |
} |
136 |
it = list_node_next(it); |
137 |
} |
138 |
} |
139 |
x = gunner_get_x(p); |
140 |
gunner_set_pos(p, x, y + SHOOTER_SPEED * ver_mov); |
141 |
if (map_collides_gunner(map, p)) gunner_set_pos(p, x, y);
|
142 |
else {
|
143 |
list_node_t *it = list_begin(shooter_list); |
144 |
while (it != list_end(shooter_list)) {
|
145 |
gunner_t *p2 = *(gunner_t**)list_node_val(it); |
146 |
if (p != p2 && gunner_collides_gunner(p, p2)) {
|
147 |
gunner_set_pos(p, x, y); |
148 |
break;
|
149 |
} |
150 |
it = list_node_next(it); |
151 |
} |
152 |
} |
153 |
} |
154 |
/** Followers movement */ {
|
155 |
list_node_t *it = list_begin(shooter_list); |
156 |
while(it != list_end(shooter_list)){
|
157 |
gunner_t *g = *(gunner_t**)list_node_val(it); |
158 |
if(gunner_get_type(g) & GUNNER_FOLLOW){
|
159 |
double theta = 0.0; |
160 |
map_where_to_follow(map, gunner_get_x(g), gunner_get_y(g), &theta); |
161 |
double c = fm_cos(theta), s = fm_sin(theta);
|
162 |
double dx = ZOMBIE_SPEED*c, dy = -ZOMBIE_SPEED*s;
|
163 |
double x = gunner_get_x(g);
|
164 |
double y = gunner_get_y(g);
|
165 |
gunner_set_pos(g, x+dx, y+dy); |
166 |
if (map_collides_gunner(map, g)){
|
167 |
gunner_set_pos(g, x, y); |
168 |
} else {
|
169 |
list_node_t *it2 = list_begin(shooter_list); |
170 |
while (it2 != list_end(shooter_list)) {
|
171 |
gunner_t *p2 = *(gunner_t**)list_node_val(it2); |
172 |
if (g != p2 && gunner_collides_gunner(g, p2)) {
|
173 |
gunner_set_pos(g, x, y); |
174 |
break;
|
175 |
} |
176 |
it2 = list_node_next(it2); |
177 |
} |
178 |
} |
179 |
gunner_set_angle(g, theta-M_PI_2); |
180 |
} |
181 |
it = list_node_next(it); |
182 |
} |
183 |
} |
184 |
} |
185 |
|
186 |
void (shoot_bullet)(const gunner_t *shooter, list_t *bullet_list, const basic_sprite_t *bsp_bullet) { |
187 |
double angle = gunner_get_angle(shooter);
|
188 |
double vx = -BULLET_SPEED * fm_sin(angle);
|
189 |
double vy = -BULLET_SPEED * fm_cos(angle);
|
190 |
bullet_t *bullet = bullet_ctor(shooter, bsp_bullet, gunner_get_x(shooter), gunner_get_y(shooter), vx, vy); |
191 |
list_insert(bullet_list, list_end(bullet_list), bullet); |
192 |
} |
193 |
|
194 |
void (update_game_state)(const map_t *map, list_t *shooter_list, list_t *bullet_list) { |
195 |
/// BULLETS
|
196 |
bullet_update_movement_list(bullet_list); |
197 |
list_node_t *bullet_it = list_begin(bullet_list); |
198 |
while (bullet_it != list_end(bullet_list)) {
|
199 |
/// Collision with walls
|
200 |
bullet_t *bullet = *(bullet_t**)list_node_val(bullet_it); |
201 |
if (map_collides_bullet(map, bullet)) {
|
202 |
list_node_t *aux = list_node_next(bullet_it); |
203 |
/// Delete bullet
|
204 |
bullet_dtor(list_erase(bullet_list, bullet_it)); |
205 |
/// Advance iterator
|
206 |
bullet_it = aux; |
207 |
continue;
|
208 |
} |
209 |
/// Collision with shooters
|
210 |
list_node_t *shooter_it = list_begin(shooter_list); |
211 |
int deleted_bullet = false; |
212 |
while(shooter_it != list_end(shooter_list)){
|
213 |
gunner_t *shooter = *(gunner_t**)list_node_val(shooter_it); |
214 |
if(gunner_collides_bullet(shooter, bullet) &&
|
215 |
gunner_get_team(shooter) != gunner_get_team(bullet_get_shooter(bullet))) { |
216 |
list_node_t *aux = list_node_next(bullet_it); |
217 |
/// Change health
|
218 |
gunner_set_curr_health(shooter, gunner_get_curr_health(shooter) - bullet_get_damage(bullet)); |
219 |
if (gunner_get_curr_health(shooter) <= 0) { |
220 |
gunner_dtor(list_erase(shooter_list, shooter_it)); |
221 |
} |
222 |
/// Delete bullet
|
223 |
bullet_dtor(list_erase(bullet_list, bullet_it)); deleted_bullet = true;
|
224 |
/// Advance iterator
|
225 |
bullet_it = aux; |
226 |
break;
|
227 |
} else shooter_it = list_node_next(shooter_it);
|
228 |
} |
229 |
if(deleted_bullet) continue; |
230 |
/// Advance iterator if necessary
|
231 |
bullet_it = list_node_next(bullet_it); |
232 |
} |
233 |
/// MELEE
|
234 |
list_node_t *it1 = list_begin(shooter_list); |
235 |
while(it1 != list_end(shooter_list)){
|
236 |
gunner_t *s1 = *list_node_val(it1); |
237 |
if(!(gunner_get_type(s1) & GUNNER_MELEE)){ it1 = list_node_next(it1); continue; } |
238 |
list_node_t *it2 = list_begin(shooter_list); |
239 |
while(it2 != list_end(shooter_list)){
|
240 |
gunner_t *s2 = *list_node_val(it2); |
241 |
if(gunner_get_team(s1) != gunner_get_team(s2) &&
|
242 |
gunner_distance(s1, s2) < MELEE_RANGE) |
243 |
gunner_set_curr_health(s2, gunner_get_curr_health(s2) - MELEE_DAMAGE); |
244 |
if(gunner_get_curr_health(s2) <= 0){ |
245 |
list_node_t *aux = list_node_next(it2); |
246 |
gunner_dtor(list_erase(shooter_list, it2)); |
247 |
it2 = aux; |
248 |
}else it2 = list_node_next(it2);
|
249 |
} |
250 |
it1 = list_node_next(it1); |
251 |
} |
252 |
} |
253 |
|
254 |
void update_scale(void) { |
255 |
static uint8_t last_plus = 0, last_minus = 0; |
256 |
if (key_presses.ctrl_pressed) {
|
257 |
if (key_presses.plus_pressed && !last_plus) {
|
258 |
double scale = ent_get_scale();
|
259 |
scale *= 1.1; |
260 |
if (scale <= MAX_SCALE) ent_set_scale(scale);
|
261 |
} |
262 |
else if (key_presses.minus_pressed && !last_minus) { |
263 |
double scale = ent_get_scale();
|
264 |
scale /= 1.1; |
265 |
if (scale >= MIN_SCALE) ent_set_scale(scale);
|
266 |
} |
267 |
|
268 |
//printf("SCALE: %d\n", (int)(ent_get_scale()*1000));
|
269 |
} |
270 |
|
271 |
last_plus = key_presses.plus_pressed; |
272 |
last_minus = key_presses.minus_pressed; |
273 |
} |
274 |
|
275 |
static int16_t mouse_x = 0, mouse_y = 0; |
276 |
|
277 |
void (update_mouse)(struct packet *p) { |
278 |
mouse_x = max_16(0, mouse_x + p->delta_x);
|
279 |
mouse_x = min_16(mouse_x, (int16_t)graph_get_XRes() - 1);
|
280 |
|
281 |
mouse_y = max_16(0, mouse_y - p->delta_y);
|
282 |
mouse_y = min_16(mouse_y, (int16_t)graph_get_YRes() - 1);
|
283 |
|
284 |
key_presses.lb_pressed = p->lb; |
285 |
} |
286 |
|
287 |
keys_t* (get_key_presses)(void) {
|
288 |
return &key_presses;
|
289 |
} |
290 |
|
291 |
int16_t* get_mouse_X(void) { return &mouse_x; } |
292 |
|
293 |
int16_t* get_mouse_Y(void) { return &mouse_y; } |
294 |
|
295 |
double get_mouse_angle(gunner_t *p) {
|
296 |
return atan2(gunner_get_y_screen(p) - mouse_y, mouse_x - gunner_get_x_screen(p));
|
297 |
} |
298 |
|
299 |
void build_host_structure(host_info_t *p, gunner_t *host, gunner_t *remote, list_t *bullet_list) {
|
300 |
// host
|
301 |
p->host_x = (int16_t)gunner_get_x (host); |
302 |
p->host_y = (int16_t)gunner_get_y (host); |
303 |
p->host_angle = (int16_t)gunner_get_angle (host); |
304 |
p->host_health = (int16_t)gunner_get_health (host); |
305 |
p->host_current_health = (int16_t)gunner_get_curr_health(host); |
306 |
|
307 |
// remote
|
308 |
p->remote_x = (int16_t)gunner_get_x (remote); |
309 |
p->remote_y = (int16_t)gunner_get_y (remote); |
310 |
p->remote_angle = (int16_t)gunner_get_angle (remote); |
311 |
p->remote_health = (int16_t)gunner_get_health (remote); |
312 |
p->remote_current_health = (int16_t)gunner_get_curr_health(remote); |
313 |
|
314 |
// bullets
|
315 |
size_t sz = list_size(bullet_list); |
316 |
p->no_bullets = sz; |
317 |
p->bullets_x = (int16_t*)realloc(p->bullets_x , sz * sizeof(int16_t));
|
318 |
p->bullets_y = (int16_t*)realloc(p->bullets_y , sz * sizeof(int16_t));
|
319 |
p->bullets_vx = (int16_t*)realloc(p->bullets_vx , sz * sizeof(int16_t));
|
320 |
p->bullets_vy = (int16_t*)realloc(p->bullets_vy , sz * sizeof(int16_t));
|
321 |
p->bullets_shooter = (bool*) realloc(p->bullets_shooter , sz * sizeof(bool )); |
322 |
|
323 |
list_node_t *it = list_begin(bullet_list); |
324 |
size_t i = 0;
|
325 |
while (it != list_end(bullet_list)) {
|
326 |
bullet_t *bullet = *list_node_val(it); |
327 |
p->bullets_x [i] = (int16_t)bullet_get_x (bullet); |
328 |
p->bullets_y [i] = (int16_t)bullet_get_y (bullet); |
329 |
p->bullets_vx [i] = (int16_t)bullet_get_vx (bullet); |
330 |
p->bullets_vy [i] = (int16_t)bullet_get_vy (bullet); |
331 |
p->bullets_shooter [i] = gunner_get_team(bullet_get_shooter(bullet)) != gunner_get_team(host); |
332 |
it = list_node_next(it); |
333 |
i++; |
334 |
} |
335 |
} |
336 |
|
337 |
void build_remote_structure(remote_info_t *p, keys_t *keys, double angle) { |
338 |
memcpy(&(p->remote_keys_pressed), keys, sizeof(keys_t));
|
339 |
p->remote_angle = angle; |
340 |
} |
341 |
|
342 |
text_timer_t* (timer_ctor)(const font_t *fnt){
|
343 |
if(fnt == NULL) return NULL; |
344 |
text_timer_t *ret = malloc(sizeof(timer_t));
|
345 |
if (ret == NULL) return NULL; |
346 |
ret->time = 0;
|
347 |
ret->text = text_ctor(fnt, "000s");
|
348 |
text_set_color(ret->text, TEXT_COLOR); |
349 |
return ret;
|
350 |
} |
351 |
|
352 |
void (timer_update)(text_timer_t *p){
|
353 |
if (p->time >= 999) return; |
354 |
p->time++; |
355 |
char buffer[100]; |
356 |
sprintf(buffer, "%03ds", p->time);
|
357 |
text_set_string(p->text, buffer); |
358 |
} |
359 |
|
360 |
void (timer_reset)(text_timer_t *p){
|
361 |
text_set_string(p->text, "000s");
|
362 |
} |
363 |
|
364 |
void (timer_dtor)(text_timer_t *p){
|
365 |
if (p == NULL) return; |
366 |
text_dtor(p->text); |
367 |
free(p); |
368 |
} |