Project

General

Profile

Statistics
| Revision:

root / proj / src / proj_func.c @ 245

History | View | Annotate | Download (5.82 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 156 up20180655
14 167 up20180655
#include "kbc_macros.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
        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
}
50 240 up20180655
51
52
void update_movement(const map_t *map, gunner_t *p, gunner_t *p2) {
53
    int ver_mov = key_presses.s_pressed - key_presses.w_pressed;
54
    int hor_mov = key_presses.d_pressed - key_presses.a_pressed;
55
    double x = gunner_get_x(p);
56
    double y = gunner_get_y(p);
57
    gunner_set_pos(p, x + SHOOTER_SPEED * hor_mov, y);
58
    if (map_collides_gunner(map, p) || gunner_collides_gunner(p, p2)) {
59
        gunner_set_pos(p, x, y);
60
    }
61 238 up20180642
        x = gunner_get_x(p);
62 240 up20180655
    gunner_set_pos(p, x, y + SHOOTER_SPEED * ver_mov);
63
    if (map_collides_gunner(map, p) || gunner_collides_gunner(p, p2)) {
64
        gunner_set_pos(p, x, y);
65 222 up20180655
    }
66 184 up20180655
}
67 240 up20180655
68 237 up20180642
void (shoot_bullet)(const gunner_t *shooter, list_t *bullet_list, const basic_sprite_t *bsp_bullet) {
69 231 up20180655
    double angle = gunner_get_angle(shooter);
70
    double vx = -BULLET_SPEED * fm_sin(angle);
71
    double vy = -BULLET_SPEED * fm_cos(angle);
72 237 up20180642
    bullet_t *bullet = bullet_ctor(shooter, bsp_bullet, gunner_get_x(shooter), gunner_get_y(shooter), vx, vy);
73 231 up20180655
    list_insert(bullet_list, list_end(bullet_list), bullet);
74
}
75
76 236 up20180642
void (update_game_state)(const map_t *map, list_t *shooter_list, list_t *bullet_list) {
77 231 up20180655
78
    bullet_update_movement_list(bullet_list);
79
80 236 up20180642
    list_node_t *bullet_it = list_begin(bullet_list);
81
    while (bullet_it != list_end(bullet_list)) {
82
        /// Collision with walls
83
        bullet_t *bullet = *(bullet_t**)list_node_val(bullet_it);
84 231 up20180655
        if (map_collides_bullet(map, bullet)) {
85 236 up20180642
            list_node_t *aux = list_node_next(bullet_it);
86
            /// Delete bullet
87
            bullet_dtor(list_erase(bullet_list, bullet_it));
88
            /// Advance iterator
89
            bullet_it = aux;
90 231 up20180655
            continue;
91
        }
92 236 up20180642
        /// Collision with shooters
93
        list_node_t *shooter_it = list_begin(shooter_list);
94
        int deleted_bullet = false;
95
        while(shooter_it != list_end(shooter_list)){
96
            gunner_t *shooter = *(gunner_t**)list_node_val(shooter_it);
97
            if(gunner_collides_bullet(shooter, bullet)) {
98
                list_node_t *aux = list_node_next(bullet_it);
99
                /// Change health
100
                gunner_set_curr_health(shooter, gunner_get_curr_health(shooter) - bullet_get_damage(bullet));
101
                if (gunner_get_curr_health(shooter) <= 0) {
102
                    shooter = list_erase(shooter_list, shooter_it);
103
                    gunner_dtor(shooter);
104
                }
105
                /// Delete bullet
106
                bullet_dtor(list_erase(bullet_list, bullet_it)); deleted_bullet = true;
107
                /// Advance iterator
108
                bullet_it = aux;
109
                break;
110
            } else shooter_it = list_node_next(shooter_it);
111 231 up20180655
        }
112 236 up20180642
        if(deleted_bullet) continue;
113
        /// Advance iterator if necessary
114
        bullet_it = list_node_next(bullet_it);
115 231 up20180655
    }
116
}
117
118 200 up20180655
void update_scale(void) {
119
    static uint8_t last_plus = 0, last_minus = 0;
120
    if (key_presses.ctrl_pressed) {
121
        if (key_presses.plus_pressed && !last_plus) {
122
            double scale = ent_get_scale();
123
            scale *= 1.1;
124
            if (scale <= MAX_SCALE) ent_set_scale(scale);
125
        }
126
        else if (key_presses.minus_pressed && !last_minus) {
127
            double scale = ent_get_scale();
128
            scale /= 1.1;
129
            if (scale >= MIN_SCALE) ent_set_scale(scale);
130
        }
131
132 222 up20180655
        //printf("SCALE: %d\n", (int)(ent_get_scale()*1000));
133 200 up20180655
    }
134
135
    last_plus = key_presses.plus_pressed;
136
    last_minus = key_presses.minus_pressed;
137
}
138
139 171 up20180655
static int32_t mouse_x = 0, mouse_y = 0;
140
141 231 up20180655
void (update_mouse)(struct packet *p) {
142 171 up20180655
    mouse_x = max(0, mouse_x + p->delta_x);
143
    mouse_x = min(mouse_x, graph_get_XRes() - 1);
144
145 173 up20180655
    mouse_y = max(0, mouse_y - p->delta_y);
146 171 up20180655
    mouse_y = min(mouse_y, graph_get_YRes() - 1);
147 231 up20180655
148
    key_presses.lb_pressed = p->lb;
149 171 up20180655
}
150
151 231 up20180655
keys_t* (get_key_presses)(void) {
152
    return &key_presses;
153
}
154
155 171 up20180655
int32_t get_mouse_X(void) { return mouse_x; }
156
157
int32_t get_mouse_Y(void) { return mouse_y; }
158
159 201 up20180642
double get_mouse_angle(gunner_t *p) {
160
    return atan2(gunner_get_y_screen(p) - mouse_y, mouse_x - gunner_get_x_screen(p));
161 167 up20180655
}