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