Project

General

Profile

Statistics
| Revision:

root / proj / src / ent.c @ 227

History | View | Annotate | Download (6.13 KB)

1
#include <lcom/lcf.h>
2

    
3
#include "ent.h"
4

    
5
#include "graph.h"
6
#include "sprite.h"
7
#include "utils.h"
8
#include <math.h>
9

    
10
static double scale = 1.0;
11
static int16_t x_origin = 0;
12
static int16_t y_origin = 0;
13

    
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
double (ent_get_scale)  (void){ return scale; }
17
double (ent_get_XLength)(void){ return graph_get_XRes()/scale; }
18
double (ent_get_YLength)(void){ return graph_get_YRes()/scale; }
19

    
20
struct gunner{
21
    double x, y; //real position
22
    sprite_t *dude;
23
    sprite_t *weapon;
24
};
25
gunner_t* (gunner_ctor)(basic_sprite_t *dude, basic_sprite_t *weapon){
26
    gunner_t *ret = malloc(sizeof(gunner_t));
27
    if(ret == NULL) return NULL;
28
    ret->x = 0.0;
29
    ret->y = 0.0;
30
    ret->dude   = sprite_ctor(dude  );
31
    ret->weapon = sprite_ctor(weapon);
32
    if(ret->dude == NULL || ret->weapon == NULL){
33
        gunner_dtor(ret);
34
        return NULL;
35
    } else return ret;
36
}
37
void (gunner_dtor)(gunner_t *p){
38
    if(p == NULL) return;
39
    sprite_dtor(p->dude);
40
    sprite_dtor(p->weapon);
41
    free(p);
42
}
43
void (gunner_set_pos)  (gunner_t *p, double x, double y){ p->x = x; p->y = y; }
44
void (gunner_set_angle)(gunner_t *p, double angle      ){
45
    sprite_set_angle(p->dude  , angle);
46
    sprite_set_angle(p->weapon, angle);
47
}
48
double  (gunner_get_x)       (const gunner_t *p){ return p->x; }
49
double  (gunner_get_y)       (const gunner_t *p){ return p->y; }
50
int16_t (gunner_get_x_screen)(const gunner_t *p){ return (p->x-x_origin)*scale; }
51
int16_t (gunner_get_y_screen)(const gunner_t *p){ return (p->y-y_origin)*scale; }
52
void (gunner_draw)(gunner_t *p){
53
    const int16_t x_screen = gunner_get_x_screen(p);
54
    const int16_t y_screen = gunner_get_y_screen(p);
55
    sprite_set_pos  (p->dude  , x_screen, y_screen);
56
    sprite_set_pos  (p->weapon, x_screen, y_screen);
57
    sprite_set_scale(p->dude  , scale);
58
    sprite_set_scale(p->weapon, scale);
59
    sprite_draw     (p->weapon);
60
    sprite_draw     (p->dude  );
61
}
62

    
63
struct bullet{
64
    double x, y; //real position
65
    double vx, vy;
66
    sprite_t *b;
67
};
68
bullet_t* (bullet_ctor)(basic_sprite_t *b, double x, double y, double vx, double vy){
69
    bullet_t *ret = malloc(sizeof(bullet_t));
70
    if(ret == NULL) return NULL;
71
    ret-> x =  x;
72
    ret-> y =  y;
73
    ret->vx = vx;
74
    ret->vy = vy;
75
    ret->b = sprite_ctor(b);
76
    if(ret->b == NULL){
77
        bullet_dtor(ret);
78
        return NULL;
79
    }
80
    double angle = atan2(-ret->vy, ret->vx);
81
    sprite_set_angle(ret->b, angle-M_PI_2);
82
    return ret;
83
}
84
void (bullet_dtor)(bullet_t *p){
85
    if(p == NULL) return;
86
    sprite_dtor(p->b);
87
    free(p);
88
}
89
double  (bullet_get_x)       (const bullet_t *p){ return p->x; }
90
double  (bullet_get_y)       (const bullet_t *p){ return p->y; }
91
int16_t (bullet_get_x_screen)(const bullet_t *p){ return (p->x-x_origin)*scale; }
92
int16_t (bullet_get_y_screen)(const bullet_t *p){ return (p->y-y_origin)*scale; }
93
void (bullet_update_movement)(bullet_t *p){
94
    p->x += p->vx;
95
    p->y += p->vy;
96
}
97
void (bullet_draw)(bullet_t *p){
98
    const int16_t x_screen = bullet_get_x_screen(p);
99
    const int16_t y_screen = bullet_get_y_screen(p);
100
    sprite_set_pos  (p->b, x_screen, y_screen);
101
    sprite_set_scale(p->b, scale);
102
    sprite_draw     (p->b);
103
}
104

    
105
struct map{
106
    basic_sprite_t *bsp_background;
107
    sprite_t *background;
108
    uint8_t *collide;
109
};
110
map_t* (map_ctor)(const char **background, const char **collide){
111
    map_t *ret = malloc(sizeof(map_t));
112
    if(ret == NULL) return NULL;
113

    
114
    ret->bsp_background = NULL;
115
    ret->background     = NULL;
116
    ret->collide        = NULL;
117

    
118
    ret->bsp_background = basic_sprite_ctor(background, 0, 0);
119
    ret->background     = sprite_ctor(ret->bsp_background);
120
    if(ret->bsp_background == NULL ||
121
        ret->background     == NULL){ map_dtor(ret); return NULL; }
122

    
123
    basic_sprite_t *bsp_collide = basic_sprite_ctor(collide, 0, 0);
124
    if(bsp_collide == NULL){ map_dtor(ret); return NULL; }
125
    const uint16_t W = basic_sprite_get_w(bsp_collide);
126
    const uint16_t H = basic_sprite_get_h(bsp_collide);
127
    ret->collide = malloc(W*H*sizeof(uint8_t));
128
    if(ret->collide == NULL){ map_dtor(ret); return NULL; }
129
    const uint8_t *m = basic_sprite_get_map(bsp_collide);
130
    for(unsigned i = 0; i < W*H; ++i){
131
        ret->collide[i] = (m[4*i+3] < ALPHA_THRESHOLD ? 1 : 0);
132
    }
133
    basic_sprite_dtor(bsp_collide);
134

    
135
    return ret;
136
}
137
void (map_dtor)(map_t *p){
138
    if(p == NULL) return;
139
    sprite_dtor(p->background);
140
    basic_sprite_dtor(p->bsp_background);
141
    free(p->collide);
142
    free(p);
143
}
144
int16_t (map_get_x_screen)(const map_t *p){ return (-x_origin)*scale; }
145
int16_t (map_get_y_screen)(const map_t *p){ return (-y_origin)*scale; }
146
int (map_collides_point)(const map_t *p, double x, double y){
147
    const uint16_t w = sprite_get_w(p->background), h = sprite_get_h(p->background);
148
    int16_t x_ = x, y_ = y;
149
    if(x_ < 0 || w <= x_ || y_ < 0 || h <= y_) return 0;
150
    uint32_t pos = x_ + y_*w;
151
    return p->collide[pos];
152
}
153

    
154
int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) {
155
    double radius = sprite_get_w(shooter->dude)/2.0;
156
    double shooter_x = gunner_get_x(shooter);
157
    double shooter_y = gunner_get_y(shooter);
158
    for (double x = -radius; x < radius; x += 1) {
159
        double y1 = sqrt(radius*radius - x*x);
160
        double y2 = -y1;
161
        if (map_collides_point(p, shooter_x + x, shooter_y + y1) || map_collides_point(p, shooter_x + x, shooter_y + y2)) return 1;
162
    }
163
    return 0;
164
}
165

    
166
int (map_collides_bullet)(const map_t *p, const bullet_t *bull){
167
    double radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0;
168
    double bullet_x = bullet_get_x(bull);
169
    double bullet_y = bullet_get_y(bull);
170
    for (double x = -radius; x < radius; x += 1){
171
        double y1 = sqrt(radius*radius - x*x);
172
        double y2 = -y1;
173
        if (map_collides_point(p, bullet_x + x, bullet_y + y1) || map_collides_point(p, bullet_x + x, bullet_y + y2)) return 1;
174
    }
175
    return 0;
176
}
177

    
178
void   (map_draw)(map_t *p){
179
    const int16_t x_screen = map_get_x_screen(p);
180
    const int16_t y_screen = map_get_y_screen(p);
181
    sprite_set_pos  (p->background, x_screen, y_screen);
182
    sprite_set_scale(p->background, scale);
183
    sprite_draw     (p->background);
184
}