Project

General

Profile

Statistics
| Revision:

root / proj / src / proj_func.c @ 290

History | View | Annotate | Download (12.7 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 251 up20180655
#include "rectangle.h"
14
#include "font.h"
15 156 up20180655
16 167 up20180655
#include <math.h>
17
18 156 up20180655
int cleanup(void) {
19
    int r = SUCCESS;
20
    if ((r = unsubscribe_all()))
21
        printf("%s: failed to unsubscribe drivers.\n", __func__);
22 166 up20180642
    if ((r = graph_cleanup()))
23
        printf("%s: graph cleanup failed\n", __func__);
24 156 up20180655
25
    return r;
26
}
27 167 up20180655
28 200 up20180655
static keys_t key_presses;
29 167 up20180655
30 184 up20180655
void update_key_presses(void) {
31 167 up20180655
    if (sz == 1) {
32
        switch(scancode[0]) {
33 200 up20180655
        case W_MAKE_CODE        : key_presses.w_pressed     = 1;        break;
34
        case W_BREAK_CODE       : key_presses.w_pressed     = 0;        break;
35
        case A_MAKE_CODE        : key_presses.a_pressed     = 1;        break;
36
        case A_BREAK_CODE       : key_presses.a_pressed     = 0;        break;
37
        case S_MAKE_CODE        : key_presses.s_pressed     = 1;        break;
38
        case S_BREAK_CODE       : key_presses.s_pressed     = 0;        break;
39
        case D_MAKE_CODE        : key_presses.d_pressed     = 1;        break;
40
        case D_BREAK_CODE       : key_presses.d_pressed     = 0;        break;
41
        case CTRL_MAKE_CODE     : key_presses.ctrl_pressed  = 1;        break;
42
        case CTRL_BREAK_CODE    : key_presses.ctrl_pressed  = 0;        break;
43 250 up20180655
        case PLUS_MAKE_CODE     : key_presses.plus_pressed  = 1;    update_scale();        break;
44
        case PLUS_BREAK_CODE    : key_presses.plus_pressed  = 0;    update_scale();        break;
45
        case MINUS_MAKE_CODE    : key_presses.minus_pressed = 1;    update_scale();        break;
46
        case MINUS_BREAK_CODE   : key_presses.minus_pressed = 0;    update_scale();        break;
47 167 up20180655
        }
48
    }
49
}
50 240 up20180655
51 246 up20180655
void update_movement(const map_t *map, gunner_t *p, keys_t *keys, list_t *shooter_list) {
52
    int ver_mov = keys->s_pressed - keys->w_pressed;
53
    int hor_mov = keys->d_pressed - keys->a_pressed;
54 240 up20180655
    double x = gunner_get_x(p);
55
    double y = gunner_get_y(p);
56 246 up20180655
57 240 up20180655
    gunner_set_pos(p, x + SHOOTER_SPEED * hor_mov, y);
58 246 up20180655
    if (map_collides_gunner(map, p)) gunner_set_pos(p, x, y);
59
    else {
60
        list_node_t *it = list_begin(shooter_list);
61
        while (it != list_end(shooter_list)) {
62
            gunner_t *p2 = *(gunner_t**)list_node_val(it);
63
            if (p != p2 && gunner_collides_gunner(p, p2)) {
64
                gunner_set_pos(p, x, y);
65
                break;
66
            }
67
            it = list_node_next(it);
68
        }
69 240 up20180655
    }
70 246 up20180655
    x = gunner_get_x(p);
71 240 up20180655
    gunner_set_pos(p, x, y + SHOOTER_SPEED * ver_mov);
72 246 up20180655
    if (map_collides_gunner(map, p)) gunner_set_pos(p, x, y);
73
    else {
74
        list_node_t *it = list_begin(shooter_list);
75
        while (it != list_end(shooter_list)) {
76
            gunner_t *p2 = *(gunner_t**)list_node_val(it);
77
            if (p != p2 && gunner_collides_gunner(p, p2)) {
78
                gunner_set_pos(p, x, y);
79
                break;
80
            }
81
            it = list_node_next(it);
82
        }
83 222 up20180655
    }
84 184 up20180655
}
85 240 up20180655
86 237 up20180642
void (shoot_bullet)(const gunner_t *shooter, list_t *bullet_list, const basic_sprite_t *bsp_bullet) {
87 231 up20180655
    double angle = gunner_get_angle(shooter);
88
    double vx = -BULLET_SPEED * fm_sin(angle);
89
    double vy = -BULLET_SPEED * fm_cos(angle);
90 237 up20180642
    bullet_t *bullet = bullet_ctor(shooter, bsp_bullet, gunner_get_x(shooter), gunner_get_y(shooter), vx, vy);
91 231 up20180655
    list_insert(bullet_list, list_end(bullet_list), bullet);
92
}
93
94 236 up20180642
void (update_game_state)(const map_t *map, list_t *shooter_list, list_t *bullet_list) {
95 231 up20180655
96
    bullet_update_movement_list(bullet_list);
97
98 236 up20180642
    list_node_t *bullet_it = list_begin(bullet_list);
99
    while (bullet_it != list_end(bullet_list)) {
100
        /// Collision with walls
101
        bullet_t *bullet = *(bullet_t**)list_node_val(bullet_it);
102 231 up20180655
        if (map_collides_bullet(map, bullet)) {
103 236 up20180642
            list_node_t *aux = list_node_next(bullet_it);
104
            /// Delete bullet
105
            bullet_dtor(list_erase(bullet_list, bullet_it));
106
            /// Advance iterator
107
            bullet_it = aux;
108 231 up20180655
            continue;
109
        }
110 236 up20180642
        /// Collision with shooters
111
        list_node_t *shooter_it = list_begin(shooter_list);
112
        int deleted_bullet = false;
113
        while(shooter_it != list_end(shooter_list)){
114
            gunner_t *shooter = *(gunner_t**)list_node_val(shooter_it);
115
            if(gunner_collides_bullet(shooter, bullet)) {
116
                list_node_t *aux = list_node_next(bullet_it);
117
                /// Change health
118
                gunner_set_curr_health(shooter, gunner_get_curr_health(shooter) - bullet_get_damage(bullet));
119
                if (gunner_get_curr_health(shooter) <= 0) {
120 246 up20180655
                    gunner_set_curr_health(shooter, gunner_get_health(shooter));
121
                    get_random_spawn(map, shooter);
122 236 up20180642
                }
123
                /// Delete bullet
124
                bullet_dtor(list_erase(bullet_list, bullet_it)); deleted_bullet = true;
125
                /// Advance iterator
126
                bullet_it = aux;
127
                break;
128
            } else shooter_it = list_node_next(shooter_it);
129 231 up20180655
        }
130 236 up20180642
        if(deleted_bullet) continue;
131
        /// Advance iterator if necessary
132
        bullet_it = list_node_next(bullet_it);
133 231 up20180655
    }
134
}
135
136 246 up20180655
void (get_random_spawn)(const map_t *map, gunner_t *p) {
137
    uint16_t w = map_get_width(map), h = map_get_height(map);
138
    double x, y;
139
140
    do {
141
        x = rand() % w;
142
        y = rand() % h;
143
        gunner_set_pos(p, x, y);
144
    } while (map_collides_gunner(map, p));
145
}
146
147 200 up20180655
void update_scale(void) {
148
    static uint8_t last_plus = 0, last_minus = 0;
149
    if (key_presses.ctrl_pressed) {
150
        if (key_presses.plus_pressed && !last_plus) {
151
            double scale = ent_get_scale();
152
            scale *= 1.1;
153
            if (scale <= MAX_SCALE) ent_set_scale(scale);
154
        }
155
        else if (key_presses.minus_pressed && !last_minus) {
156
            double scale = ent_get_scale();
157
            scale /= 1.1;
158
            if (scale >= MIN_SCALE) ent_set_scale(scale);
159
        }
160
161 222 up20180655
        //printf("SCALE: %d\n", (int)(ent_get_scale()*1000));
162 200 up20180655
    }
163
164
    last_plus = key_presses.plus_pressed;
165
    last_minus = key_presses.minus_pressed;
166
}
167
168 171 up20180655
static int32_t mouse_x = 0, mouse_y = 0;
169
170 231 up20180655
void (update_mouse)(struct packet *p) {
171 171 up20180655
    mouse_x = max(0, mouse_x + p->delta_x);
172
    mouse_x = min(mouse_x, graph_get_XRes() - 1);
173
174 173 up20180655
    mouse_y = max(0, mouse_y - p->delta_y);
175 171 up20180655
    mouse_y = min(mouse_y, graph_get_YRes() - 1);
176 231 up20180655
177
    key_presses.lb_pressed = p->lb;
178 171 up20180655
}
179
180 231 up20180655
keys_t* (get_key_presses)(void) {
181
    return &key_presses;
182
}
183
184 250 up20180655
int32_t* get_mouse_X(void) { return &mouse_x; }
185 171 up20180655
186 250 up20180655
int32_t* get_mouse_Y(void) { return &mouse_y; }
187 171 up20180655
188 201 up20180642
double get_mouse_angle(gunner_t *p) {
189
    return atan2(gunner_get_y_screen(p) - mouse_y, mouse_x - gunner_get_x_screen(p));
190 167 up20180655
}
191 250 up20180655
192 251 up20180655
struct menu {
193
    rectangle_t *r0, *r1, *r2;
194
    text_t      *t0, *t1, *t2;
195
    rectangle_t *frame;
196
};
197
198
menu_t* (menu_ctor)(const font_t *fnt){
199
    if(fnt == NULL) return NULL;
200
    menu_t *ret = (menu_t*)malloc(sizeof(menu_t));
201
    if (ret == NULL) return NULL;
202
    ret->r0 = rectangle_ctor(0, 0, 400, 100);
203
    ret->r1 = rectangle_ctor(0, 0, 400, 100);
204
    ret->r2 = rectangle_ctor(0, 0, 400, 100);
205
    ret->t0 = text_ctor(fnt, "PLAY");
206
    ret->t1 = text_ctor(fnt, "TEST");
207
    ret->t2 = text_ctor(fnt, "EXIT");
208
    ret->frame = rectangle_ctor(0, 0, 800, 500);
209
    if (ret->r0 == NULL || ret->r1 == NULL || ret->r2 == NULL ||
210
        ret->t0 == NULL || ret->t1 == NULL || ret->t2 == NULL ||
211
        ret->frame == NULL) return NULL;
212
    // VISUAL
213 253 up20180642
    rectangle_set_fill_color(ret->r0, GRAPH_BLACK);
214 251 up20180655
    rectangle_set_outline_width(ret->r0, 2);
215 253 up20180642
    rectangle_set_outline_color(ret->r0, GRAPH_WHITE);
216 290 up20180655
    rectangle_set_fill_trans(ret->r0, GRAPH_TRANSPARENT);
217 253 up20180642
    rectangle_set_fill_color(ret->r1, GRAPH_BLACK);
218 251 up20180655
    rectangle_set_outline_width(ret->r1, 2);
219 253 up20180642
    rectangle_set_outline_color(ret->r1, GRAPH_WHITE);
220 290 up20180655
    rectangle_set_fill_trans(ret->r1, GRAPH_TRANSPARENT);
221 253 up20180642
    rectangle_set_fill_color(ret->r2, GRAPH_BLACK);
222 251 up20180655
    rectangle_set_outline_width(ret->r2, 2);
223 253 up20180642
    rectangle_set_outline_color(ret->r2, GRAPH_WHITE);
224 290 up20180655
    rectangle_set_fill_trans(ret->r2, GRAPH_TRANSPARENT);
225 251 up20180655
    text_set_valign(ret->t0, text_valign_center);
226
    text_set_halign(ret->t0, text_halign_center);
227
    text_set_color(ret->t0, TEXT_COLOR);
228
    text_set_valign(ret->t1, text_valign_center);
229
    text_set_halign(ret->t1, text_halign_center);
230
    text_set_color(ret->t1, TEXT_COLOR);
231
    text_set_valign(ret->t2, text_valign_center);
232
    text_set_halign(ret->t2, text_halign_center);
233
    text_set_color(ret->t2, TEXT_COLOR);
234 253 up20180642
    rectangle_set_fill_color(ret->frame, GRAPH_BLACK);
235 251 up20180655
    rectangle_set_outline_width(ret->frame, 6);
236 253 up20180642
    rectangle_set_outline_color(ret->frame, GRAPH_WHITE);
237 290 up20180655
    rectangle_set_fill_trans(ret->frame, GRAPH_TRANSPARENT);
238 251 up20180655
    // POSITIONS
239
    rectangle_set_pos(ret->r0,
240
                      graph_get_XRes()/2    - rectangle_get_w(ret->r0)/2,
241
                      graph_get_YRes()*0.35 - rectangle_get_h(ret->r0)/2);
242
243
244
    rectangle_set_pos(ret->r1,
245
                    graph_get_XRes()/2    - rectangle_get_w(ret->r1)/2,
246
                    graph_get_YRes()*0.5 - rectangle_get_h(ret->r1)/2);
247
248
249
    rectangle_set_pos(ret->r2,
250
                  graph_get_XRes()/2    - rectangle_get_w(ret->r2)/2,
251
                  graph_get_YRes()*0.65 - rectangle_get_h(ret->r2)/2);
252
253
    text_set_pos(ret->t0, rectangle_get_x(ret->r0)+rectangle_get_w(ret->r0)/2,
254
                          rectangle_get_y(ret->r0)+rectangle_get_h(ret->r0)/2);
255
256
    text_set_pos(ret->t1, rectangle_get_x(ret->r1)+rectangle_get_w(ret->r1)/2,
257
                          rectangle_get_y(ret->r1)+rectangle_get_h(ret->r1)/2);
258
259
    text_set_pos(ret->t2, rectangle_get_x(ret->r2)+rectangle_get_w(ret->r2)/2,
260
                          rectangle_get_y(ret->r2)+rectangle_get_h(ret->r2)/2);
261
262
    rectangle_set_pos(ret->frame,
263
                  graph_get_XRes()/2    - rectangle_get_w(ret->frame)/2,
264
                  graph_get_YRes()*0.5 - rectangle_get_h(ret->frame)/2);
265
    return ret;
266
}
267
268
static int highlighted = -1;
269
270
int (menu_update_state)(menu_t *menu, int click) {
271
272
    if (rectangle_collide_point(menu->r0, mouse_x, mouse_y)) {
273
        highlighted = GAME;
274
        if (click) return GAME;
275
    } else if (rectangle_collide_point(menu->r1, mouse_x, mouse_y)) {
276
        highlighted = TEST;
277
        if (click) return TEST;
278
    } else if (rectangle_collide_point(menu->r2, mouse_x, mouse_y)) {
279
        highlighted = EXIT;
280
        if (click) return EXIT;
281
    } else {
282
        highlighted = -1;
283
    }
284
    return MENU;
285
}
286
287
void (menu_draw)(menu_t *menu) {
288
    rectangle_draw(menu->frame);
289
    switch (highlighted) {
290
    case GAME:
291
        rectangle_set_fill_color(menu->r0, HIGHLIGHT_COLOR);
292
        rectangle_draw(menu->r0);
293
        rectangle_draw(menu->r1);
294
        rectangle_draw(menu->r2);
295 253 up20180642
        rectangle_set_fill_color(menu->r0, GRAPH_BLACK);
296 251 up20180655
        break;
297
    case TEST:
298
        rectangle_set_fill_color(menu->r1, HIGHLIGHT_COLOR);
299
        rectangle_draw(menu->r0);
300
        rectangle_draw(menu->r1);
301
        rectangle_draw(menu->r2);
302 253 up20180642
        rectangle_set_fill_color(menu->r1, GRAPH_BLACK);
303 251 up20180655
        break;
304
    case EXIT:
305
        rectangle_set_fill_color(menu->r2, HIGHLIGHT_COLOR);
306
        rectangle_draw(menu->r0);
307
        rectangle_draw(menu->r1);
308
        rectangle_draw(menu->r2);
309 253 up20180642
        rectangle_set_fill_color(menu->r2, GRAPH_BLACK);
310 251 up20180655
        break;
311
    default:
312
        rectangle_draw(menu->r0);
313
        rectangle_draw(menu->r1);
314
        rectangle_draw(menu->r2);
315
        break;
316
    }
317
    text_draw(menu->t0);
318
    text_draw(menu->t1);
319
    text_draw(menu->t2);
320
}
321
322
void (menu_dtor)(menu_t *p){
323
    rectangle_dtor(p->r0);
324
    rectangle_dtor(p->r1);
325
    rectangle_dtor(p->r2);
326
    rectangle_dtor(p->frame);
327
    text_dtor(p->t0);
328
    text_dtor(p->t1);
329
    text_dtor(p->t2);
330
    free(p);
331
}
332
333 250 up20180655
text_timer_t* (timer_ctor)(const font_t *fnt){
334
    if(fnt == NULL) return NULL;
335
    text_timer_t *ret = malloc(sizeof(timer_t));
336
    if (ret == NULL) return NULL;
337
    ret->time = 0;
338
    ret->text = text_ctor(fnt, "000s");
339
    ret->array = text_get_string(ret->text);
340 251 up20180655
    text_set_color(ret->text, TEXT_COLOR);
341 250 up20180655
    return ret;
342
}
343
344
void (timer_update)(text_timer_t *p){
345
    if (p->time >= 999) return;
346
    p->time++;
347
    p->array[2] = p->time % 10 + '0';
348
    p->array[1] = (p->time/10) % 10 + '0';
349
    p->array[0] = (p->time/100) % 10 + '0';
350
}
351
352 251 up20180655
void (timer_reset)(text_timer_t *p){
353
    p->time = 0;
354
    p->array[2] = '0';
355
    p->array[1] = '0';
356
    p->array[0] = '0';
357
}
358
359 250 up20180655
void (timer_dtor)(text_timer_t *p){
360
    if (p == NULL) return;
361
    text_dtor(p->text);
362
    free(p);
363
}