root / proj / src / proj_func.c @ 319
History | View | Annotate | Download (11 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 |
void host_info_dtor(host_info_t *p) {
|
31 |
if (p==NULL) return; |
32 |
|
33 |
if ((p->bullets_x) != NULL){ free(p->bullets_x); p->bullets_x = NULL; } |
34 |
|
35 |
if ((p->bullets_y) != NULL){ free(p->bullets_y); p->bullets_y = NULL; } |
36 |
|
37 |
if ((p->bullets_vx) != NULL){ free(p->bullets_vx); p->bullets_vx = NULL; } |
38 |
|
39 |
if ((p->bullets_vy) != NULL){ free(p->bullets_vy); p->bullets_vy = NULL; } |
40 |
|
41 |
if ((p->bullets_shooter) != NULL){ free(p->bullets_shooter); p->bullets_shooter = NULL; } |
42 |
|
43 |
free(p); |
44 |
} |
45 |
|
46 |
void remote_info_dtor(remote_info_t *p) {
|
47 |
if (p==NULL) return; |
48 |
|
49 |
if ((p->bullets_x) != NULL){ free(p->bullets_x); p->bullets_x = NULL; } |
50 |
|
51 |
if ((p->bullets_y) != NULL){ free(p->bullets_y); p->bullets_y = NULL; } |
52 |
|
53 |
if ((p->bullets_angle) != NULL){ free(p->bullets_angle); p->bullets_angle = NULL; } |
54 |
|
55 |
free(p); |
56 |
} |
57 |
|
58 |
void update_key_presses(void) { |
59 |
if (sz == 1) { |
60 |
switch(scancode[0]) { |
61 |
case W_MAKE_CODE : key_presses.w_pressed = 1; break; |
62 |
case W_BREAK_CODE : key_presses.w_pressed = 0; break; |
63 |
case A_MAKE_CODE : key_presses.a_pressed = 1; break; |
64 |
case A_BREAK_CODE : key_presses.a_pressed = 0; break; |
65 |
case S_MAKE_CODE : key_presses.s_pressed = 1; break; |
66 |
case S_BREAK_CODE : key_presses.s_pressed = 0; break; |
67 |
case D_MAKE_CODE : key_presses.d_pressed = 1; break; |
68 |
case D_BREAK_CODE : key_presses.d_pressed = 0; break; |
69 |
case CTRL_MAKE_CODE : key_presses.ctrl_pressed = 1; break; |
70 |
case CTRL_BREAK_CODE : key_presses.ctrl_pressed = 0; break; |
71 |
case PLUS_MAKE_CODE : key_presses.plus_pressed = 1; update_scale(); break; |
72 |
case PLUS_BREAK_CODE : key_presses.plus_pressed = 0; update_scale(); break; |
73 |
case MINUS_MAKE_CODE : key_presses.minus_pressed = 1; update_scale(); break; |
74 |
case MINUS_BREAK_CODE : key_presses.minus_pressed = 0; update_scale(); break; |
75 |
} |
76 |
} |
77 |
} |
78 |
|
79 |
void update_movement(map_t *map, gunner_t *p, keys_t *keys, list_t *shooter_list) {
|
80 |
int ver_mov = keys->s_pressed - keys->w_pressed;
|
81 |
int hor_mov = keys->d_pressed - keys->a_pressed;
|
82 |
double x = gunner_get_x(p);
|
83 |
double y = gunner_get_y(p);
|
84 |
|
85 |
gunner_set_pos(p, x + SHOOTER_SPEED * hor_mov, y); |
86 |
if (map_collides_gunner(map, p)) gunner_set_pos(p, x, y);
|
87 |
else {
|
88 |
list_node_t *it = list_begin(shooter_list); |
89 |
while (it != list_end(shooter_list)) {
|
90 |
gunner_t *p2 = *(gunner_t**)list_node_val(it); |
91 |
if (p != p2 && gunner_collides_gunner(p, p2)) {
|
92 |
gunner_set_pos(p, x, y); |
93 |
break;
|
94 |
} |
95 |
it = list_node_next(it); |
96 |
} |
97 |
} |
98 |
x = gunner_get_x(p); |
99 |
gunner_set_pos(p, x, y + SHOOTER_SPEED * ver_mov); |
100 |
if (map_collides_gunner(map, p)) gunner_set_pos(p, x, y);
|
101 |
else {
|
102 |
list_node_t *it = list_begin(shooter_list); |
103 |
while (it != list_end(shooter_list)) {
|
104 |
gunner_t *p2 = *(gunner_t**)list_node_val(it); |
105 |
if (p != p2 && gunner_collides_gunner(p, p2)) {
|
106 |
gunner_set_pos(p, x, y); |
107 |
break;
|
108 |
} |
109 |
it = list_node_next(it); |
110 |
} |
111 |
} |
112 |
|
113 |
// Update zombie positions
|
114 |
list_node_t *it = list_begin(shooter_list); |
115 |
while(it != list_end(shooter_list)){
|
116 |
gunner_t *g = *(gunner_t**)list_node_val(it); |
117 |
if(gunner_get_type(g) & gunner_follow){
|
118 |
float theta = 0.0; |
119 |
map_where_to_follow(map, gunner_get_x(g), gunner_get_y(g), &theta); |
120 |
float c = fm_cos(theta), s = fm_sin(theta);
|
121 |
float dx = ZOMBIE_SPEED*c, dy = -ZOMBIE_SPEED*s;
|
122 |
double x = gunner_get_x(g);
|
123 |
double y = gunner_get_y(g);
|
124 |
gunner_set_pos(g, x+dx, y+dy); |
125 |
if (map_collides_gunner(map, g)){
|
126 |
//printf("Zombie colliding with map\n");
|
127 |
gunner_set_pos(g, 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 (g != p2 && gunner_collides_gunner(g, p2)) {
|
133 |
//printf("Zombie colliding with zombie\n");
|
134 |
gunner_set_pos(g, x, y); |
135 |
break;
|
136 |
} |
137 |
it = list_node_next(it); |
138 |
} |
139 |
} |
140 |
gunner_set_angle(g, theta-M_PI_2); /*printf("angle: %d.%d\n", (int)theta, abs((int)(theta*1000)%1000));*/
|
141 |
} |
142 |
it = list_node_next(it); |
143 |
} |
144 |
} |
145 |
|
146 |
void (shoot_bullet)(const gunner_t *shooter, list_t *bullet_list, const basic_sprite_t *bsp_bullet) { |
147 |
double angle = gunner_get_angle(shooter);
|
148 |
double vx = -BULLET_SPEED * fm_sin(angle);
|
149 |
double vy = -BULLET_SPEED * fm_cos(angle);
|
150 |
bullet_t *bullet = bullet_ctor(shooter, bsp_bullet, gunner_get_x(shooter), gunner_get_y(shooter), vx, vy); |
151 |
list_insert(bullet_list, list_end(bullet_list), bullet); |
152 |
} |
153 |
|
154 |
void (update_game_state)(const map_t *map, list_t *shooter_list, list_t *bullet_list) { |
155 |
/// BULLETS
|
156 |
bullet_update_movement_list(bullet_list); |
157 |
list_node_t *bullet_it = list_begin(bullet_list); |
158 |
while (bullet_it != list_end(bullet_list)) {
|
159 |
/// Collision with walls
|
160 |
bullet_t *bullet = *(bullet_t**)list_node_val(bullet_it); |
161 |
if (map_collides_bullet(map, bullet)) {
|
162 |
list_node_t *aux = list_node_next(bullet_it); |
163 |
/// Delete bullet
|
164 |
bullet_dtor(list_erase(bullet_list, bullet_it)); |
165 |
/// Advance iterator
|
166 |
bullet_it = aux; |
167 |
continue;
|
168 |
} |
169 |
/// Collision with shooters
|
170 |
list_node_t *shooter_it = list_begin(shooter_list); |
171 |
int deleted_bullet = false; |
172 |
while(shooter_it != list_end(shooter_list)){
|
173 |
gunner_t *shooter = *(gunner_t**)list_node_val(shooter_it); |
174 |
if(gunner_collides_bullet(shooter, bullet) &&
|
175 |
gunner_get_team(shooter) != gunner_get_team(bullet_get_shooter(bullet))) { |
176 |
list_node_t *aux = list_node_next(bullet_it); |
177 |
/// Change health
|
178 |
gunner_set_curr_health(shooter, gunner_get_curr_health(shooter) - bullet_get_damage(bullet)); |
179 |
if (gunner_get_curr_health(shooter) <= 0) { |
180 |
gunner_dtor(list_erase(shooter_list, shooter_it)); |
181 |
} |
182 |
/// Delete bullet
|
183 |
bullet_dtor(list_erase(bullet_list, bullet_it)); deleted_bullet = true;
|
184 |
/// Advance iterator
|
185 |
bullet_it = aux; |
186 |
break;
|
187 |
} else shooter_it = list_node_next(shooter_it);
|
188 |
} |
189 |
if(deleted_bullet) continue; |
190 |
/// Advance iterator if necessary
|
191 |
bullet_it = list_node_next(bullet_it); |
192 |
} |
193 |
/// MELEE
|
194 |
list_node_t *it1 = list_begin(shooter_list); |
195 |
while(it1 != list_end(shooter_list)){
|
196 |
gunner_t *s1 = *list_node_val(it1); |
197 |
if(!(gunner_get_type(s1) & gunner_melee)){ it1 = list_node_next(it1); continue; } |
198 |
list_node_t *it2 = list_begin(shooter_list); |
199 |
while(it2 != list_end(shooter_list)){
|
200 |
gunner_t *s2 = *list_node_val(it2); |
201 |
if(gunner_get_team(s1) != gunner_get_team(s2) &&
|
202 |
gunner_distance(s1, s2) < MELEE_RANGE) |
203 |
gunner_set_curr_health(s2, gunner_get_curr_health(s2) - MELEE_DAMAGE); |
204 |
if(gunner_get_curr_health(s2) <= 0){ |
205 |
list_node_t *aux = list_node_next(it2); |
206 |
gunner_dtor(list_erase(shooter_list, it2)); |
207 |
it2 = aux; |
208 |
}else it2 = list_node_next(it2);
|
209 |
} |
210 |
it1 = list_node_next(it1); |
211 |
} |
212 |
} |
213 |
|
214 |
void (get_random_spawn)(const map_t *map, gunner_t *p, list_t *l) { |
215 |
uint16_t w = map_get_width(map), h = map_get_height(map); |
216 |
double x, y;
|
217 |
|
218 |
while(true){ |
219 |
x = rand() % w; |
220 |
y = rand() % h; |
221 |
gunner_set_pos(p, x, y); |
222 |
if(map_collides_gunner(map, p)) continue; |
223 |
int collides = false; |
224 |
list_node_t *it = list_begin(l); |
225 |
while(it != list_end(l)){
|
226 |
if(gunner_collides_gunner(p, *list_node_val(it))){
|
227 |
collides = true;
|
228 |
break;
|
229 |
} |
230 |
it = list_node_next(it); |
231 |
} |
232 |
if(!collides) return; |
233 |
} |
234 |
} |
235 |
|
236 |
void update_scale(void) { |
237 |
static uint8_t last_plus = 0, last_minus = 0; |
238 |
if (key_presses.ctrl_pressed) {
|
239 |
if (key_presses.plus_pressed && !last_plus) {
|
240 |
double scale = ent_get_scale();
|
241 |
scale *= 1.1; |
242 |
if (scale <= MAX_SCALE) ent_set_scale(scale);
|
243 |
} |
244 |
else if (key_presses.minus_pressed && !last_minus) { |
245 |
double scale = ent_get_scale();
|
246 |
scale /= 1.1; |
247 |
if (scale >= MIN_SCALE) ent_set_scale(scale);
|
248 |
} |
249 |
|
250 |
//printf("SCALE: %d\n", (int)(ent_get_scale()*1000));
|
251 |
} |
252 |
|
253 |
last_plus = key_presses.plus_pressed; |
254 |
last_minus = key_presses.minus_pressed; |
255 |
} |
256 |
|
257 |
static int32_t mouse_x = 0, mouse_y = 0; |
258 |
|
259 |
void (update_mouse)(struct packet *p) { |
260 |
mouse_x = max(0, mouse_x + p->delta_x);
|
261 |
mouse_x = min(mouse_x, graph_get_XRes() - 1);
|
262 |
|
263 |
mouse_y = max(0, mouse_y - p->delta_y);
|
264 |
mouse_y = min(mouse_y, graph_get_YRes() - 1);
|
265 |
|
266 |
key_presses.lb_pressed = p->lb; |
267 |
} |
268 |
|
269 |
keys_t* (get_key_presses)(void) {
|
270 |
return &key_presses;
|
271 |
} |
272 |
|
273 |
int32_t* get_mouse_X(void) { return &mouse_x; } |
274 |
|
275 |
int32_t* get_mouse_Y(void) { return &mouse_y; } |
276 |
|
277 |
double get_mouse_angle(gunner_t *p) {
|
278 |
return atan2(gunner_get_y_screen(p) - mouse_y, mouse_x - gunner_get_x_screen(p));
|
279 |
} |
280 |
|
281 |
text_timer_t* (timer_ctor)(const font_t *fnt){
|
282 |
if(fnt == NULL) return NULL; |
283 |
text_timer_t *ret = malloc(sizeof(timer_t));
|
284 |
if (ret == NULL) return NULL; |
285 |
ret->time = 0;
|
286 |
ret->text = text_ctor(fnt, "000s");
|
287 |
text_set_color(ret->text, TEXT_COLOR); |
288 |
return ret;
|
289 |
} |
290 |
|
291 |
void (timer_update)(text_timer_t *p){
|
292 |
if (p->time >= 999) return; |
293 |
p->time++; |
294 |
char buffer[100]; |
295 |
sprintf(buffer, "%03ds", p->time);
|
296 |
text_set_string(p->text, buffer); |
297 |
} |
298 |
|
299 |
void (timer_reset)(text_timer_t *p){
|
300 |
text_set_string(p->text, "000s");
|
301 |
} |
302 |
|
303 |
void (timer_dtor)(text_timer_t *p){
|
304 |
if (p == NULL) return; |
305 |
text_dtor(p->text); |
306 |
free(p); |
307 |
} |