Project

General

Profile

Statistics
| Revision:

root / proj / src / proj_func.c @ 236

History | View | Annotate | Download (5.94 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, gunner_t *p2) {
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) || gunner_collides_gunner(p, p2)) {
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) || gunner_collides_gunner(p, p2)) {
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(shooter, 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, list_t *shooter_list, list_t *bullet_list) {
78

    
79
    bullet_update_movement_list(bullet_list);
80

    
81
    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
        if (map_collides_bullet(map, bullet)) {
86
            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
            continue;
92
        }
93
        /// 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
        }
113
        if(deleted_bullet) continue;
114
        /// Advance iterator if necessary
115
        bullet_it = list_node_next(bullet_it);
116
    }
117
}
118

    
119
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
        //printf("SCALE: %d\n", (int)(ent_get_scale()*1000));
134
    }
135

    
136
    last_plus = key_presses.plus_pressed;
137
    last_minus = key_presses.minus_pressed;
138
}
139

    
140
static int32_t mouse_x = 0, mouse_y = 0;
141

    
142
void (update_mouse)(struct packet *p) {
143
    mouse_x = max(0, mouse_x + p->delta_x);
144
    mouse_x = min(mouse_x, graph_get_XRes() - 1);
145

    
146
    mouse_y = max(0, mouse_y - p->delta_y);
147
    mouse_y = min(mouse_y, graph_get_YRes() - 1);
148

    
149
    key_presses.lb_pressed = p->lb;
150
}
151

    
152
keys_t* (get_key_presses)(void) {
153
    return &key_presses;
154
}
155

    
156
int32_t get_mouse_X(void) { return mouse_x; }
157

    
158
int32_t get_mouse_Y(void) { return mouse_y; }
159

    
160
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
}
163

    
164
int get_hor_movement(void) { return hor_mov; }
165

    
166
int get_ver_movement(void) { return ver_mov; }