root / proj / src / proj_func.c @ 228
History | View | Annotate | Download (3.6 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 | 156 | up20180655 | |
13 | 167 | up20180655 | #include "kbc_macros.h" |
14 | 156 | up20180655 | |
15 | 167 | up20180655 | #include <math.h> |
16 | |||
17 | 156 | up20180655 | int cleanup(void) { |
18 | int r = SUCCESS;
|
||
19 | if ((r = unsubscribe_all()))
|
||
20 | printf("%s: failed to unsubscribe drivers.\n", __func__);
|
||
21 | 166 | up20180642 | if ((r = graph_cleanup()))
|
22 | printf("%s: graph cleanup failed\n", __func__);
|
||
23 | 156 | up20180655 | |
24 | return r;
|
||
25 | } |
||
26 | 167 | up20180655 | |
27 | static int hor_mov = REST, ver_mov = REST; |
||
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 | case PLUS_MAKE_CODE : key_presses.plus_pressed = 1; break; |
||
44 | case PLUS_BREAK_CODE : key_presses.plus_pressed = 0; break; |
||
45 | case MINUS_MAKE_CODE : key_presses.minus_pressed = 1; break; |
||
46 | case MINUS_BREAK_CODE : key_presses.minus_pressed = 0; break; |
||
47 | 167 | up20180655 | } |
48 | } |
||
49 | 200 | up20180655 | ver_mov = key_presses.s_pressed - key_presses.w_pressed; |
50 | hor_mov = key_presses.d_pressed - key_presses.a_pressed; |
||
51 | 167 | up20180655 | } |
52 | |||
53 | 222 | up20180655 | void update_movement(const map_t *map, gunner_t *p) { |
54 | 184 | up20180655 | static const int speed = 5; |
55 | 222 | up20180655 | double x = gunner_get_x(p);
|
56 | double y = gunner_get_y(p);
|
||
57 | 225 | up20180655 | gunner_set_pos(p, x + speed * hor_mov, y); |
58 | 222 | up20180655 | if (map_collides_gunner(map, p)) {
|
59 | gunner_set_pos(p, x, y); |
||
60 | } |
||
61 | 225 | up20180655 | x = gunner_get_x(p); |
62 | gunner_set_pos(p, x, y + speed * ver_mov); |
||
63 | if (map_collides_gunner(map, p)) {
|
||
64 | gunner_set_pos(p, x, y); |
||
65 | } |
||
66 | 184 | up20180655 | } |
67 | |||
68 | 200 | up20180655 | void update_scale(void) { |
69 | static uint8_t last_plus = 0, last_minus = 0; |
||
70 | if (key_presses.ctrl_pressed) {
|
||
71 | if (key_presses.plus_pressed && !last_plus) {
|
||
72 | double scale = ent_get_scale();
|
||
73 | scale *= 1.1; |
||
74 | if (scale <= MAX_SCALE) ent_set_scale(scale);
|
||
75 | } |
||
76 | else if (key_presses.minus_pressed && !last_minus) { |
||
77 | double scale = ent_get_scale();
|
||
78 | scale /= 1.1; |
||
79 | if (scale >= MIN_SCALE) ent_set_scale(scale);
|
||
80 | } |
||
81 | |||
82 | 222 | up20180655 | //printf("SCALE: %d\n", (int)(ent_get_scale()*1000));
|
83 | 200 | up20180655 | } |
84 | |||
85 | last_plus = key_presses.plus_pressed; |
||
86 | last_minus = key_presses.minus_pressed; |
||
87 | } |
||
88 | |||
89 | 171 | up20180655 | static int32_t mouse_x = 0, mouse_y = 0; |
90 | |||
91 | void update_mouse_position(struct packet *p) { |
||
92 | mouse_x = max(0, mouse_x + p->delta_x);
|
||
93 | mouse_x = min(mouse_x, graph_get_XRes() - 1);
|
||
94 | |||
95 | 173 | up20180655 | mouse_y = max(0, mouse_y - p->delta_y);
|
96 | 171 | up20180655 | mouse_y = min(mouse_y, graph_get_YRes() - 1);
|
97 | } |
||
98 | |||
99 | int32_t get_mouse_X(void) { return mouse_x; } |
||
100 | |||
101 | int32_t get_mouse_Y(void) { return mouse_y; } |
||
102 | |||
103 | 201 | up20180642 | double get_mouse_angle(gunner_t *p) {
|
104 | return atan2(gunner_get_y_screen(p) - mouse_y, mouse_x - gunner_get_x_screen(p));
|
||
105 | 167 | up20180655 | } |
106 | |||
107 | int get_hor_movement(void) { return hor_mov; } |
||
108 | |||
109 | int get_ver_movement(void) { return ver_mov; } |