Project

General

Profile

Statistics
| Revision:

root / proj / src / proj_func.c @ 234

History | View | Annotate | Download (5.34 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
#include "bullet.h"
14 156 up20180655
15 167 up20180655
#include "kbc_macros.h"
16 156 up20180655
17 167 up20180655
#include <math.h>
18
19 156 up20180655
int cleanup(void) {
20
    int r = SUCCESS;
21
    if ((r = unsubscribe_all()))
22
        printf("%s: failed to unsubscribe drivers.\n", __func__);
23 166 up20180642
    if ((r = graph_cleanup()))
24
        printf("%s: graph cleanup failed\n", __func__);
25 156 up20180655
26
    return r;
27
}
28 167 up20180655
29
static int hor_mov = REST, ver_mov = REST;
30 200 up20180655
static keys_t key_presses;
31 167 up20180655
32 184 up20180655
void update_key_presses(void) {
33 167 up20180655
    if (sz == 1) {
34
        switch(scancode[0]) {
35 200 up20180655
        case W_MAKE_CODE        : key_presses.w_pressed     = 1;        break;
36
        case W_BREAK_CODE       : key_presses.w_pressed     = 0;        break;
37
        case A_MAKE_CODE        : key_presses.a_pressed     = 1;        break;
38
        case A_BREAK_CODE       : key_presses.a_pressed     = 0;        break;
39
        case S_MAKE_CODE        : key_presses.s_pressed     = 1;        break;
40
        case S_BREAK_CODE       : key_presses.s_pressed     = 0;        break;
41
        case D_MAKE_CODE        : key_presses.d_pressed     = 1;        break;
42
        case D_BREAK_CODE       : key_presses.d_pressed     = 0;        break;
43
        case CTRL_MAKE_CODE     : key_presses.ctrl_pressed  = 1;        break;
44
        case CTRL_BREAK_CODE    : key_presses.ctrl_pressed  = 0;        break;
45
        case PLUS_MAKE_CODE     : key_presses.plus_pressed  = 1;        break;
46
        case PLUS_BREAK_CODE    : key_presses.plus_pressed  = 0;        break;
47
        case MINUS_MAKE_CODE    : key_presses.minus_pressed = 1;        break;
48
        case MINUS_BREAK_CODE   : key_presses.minus_pressed = 0;        break;
49 167 up20180655
        }
50
    }
51 200 up20180655
    ver_mov = key_presses.s_pressed - key_presses.w_pressed;
52
    hor_mov = key_presses.d_pressed - key_presses.a_pressed;
53 167 up20180655
}
54
55 233 up20180655
void update_movement(const map_t *map, gunner_t *p, gunner_t *p2) {
56 222 up20180655
    double x = gunner_get_x(p);
57
    double y = gunner_get_y(p);
58 231 up20180655
    gunner_set_pos(p, x + SHOOTER_SPEED * hor_mov, y);
59 233 up20180655
    if (map_collides_gunner(map, p) || gunner_collides_gunner(p, p2)) {
60 222 up20180655
        gunner_set_pos(p, x, y);
61
    }
62 225 up20180655
    x = gunner_get_x(p);
63 231 up20180655
    gunner_set_pos(p, x, y + SHOOTER_SPEED * ver_mov);
64 233 up20180655
    if (map_collides_gunner(map, p) || gunner_collides_gunner(p, p2)) {
65 225 up20180655
        gunner_set_pos(p, x, y);
66
    }
67 184 up20180655
}
68
69 231 up20180655
void (shoot_bullet)(const gunner_t *shooter, list_t *bullet_list) {
70
    double angle = gunner_get_angle(shooter);
71
    double vx = -BULLET_SPEED * fm_sin(angle);
72
    double vy = -BULLET_SPEED * fm_cos(angle);
73
    bullet_t *bullet = bullet_ctor(get_bullet(), gunner_get_x(shooter), gunner_get_y(shooter), vx, vy);
74
    list_insert(bullet_list, list_end(bullet_list), bullet);
75
}
76
77
void (update_game_state)(const map_t *map, gunner_t *shooter, list_t *bullet_list) {
78
79
    bullet_update_movement_list(bullet_list);
80
81
    list_node_t *it = list_begin(bullet_list);
82
    while (it != list_end(bullet_list)) {
83
        bullet_t *bullet = *(bullet_t**)list_node_val(it);
84
        if (map_collides_bullet(map, bullet)) {
85
            list_node_t *aux = list_node_next(it);
86
            bullet = (bullet_t*)list_erase(bullet_list, it);
87
            free(bullet);
88
            it = aux;
89
            continue;
90
        }
91
92
        if (gunner_collides_bullet(shooter, bullet)) {
93
            list_node_t *aux = list_node_next(it);
94
            bullet = (bullet_t*)list_erase(bullet_list, it);
95
            gunner_set_curr_health(shooter, gunner_get_curr_health(shooter) - bullet_get_damage(bullet));
96 232 up20180655
            if (gunner_get_curr_health(shooter) <= 0) {
97
                gunner_set_curr_health(shooter, gunner_get_health(shooter));
98
                gunner_set_pos(shooter, gunner_get_spawn_x(shooter), gunner_get_spawn_y(shooter));
99
            }
100
101 231 up20180655
            free(bullet);
102
            it = aux;
103
            continue;
104
        }
105
106
        it = list_node_next(it);
107
    }
108
}
109
110 200 up20180655
void update_scale(void) {
111
    static uint8_t last_plus = 0, last_minus = 0;
112
    if (key_presses.ctrl_pressed) {
113
        if (key_presses.plus_pressed && !last_plus) {
114
            double scale = ent_get_scale();
115
            scale *= 1.1;
116
            if (scale <= MAX_SCALE) ent_set_scale(scale);
117
        }
118
        else if (key_presses.minus_pressed && !last_minus) {
119
            double scale = ent_get_scale();
120
            scale /= 1.1;
121
            if (scale >= MIN_SCALE) ent_set_scale(scale);
122
        }
123
124 222 up20180655
        //printf("SCALE: %d\n", (int)(ent_get_scale()*1000));
125 200 up20180655
    }
126
127
    last_plus = key_presses.plus_pressed;
128
    last_minus = key_presses.minus_pressed;
129
}
130
131 171 up20180655
static int32_t mouse_x = 0, mouse_y = 0;
132
133 231 up20180655
void (update_mouse)(struct packet *p) {
134 171 up20180655
    mouse_x = max(0, mouse_x + p->delta_x);
135
    mouse_x = min(mouse_x, graph_get_XRes() - 1);
136
137 173 up20180655
    mouse_y = max(0, mouse_y - p->delta_y);
138 171 up20180655
    mouse_y = min(mouse_y, graph_get_YRes() - 1);
139 231 up20180655
140
    key_presses.lb_pressed = p->lb;
141 171 up20180655
}
142
143 231 up20180655
keys_t* (get_key_presses)(void) {
144
    return &key_presses;
145
}
146
147 171 up20180655
int32_t get_mouse_X(void) { return mouse_x; }
148
149
int32_t get_mouse_Y(void) { return mouse_y; }
150
151 201 up20180642
double get_mouse_angle(gunner_t *p) {
152
    return atan2(gunner_get_y_screen(p) - mouse_y, mouse_x - gunner_get_x_screen(p));
153 167 up20180655
}
154
155
int get_hor_movement(void) { return hor_mov; }
156
157
int get_ver_movement(void) { return ver_mov; }