root / proj / src / proj_func.c @ 239
History | View | Annotate | Download (6.09 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 |
|
14 |
#include "kbc_macros.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 int hor_mov = REST, ver_mov = REST; |
29 |
static keys_t key_presses;
|
30 |
|
31 |
void update_key_presses(void) { |
32 |
if (sz == 1) { |
33 |
switch(scancode[0]) { |
34 |
case W_MAKE_CODE : key_presses.w_pressed = 1; break; |
35 |
case W_BREAK_CODE : key_presses.w_pressed = 0; break; |
36 |
case A_MAKE_CODE : key_presses.a_pressed = 1; break; |
37 |
case A_BREAK_CODE : key_presses.a_pressed = 0; break; |
38 |
case S_MAKE_CODE : key_presses.s_pressed = 1; break; |
39 |
case S_BREAK_CODE : key_presses.s_pressed = 0; break; |
40 |
case D_MAKE_CODE : key_presses.d_pressed = 1; break; |
41 |
case D_BREAK_CODE : key_presses.d_pressed = 0; break; |
42 |
case CTRL_MAKE_CODE : key_presses.ctrl_pressed = 1; break; |
43 |
case CTRL_BREAK_CODE : key_presses.ctrl_pressed = 0; break; |
44 |
case PLUS_MAKE_CODE : key_presses.plus_pressed = 1; break; |
45 |
case PLUS_BREAK_CODE : key_presses.plus_pressed = 0; break; |
46 |
case MINUS_MAKE_CODE : key_presses.minus_pressed = 1; break; |
47 |
case MINUS_BREAK_CODE : key_presses.minus_pressed = 0; break; |
48 |
} |
49 |
} |
50 |
ver_mov = key_presses.s_pressed - key_presses.w_pressed; |
51 |
hor_mov = key_presses.d_pressed - key_presses.a_pressed; |
52 |
} |
53 |
/*
|
54 |
void update_movement(const map_t *map, list_t *shooter_list){
|
55 |
list_node_t *it1 = list_begin(shooter_list);
|
56 |
while(it1 != list_end(shooter_list)){
|
57 |
double x = gunner_get_x(p);
|
58 |
double y = gunner_get_y(p);
|
59 |
gunner_set_pos(p, x + SHOOTER_SPEED * hor_mov, y);
|
60 |
if (map_collides_gunner(map, p) || gunner_collides_gunner(p, p2)) {
|
61 |
gunner_set_pos(p, x, y);
|
62 |
}
|
63 |
x = gunner_get_x(p);
|
64 |
gunner_set_pos(p, x, y + SHOOTER_SPEED * ver_mov);
|
65 |
if (map_collides_gunner(map, p) || gunner_collides_gunner(p, p2)) {
|
66 |
gunner_set_pos(p, x, y);
|
67 |
}
|
68 |
}
|
69 |
}
|
70 |
*/
|
71 |
void (shoot_bullet)(const gunner_t *shooter, list_t *bullet_list, const basic_sprite_t *bsp_bullet) { |
72 |
double angle = gunner_get_angle(shooter);
|
73 |
double vx = -BULLET_SPEED * fm_sin(angle);
|
74 |
double vy = -BULLET_SPEED * fm_cos(angle);
|
75 |
bullet_t *bullet = bullet_ctor(shooter, bsp_bullet, gunner_get_x(shooter), gunner_get_y(shooter), vx, vy); |
76 |
list_insert(bullet_list, list_end(bullet_list), bullet); |
77 |
} |
78 |
|
79 |
void (update_game_state)(const map_t *map, list_t *shooter_list, list_t *bullet_list) { |
80 |
|
81 |
bullet_update_movement_list(bullet_list); |
82 |
|
83 |
list_node_t *bullet_it = list_begin(bullet_list); |
84 |
while (bullet_it != list_end(bullet_list)) {
|
85 |
/// Collision with walls
|
86 |
bullet_t *bullet = *(bullet_t**)list_node_val(bullet_it); |
87 |
if (map_collides_bullet(map, bullet)) {
|
88 |
list_node_t *aux = list_node_next(bullet_it); |
89 |
/// Delete bullet
|
90 |
bullet_dtor(list_erase(bullet_list, bullet_it)); |
91 |
/// Advance iterator
|
92 |
bullet_it = aux; |
93 |
continue;
|
94 |
} |
95 |
/// Collision with shooters
|
96 |
list_node_t *shooter_it = list_begin(shooter_list); |
97 |
int deleted_bullet = false; |
98 |
while(shooter_it != list_end(shooter_list)){
|
99 |
gunner_t *shooter = *(gunner_t**)list_node_val(shooter_it); |
100 |
if(gunner_collides_bullet(shooter, bullet)) {
|
101 |
list_node_t *aux = list_node_next(bullet_it); |
102 |
/// Change health
|
103 |
gunner_set_curr_health(shooter, gunner_get_curr_health(shooter) - bullet_get_damage(bullet)); |
104 |
if (gunner_get_curr_health(shooter) <= 0) { |
105 |
shooter = list_erase(shooter_list, shooter_it); |
106 |
gunner_dtor(shooter); |
107 |
} |
108 |
/// Delete bullet
|
109 |
bullet_dtor(list_erase(bullet_list, bullet_it)); deleted_bullet = true;
|
110 |
/// Advance iterator
|
111 |
bullet_it = aux; |
112 |
break;
|
113 |
} else shooter_it = list_node_next(shooter_it);
|
114 |
} |
115 |
if(deleted_bullet) continue; |
116 |
/// Advance iterator if necessary
|
117 |
bullet_it = list_node_next(bullet_it); |
118 |
} |
119 |
} |
120 |
|
121 |
void update_scale(void) { |
122 |
static uint8_t last_plus = 0, last_minus = 0; |
123 |
if (key_presses.ctrl_pressed) {
|
124 |
if (key_presses.plus_pressed && !last_plus) {
|
125 |
double scale = ent_get_scale();
|
126 |
scale *= 1.1; |
127 |
if (scale <= MAX_SCALE) ent_set_scale(scale);
|
128 |
} |
129 |
else if (key_presses.minus_pressed && !last_minus) { |
130 |
double scale = ent_get_scale();
|
131 |
scale /= 1.1; |
132 |
if (scale >= MIN_SCALE) ent_set_scale(scale);
|
133 |
} |
134 |
|
135 |
//printf("SCALE: %d\n", (int)(ent_get_scale()*1000));
|
136 |
} |
137 |
|
138 |
last_plus = key_presses.plus_pressed; |
139 |
last_minus = key_presses.minus_pressed; |
140 |
} |
141 |
|
142 |
static int32_t mouse_x = 0, mouse_y = 0; |
143 |
|
144 |
void (update_mouse)(struct packet *p) { |
145 |
mouse_x = max(0, mouse_x + p->delta_x);
|
146 |
mouse_x = min(mouse_x, graph_get_XRes() - 1);
|
147 |
|
148 |
mouse_y = max(0, mouse_y - p->delta_y);
|
149 |
mouse_y = min(mouse_y, graph_get_YRes() - 1);
|
150 |
|
151 |
key_presses.lb_pressed = p->lb; |
152 |
} |
153 |
|
154 |
keys_t* (get_key_presses)(void) {
|
155 |
return &key_presses;
|
156 |
} |
157 |
|
158 |
int32_t get_mouse_X(void) { return mouse_x; } |
159 |
|
160 |
int32_t get_mouse_Y(void) { return mouse_y; } |
161 |
|
162 |
double get_mouse_angle(gunner_t *p) {
|
163 |
return atan2(gunner_get_y_screen(p) - mouse_y, mouse_x - gunner_get_x_screen(p));
|
164 |
} |
165 |
|
166 |
int get_hor_movement(void) { return hor_mov; } |
167 |
|
168 |
int get_ver_movement(void) { return ver_mov; } |