Project

General

Profile

Statistics
| Revision:

root / proj / src / proj_func.c @ 257

History | View | Annotate | Download (12.5 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 "rectangle.h"
14
#include "font.h"
15

    
16
#include "kbc_macros.h"
17

    
18
#include <math.h>
19

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

    
27
    return r;
28
}
29

    
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;    update_scale();        break;
46
        case PLUS_BREAK_CODE    : key_presses.plus_pressed  = 0;    update_scale();        break;
47
        case MINUS_MAKE_CODE    : key_presses.minus_pressed = 1;    update_scale();        break;
48
        case MINUS_BREAK_CODE   : key_presses.minus_pressed = 0;    update_scale();        break;
49
        }
50
    }
51
}
52

    
53
void update_movement(const map_t *map, gunner_t *p, keys_t *keys, list_t *shooter_list) {
54
    int ver_mov = keys->s_pressed - keys->w_pressed;
55
    int hor_mov = keys->d_pressed - keys->a_pressed;
56
    double x = gunner_get_x(p);
57
    double y = gunner_get_y(p);
58

    
59
    gunner_set_pos(p, x + SHOOTER_SPEED * hor_mov, y);
60
    if (map_collides_gunner(map, p)) gunner_set_pos(p, x, y);
61
    else {
62
        list_node_t *it = list_begin(shooter_list);
63
        while (it != list_end(shooter_list)) {
64
            gunner_t *p2 = *(gunner_t**)list_node_val(it);
65
            if (p != p2 && gunner_collides_gunner(p, p2)) {
66
                gunner_set_pos(p, x, y);
67
                break;
68
            }
69
            it = list_node_next(it);
70
        }
71
    }
72
    x = gunner_get_x(p);
73
    gunner_set_pos(p, x, y + SHOOTER_SPEED * ver_mov);
74
    if (map_collides_gunner(map, p)) gunner_set_pos(p, x, y);
75
    else {
76
        list_node_t *it = list_begin(shooter_list);
77
        while (it != list_end(shooter_list)) {
78
            gunner_t *p2 = *(gunner_t**)list_node_val(it);
79
            if (p != p2 && gunner_collides_gunner(p, p2)) {
80
                gunner_set_pos(p, x, y);
81
                break;
82
            }
83
            it = list_node_next(it);
84
        }
85
    }
86
}
87

    
88
void (shoot_bullet)(const gunner_t *shooter, list_t *bullet_list, const basic_sprite_t *bsp_bullet) {
89
    double angle = gunner_get_angle(shooter);
90
    double vx = -BULLET_SPEED * fm_sin(angle);
91
    double vy = -BULLET_SPEED * fm_cos(angle);
92
    bullet_t *bullet = bullet_ctor(shooter, bsp_bullet, gunner_get_x(shooter), gunner_get_y(shooter), vx, vy);
93
    list_insert(bullet_list, list_end(bullet_list), bullet);
94
}
95

    
96
void (update_game_state)(const map_t *map, list_t *shooter_list, list_t *bullet_list) {
97

    
98
    bullet_update_movement_list(bullet_list);
99

    
100
    list_node_t *bullet_it = list_begin(bullet_list);
101
    while (bullet_it != list_end(bullet_list)) {
102
        /// Collision with walls
103
        bullet_t *bullet = *(bullet_t**)list_node_val(bullet_it);
104
        if (map_collides_bullet(map, bullet)) {
105
            list_node_t *aux = list_node_next(bullet_it);
106
            /// Delete bullet
107
            bullet_dtor(list_erase(bullet_list, bullet_it));
108
            /// Advance iterator
109
            bullet_it = aux;
110
            continue;
111
        }
112
        /// Collision with shooters
113
        list_node_t *shooter_it = list_begin(shooter_list);
114
        int deleted_bullet = false;
115
        while(shooter_it != list_end(shooter_list)){
116
            gunner_t *shooter = *(gunner_t**)list_node_val(shooter_it);
117
            if(gunner_collides_bullet(shooter, bullet)) {
118
                list_node_t *aux = list_node_next(bullet_it);
119
                /// Change health
120
                gunner_set_curr_health(shooter, gunner_get_curr_health(shooter) - bullet_get_damage(bullet));
121
                if (gunner_get_curr_health(shooter) <= 0) {
122
                    gunner_set_curr_health(shooter, gunner_get_health(shooter));
123
                    get_random_spawn(map, shooter);
124
                }
125
                /// Delete bullet
126
                bullet_dtor(list_erase(bullet_list, bullet_it)); deleted_bullet = true;
127
                /// Advance iterator
128
                bullet_it = aux;
129
                break;
130
            } else shooter_it = list_node_next(shooter_it);
131
        }
132
        if(deleted_bullet) continue;
133
        /// Advance iterator if necessary
134
        bullet_it = list_node_next(bullet_it);
135
    }
136
}
137

    
138
void (get_random_spawn)(const map_t *map, gunner_t *p) {
139
    uint16_t w = map_get_width(map), h = map_get_height(map);
140
    double x, y;
141

    
142
    do {
143
        x = rand() % w;
144
        y = rand() % h;
145
        gunner_set_pos(p, x, y);
146
    } while (map_collides_gunner(map, p));
147
}
148

    
149
void update_scale(void) {
150
    static uint8_t last_plus = 0, last_minus = 0;
151
    if (key_presses.ctrl_pressed) {
152
        if (key_presses.plus_pressed && !last_plus) {
153
            double scale = ent_get_scale();
154
            scale *= 1.1;
155
            if (scale <= MAX_SCALE) ent_set_scale(scale);
156
        }
157
        else if (key_presses.minus_pressed && !last_minus) {
158
            double scale = ent_get_scale();
159
            scale /= 1.1;
160
            if (scale >= MIN_SCALE) ent_set_scale(scale);
161
        }
162

    
163
        //printf("SCALE: %d\n", (int)(ent_get_scale()*1000));
164
    }
165

    
166
    last_plus = key_presses.plus_pressed;
167
    last_minus = key_presses.minus_pressed;
168
}
169

    
170
static int32_t mouse_x = 0, mouse_y = 0;
171

    
172
void (update_mouse)(struct packet *p) {
173
    mouse_x = max(0, mouse_x + p->delta_x);
174
    mouse_x = min(mouse_x, graph_get_XRes() - 1);
175

    
176
    mouse_y = max(0, mouse_y - p->delta_y);
177
    mouse_y = min(mouse_y, graph_get_YRes() - 1);
178

    
179
    key_presses.lb_pressed = p->lb;
180
}
181

    
182
keys_t* (get_key_presses)(void) {
183
    return &key_presses;
184
}
185

    
186
int32_t* get_mouse_X(void) { return &mouse_x; }
187

    
188
int32_t* get_mouse_Y(void) { return &mouse_y; }
189

    
190
double get_mouse_angle(gunner_t *p) {
191
    return atan2(gunner_get_y_screen(p) - mouse_y, mouse_x - gunner_get_x_screen(p));
192
}
193

    
194
struct menu {
195
    rectangle_t *r0, *r1, *r2;
196
    text_t      *t0, *t1, *t2;
197
    rectangle_t *frame;
198
};
199

    
200
menu_t* (menu_ctor)(const font_t *fnt){
201
    if(fnt == NULL) return NULL;
202
    menu_t *ret = (menu_t*)malloc(sizeof(menu_t));
203
    if (ret == NULL) return NULL;
204
    ret->r0 = rectangle_ctor(0, 0, 400, 100);
205
    ret->r1 = rectangle_ctor(0, 0, 400, 100);
206
    ret->r2 = rectangle_ctor(0, 0, 400, 100);
207
    ret->t0 = text_ctor(fnt, "PLAY");
208
    ret->t1 = text_ctor(fnt, "TEST");
209
    ret->t2 = text_ctor(fnt, "EXIT");
210
    ret->frame = rectangle_ctor(0, 0, 800, 500);
211
    if (ret->r0 == NULL || ret->r1 == NULL || ret->r2 == NULL ||
212
        ret->t0 == NULL || ret->t1 == NULL || ret->t2 == NULL ||
213
        ret->frame == NULL) return NULL;
214
    // VISUAL
215
    rectangle_set_fill_color(ret->r0, GRAPH_BLACK);
216
    rectangle_set_outline_width(ret->r0, 2);
217
    rectangle_set_outline_color(ret->r0, GRAPH_WHITE);
218
    rectangle_set_fill_color(ret->r1, GRAPH_BLACK);
219
    rectangle_set_outline_width(ret->r1, 2);
220
    rectangle_set_outline_color(ret->r1, GRAPH_WHITE);
221
    rectangle_set_fill_color(ret->r2, GRAPH_BLACK);
222
    rectangle_set_outline_width(ret->r2, 2);
223
    rectangle_set_outline_color(ret->r2, GRAPH_WHITE);
224
    text_set_valign(ret->t0, text_valign_center);
225
    text_set_halign(ret->t0, text_halign_center);
226
    text_set_color(ret->t0, TEXT_COLOR);
227
    text_set_valign(ret->t1, text_valign_center);
228
    text_set_halign(ret->t1, text_halign_center);
229
    text_set_color(ret->t1, TEXT_COLOR);
230
    text_set_valign(ret->t2, text_valign_center);
231
    text_set_halign(ret->t2, text_halign_center);
232
    text_set_color(ret->t2, TEXT_COLOR);
233
    rectangle_set_fill_color(ret->frame, GRAPH_BLACK);
234
    rectangle_set_outline_width(ret->frame, 6);
235
    rectangle_set_outline_color(ret->frame, GRAPH_WHITE);
236
    // POSITIONS
237
    rectangle_set_pos(ret->r0,
238
                      graph_get_XRes()/2    - rectangle_get_w(ret->r0)/2,
239
                      graph_get_YRes()*0.35 - rectangle_get_h(ret->r0)/2);
240

    
241

    
242
    rectangle_set_pos(ret->r1,
243
                    graph_get_XRes()/2    - rectangle_get_w(ret->r1)/2,
244
                    graph_get_YRes()*0.5 - rectangle_get_h(ret->r1)/2);
245

    
246

    
247
    rectangle_set_pos(ret->r2,
248
                  graph_get_XRes()/2    - rectangle_get_w(ret->r2)/2,
249
                  graph_get_YRes()*0.65 - rectangle_get_h(ret->r2)/2);
250

    
251
    text_set_pos(ret->t0, rectangle_get_x(ret->r0)+rectangle_get_w(ret->r0)/2,
252
                          rectangle_get_y(ret->r0)+rectangle_get_h(ret->r0)/2);
253

    
254
    text_set_pos(ret->t1, rectangle_get_x(ret->r1)+rectangle_get_w(ret->r1)/2,
255
                          rectangle_get_y(ret->r1)+rectangle_get_h(ret->r1)/2);
256

    
257
    text_set_pos(ret->t2, rectangle_get_x(ret->r2)+rectangle_get_w(ret->r2)/2,
258
                          rectangle_get_y(ret->r2)+rectangle_get_h(ret->r2)/2);
259

    
260
    rectangle_set_pos(ret->frame,
261
                  graph_get_XRes()/2    - rectangle_get_w(ret->frame)/2,
262
                  graph_get_YRes()*0.5 - rectangle_get_h(ret->frame)/2);
263
    return ret;
264
}
265

    
266
static int highlighted = -1;
267

    
268
int (menu_update_state)(menu_t *menu, int click) {
269

    
270
    if (rectangle_collide_point(menu->r0, mouse_x, mouse_y)) {
271
        highlighted = GAME;
272
        if (click) return GAME;
273
    } else if (rectangle_collide_point(menu->r1, mouse_x, mouse_y)) {
274
        highlighted = TEST;
275
        if (click) return TEST;
276
    } else if (rectangle_collide_point(menu->r2, mouse_x, mouse_y)) {
277
        highlighted = EXIT;
278
        if (click) return EXIT;
279
    } else {
280
        highlighted = -1;
281
    }
282
    return MENU;
283
}
284

    
285
void (menu_draw)(menu_t *menu) {
286
    rectangle_draw(menu->frame);
287
    switch (highlighted) {
288
    case GAME:
289
        rectangle_set_fill_color(menu->r0, HIGHLIGHT_COLOR);
290
        rectangle_draw(menu->r0);
291
        rectangle_draw(menu->r1);
292
        rectangle_draw(menu->r2);
293
        rectangle_set_fill_color(menu->r0, GRAPH_BLACK);
294
        break;
295
    case TEST:
296
        rectangle_set_fill_color(menu->r1, HIGHLIGHT_COLOR);
297
        rectangle_draw(menu->r0);
298
        rectangle_draw(menu->r1);
299
        rectangle_draw(menu->r2);
300
        rectangle_set_fill_color(menu->r1, GRAPH_BLACK);
301
        break;
302
    case EXIT:
303
        rectangle_set_fill_color(menu->r2, HIGHLIGHT_COLOR);
304
        rectangle_draw(menu->r0);
305
        rectangle_draw(menu->r1);
306
        rectangle_draw(menu->r2);
307
        rectangle_set_fill_color(menu->r2, GRAPH_BLACK);
308
        break;
309
    default:
310
        rectangle_draw(menu->r0);
311
        rectangle_draw(menu->r1);
312
        rectangle_draw(menu->r2);
313
        break;
314
    }
315
    text_draw(menu->t0);
316
    text_draw(menu->t1);
317
    text_draw(menu->t2);
318
}
319

    
320
void (menu_dtor)(menu_t *p){
321
    rectangle_dtor(p->r0);
322
    rectangle_dtor(p->r1);
323
    rectangle_dtor(p->r2);
324
    rectangle_dtor(p->frame);
325
    text_dtor(p->t0);
326
    text_dtor(p->t1);
327
    text_dtor(p->t2);
328
    free(p);
329
}
330

    
331
text_timer_t* (timer_ctor)(const font_t *fnt){
332
    if(fnt == NULL) return NULL;
333
    text_timer_t *ret = malloc(sizeof(timer_t));
334
    if (ret == NULL) return NULL;
335
    ret->time = 0;
336
    ret->text = text_ctor(fnt, "000s");
337
    ret->array = text_get_string(ret->text);
338
    text_set_color(ret->text, TEXT_COLOR);
339
    return ret;
340
}
341

    
342
void (timer_update)(text_timer_t *p){
343
    if (p->time >= 999) return;
344
    p->time++;
345
    p->array[2] = p->time % 10 + '0';
346
    p->array[1] = (p->time/10) % 10 + '0';
347
    p->array[0] = (p->time/100) % 10 + '0';
348
}
349

    
350
void (timer_reset)(text_timer_t *p){
351
    p->time = 0;
352
    p->array[2] = '0';
353
    p->array[1] = '0';
354
    p->array[0] = '0';
355
}
356

    
357
void (timer_dtor)(text_timer_t *p){
358
    if (p == NULL) return;
359
    text_dtor(p->text);
360
    free(p);
361
}