root / proj / src / proj_func.c @ 305
History | View | Annotate | Download (8.49 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(const 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 |
|
86 |
void (shoot_bullet)(const gunner_t *shooter, list_t *bullet_list, const basic_sprite_t *bsp_bullet) { |
87 |
double angle = gunner_get_angle(shooter);
|
88 |
double vx = -BULLET_SPEED * fm_sin(angle);
|
89 |
double vy = -BULLET_SPEED * fm_cos(angle);
|
90 |
bullet_t *bullet = bullet_ctor(shooter, bsp_bullet, gunner_get_x(shooter), gunner_get_y(shooter), vx, vy); |
91 |
list_insert(bullet_list, list_end(bullet_list), bullet); |
92 |
} |
93 |
|
94 |
void (update_game_state)(const map_t *map, list_t *shooter_list, list_t *bullet_list) { |
95 |
/// BULLETS
|
96 |
bullet_update_movement_list(bullet_list); |
97 |
list_node_t *bullet_it = list_begin(bullet_list); |
98 |
while (bullet_it != list_end(bullet_list)) {
|
99 |
/// Collision with walls
|
100 |
bullet_t *bullet = *(bullet_t**)list_node_val(bullet_it); |
101 |
if (map_collides_bullet(map, bullet)) {
|
102 |
list_node_t *aux = list_node_next(bullet_it); |
103 |
/// Delete bullet
|
104 |
bullet_dtor(list_erase(bullet_list, bullet_it)); |
105 |
/// Advance iterator
|
106 |
bullet_it = aux; |
107 |
continue;
|
108 |
} |
109 |
/// Collision with shooters
|
110 |
list_node_t *shooter_it = list_begin(shooter_list); |
111 |
int deleted_bullet = false; |
112 |
while(shooter_it != list_end(shooter_list)){
|
113 |
gunner_t *shooter = *(gunner_t**)list_node_val(shooter_it); |
114 |
if(gunner_collides_bullet(shooter, bullet) &&
|
115 |
gunner_get_team(shooter) != gunner_get_team(bullet_get_shooter(bullet))) { |
116 |
list_node_t *aux = list_node_next(bullet_it); |
117 |
/// Change health
|
118 |
gunner_set_curr_health(shooter, gunner_get_curr_health(shooter) - bullet_get_damage(bullet)); |
119 |
if (gunner_get_curr_health(shooter) <= 0) { |
120 |
gunner_dtor(list_erase(shooter_list, shooter_it)); |
121 |
} |
122 |
/// Delete bullet
|
123 |
bullet_dtor(list_erase(bullet_list, bullet_it)); deleted_bullet = true;
|
124 |
/// Advance iterator
|
125 |
bullet_it = aux; |
126 |
break;
|
127 |
} else shooter_it = list_node_next(shooter_it);
|
128 |
} |
129 |
if(deleted_bullet) continue; |
130 |
/// Advance iterator if necessary
|
131 |
bullet_it = list_node_next(bullet_it); |
132 |
} |
133 |
/// MELEE
|
134 |
list_node_t *it1 = list_begin(shooter_list); |
135 |
while(it1 != list_end(shooter_list)){
|
136 |
gunner_t *s1 = *list_node_val(it1); |
137 |
if(!(gunner_get_type(s1) & gunner_melee)){ it1 = list_node_next(it1); continue; } |
138 |
list_node_t *it2 = list_begin(shooter_list); |
139 |
while(it2 != list_end(shooter_list)){
|
140 |
gunner_t *s2 = *list_node_val(it2); |
141 |
if(s1 != s2 && gunner_distance(s1, s2) < MELEE_RANGE)
|
142 |
gunner_set_curr_health(s2, gunner_get_curr_health(s2) - MELEE_DAMAGE); |
143 |
if(gunner_get_curr_health(s2) <= 0){ |
144 |
list_node_t *aux = list_node_next(it2); |
145 |
gunner_dtor(list_erase(shooter_list, it2)); |
146 |
it2 = aux; |
147 |
}else it2 = list_node_next(it2);
|
148 |
} |
149 |
it1 = list_node_next(it1); |
150 |
} |
151 |
} |
152 |
|
153 |
void (get_random_spawn)(const map_t *map, gunner_t *p) { |
154 |
uint16_t w = map_get_width(map), h = map_get_height(map); |
155 |
double x, y;
|
156 |
|
157 |
do {
|
158 |
x = rand() % w; |
159 |
y = rand() % h; |
160 |
gunner_set_pos(p, x, y); |
161 |
} while (map_collides_gunner(map, p));
|
162 |
} |
163 |
|
164 |
void update_scale(void) { |
165 |
static uint8_t last_plus = 0, last_minus = 0; |
166 |
if (key_presses.ctrl_pressed) {
|
167 |
if (key_presses.plus_pressed && !last_plus) {
|
168 |
double scale = ent_get_scale();
|
169 |
scale *= 1.1; |
170 |
if (scale <= MAX_SCALE) ent_set_scale(scale);
|
171 |
} |
172 |
else if (key_presses.minus_pressed && !last_minus) { |
173 |
double scale = ent_get_scale();
|
174 |
scale /= 1.1; |
175 |
if (scale >= MIN_SCALE) ent_set_scale(scale);
|
176 |
} |
177 |
|
178 |
//printf("SCALE: %d\n", (int)(ent_get_scale()*1000));
|
179 |
} |
180 |
|
181 |
last_plus = key_presses.plus_pressed; |
182 |
last_minus = key_presses.minus_pressed; |
183 |
} |
184 |
|
185 |
static int32_t mouse_x = 0, mouse_y = 0; |
186 |
|
187 |
void (update_mouse)(struct packet *p) { |
188 |
mouse_x = max(0, mouse_x + p->delta_x);
|
189 |
mouse_x = min(mouse_x, graph_get_XRes() - 1);
|
190 |
|
191 |
mouse_y = max(0, mouse_y - p->delta_y);
|
192 |
mouse_y = min(mouse_y, graph_get_YRes() - 1);
|
193 |
|
194 |
key_presses.lb_pressed = p->lb; |
195 |
} |
196 |
|
197 |
keys_t* (get_key_presses)(void) {
|
198 |
return &key_presses;
|
199 |
} |
200 |
|
201 |
int32_t* get_mouse_X(void) { return &mouse_x; } |
202 |
|
203 |
int32_t* get_mouse_Y(void) { return &mouse_y; } |
204 |
|
205 |
double get_mouse_angle(gunner_t *p) {
|
206 |
return atan2(gunner_get_y_screen(p) - mouse_y, mouse_x - gunner_get_x_screen(p));
|
207 |
} |
208 |
|
209 |
text_timer_t* (timer_ctor)(const font_t *fnt){
|
210 |
if(fnt == NULL) return NULL; |
211 |
text_timer_t *ret = malloc(sizeof(timer_t));
|
212 |
if (ret == NULL) return NULL; |
213 |
ret->time = 0;
|
214 |
ret->text = text_ctor(fnt, "000s");
|
215 |
ret->array = text_get_string(ret->text); |
216 |
text_set_color(ret->text, TEXT_COLOR); |
217 |
return ret;
|
218 |
} |
219 |
|
220 |
void (timer_update)(text_timer_t *p){
|
221 |
if (p->time >= 999) return; |
222 |
p->time++; |
223 |
p->array[2] = p->time % 10 + '0'; |
224 |
p->array[1] = (p->time/10) % 10 + '0'; |
225 |
p->array[0] = (p->time/100) % 10 + '0'; |
226 |
} |
227 |
|
228 |
void (timer_reset)(text_timer_t *p){
|
229 |
p->time = 0;
|
230 |
p->array[2] = '0'; |
231 |
p->array[1] = '0'; |
232 |
p->array[0] = '0'; |
233 |
} |
234 |
|
235 |
void (timer_dtor)(text_timer_t *p){
|
236 |
if (p == NULL) return; |
237 |
text_dtor(p->text); |
238 |
free(p); |
239 |
} |