Project

General

Profile

Statistics
| Revision:

root / proj / src / proj_func.c @ 231

History | View | Annotate | Download (5.02 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
#include "bullet.h"
14

    
15
#include "kbc_macros.h"
16

    
17
#include <math.h>
18

    
19
int cleanup(void) {
20
    int r = SUCCESS;
21
    if ((r = unsubscribe_all()))
22
        printf("%s: failed to unsubscribe drivers.\n", __func__);
23
    if ((r = graph_cleanup()))
24
        printf("%s: graph cleanup failed\n", __func__);
25

    
26
    return r;
27
}
28

    
29
static int hor_mov = REST, ver_mov = REST;
30
static keys_t key_presses;
31

    
32
void update_key_presses(void) {
33
    if (sz == 1) {
34
        switch(scancode[0]) {
35
        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
        }
50
    }
51
    ver_mov = key_presses.s_pressed - key_presses.w_pressed;
52
    hor_mov = key_presses.d_pressed - key_presses.a_pressed;
53
}
54

    
55
void update_movement(const map_t *map, gunner_t *p) {
56
    double x = gunner_get_x(p);
57
    double y = gunner_get_y(p);
58
    gunner_set_pos(p, x + SHOOTER_SPEED * hor_mov, y);
59
    if (map_collides_gunner(map, p)) {
60
        gunner_set_pos(p, x, y);
61
    }
62
    x = gunner_get_x(p);
63
    gunner_set_pos(p, x, y + SHOOTER_SPEED * ver_mov);
64
    if (map_collides_gunner(map, p)) {
65
        gunner_set_pos(p, x, y);
66
    }
67
}
68

    
69
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
            free(bullet);
97
            it = aux;
98
            continue;
99
        }
100

    
101
        it = list_node_next(it);
102
    }
103
}
104

    
105
void update_scale(void) {
106
    static uint8_t last_plus = 0, last_minus = 0;
107
    if (key_presses.ctrl_pressed) {
108
        if (key_presses.plus_pressed && !last_plus) {
109
            double scale = ent_get_scale();
110
            scale *= 1.1;
111
            if (scale <= MAX_SCALE) ent_set_scale(scale);
112
        }
113
        else if (key_presses.minus_pressed && !last_minus) {
114
            double scale = ent_get_scale();
115
            scale /= 1.1;
116
            if (scale >= MIN_SCALE) ent_set_scale(scale);
117
        }
118

    
119
        //printf("SCALE: %d\n", (int)(ent_get_scale()*1000));
120
    }
121

    
122
    last_plus = key_presses.plus_pressed;
123
    last_minus = key_presses.minus_pressed;
124
}
125

    
126
static int32_t mouse_x = 0, mouse_y = 0;
127

    
128
void (update_mouse)(struct packet *p) {
129
    mouse_x = max(0, mouse_x + p->delta_x);
130
    mouse_x = min(mouse_x, graph_get_XRes() - 1);
131

    
132
    mouse_y = max(0, mouse_y - p->delta_y);
133
    mouse_y = min(mouse_y, graph_get_YRes() - 1);
134

    
135
    key_presses.lb_pressed = p->lb;
136
}
137

    
138
keys_t* (get_key_presses)(void) {
139
    return &key_presses;
140
}
141

    
142
int32_t get_mouse_X(void) { return mouse_x; }
143

    
144
int32_t get_mouse_Y(void) { return mouse_y; }
145

    
146
double get_mouse_angle(gunner_t *p) {
147
    return atan2(gunner_get_y_screen(p) - mouse_y, mouse_x - gunner_get_x_screen(p));
148
}
149

    
150
int get_hor_movement(void) { return hor_mov; }
151

    
152
int get_ver_movement(void) { return ver_mov; }