Project

General

Profile

Statistics
| Revision:

root / proj / src / ent.c @ 248

History | View | Annotate | Download (10.5 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 "rectangle.h"
9
#include <math.h>
10

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

    
15
void (ent_set_scale) (double n){ scale = n; }
16
void (ent_set_origin)(double x, double y){ x_origin = x; y_origin = y; }
17
double (ent_get_scale)  (void){ return scale; }
18
double (ent_get_XLength)(void){ return graph_get_XRes()/scale; }
19
double (ent_get_YLength)(void){ return graph_get_YRes()/scale; }
20

    
21
struct gunner{
22
    double x, y; //real position
23
    double spawn_x, spawn_y;
24
    sprite_t *dude;
25
    sprite_t *weapon;
26
    int health, current_health;
27
};
28
gunner_t* (gunner_ctor)(basic_sprite_t *dude, basic_sprite_t *weapon){
29
    gunner_t *ret = malloc(sizeof(gunner_t));
30
    if(ret == NULL) return NULL;
31
    ret->spawn_x = 0.0;
32
    ret->spawn_y = 0.0;
33
    ret->x = 0.0;
34
    ret->y = 0.0;
35
    ret->health = 100;
36
    ret->current_health = ret->health;
37
    ret->dude   = sprite_ctor(dude  );
38
    ret->weapon = sprite_ctor(weapon);
39
    if(ret->dude == NULL || ret->weapon == NULL){
40
        gunner_dtor(ret);
41
        return NULL;
42
    } else return ret;
43
}
44
void (gunner_dtor)(gunner_t *p){
45
    if(p == NULL) return;
46
    sprite_dtor(p->dude);
47
    sprite_dtor(p->weapon);
48
    free(p);
49
}
50
void (gunner_set_pos)  (gunner_t *p, double x, double y){ p->x = x; p->y = y; }
51
void (gunner_set_spawn)  (gunner_t *p, double x, double y){ p->spawn_x = x; p->spawn_y = y; }
52
void (gunner_set_angle)(gunner_t *p, double angle      ){
53
    sprite_set_angle(p->dude  , angle);
54
    sprite_set_angle(p->weapon, angle);
55
}
56
void (gunner_set_health)        (gunner_t *p, int health) {
57
    if (health < 0) health = 0;
58
    p->health = health;
59
}
60
void (gunner_set_curr_health)   (gunner_t *p, int health) {
61
    if (health < 0) health = 0;
62
    p->current_health = health;
63
}
64
double  (gunner_get_x)              (const gunner_t *p){ return p->x; }
65
double  (gunner_get_y)              (const gunner_t *p){ return p->y; }
66
double  (gunner_get_spawn_x)        (const gunner_t *p){ return p->spawn_x; }
67
double  (gunner_get_spawn_y)        (const gunner_t *p){ return p->spawn_y; }
68
double  (gunner_get_angle)          (const gunner_t *p){ return sprite_get_angle(p->dude); }
69
int     (gunner_get_health)         (const gunner_t *p){ return p->health; }
70
int     (gunner_get_curr_health)    (const gunner_t *p){ return p->current_health; }
71
int16_t (gunner_get_x_screen)       (const gunner_t *p){ return (p->x-x_origin)*scale; }
72
int16_t (gunner_get_y_screen)       (const gunner_t *p){ return (p->y-y_origin)*scale; }
73
void (gunner_draw)(gunner_t *p){
74
    const int16_t x_screen = gunner_get_x_screen(p);
75
    const int16_t y_screen = gunner_get_y_screen(p);
76
    sprite_set_pos  (p->dude  , x_screen, y_screen);
77
    sprite_set_pos  (p->weapon, x_screen, y_screen);
78
    sprite_set_scale(p->dude  , scale);
79
    sprite_set_scale(p->weapon, scale);
80
    sprite_draw     (p->weapon);
81
    sprite_draw     (p->dude  );
82
    gunner_draw_health(p);
83
}
84

    
85
void (gunner_draw_health)(const gunner_t *p) {
86
    int16_t w = sprite_get_w(p->dude);
87
    int16_t h = sprite_get_h(p->dude);
88
    double x = gunner_get_x_screen(p) - w/2;
89
    double y = gunner_get_y_screen(p) - h/2 - 10;
90
    int curr_health = gunner_get_curr_health(p);
91
    int health = gunner_get_health(p);
92
    double perc = (double)curr_health/health;
93
    rectangle_t *green_bar = rectangle_ctor(x, y, (int16_t)(w*perc), 10);
94
    rectangle_set_fill_color(green_bar, 0x00FF00);
95
    rectangle_t *red_bar = rectangle_ctor(x+(int16_t)(w*perc), y, (int16_t)(w*(1-perc)), 10);
96
    rectangle_set_fill_color(red_bar, 0xFF0000);
97
    rectangle_draw(green_bar);
98
    rectangle_draw(red_bar);
99
    rectangle_dtor(green_bar);
100
    rectangle_dtor(red_bar);
101
}
102

    
103
struct bullet{
104
    const gunner_t *shooter;
105
    double x, y; //real position
106
    double vx, vy;
107
    sprite_t *b;
108
    int damage;
109
};
110
bullet_t* (bullet_ctor)(const gunner_t *shooter, const basic_sprite_t *b, double x, double y, double vx, double vy){
111
    bullet_t *ret = malloc(sizeof(bullet_t));
112
    if(ret == NULL) return NULL;
113
    ret->shooter = shooter;
114
    ret-> x =  x;
115
    ret-> y =  y;
116
    ret->vx = vx;
117
    ret->vy = vy;
118
    ret->damage = 10;
119
    ret->b = sprite_ctor(b);
120
    if(ret->b == NULL){
121
        bullet_dtor(ret);
122
        return NULL;
123
    }
124
    double angle = atan2(-ret->vy, ret->vx);
125
    sprite_set_angle(ret->b, angle-M_PI_2);
126
    return ret;
127
}
128
void (bullet_dtor)(bullet_t *p){
129
    if(p == NULL) return;
130
    sprite_dtor(p->b);
131
    free(p);
132
}
133
double  (bullet_get_x)       (const bullet_t *p){ return p->x; }
134
double  (bullet_get_y)       (const bullet_t *p){ return p->y; }
135
int16_t (bullet_get_x_screen)(const bullet_t *p){ return (p->x-x_origin)*scale; }
136
int16_t (bullet_get_y_screen)(const bullet_t *p){ return (p->y-y_origin)*scale; }
137
int     (bullet_get_damage)  (const bullet_t *p){ return p->damage; }
138
void    (bullet_set_damage)  (bullet_t *p, int damage) {
139
    if (damage < 0) damage = 0;
140
    p->damage = damage;
141
}
142
void (bullet_update_movement)(bullet_t *p){
143
    p->x += p->vx;
144
    p->y += p->vy;
145
}
146

    
147
void (bullet_update_movement_list)(list_t *bullet_list){
148
    if (bullet_list == NULL) return;
149
    if (list_size(bullet_list) == 0) return;
150

    
151
    list_node_t *it = list_begin(bullet_list);
152
    while (it != list_end(bullet_list)) {
153
        bullet_update_movement(*(bullet_t**)list_node_val(it));
154
        it = list_node_next(it);
155
    }
156
}
157

    
158
void (bullet_draw)(bullet_t *p){
159
    const int16_t x_screen = bullet_get_x_screen(p);
160
    const int16_t y_screen = bullet_get_y_screen(p);
161
    sprite_set_pos  (p->b, x_screen, y_screen);
162
    sprite_set_scale(p->b, scale);
163
    sprite_draw     (p->b);
164
}
165

    
166
void (gunner_draw_list)(list_t *shooter_list) {
167
    if (list_size(shooter_list) == 0) return;
168

    
169
    list_node_t *it = list_begin(shooter_list);
170
    while (it != list_end(shooter_list)) {
171
        gunner_draw(*(gunner_t**)list_node_val(it));
172
        it = list_node_next(it);
173
    }
174
}
175

    
176
void (bullet_draw_list)(list_t *bullet_list) {
177
    if (list_size(bullet_list) == 0) return;
178

    
179
    list_node_t *it = list_begin(bullet_list);
180
    while (it != list_end(bullet_list)) {
181
        bullet_draw(*(bullet_t**)list_node_val(it));
182
        it = list_node_next(it);
183
    }
184
}
185

    
186
struct map{
187
    basic_sprite_t *bsp_background;
188
    sprite_t *background;
189
    uint8_t *collide;
190
};
191
map_t* (map_ctor)(const char **background, const char **collide){
192
    map_t *ret = malloc(sizeof(map_t));
193
    if(ret == NULL) return NULL;
194

    
195
    ret->bsp_background = NULL;
196
    ret->background     = NULL;
197
    ret->collide        = NULL;
198

    
199
    ret->bsp_background = basic_sprite_ctor(background, 0, 0);
200
    ret->background     = sprite_ctor(ret->bsp_background);
201
    if(ret->bsp_background == NULL ||
202
        ret->background     == NULL){ map_dtor(ret); return NULL; }
203

    
204
    basic_sprite_t *bsp_collide = basic_sprite_ctor(collide, 0, 0);
205
    if(bsp_collide == NULL){ map_dtor(ret); return NULL; }
206
    const uint16_t W = basic_sprite_get_w(bsp_collide);
207
    const uint16_t H = basic_sprite_get_h(bsp_collide);
208
    ret->collide = malloc(W*H*sizeof(uint8_t));
209
    if(ret->collide == NULL){ map_dtor(ret); return NULL; }
210
    const uint8_t *m = basic_sprite_get_map(bsp_collide);
211
    for(unsigned i = 0; i < W*H; ++i){
212
        ret->collide[i] = (m[4*i+3] < ALPHA_THRESHOLD ? 1 : 0);
213
    }
214
    basic_sprite_dtor(bsp_collide);
215

    
216
    return ret;
217
}
218
void (map_dtor)(map_t *p){
219
    if(p == NULL) return;
220
    sprite_dtor(p->background);
221
    basic_sprite_dtor(p->bsp_background);
222
    free(p->collide);
223
    free(p);
224
}
225
int16_t (map_get_x_screen)(const map_t *p){ return (-x_origin)*scale; }
226
int16_t (map_get_y_screen)(const map_t *p){ return (-y_origin)*scale; }
227
int16_t (map_get_width)   (const map_t *p){ return sprite_get_w(p->background); }
228
int16_t (map_get_height)  (const map_t *p){ return sprite_get_h(p->background); }
229
int (map_collides_point)(const map_t *p, double x, double y){
230
    const uint16_t w = sprite_get_w(p->background), h = sprite_get_h(p->background);
231
    int16_t x_ = x, y_ = y;
232
    if(x_ < 0 || w <= x_ || y_ < 0 || h <= y_) return 0;
233
    uint32_t pos = x_ + y_*w;
234
    return p->collide[pos];
235
}
236

    
237
int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) {
238
    double radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0;
239
    double shooter_x = gunner_get_x(shooter);
240
    double shooter_y = gunner_get_y(shooter);
241
    for (double x = -radius; x < radius; x += 1) {
242
        double y1 = sqrt(radius*radius - x*x);
243
        double y2 = -y1;
244
        if (map_collides_point(p, shooter_x + x, shooter_y + y1) || map_collides_point(p, shooter_x + x, shooter_y + y2)) return 1;
245
    }
246
    return 0;
247
}
248

    
249
int (map_collides_bullet)(const map_t *p, const bullet_t *bull){
250
    double radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0;
251
    double bullet_x = bullet_get_x(bull);
252
    double bullet_y = bullet_get_y(bull);
253
    for (double x = -radius; x < radius; x += 1){
254
        double y1 = sqrt(radius*radius - x*x);
255
        double y2 = -y1;
256
        if (map_collides_point(p, bullet_x + x, bullet_y + y1) || map_collides_point(p, bullet_x + x, bullet_y + y2)) return 1;
257
    }
258
    return 0;
259
}
260

    
261
int (gunner_collides_bullet)(const gunner_t *shooter, const bullet_t *bull){
262
    if(bull->shooter == shooter) return false;
263

    
264
    double shooter_radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0;
265
    double shooter_x = gunner_get_x(shooter);
266
    double shooter_y = gunner_get_y(shooter);
267

    
268
    double bullet_radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0;
269
    double bullet_x = bullet_get_x(bull);
270
    double bullet_y = bullet_get_y(bull);
271

    
272
    double dx = shooter_x - bullet_x;
273
    double dy = shooter_y - bullet_y;
274
    double distance = sqrt(dx*dx + dy*dy);
275
    return distance <= shooter_radius+bullet_radius;
276
}
277

    
278
int (gunner_collides_gunner)(const gunner_t *shooter1, const gunner_t *shooter2) {
279
    double shooter1_radius = max(sprite_get_w(shooter1->dude), sprite_get_h(shooter1->dude))/2.0;
280
    double shooter1_x = gunner_get_x(shooter1);
281
    double shooter1_y = gunner_get_y(shooter1);
282

    
283
    double shooter2_radius = max(sprite_get_w(shooter2->dude), sprite_get_h(shooter2->dude))/2.0;
284
    double shooter2_x = gunner_get_x(shooter2);
285
    double shooter2_y = gunner_get_y(shooter2);
286

    
287
    double dx = shooter1_x - shooter2_x;
288
    double dy = shooter1_y - shooter2_y;
289
    double distance = sqrt(dx*dx + dy*dy);
290
    return distance <= shooter1_radius+shooter2_radius;
291
}
292

    
293
void   (map_draw)(map_t *p){
294
    const int16_t x_screen = map_get_x_screen(p);
295
    const int16_t y_screen = map_get_y_screen(p);
296
    sprite_set_pos  (p->background, x_screen, y_screen);
297
    sprite_set_scale(p->background, scale);
298
    sprite_draw     (p->background);
299
}