Project

General

Profile

Statistics
| Revision:

root / proj / src / ent.c @ 286

History | View | Annotate | Download (10.4 KB)

1 193 up20180642
#include <lcom/lcf.h>
2
3
#include "ent.h"
4
5
#include "graph.h"
6 226 up20180642
#include "utils.h"
7 229 up20180655
#include "rectangle.h"
8 220 up20180655
#include <math.h>
9
10 193 up20180642
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 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 193 up20180642
};
27 201 up20180642
gunner_t* (gunner_ctor)(basic_sprite_t *dude, basic_sprite_t *weapon){
28
    gunner_t *ret = malloc(sizeof(gunner_t));
29 193 up20180642
    if(ret == NULL) return NULL;
30 232 up20180655
    ret->spawn_x = 0.0;
31
    ret->spawn_y = 0.0;
32 193 up20180642
    ret->x = 0.0;
33
    ret->y = 0.0;
34 229 up20180655
    ret->health = 100;
35 230 up20180655
    ret->current_health = ret->health;
36 193 up20180642
    ret->dude   = sprite_ctor(dude  );
37
    ret->weapon = sprite_ctor(weapon);
38
    if(ret->dude == NULL || ret->weapon == NULL){
39 201 up20180642
        gunner_dtor(ret);
40 193 up20180642
        return NULL;
41
    } else return ret;
42
}
43 201 up20180642
void (gunner_dtor)(gunner_t *p){
44 193 up20180642
    if(p == NULL) return;
45
    sprite_dtor(p->dude);
46
    sprite_dtor(p->weapon);
47
    free(p);
48
}
49 216 up20180642
void (gunner_set_pos)  (gunner_t *p, double x, double y){ p->x = x; p->y = y; }
50 232 up20180655
void (gunner_set_spawn)  (gunner_t *p, double x, double y){ p->spawn_x = x; p->spawn_y = y; }
51 216 up20180642
void (gunner_set_angle)(gunner_t *p, double angle      ){
52 193 up20180642
    sprite_set_angle(p->dude  , angle);
53
    sprite_set_angle(p->weapon, angle);
54
}
55 267 up20180655
void (gunner_set_health)        (gunner_t *p, double health) {
56 230 up20180655
    if (health < 0) health = 0;
57
    p->health = health;
58
}
59 267 up20180655
void (gunner_set_curr_health)   (gunner_t *p, double health) {
60 230 up20180655
    if (health < 0) health = 0;
61
    p->current_health = health;
62
}
63 229 up20180655
double  (gunner_get_x)              (const gunner_t *p){ return p->x; }
64
double  (gunner_get_y)              (const gunner_t *p){ return p->y; }
65 232 up20180655
double  (gunner_get_spawn_x)        (const gunner_t *p){ return p->spawn_x; }
66
double  (gunner_get_spawn_y)        (const gunner_t *p){ return p->spawn_y; }
67 231 up20180655
double  (gunner_get_angle)          (const gunner_t *p){ return sprite_get_angle(p->dude); }
68 267 up20180655
double  (gunner_get_health)         (const gunner_t *p){ return p->health; }
69
double  (gunner_get_curr_health)    (const gunner_t *p){ return p->current_health; }
70 229 up20180655
int16_t (gunner_get_x_screen)       (const gunner_t *p){ return (p->x-x_origin)*scale; }
71
int16_t (gunner_get_y_screen)       (const gunner_t *p){ return (p->y-y_origin)*scale; }
72 201 up20180642
void (gunner_draw)(gunner_t *p){
73
    const int16_t x_screen = gunner_get_x_screen(p);
74
    const int16_t y_screen = gunner_get_y_screen(p);
75 193 up20180642
    sprite_set_pos  (p->dude  , x_screen, y_screen);
76
    sprite_set_pos  (p->weapon, x_screen, y_screen);
77
    sprite_set_scale(p->dude  , scale);
78
    sprite_set_scale(p->weapon, scale);
79
    sprite_draw     (p->weapon);
80
    sprite_draw     (p->dude  );
81 229 up20180655
    gunner_draw_health(p);
82 193 up20180642
}
83 203 up20180642
84 229 up20180655
void (gunner_draw_health)(const gunner_t *p) {
85
    int16_t w = sprite_get_w(p->dude);
86
    int16_t h = sprite_get_h(p->dude);
87 230 up20180655
    double x = gunner_get_x_screen(p) - w/2;
88
    double y = gunner_get_y_screen(p) - h/2 - 10;
89 267 up20180655
    double curr_health = gunner_get_curr_health(p);
90
    double health = gunner_get_health(p);
91
    double perc = curr_health/health;
92 229 up20180655
    rectangle_t *green_bar = rectangle_ctor(x, y, (int16_t)(w*perc), 10);
93
    rectangle_set_fill_color(green_bar, 0x00FF00);
94
    rectangle_t *red_bar = rectangle_ctor(x+(int16_t)(w*perc), y, (int16_t)(w*(1-perc)), 10);
95
    rectangle_set_fill_color(red_bar, 0xFF0000);
96
    rectangle_draw(green_bar);
97
    rectangle_draw(red_bar);
98
    rectangle_dtor(green_bar);
99
    rectangle_dtor(red_bar);
100
}
101
102 203 up20180642
struct bullet{
103 236 up20180642
    const gunner_t *shooter;
104 203 up20180642
    double x, y; //real position
105 226 up20180642
    double vx, vy;
106 203 up20180642
    sprite_t *b;
107 267 up20180655
    double damage;
108 203 up20180642
};
109 237 up20180642
bullet_t* (bullet_ctor)(const gunner_t *shooter, const basic_sprite_t *b, double x, double y, double vx, double vy){
110 203 up20180642
    bullet_t *ret = malloc(sizeof(bullet_t));
111
    if(ret == NULL) return NULL;
112 236 up20180642
    ret->shooter = shooter;
113 227 up20180642
    ret-> x =  x;
114
    ret-> y =  y;
115
    ret->vx = vx;
116
    ret->vy = vy;
117 232 up20180655
    ret->damage = 10;
118 203 up20180642
    ret->b = sprite_ctor(b);
119
    if(ret->b == NULL){
120
        bullet_dtor(ret);
121
        return NULL;
122 227 up20180642
    }
123
    double angle = atan2(-ret->vy, ret->vx);
124
    sprite_set_angle(ret->b, angle-M_PI_2);
125
    return ret;
126 203 up20180642
}
127
void (bullet_dtor)(bullet_t *p){
128
    if(p == NULL) return;
129
    sprite_dtor(p->b);
130
    free(p);
131
}
132
double  (bullet_get_x)       (const bullet_t *p){ return p->x; }
133
double  (bullet_get_y)       (const bullet_t *p){ return p->y; }
134
int16_t (bullet_get_x_screen)(const bullet_t *p){ return (p->x-x_origin)*scale; }
135
int16_t (bullet_get_y_screen)(const bullet_t *p){ return (p->y-y_origin)*scale; }
136 267 up20180655
double  (bullet_get_damage)  (const bullet_t *p){ return p->damage; }
137
void    (bullet_set_damage)  (bullet_t *p, double damage) {
138 230 up20180655
    if (damage < 0) damage = 0;
139
    p->damage = damage;
140
}
141 226 up20180642
void (bullet_update_movement)(bullet_t *p){
142
    p->x += p->vx;
143
    p->y += p->vy;
144
}
145 231 up20180655
146
void (bullet_update_movement_list)(list_t *bullet_list){
147
    if (list_size(bullet_list) == 0) return;
148
149
    list_node_t *it = list_begin(bullet_list);
150
    while (it != list_end(bullet_list)) {
151
        bullet_update_movement(*(bullet_t**)list_node_val(it));
152
        it = list_node_next(it);
153
    }
154
}
155
156 203 up20180642
void (bullet_draw)(bullet_t *p){
157
    const int16_t x_screen = bullet_get_x_screen(p);
158
    const int16_t y_screen = bullet_get_y_screen(p);
159
    sprite_set_pos  (p->b, x_screen, y_screen);
160
    sprite_set_scale(p->b, scale);
161
    sprite_draw     (p->b);
162
}
163 216 up20180642
164 248 up20180655
void (gunner_draw_list)(list_t *shooter_list) {
165
    if (list_size(shooter_list) == 0) return;
166
167
    list_node_t *it = list_begin(shooter_list);
168
    while (it != list_end(shooter_list)) {
169
        gunner_draw(*(gunner_t**)list_node_val(it));
170
        it = list_node_next(it);
171
    }
172
}
173
174 231 up20180655
void (bullet_draw_list)(list_t *bullet_list) {
175
    if (list_size(bullet_list) == 0) return;
176
177
    list_node_t *it = list_begin(bullet_list);
178
    while (it != list_end(bullet_list)) {
179
        bullet_draw(*(bullet_t**)list_node_val(it));
180
        it = list_node_next(it);
181
    }
182
}
183
184 216 up20180642
struct map{
185
    basic_sprite_t *bsp_background;
186
    sprite_t *background;
187
    uint8_t *collide;
188
};
189
map_t* (map_ctor)(const char **background, const char **collide){
190
    map_t *ret = malloc(sizeof(map_t));
191
    if(ret == NULL) return NULL;
192
193
    ret->bsp_background = NULL;
194
    ret->background     = NULL;
195
    ret->collide        = NULL;
196
197
    ret->bsp_background = basic_sprite_ctor(background, 0, 0);
198
    ret->background     = sprite_ctor(ret->bsp_background);
199
    if(ret->bsp_background == NULL ||
200
        ret->background     == NULL){ map_dtor(ret); return NULL; }
201
202
    basic_sprite_t *bsp_collide = basic_sprite_ctor(collide, 0, 0);
203
    if(bsp_collide == NULL){ map_dtor(ret); return NULL; }
204
    const uint16_t W = basic_sprite_get_w(bsp_collide);
205
    const uint16_t H = basic_sprite_get_h(bsp_collide);
206
    ret->collide = malloc(W*H*sizeof(uint8_t));
207
    if(ret->collide == NULL){ map_dtor(ret); return NULL; }
208
    const uint8_t *m = basic_sprite_get_map(bsp_collide);
209
    for(unsigned i = 0; i < W*H; ++i){
210
        ret->collide[i] = (m[4*i+3] < ALPHA_THRESHOLD ? 1 : 0);
211
    }
212
    basic_sprite_dtor(bsp_collide);
213
214
    return ret;
215
}
216
void (map_dtor)(map_t *p){
217
    if(p == NULL) return;
218
    sprite_dtor(p->background);
219
    basic_sprite_dtor(p->bsp_background);
220
    free(p->collide);
221
    free(p);
222
}
223
int16_t (map_get_x_screen)(const map_t *p){ return (-x_origin)*scale; }
224
int16_t (map_get_y_screen)(const map_t *p){ return (-y_origin)*scale; }
225 246 up20180655
int16_t (map_get_width)   (const map_t *p){ return sprite_get_w(p->background); }
226
int16_t (map_get_height)  (const map_t *p){ return sprite_get_h(p->background); }
227 220 up20180655
int (map_collides_point)(const map_t *p, double x, double y){
228 216 up20180642
    const uint16_t w = sprite_get_w(p->background), h = sprite_get_h(p->background);
229
    int16_t x_ = x, y_ = y;
230
    if(x_ < 0 || w <= x_ || y_ < 0 || h <= y_) return 0;
231
    uint32_t pos = x_ + y_*w;
232
    return p->collide[pos];
233
}
234 220 up20180655
235 226 up20180642
int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) {
236 228 up20180655
    double radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0;
237 220 up20180655
    double shooter_x = gunner_get_x(shooter);
238 224 up20180655
    double shooter_y = gunner_get_y(shooter);
239
    for (double x = -radius; x < radius; x += 1) {
240
        double y1 = sqrt(radius*radius - x*x);
241 220 up20180655
        double y2 = -y1;
242 224 up20180655
        if (map_collides_point(p, shooter_x + x, shooter_y + y1) || map_collides_point(p, shooter_x + x, shooter_y + y2)) return 1;
243 220 up20180655
    }
244
    return 0;
245
}
246
247 226 up20180642
int (map_collides_bullet)(const map_t *p, const bullet_t *bull){
248
    double radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0;
249
    double bullet_x = bullet_get_x(bull);
250
    double bullet_y = bullet_get_y(bull);
251
    for (double x = -radius; x < radius; x += 1){
252
        double y1 = sqrt(radius*radius - x*x);
253
        double y2 = -y1;
254
        if (map_collides_point(p, bullet_x + x, bullet_y + y1) || map_collides_point(p, bullet_x + x, bullet_y + y2)) return 1;
255
    }
256 220 up20180655
    return 0;
257
}
258
259 228 up20180655
int (gunner_collides_bullet)(const gunner_t *shooter, const bullet_t *bull){
260 236 up20180642
    if(bull->shooter == shooter) return false;
261
262 228 up20180655
    double shooter_radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0;
263
    double shooter_x = gunner_get_x(shooter);
264
    double shooter_y = gunner_get_y(shooter);
265
266
    double bullet_radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0;
267
    double bullet_x = bullet_get_x(bull);
268
    double bullet_y = bullet_get_y(bull);
269
270
    double dx = shooter_x - bullet_x;
271
    double dy = shooter_y - bullet_y;
272
    double distance = sqrt(dx*dx + dy*dy);
273
    return distance <= shooter_radius+bullet_radius;
274
}
275
276 233 up20180655
int (gunner_collides_gunner)(const gunner_t *shooter1, const gunner_t *shooter2) {
277
    double shooter1_radius = max(sprite_get_w(shooter1->dude), sprite_get_h(shooter1->dude))/2.0;
278
    double shooter1_x = gunner_get_x(shooter1);
279
    double shooter1_y = gunner_get_y(shooter1);
280
281
    double shooter2_radius = max(sprite_get_w(shooter2->dude), sprite_get_h(shooter2->dude))/2.0;
282
    double shooter2_x = gunner_get_x(shooter2);
283
    double shooter2_y = gunner_get_y(shooter2);
284
285
    double dx = shooter1_x - shooter2_x;
286
    double dy = shooter1_y - shooter2_y;
287
    double distance = sqrt(dx*dx + dy*dy);
288
    return distance <= shooter1_radius+shooter2_radius;
289
}
290
291 216 up20180642
void   (map_draw)(map_t *p){
292
    const int16_t x_screen = map_get_x_screen(p);
293
    const int16_t y_screen = map_get_y_screen(p);
294
    sprite_set_pos  (p->background, x_screen, y_screen);
295
    sprite_set_scale(p->background, scale);
296
    sprite_draw     (p->background);
297
}