Project

General

Profile

Statistics
| Revision:

root / proj / src / proj_func.c @ 236

History | View | Annotate | Download (5.94 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 236 up20180642
    bullet_t *bullet = bullet_ctor(shooter, get_bullet(), gunner_get_x(shooter), gunner_get_y(shooter), vx, vy);
74 231 up20180655
    list_insert(bullet_list, list_end(bullet_list), bullet);
75
}
76
77 236 up20180642
void (update_game_state)(const map_t *map, list_t *shooter_list, list_t *bullet_list) {
78 231 up20180655
79
    bullet_update_movement_list(bullet_list);
80
81 236 up20180642
    list_node_t *bullet_it = list_begin(bullet_list);
82
    while (bullet_it != list_end(bullet_list)) {
83
        /// Collision with walls
84
        bullet_t *bullet = *(bullet_t**)list_node_val(bullet_it);
85 231 up20180655
        if (map_collides_bullet(map, bullet)) {
86 236 up20180642
            list_node_t *aux = list_node_next(bullet_it);
87
            /// Delete bullet
88
            bullet_dtor(list_erase(bullet_list, bullet_it));
89
            /// Advance iterator
90
            bullet_it = aux;
91 231 up20180655
            continue;
92
        }
93 236 up20180642
        /// Collision with shooters
94
        list_node_t *shooter_it = list_begin(shooter_list);
95
        int deleted_bullet = false;
96
        while(shooter_it != list_end(shooter_list)){
97
            gunner_t *shooter = *(gunner_t**)list_node_val(shooter_it);
98
            if(gunner_collides_bullet(shooter, bullet)) {
99
                list_node_t *aux = list_node_next(bullet_it);
100
                /// Change health
101
                gunner_set_curr_health(shooter, gunner_get_curr_health(shooter) - bullet_get_damage(bullet));
102
                if (gunner_get_curr_health(shooter) <= 0) {
103
                    shooter = list_erase(shooter_list, shooter_it);
104
                    gunner_dtor(shooter);
105
                }
106
                /// Delete bullet
107
                bullet_dtor(list_erase(bullet_list, bullet_it)); deleted_bullet = true;
108
                /// Advance iterator
109
                bullet_it = aux;
110
                break;
111
            } else shooter_it = list_node_next(shooter_it);
112 231 up20180655
        }
113 236 up20180642
        if(deleted_bullet) continue;
114
        /// Advance iterator if necessary
115
        bullet_it = list_node_next(bullet_it);
116 231 up20180655
    }
117
}
118
119 200 up20180655
void update_scale(void) {
120
    static uint8_t last_plus = 0, last_minus = 0;
121
    if (key_presses.ctrl_pressed) {
122
        if (key_presses.plus_pressed && !last_plus) {
123
            double scale = ent_get_scale();
124
            scale *= 1.1;
125
            if (scale <= MAX_SCALE) ent_set_scale(scale);
126
        }
127
        else if (key_presses.minus_pressed && !last_minus) {
128
            double scale = ent_get_scale();
129
            scale /= 1.1;
130
            if (scale >= MIN_SCALE) ent_set_scale(scale);
131
        }
132
133 222 up20180655
        //printf("SCALE: %d\n", (int)(ent_get_scale()*1000));
134 200 up20180655
    }
135
136
    last_plus = key_presses.plus_pressed;
137
    last_minus = key_presses.minus_pressed;
138
}
139
140 171 up20180655
static int32_t mouse_x = 0, mouse_y = 0;
141
142 231 up20180655
void (update_mouse)(struct packet *p) {
143 171 up20180655
    mouse_x = max(0, mouse_x + p->delta_x);
144
    mouse_x = min(mouse_x, graph_get_XRes() - 1);
145
146 173 up20180655
    mouse_y = max(0, mouse_y - p->delta_y);
147 171 up20180655
    mouse_y = min(mouse_y, graph_get_YRes() - 1);
148 231 up20180655
149
    key_presses.lb_pressed = p->lb;
150 171 up20180655
}
151
152 231 up20180655
keys_t* (get_key_presses)(void) {
153
    return &key_presses;
154
}
155
156 171 up20180655
int32_t get_mouse_X(void) { return mouse_x; }
157
158
int32_t get_mouse_Y(void) { return mouse_y; }
159
160 201 up20180642
double get_mouse_angle(gunner_t *p) {
161
    return atan2(gunner_get_y_screen(p) - mouse_y, mouse_x - gunner_get_x_screen(p));
162 167 up20180655
}
163
164
int get_hor_movement(void) { return hor_mov; }
165
166
int get_ver_movement(void) { return ver_mov; }