Project

General

Profile

Statistics
| Revision:

root / proj / src / ent.c @ 301

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