Project

General

Profile

Revision 307

working on dijkstra for zombies

View differences:

ent.c
5 5
#include "graph.h"
6 6
#include "utils.h"
7 7
#include "rectangle.h"
8
#include "font.h"
9
#include "errors.h"
10
#include "queue.h"
8 11
#include <math.h>
9 12

  
13
#define GREEN_HEALTH_COLOR      0x009900
14

  
10 15
static double scale = 1.0;
11 16
static int16_t x_origin = 0;
12 17
static int16_t y_origin = 0;
......
23 28
    sprite_t *dude;
24 29
    sprite_t *weapon;
25 30
    double health, current_health;
31
    text_t *txt;
26 32
    gunner_type type;
27 33
    int team;
28 34
};
......
35 41
    ret->y = 0.0;
36 42
    ret->health = 100;
37 43
    ret->current_health = ret->health;
44
    ret->txt = text_ctor(consolas, "");
38 45
    ret->type = type;
39 46
    ret->team = team;
40 47
    ret->dude   = sprite_ctor(dude  );
......
42 49
    if(ret->dude == NULL || ret->weapon == NULL){
43 50
        gunner_dtor(ret);
44 51
        return NULL;
45
    } else return ret;
52
    }
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;
46 58
}
47 59
void (gunner_dtor)(gunner_t *p){
48 60
    if(p == NULL) return;
49 61
    sprite_dtor(p->dude);
50 62
    sprite_dtor(p->weapon);
63
    text_dtor(p->txt);
51 64
    free(p);
52 65
}
53 66
void (gunner_set_pos)  (gunner_t *p, double x, double y){ p->x = x; p->y = y; }
......
96 109
    double health = gunner_get_health(p);
97 110
    double perc = curr_health/health;
98 111
    rectangle_t *green_bar = rectangle_ctor(x, y, (int16_t)(w*perc), 10);
99
    rectangle_set_fill_color(green_bar, 0x00FF00);
112
    rectangle_set_fill_color(green_bar, GREEN_HEALTH_COLOR);
100 113
    rectangle_t *red_bar = rectangle_ctor(x+(int16_t)(w*perc), y, (int16_t)(w*(1-perc)), 10);
101
    rectangle_set_fill_color(red_bar, 0xFF0000);
114
    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);
102 118
    rectangle_draw(green_bar);
103 119
    rectangle_draw(red_bar);
120
    text_draw(p->txt);
104 121
    rectangle_dtor(green_bar);
105 122
    rectangle_dtor(red_bar);
123

  
106 124
}
107 125

  
108 126
double (gunner_distance)(const gunner_t *p1, const gunner_t *p2){
......
198 216
    basic_sprite_t *bsp_background;
199 217
    sprite_t *background;
200 218
    uint8_t *collide;
219
    int *x_prev;
220
    int *y_prev;
201 221
};
202 222
map_t* (map_ctor)(const char **background, const char **collide){
203 223
    map_t *ret = malloc(sizeof(map_t));
......
206 226
    ret->bsp_background = NULL;
207 227
    ret->background     = NULL;
208 228
    ret->collide        = NULL;
229
    ret->x_prev         = NULL;
230
    ret->y_prev         = NULL;
209 231

  
210 232
    ret->bsp_background = basic_sprite_ctor(background, 0, 0);
211 233
    ret->background     = sprite_ctor(ret->bsp_background);
212 234
    if(ret->bsp_background == NULL ||
213
        ret->background     == NULL){ map_dtor(ret); return NULL; }
235
       ret->background     == NULL){ map_dtor(ret); return NULL; }
214 236

  
215 237
    basic_sprite_t *bsp_collide = basic_sprite_ctor(collide, 0, 0);
216 238
    if(bsp_collide == NULL){ map_dtor(ret); return NULL; }
......
224 246
    }
225 247
    basic_sprite_dtor(bsp_collide);
226 248

  
249
    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
    }
227 256
    return ret;
228 257
}
229 258
void (map_dtor)(map_t *p){
......
231 260
    sprite_dtor(p->background);
232 261
    basic_sprite_dtor(p->bsp_background);
233 262
    free(p->collide);
263
    free(p->x_prev);
264
    free(p->y_prev);
234 265
    free(p);
235 266
}
236 267
int16_t (map_get_x_screen)(const map_t *p){ return (-x_origin)*scale; }
......
244 275
    uint32_t pos = x_ + y_*w;
245 276
    return p->collide[pos];
246 277
}
278
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
}
247 299

  
300

  
248 301
int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) {
249 302
    double radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0;
250 303
    double shooter_x = gunner_get_x(shooter);

Also available in: Unified diff