Project

General

Profile

Statistics
| Revision:

root / proj / project / src / ent.c @ 364

History | View | Annotate | Download (14.3 KB)

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