Project

General

Profile

Revision 216

many changes. implemented collision detection

View differences:

ent.c
3 3
#include "ent.h"
4 4

  
5 5
#include "graph.h"
6
#include "sprite.h"
6 7

  
7 8
static double scale = 1.0;
8 9
static int16_t x_origin = 0;
......
37 38
    sprite_dtor(p->weapon);
38 39
    free(p);
39 40
}
40
void (gunner_set_pos)  (gunner_t *p, int16_t x, int16_t y){ p->x = x; p->y = y; }
41
void (gunner_set_angle)(gunner_t *p, double angle        ){
41
void (gunner_set_pos)  (gunner_t *p, double x, double y){ p->x = x; p->y = y; }
42
void (gunner_set_angle)(gunner_t *p, double angle      ){
42 43
    sprite_set_angle(p->dude  , angle);
43 44
    sprite_set_angle(p->weapon, angle);
44 45
}
......
77 78
    sprite_dtor(p->b);
78 79
    free(p);
79 80
}
80
void (bullet_set_pos)  (bullet_t *p, int16_t x, int16_t y){ p->x = x; p->y = y; }
81
void (bullet_set_angle)(bullet_t *p, double angle        ){ sprite_set_angle(p->b, angle); }
81
void (bullet_set_pos)  (bullet_t *p, double x, double y){ p->x = x; p->y = y; }
82
void (bullet_set_angle)(bullet_t *p, double angle      ){ sprite_set_angle(p->b, angle); }
82 83
double  (bullet_get_x)       (const bullet_t *p){ return p->x; }
83 84
double  (bullet_get_y)       (const bullet_t *p){ return p->y; }
84 85
int16_t (bullet_get_x_screen)(const bullet_t *p){ return (p->x-x_origin)*scale; }
......
90 91
    sprite_set_scale(p->b, scale);
91 92
    sprite_draw     (p->b);
92 93
}
94

  
95
struct map{
96
    basic_sprite_t *bsp_background;
97
    sprite_t *background;
98
    uint8_t *collide;
99
};
100
map_t* (map_ctor)(const char **background, const char **collide){
101
    map_t *ret = malloc(sizeof(map_t));
102
    if(ret == NULL) return NULL;
103

  
104
    ret->bsp_background = NULL;
105
    ret->background     = NULL;
106
    ret->collide        = NULL;
107

  
108
    ret->bsp_background = basic_sprite_ctor(background, 0, 0);
109
    ret->background     = sprite_ctor(ret->bsp_background);
110
    if(ret->bsp_background == NULL ||
111
        ret->background     == NULL){ map_dtor(ret); return NULL; }
112

  
113
    basic_sprite_t *bsp_collide = basic_sprite_ctor(collide, 0, 0);
114
    if(bsp_collide == NULL){ map_dtor(ret); return NULL; }
115
    const uint16_t W = basic_sprite_get_w(bsp_collide);
116
    const uint16_t H = basic_sprite_get_h(bsp_collide);
117
    ret->collide = malloc(W*H*sizeof(uint8_t));
118
    if(ret->collide == NULL){ map_dtor(ret); return NULL; }
119
    const uint8_t *m = basic_sprite_get_map(bsp_collide);
120
    for(unsigned i = 0; i < W*H; ++i){
121
        ret->collide[i] = (m[4*i+3] < ALPHA_THRESHOLD ? 1 : 0);
122
    }
123
    basic_sprite_dtor(bsp_collide);
124

  
125
    return ret;
126
}
127
void (map_dtor)(map_t *p){
128
    if(p == NULL) return;
129
    sprite_dtor(p->background);
130
    basic_sprite_dtor(p->bsp_background);
131
    free(p->collide);
132
    free(p);
133
}
134
int16_t (map_get_x_screen)(const map_t *p){ return (-x_origin)*scale; }
135
int16_t (map_get_y_screen)(const map_t *p){ return (-y_origin)*scale; }
136
int (map_collides)(const map_t *p, double x, double y){
137
    const uint16_t w = sprite_get_w(p->background), h = sprite_get_h(p->background);
138
    int16_t x_ = x, y_ = y;
139
    if(x_ < 0 || w <= x_ || y_ < 0 || h <= y_) return 0;
140
    uint32_t pos = x_ + y_*w;
141
    return p->collide[pos];
142
}
143
void   (map_draw)(map_t *p){
144
    const int16_t x_screen = map_get_x_screen(p);
145
    const int16_t y_screen = map_get_y_screen(p);
146
    sprite_set_pos  (p->background, x_screen, y_screen);
147
    sprite_set_scale(p->background, scale);
148
    sprite_draw     (p->background);
149
}

Also available in: Unified diff