Project

General

Profile

Statistics
| Revision:

root / proj / src / ent.c @ 308

History | View | Annotate | Download (13.3 KB)

1 193 up20180642
#include <lcom/lcf.h>
2
3
#include "ent.h"
4
5
#include "graph.h"
6 226 up20180642
#include "utils.h"
7 229 up20180655
#include "rectangle.h"
8 307 up20180642
#include "font.h"
9
#include "errors.h"
10
#include "queue.h"
11 220 up20180655
#include <math.h>
12
13 307 up20180642
#define GREEN_HEALTH_COLOR      0x009900
14
15 193 up20180642
static double scale = 1.0;
16
static int16_t x_origin = 0;
17
static int16_t y_origin = 0;
18
19
void (ent_set_scale) (double n){ scale = n; }
20
void (ent_set_origin)(double x, double y){ x_origin = x; y_origin = y; }
21 194 up20180642
double (ent_get_scale)  (void){ return scale; }
22 193 up20180642
double (ent_get_XLength)(void){ return graph_get_XRes()/scale; }
23
double (ent_get_YLength)(void){ return graph_get_YRes()/scale; }
24
25 201 up20180642
struct gunner{
26 203 up20180642
    double x, y; //real position
27 232 up20180655
    double spawn_x, spawn_y;
28 193 up20180642
    sprite_t *dude;
29
    sprite_t *weapon;
30 267 up20180655
    double health, current_health;
31 307 up20180642
    text_t *txt;
32 301 up20180642
    gunner_type type;
33 302 up20180642
    int team;
34 193 up20180642
};
35 302 up20180642
gunner_t* (gunner_ctor)(basic_sprite_t *dude, basic_sprite_t *weapon, gunner_type type, int team){
36 201 up20180642
    gunner_t *ret = malloc(sizeof(gunner_t));
37 193 up20180642
    if(ret == NULL) return NULL;
38 232 up20180655
    ret->spawn_x = 0.0;
39
    ret->spawn_y = 0.0;
40 193 up20180642
    ret->x = 0.0;
41
    ret->y = 0.0;
42 229 up20180655
    ret->health = 100;
43 230 up20180655
    ret->current_health = ret->health;
44 307 up20180642
    ret->txt = text_ctor(consolas, "");
45 302 up20180642
    ret->type = type;
46
    ret->team = team;
47 193 up20180642
    ret->dude   = sprite_ctor(dude  );
48
    ret->weapon = sprite_ctor(weapon);
49
    if(ret->dude == NULL || ret->weapon == NULL){
50 201 up20180642
        gunner_dtor(ret);
51 193 up20180642
        return NULL;
52 307 up20180642
    }
53
    text_set_size(ret->txt, 15);
54
    text_set_valign(ret->txt, text_valign_center);
55
    text_set_halign(ret->txt, text_halign_center);
56
    text_set_color(ret->txt, GRAPH_WHITE);
57
    return ret;
58 193 up20180642
}
59 201 up20180642
void (gunner_dtor)(gunner_t *p){
60 193 up20180642
    if(p == NULL) return;
61
    sprite_dtor(p->dude);
62
    sprite_dtor(p->weapon);
63 307 up20180642
    text_dtor(p->txt);
64 193 up20180642
    free(p);
65
}
66 216 up20180642
void (gunner_set_pos)  (gunner_t *p, double x, double y){ p->x = x; p->y = y; }
67 232 up20180655
void (gunner_set_spawn)  (gunner_t *p, double x, double y){ p->spawn_x = x; p->spawn_y = y; }
68 216 up20180642
void (gunner_set_angle)(gunner_t *p, double angle      ){
69 193 up20180642
    sprite_set_angle(p->dude  , angle);
70
    sprite_set_angle(p->weapon, angle);
71
}
72 267 up20180655
void (gunner_set_health)        (gunner_t *p, double health) {
73 230 up20180655
    if (health < 0) health = 0;
74
    p->health = health;
75
}
76 267 up20180655
void (gunner_set_curr_health)   (gunner_t *p, double health) {
77 230 up20180655
    if (health < 0) health = 0;
78
    p->current_health = health;
79
}
80 229 up20180655
double  (gunner_get_x)              (const gunner_t *p){ return p->x; }
81
double  (gunner_get_y)              (const gunner_t *p){ return p->y; }
82 232 up20180655
double  (gunner_get_spawn_x)        (const gunner_t *p){ return p->spawn_x; }
83
double  (gunner_get_spawn_y)        (const gunner_t *p){ return p->spawn_y; }
84 231 up20180655
double  (gunner_get_angle)          (const gunner_t *p){ return sprite_get_angle(p->dude); }
85 267 up20180655
double  (gunner_get_health)         (const gunner_t *p){ return p->health; }
86
double  (gunner_get_curr_health)    (const gunner_t *p){ return p->current_health; }
87 229 up20180655
int16_t (gunner_get_x_screen)       (const gunner_t *p){ return (p->x-x_origin)*scale; }
88
int16_t (gunner_get_y_screen)       (const gunner_t *p){ return (p->y-y_origin)*scale; }
89 301 up20180642
gunner_type (gunner_get_type)       (const gunner_t *p){ return p->type; }
90 302 up20180642
int     (gunner_get_team)           (const gunner_t *p){ return p->team; }
91 201 up20180642
void (gunner_draw)(gunner_t *p){
92
    const int16_t x_screen = gunner_get_x_screen(p);
93
    const int16_t y_screen = gunner_get_y_screen(p);
94 193 up20180642
    sprite_set_pos  (p->dude  , x_screen, y_screen);
95
    sprite_set_pos  (p->weapon, x_screen, y_screen);
96
    sprite_set_scale(p->dude  , scale);
97
    sprite_set_scale(p->weapon, scale);
98
    sprite_draw     (p->weapon);
99
    sprite_draw     (p->dude  );
100 229 up20180655
    gunner_draw_health(p);
101 193 up20180642
}
102 203 up20180642
103 229 up20180655
void (gunner_draw_health)(const gunner_t *p) {
104
    int16_t w = sprite_get_w(p->dude);
105
    int16_t h = sprite_get_h(p->dude);
106 230 up20180655
    double x = gunner_get_x_screen(p) - w/2;
107
    double y = gunner_get_y_screen(p) - h/2 - 10;
108 267 up20180655
    double curr_health = gunner_get_curr_health(p);
109
    double health = gunner_get_health(p);
110
    double perc = curr_health/health;
111 229 up20180655
    rectangle_t *green_bar = rectangle_ctor(x, y, (int16_t)(w*perc), 10);
112 307 up20180642
    rectangle_set_fill_color(green_bar, GREEN_HEALTH_COLOR);
113 229 up20180655
    rectangle_t *red_bar = rectangle_ctor(x+(int16_t)(w*perc), y, (int16_t)(w*(1-perc)), 10);
114 307 up20180642
    rectangle_set_fill_color(red_bar, GRAPH_RED);
115
    char buf[20]; sprintf(buf, "%d/%d", (int)p->current_health, (int)p->health);
116
    text_set_text(p->txt, buf);
117
    text_set_pos(p->txt, x+w/2, y+10/2);
118 229 up20180655
    rectangle_draw(green_bar);
119
    rectangle_draw(red_bar);
120 307 up20180642
    text_draw(p->txt);
121 229 up20180655
    rectangle_dtor(green_bar);
122
    rectangle_dtor(red_bar);
123 307 up20180642
124 229 up20180655
}
125
126 302 up20180642
double (gunner_distance)(const gunner_t *p1, const gunner_t *p2){
127
    double dx = gunner_get_x(p1) - gunner_get_x(p2);
128
    double dy = gunner_get_y(p1) - gunner_get_y(p2);
129
    return sqrt(dx*dx+dy*dy);
130
}
131
132 203 up20180642
struct bullet{
133 236 up20180642
    const gunner_t *shooter;
134 203 up20180642
    double x, y; //real position
135 226 up20180642
    double vx, vy;
136 203 up20180642
    sprite_t *b;
137 267 up20180655
    double damage;
138 203 up20180642
};
139 237 up20180642
bullet_t* (bullet_ctor)(const gunner_t *shooter, const basic_sprite_t *b, double x, double y, double vx, double vy){
140 203 up20180642
    bullet_t *ret = malloc(sizeof(bullet_t));
141
    if(ret == NULL) return NULL;
142 236 up20180642
    ret->shooter = shooter;
143 227 up20180642
    ret-> x =  x;
144
    ret-> y =  y;
145
    ret->vx = vx;
146
    ret->vy = vy;
147 232 up20180655
    ret->damage = 10;
148 203 up20180642
    ret->b = sprite_ctor(b);
149
    if(ret->b == NULL){
150
        bullet_dtor(ret);
151
        return NULL;
152 227 up20180642
    }
153
    double angle = atan2(-ret->vy, ret->vx);
154
    sprite_set_angle(ret->b, angle-M_PI_2);
155
    return ret;
156 203 up20180642
}
157
void (bullet_dtor)(bullet_t *p){
158
    if(p == NULL) return;
159
    sprite_dtor(p->b);
160
    free(p);
161
}
162
double  (bullet_get_x)       (const bullet_t *p){ return p->x; }
163
double  (bullet_get_y)       (const bullet_t *p){ return p->y; }
164
int16_t (bullet_get_x_screen)(const bullet_t *p){ return (p->x-x_origin)*scale; }
165
int16_t (bullet_get_y_screen)(const bullet_t *p){ return (p->y-y_origin)*scale; }
166 267 up20180655
double  (bullet_get_damage)  (const bullet_t *p){ return p->damage; }
167
void    (bullet_set_damage)  (bullet_t *p, double damage) {
168 230 up20180655
    if (damage < 0) damage = 0;
169
    p->damage = damage;
170
}
171 302 up20180642
const gunner_t* (bullet_get_shooter)(const bullet_t *p){ return p->shooter; }
172 226 up20180642
void (bullet_update_movement)(bullet_t *p){
173
    p->x += p->vx;
174
    p->y += p->vy;
175
}
176 231 up20180655
177
void (bullet_update_movement_list)(list_t *bullet_list){
178
    if (list_size(bullet_list) == 0) return;
179
180
    list_node_t *it = list_begin(bullet_list);
181
    while (it != list_end(bullet_list)) {
182
        bullet_update_movement(*(bullet_t**)list_node_val(it));
183
        it = list_node_next(it);
184
    }
185
}
186
187 203 up20180642
void (bullet_draw)(bullet_t *p){
188
    const int16_t x_screen = bullet_get_x_screen(p);
189
    const int16_t y_screen = bullet_get_y_screen(p);
190
    sprite_set_pos  (p->b, x_screen, y_screen);
191
    sprite_set_scale(p->b, scale);
192
    sprite_draw     (p->b);
193
}
194 216 up20180642
195 248 up20180655
void (gunner_draw_list)(list_t *shooter_list) {
196
    if (list_size(shooter_list) == 0) return;
197
198
    list_node_t *it = list_begin(shooter_list);
199
    while (it != list_end(shooter_list)) {
200
        gunner_draw(*(gunner_t**)list_node_val(it));
201
        it = list_node_next(it);
202
    }
203
}
204
205 231 up20180655
void (bullet_draw_list)(list_t *bullet_list) {
206
    if (list_size(bullet_list) == 0) return;
207
208
    list_node_t *it = list_begin(bullet_list);
209
    while (it != list_end(bullet_list)) {
210
        bullet_draw(*(bullet_t**)list_node_val(it));
211
        it = list_node_next(it);
212
    }
213
}
214
215 216 up20180642
struct map{
216
    basic_sprite_t *bsp_background;
217
    sprite_t *background;
218
    uint8_t *collide;
219 307 up20180642
    int *x_prev;
220
    int *y_prev;
221 216 up20180642
};
222
map_t* (map_ctor)(const char **background, const char **collide){
223
    map_t *ret = malloc(sizeof(map_t));
224
    if(ret == NULL) return NULL;
225
226
    ret->bsp_background = NULL;
227
    ret->background     = NULL;
228
    ret->collide        = NULL;
229 307 up20180642
    ret->x_prev         = NULL;
230
    ret->y_prev         = NULL;
231 216 up20180642
232
    ret->bsp_background = basic_sprite_ctor(background, 0, 0);
233
    ret->background     = sprite_ctor(ret->bsp_background);
234
    if(ret->bsp_background == NULL ||
235 307 up20180642
       ret->background     == NULL){ map_dtor(ret); return NULL; }
236 216 up20180642
237
    basic_sprite_t *bsp_collide = basic_sprite_ctor(collide, 0, 0);
238
    if(bsp_collide == NULL){ map_dtor(ret); return NULL; }
239
    const uint16_t W = basic_sprite_get_w(bsp_collide);
240
    const uint16_t H = basic_sprite_get_h(bsp_collide);
241
    ret->collide = malloc(W*H*sizeof(uint8_t));
242
    if(ret->collide == NULL){ map_dtor(ret); return NULL; }
243
    const uint8_t *m = basic_sprite_get_map(bsp_collide);
244
    for(unsigned i = 0; i < W*H; ++i){
245
        ret->collide[i] = (m[4*i+3] < ALPHA_THRESHOLD ? 1 : 0);
246
    }
247
    basic_sprite_dtor(bsp_collide);
248
249 307 up20180642
    ret->x_prev = malloc(W*H*sizeof(int));
250
    ret->y_prev = malloc(W*H*sizeof(int));
251
252
    if(ret->x_prev == NULL || ret->y_prev == NULL){
253
        map_dtor(ret);
254
        return NULL;
255
    }
256 216 up20180642
    return ret;
257
}
258
void (map_dtor)(map_t *p){
259
    if(p == NULL) return;
260
    sprite_dtor(p->background);
261
    basic_sprite_dtor(p->bsp_background);
262
    free(p->collide);
263 307 up20180642
    free(p->x_prev);
264
    free(p->y_prev);
265 216 up20180642
    free(p);
266
}
267
int16_t (map_get_x_screen)(const map_t *p){ return (-x_origin)*scale; }
268
int16_t (map_get_y_screen)(const map_t *p){ return (-y_origin)*scale; }
269 246 up20180655
int16_t (map_get_width)   (const map_t *p){ return sprite_get_w(p->background); }
270
int16_t (map_get_height)  (const map_t *p){ return sprite_get_h(p->background); }
271 220 up20180655
int (map_collides_point)(const map_t *p, double x, double y){
272 216 up20180642
    const uint16_t w = sprite_get_w(p->background), h = sprite_get_h(p->background);
273
    int16_t x_ = x, y_ = y;
274
    if(x_ < 0 || w <= x_ || y_ < 0 || h <= y_) return 0;
275
    uint32_t pos = x_ + y_*w;
276
    return p->collide[pos];
277
}
278 307 up20180642
int (map_make_dijkstra)(map_t *p, uint16_t x, uint16_t y){
279
    const uint16_t W = basic_sprite_get_w(p->bsp_background),
280
                   H = basic_sprite_get_h(p->bsp_background);
281
    for(size_t i = 0; i < W*H; ++i)
282
        p->x_prev[i] = p->y_prev[i] = -1;
283
    /// ACTUAL DIJKSTRA
284
    queue_t *q = queue_ctor();
285
    int *ptr;
286
    p->x_prev[y*W+x] = x; p->x_prev[y*W+x] = y;
287
    ptr = malloc(sizeof(int)); *ptr = y*W+x; queue_push(q, ptr);
288
    while(!queue_empty(q)){
289
        int c = *(int*)list_node_val(queue_top(q)); free(queue_top(q)); queue_pop(q);
290
        if(p->collide[c]){ p->x_prev[c] = p->y_prev[c] = -1; continue; }
291
        uint16_t x = c%W, y = c/W;
292
        if(0   <= x-1){ int pos = y*W+(x-1); if(p->x_prev[pos] == -1){ p->x_prev[pos] = x; p->y_prev[pos] = y; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }}
293
        if(x+1 <  W  ){ int pos = y*W+(x+1); if(p->x_prev[pos] == -1){ p->x_prev[pos] = x; p->y_prev[pos] = y; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }}
294
        if(0   <= y-1){ int pos = (y-1)*W+x; if(p->x_prev[pos] == -1){ p->x_prev[pos] = x; p->y_prev[pos] = y; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }}
295
        if(y+1 <  H  ){ int pos = (y+1)*W+x; if(p->x_prev[pos] == -1){ p->x_prev[pos] = x; p->y_prev[pos] = y; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }}
296
    }
297
    return SUCCESS;
298
}
299 220 up20180655
300 307 up20180642
301 226 up20180642
int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) {
302 228 up20180655
    double radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0;
303 220 up20180655
    double shooter_x = gunner_get_x(shooter);
304 224 up20180655
    double shooter_y = gunner_get_y(shooter);
305
    for (double x = -radius; x < radius; x += 1) {
306
        double y1 = sqrt(radius*radius - x*x);
307 220 up20180655
        double y2 = -y1;
308 224 up20180655
        if (map_collides_point(p, shooter_x + x, shooter_y + y1) || map_collides_point(p, shooter_x + x, shooter_y + y2)) return 1;
309 220 up20180655
    }
310
    return 0;
311
}
312
313 226 up20180642
int (map_collides_bullet)(const map_t *p, const bullet_t *bull){
314
    double radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0;
315
    double bullet_x = bullet_get_x(bull);
316
    double bullet_y = bullet_get_y(bull);
317
    for (double x = -radius; x < radius; x += 1){
318
        double y1 = sqrt(radius*radius - x*x);
319
        double y2 = -y1;
320
        if (map_collides_point(p, bullet_x + x, bullet_y + y1) || map_collides_point(p, bullet_x + x, bullet_y + y2)) return 1;
321
    }
322 220 up20180655
    return 0;
323
}
324
325 228 up20180655
int (gunner_collides_bullet)(const gunner_t *shooter, const bullet_t *bull){
326 236 up20180642
    if(bull->shooter == shooter) return false;
327
328 228 up20180655
    double shooter_radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0;
329
    double shooter_x = gunner_get_x(shooter);
330
    double shooter_y = gunner_get_y(shooter);
331
332
    double bullet_radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0;
333
    double bullet_x = bullet_get_x(bull);
334
    double bullet_y = bullet_get_y(bull);
335
336
    double dx = shooter_x - bullet_x;
337
    double dy = shooter_y - bullet_y;
338
    double distance = sqrt(dx*dx + dy*dy);
339
    return distance <= shooter_radius+bullet_radius;
340
}
341
342 308 up20180655
double (distance_gunners)(const gunner_t *shooter1, const gunner_t *shooter2) {
343 233 up20180655
    double shooter1_x = gunner_get_x(shooter1);
344
    double shooter1_y = gunner_get_y(shooter1);
345
346
    double shooter2_x = gunner_get_x(shooter2);
347
    double shooter2_y = gunner_get_y(shooter2);
348
349
    double dx = shooter1_x - shooter2_x;
350
    double dy = shooter1_y - shooter2_y;
351 308 up20180655
    return sqrt(dx*dx + dy*dy);
352
}
353
354
int (gunner_collides_gunner)(const gunner_t *shooter1, const gunner_t *shooter2) {
355
    double shooter1_radius = max(sprite_get_w(shooter1->dude), sprite_get_h(shooter1->dude))/2.0;
356
    double shooter2_radius = max(sprite_get_w(shooter2->dude), sprite_get_h(shooter2->dude))/2.0;
357
    double distance = distance_gunners(shooter1, shooter2);
358 233 up20180655
    return distance <= shooter1_radius+shooter2_radius;
359
}
360
361 216 up20180642
void   (map_draw)(map_t *p){
362
    const int16_t x_screen = map_get_x_screen(p);
363
    const int16_t y_screen = map_get_y_screen(p);
364
    sprite_set_pos  (p->background, x_screen, y_screen);
365
    sprite_set_scale(p->background, scale);
366
    sprite_draw     (p->background);
367
}