root / proj / src / proj_func.c @ 308
History | View | Annotate | Download (9.35 KB)
1 | 167 | up20180655 | #include <lcom/lcf.h> |
---|---|---|---|
2 | |||
3 | 156 | up20180655 | #include "proj_func.h" |
4 | 167 | up20180655 | |
5 | 156 | up20180655 | #include "interrupts_func.h" |
6 | 166 | up20180642 | #include "graph.h" |
7 | 167 | up20180655 | #include "keyboard.h" |
8 | 156 | up20180655 | #include "errors.h" |
9 | 167 | up20180655 | #include "proj_macros.h" |
10 | #include "utils.h" |
||
11 | 200 | up20180655 | #include "ent.h" |
12 | 231 | up20180655 | #include "fast_math.h" |
13 | 251 | up20180655 | #include "rectangle.h" |
14 | #include "font.h" |
||
15 | 156 | up20180655 | |
16 | 167 | up20180655 | #include <math.h> |
17 | |||
18 | 156 | up20180655 | int cleanup(void) { |
19 | int r = SUCCESS;
|
||
20 | if ((r = unsubscribe_all()))
|
||
21 | printf("%s: failed to unsubscribe drivers.\n", __func__);
|
||
22 | 166 | up20180642 | if ((r = graph_cleanup()))
|
23 | printf("%s: graph cleanup failed\n", __func__);
|
||
24 | 156 | up20180655 | |
25 | return r;
|
||
26 | } |
||
27 | 167 | up20180655 | |
28 | 200 | up20180655 | static keys_t key_presses;
|
29 | 167 | up20180655 | |
30 | 184 | up20180655 | void update_key_presses(void) { |
31 | 167 | up20180655 | if (sz == 1) { |
32 | switch(scancode[0]) { |
||
33 | 200 | up20180655 | 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 | 250 | up20180655 | 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 | 167 | up20180655 | } |
48 | } |
||
49 | } |
||
50 | 240 | up20180655 | |
51 | 307 | up20180642 | void update_movement(map_t *map, gunner_t *p, keys_t *keys, list_t *shooter_list) {
|
52 | 246 | up20180655 | int ver_mov = keys->s_pressed - keys->w_pressed;
|
53 | int hor_mov = keys->d_pressed - keys->a_pressed;
|
||
54 | 240 | up20180655 | double x = gunner_get_x(p);
|
55 | double y = gunner_get_y(p);
|
||
56 | 246 | up20180655 | |
57 | 240 | up20180655 | gunner_set_pos(p, x + SHOOTER_SPEED * hor_mov, y); |
58 | 246 | up20180655 | 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 | 240 | up20180655 | } |
70 | 246 | up20180655 | x = gunner_get_x(p); |
71 | 240 | up20180655 | gunner_set_pos(p, x, y + SHOOTER_SPEED * ver_mov); |
72 | 246 | up20180655 | 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 | 222 | up20180655 | } |
84 | 307 | up20180642 | |
85 | // Update zombie positions
|
||
86 | map_make_dijkstra(map, gunner_get_x(p), gunner_get_y(p)); |
||
87 | list_node_t *it = list_begin(shooter_list); |
||
88 | while(it != list_end(shooter_list)){
|
||
89 | gunner_t *g = *(gunner_t**)list_node_val(it); |
||
90 | if(gunner_get_type(g) & gunner_follow){
|
||
91 | //float theta = 0.0;
|
||
92 | //map_where_to_follow(map, &theta);
|
||
93 | //float c = fm_cos(theta), s = fm_sin(theta);
|
||
94 | |||
95 | } |
||
96 | it = list_node_next(it); |
||
97 | } |
||
98 | 184 | up20180655 | } |
99 | 240 | up20180655 | |
100 | 237 | up20180642 | void (shoot_bullet)(const gunner_t *shooter, list_t *bullet_list, const basic_sprite_t *bsp_bullet) { |
101 | 231 | up20180655 | double angle = gunner_get_angle(shooter);
|
102 | double vx = -BULLET_SPEED * fm_sin(angle);
|
||
103 | double vy = -BULLET_SPEED * fm_cos(angle);
|
||
104 | 237 | up20180642 | bullet_t *bullet = bullet_ctor(shooter, bsp_bullet, gunner_get_x(shooter), gunner_get_y(shooter), vx, vy); |
105 | 231 | up20180655 | list_insert(bullet_list, list_end(bullet_list), bullet); |
106 | } |
||
107 | |||
108 | 236 | up20180642 | void (update_game_state)(const map_t *map, list_t *shooter_list, list_t *bullet_list) { |
109 | 302 | up20180642 | /// BULLETS
|
110 | 231 | up20180655 | bullet_update_movement_list(bullet_list); |
111 | 236 | up20180642 | list_node_t *bullet_it = list_begin(bullet_list); |
112 | while (bullet_it != list_end(bullet_list)) {
|
||
113 | /// Collision with walls
|
||
114 | bullet_t *bullet = *(bullet_t**)list_node_val(bullet_it); |
||
115 | 231 | up20180655 | if (map_collides_bullet(map, bullet)) {
|
116 | 236 | up20180642 | list_node_t *aux = list_node_next(bullet_it); |
117 | /// Delete bullet
|
||
118 | bullet_dtor(list_erase(bullet_list, bullet_it)); |
||
119 | /// Advance iterator
|
||
120 | bullet_it = aux; |
||
121 | 231 | up20180655 | continue;
|
122 | } |
||
123 | 236 | up20180642 | /// Collision with shooters
|
124 | list_node_t *shooter_it = list_begin(shooter_list); |
||
125 | int deleted_bullet = false; |
||
126 | while(shooter_it != list_end(shooter_list)){
|
||
127 | gunner_t *shooter = *(gunner_t**)list_node_val(shooter_it); |
||
128 | 302 | up20180642 | if(gunner_collides_bullet(shooter, bullet) &&
|
129 | gunner_get_team(shooter) != gunner_get_team(bullet_get_shooter(bullet))) { |
||
130 | 236 | up20180642 | list_node_t *aux = list_node_next(bullet_it); |
131 | /// Change health
|
||
132 | gunner_set_curr_health(shooter, gunner_get_curr_health(shooter) - bullet_get_damage(bullet)); |
||
133 | if (gunner_get_curr_health(shooter) <= 0) { |
||
134 | 301 | up20180642 | gunner_dtor(list_erase(shooter_list, shooter_it)); |
135 | 236 | up20180642 | } |
136 | /// Delete bullet
|
||
137 | bullet_dtor(list_erase(bullet_list, bullet_it)); deleted_bullet = true;
|
||
138 | /// Advance iterator
|
||
139 | bullet_it = aux; |
||
140 | break;
|
||
141 | } else shooter_it = list_node_next(shooter_it);
|
||
142 | 231 | up20180655 | } |
143 | 236 | up20180642 | if(deleted_bullet) continue; |
144 | /// Advance iterator if necessary
|
||
145 | bullet_it = list_node_next(bullet_it); |
||
146 | 231 | up20180655 | } |
147 | 305 | up20180642 | /// MELEE
|
148 | 302 | up20180642 | list_node_t *it1 = list_begin(shooter_list); |
149 | while(it1 != list_end(shooter_list)){
|
||
150 | gunner_t *s1 = *list_node_val(it1); |
||
151 | 305 | up20180642 | if(!(gunner_get_type(s1) & gunner_melee)){ it1 = list_node_next(it1); continue; } |
152 | 302 | up20180642 | list_node_t *it2 = list_begin(shooter_list); |
153 | while(it2 != list_end(shooter_list)){
|
||
154 | gunner_t *s2 = *list_node_val(it2); |
||
155 | 307 | up20180642 | if(gunner_get_team(s1) != gunner_get_team(s2) &&
|
156 | gunner_distance(s1, s2) < MELEE_RANGE) |
||
157 | 305 | up20180642 | gunner_set_curr_health(s2, gunner_get_curr_health(s2) - MELEE_DAMAGE); |
158 | 302 | up20180642 | if(gunner_get_curr_health(s2) <= 0){ |
159 | list_node_t *aux = list_node_next(it2); |
||
160 | gunner_dtor(list_erase(shooter_list, it2)); |
||
161 | it2 = aux; |
||
162 | }else it2 = list_node_next(it2);
|
||
163 | } |
||
164 | it1 = list_node_next(it1); |
||
165 | } |
||
166 | 231 | up20180655 | } |
167 | |||
168 | 307 | up20180642 | void (get_random_spawn)(const map_t *map, gunner_t *p, list_t *l) { |
169 | 246 | up20180655 | uint16_t w = map_get_width(map), h = map_get_height(map); |
170 | double x, y;
|
||
171 | |||
172 | 307 | up20180642 | while(true){ |
173 | 246 | up20180655 | x = rand() % w; |
174 | y = rand() % h; |
||
175 | gunner_set_pos(p, x, y); |
||
176 | 307 | up20180642 | if(map_collides_gunner(map, p)) continue; |
177 | int collides = false; |
||
178 | list_node_t *it = list_begin(l); |
||
179 | while(it != list_end(l)){
|
||
180 | if(gunner_collides_gunner(p, *list_node_val(it))){
|
||
181 | collides = true;
|
||
182 | break;
|
||
183 | } |
||
184 | it = list_node_next(it); |
||
185 | } |
||
186 | if(!collides) return; |
||
187 | } |
||
188 | 246 | up20180655 | } |
189 | |||
190 | 200 | up20180655 | void update_scale(void) { |
191 | static uint8_t last_plus = 0, last_minus = 0; |
||
192 | if (key_presses.ctrl_pressed) {
|
||
193 | if (key_presses.plus_pressed && !last_plus) {
|
||
194 | double scale = ent_get_scale();
|
||
195 | scale *= 1.1; |
||
196 | if (scale <= MAX_SCALE) ent_set_scale(scale);
|
||
197 | } |
||
198 | else if (key_presses.minus_pressed && !last_minus) { |
||
199 | double scale = ent_get_scale();
|
||
200 | scale /= 1.1; |
||
201 | if (scale >= MIN_SCALE) ent_set_scale(scale);
|
||
202 | } |
||
203 | |||
204 | 222 | up20180655 | //printf("SCALE: %d\n", (int)(ent_get_scale()*1000));
|
205 | 200 | up20180655 | } |
206 | |||
207 | last_plus = key_presses.plus_pressed; |
||
208 | last_minus = key_presses.minus_pressed; |
||
209 | } |
||
210 | |||
211 | 171 | up20180655 | static int32_t mouse_x = 0, mouse_y = 0; |
212 | |||
213 | 231 | up20180655 | void (update_mouse)(struct packet *p) { |
214 | 171 | up20180655 | mouse_x = max(0, mouse_x + p->delta_x);
|
215 | mouse_x = min(mouse_x, graph_get_XRes() - 1);
|
||
216 | |||
217 | 173 | up20180655 | mouse_y = max(0, mouse_y - p->delta_y);
|
218 | 171 | up20180655 | mouse_y = min(mouse_y, graph_get_YRes() - 1);
|
219 | 231 | up20180655 | |
220 | key_presses.lb_pressed = p->lb; |
||
221 | 171 | up20180655 | } |
222 | |||
223 | 231 | up20180655 | keys_t* (get_key_presses)(void) {
|
224 | return &key_presses;
|
||
225 | } |
||
226 | |||
227 | 250 | up20180655 | int32_t* get_mouse_X(void) { return &mouse_x; } |
228 | 171 | up20180655 | |
229 | 250 | up20180655 | int32_t* get_mouse_Y(void) { return &mouse_y; } |
230 | 171 | up20180655 | |
231 | 201 | up20180642 | double get_mouse_angle(gunner_t *p) {
|
232 | return atan2(gunner_get_y_screen(p) - mouse_y, mouse_x - gunner_get_x_screen(p));
|
||
233 | 167 | up20180655 | } |
234 | 250 | up20180655 | |
235 | text_timer_t* (timer_ctor)(const font_t *fnt){
|
||
236 | if(fnt == NULL) return NULL; |
||
237 | text_timer_t *ret = malloc(sizeof(timer_t));
|
||
238 | if (ret == NULL) return NULL; |
||
239 | ret->time = 0;
|
||
240 | ret->text = text_ctor(fnt, "000s");
|
||
241 | ret->array = text_get_string(ret->text); |
||
242 | 251 | up20180655 | text_set_color(ret->text, TEXT_COLOR); |
243 | 250 | up20180655 | return ret;
|
244 | } |
||
245 | |||
246 | void (timer_update)(text_timer_t *p){
|
||
247 | if (p->time >= 999) return; |
||
248 | p->time++; |
||
249 | p->array[2] = p->time % 10 + '0'; |
||
250 | p->array[1] = (p->time/10) % 10 + '0'; |
||
251 | p->array[0] = (p->time/100) % 10 + '0'; |
||
252 | } |
||
253 | |||
254 | 251 | up20180655 | void (timer_reset)(text_timer_t *p){
|
255 | p->time = 0;
|
||
256 | p->array[2] = '0'; |
||
257 | p->array[1] = '0'; |
||
258 | p->array[0] = '0'; |
||
259 | } |
||
260 | |||
261 | 250 | up20180655 | void (timer_dtor)(text_timer_t *p){
|
262 | if (p == NULL) return; |
||
263 | text_dtor(p->text); |
||
264 | free(p); |
||
265 | } |