root / proj / src / ent.c @ 320
History | View | Annotate | Download (14.7 KB)
1 |
#include <lcom/lcf.h> |
---|---|
2 |
|
3 |
#include "ent.h" |
4 |
|
5 |
#include "graph.h" |
6 |
#include "utils.h" |
7 |
#include "rectangle.h" |
8 |
#include "font.h" |
9 |
#include "errors.h" |
10 |
#include "queue.h" |
11 |
#include <math.h> |
12 |
|
13 |
#define GREEN_HEALTH_COLOR 0x009900 |
14 |
|
15 |
static double scale = 1.0; |
16 |
static int16_t x_origin = 0; |
17 |
static int16_t y_origin = 0; |
18 |
|
19 |
void (ent_set_scale) (double n){ scale = n; } |
20 |
void (ent_set_origin)(double x, double y){ x_origin = x; y_origin = y; } |
21 |
double (ent_get_scale) (void){ return scale; } |
22 |
double (ent_get_XLength)(void){ return graph_get_XRes()/scale; } |
23 |
double (ent_get_YLength)(void){ return graph_get_YRes()/scale; } |
24 |
|
25 |
struct gunner{
|
26 |
double x, y; //real position |
27 |
double spawn_x, spawn_y;
|
28 |
sprite_t *dude; |
29 |
sprite_t *weapon; |
30 |
double health, current_health;
|
31 |
rectangle_t *green_bar, *red_bar; |
32 |
text_t *txt; |
33 |
gunner_type type; |
34 |
int team;
|
35 |
}; |
36 |
gunner_t* (gunner_ctor)(basic_sprite_t *dude, basic_sprite_t *weapon, gunner_type type, int team){
|
37 |
gunner_t *ret = malloc(sizeof(gunner_t));
|
38 |
if(ret == NULL) return NULL; |
39 |
ret->spawn_x = 0.0; |
40 |
ret->spawn_y = 0.0; |
41 |
ret->x = 0.0; |
42 |
ret->y = 0.0; |
43 |
ret->health = 100;
|
44 |
ret->current_health = ret->health; |
45 |
ret->green_bar = rectangle_ctor(0,0,0,0); |
46 |
ret->red_bar = rectangle_ctor(0,0,0,0); |
47 |
ret->txt = text_ctor(font_get_default(), "");
|
48 |
ret->type = type; |
49 |
ret->team = team; |
50 |
ret->dude = sprite_ctor(dude ); |
51 |
ret->weapon = sprite_ctor(weapon); |
52 |
if(ret->txt == NULL || ret->dude == NULL || ret->weapon == NULL || ret->green_bar == NULL || ret->red_bar == NULL){ |
53 |
gunner_dtor(ret); |
54 |
return NULL; |
55 |
} |
56 |
rectangle_set_fill_color(ret->green_bar, GREEN_HEALTH_COLOR); |
57 |
rectangle_set_fill_color(ret->red_bar , GRAPH_RED ); |
58 |
text_set_size(ret->txt, 15);
|
59 |
text_set_valign(ret->txt, text_valign_center); |
60 |
text_set_halign(ret->txt, text_halign_center); |
61 |
text_set_color(ret->txt, GRAPH_WHITE); |
62 |
return ret;
|
63 |
} |
64 |
void (gunner_dtor)(gunner_t *p){
|
65 |
if(p == NULL) return; |
66 |
sprite_dtor(p->dude); |
67 |
sprite_dtor(p->weapon); |
68 |
rectangle_dtor(p->green_bar); |
69 |
rectangle_dtor(p->red_bar); |
70 |
text_dtor(p->txt); |
71 |
free(p); |
72 |
} |
73 |
void (gunner_set_pos) (gunner_t *p, double x, double y){ p->x = x; p->y = y; } |
74 |
void (gunner_set_spawn) (gunner_t *p, double x, double y){ p->spawn_x = x; p->spawn_y = y; } |
75 |
void (gunner_set_angle)(gunner_t *p, double angle ){ |
76 |
sprite_set_angle(p->dude , angle); |
77 |
sprite_set_angle(p->weapon, angle); |
78 |
} |
79 |
void (gunner_set_health) (gunner_t *p, double health) { |
80 |
if (health < 0) health = 0; |
81 |
p->health = health; |
82 |
} |
83 |
void (gunner_set_curr_health) (gunner_t *p, double health) { |
84 |
if (health < 0) health = 0; |
85 |
p->current_health = health; |
86 |
} |
87 |
double (gunner_get_x) (const gunner_t *p){ return p->x; } |
88 |
double (gunner_get_y) (const gunner_t *p){ return p->y; } |
89 |
double (gunner_get_spawn_x) (const gunner_t *p){ return p->spawn_x; } |
90 |
double (gunner_get_spawn_y) (const gunner_t *p){ return p->spawn_y; } |
91 |
double (gunner_get_angle) (const gunner_t *p){ return sprite_get_angle(p->dude); } |
92 |
double (gunner_get_health) (const gunner_t *p){ return p->health; } |
93 |
double (gunner_get_curr_health) (const gunner_t *p){ return p->current_health; } |
94 |
int16_t (gunner_get_x_screen) (const gunner_t *p){ return (p->x-x_origin)*scale; } |
95 |
int16_t (gunner_get_y_screen) (const gunner_t *p){ return (p->y-y_origin)*scale; } |
96 |
gunner_type (gunner_get_type) (const gunner_t *p){ return p->type; } |
97 |
int (gunner_get_team) (const gunner_t *p){ return p->team; } |
98 |
void (gunner_draw)(gunner_t *p){
|
99 |
const int16_t x_screen = gunner_get_x_screen(p);
|
100 |
const int16_t y_screen = gunner_get_y_screen(p);
|
101 |
sprite_set_pos (p->dude , x_screen, y_screen); |
102 |
sprite_set_pos (p->weapon, x_screen, y_screen); |
103 |
sprite_set_scale(p->dude , scale); |
104 |
sprite_set_scale(p->weapon, scale); |
105 |
sprite_draw (p->weapon); |
106 |
sprite_draw (p->dude ); |
107 |
gunner_draw_health(p); |
108 |
} |
109 |
void (gunner_draw_health)(const gunner_t *p) { |
110 |
int16_t w = sprite_get_w(p->dude); |
111 |
int16_t h = sprite_get_h(p->dude); |
112 |
double x = gunner_get_x_screen(p) - w/2; |
113 |
double y = gunner_get_y_screen(p) - h/2 - 10; |
114 |
double curr_health = gunner_get_curr_health(p);
|
115 |
double health = gunner_get_health(p);
|
116 |
double perc = curr_health/health;
|
117 |
rectangle_set_pos(p->green_bar, x, y); rectangle_set_size(p->green_bar, (int16_t)(w*perc), 10);
|
118 |
rectangle_set_pos(p->red_bar, x+(int16_t)(w*perc), y); rectangle_set_size(p->red_bar, (int16_t)(w*(1-perc)), 10); |
119 |
char buf[20]; sprintf(buf, "%d/%d", (int)p->current_health, (int)p->health); |
120 |
text_set_string(p->txt, buf); |
121 |
text_set_pos(p->txt, x+w/2, y+10/2); |
122 |
rectangle_draw(p->green_bar); |
123 |
rectangle_draw(p->red_bar); |
124 |
text_draw(p->txt); |
125 |
} |
126 |
|
127 |
double (gunner_distance)(const gunner_t *p1, const gunner_t *p2){ |
128 |
double dx = gunner_get_x(p1) - gunner_get_x(p2);
|
129 |
double dy = gunner_get_y(p1) - gunner_get_y(p2);
|
130 |
return sqrt(dx*dx+dy*dy);
|
131 |
} |
132 |
|
133 |
struct bullet{
|
134 |
const gunner_t *shooter;
|
135 |
double x, y; //real position |
136 |
double vx, vy;
|
137 |
sprite_t *b; |
138 |
double damage;
|
139 |
}; |
140 |
bullet_t* (bullet_ctor)(const gunner_t *shooter, const basic_sprite_t *b, double x, double y, double vx, double vy){ |
141 |
bullet_t *ret = malloc(sizeof(bullet_t));
|
142 |
if(ret == NULL) return NULL; |
143 |
ret->shooter = shooter; |
144 |
ret-> x = x; |
145 |
ret-> y = y; |
146 |
ret->vx = vx; |
147 |
ret->vy = vy; |
148 |
ret->damage = 10;
|
149 |
ret->b = sprite_ctor(b); |
150 |
if(ret->b == NULL){ |
151 |
bullet_dtor(ret); |
152 |
return NULL; |
153 |
} |
154 |
double angle = atan2(-ret->vy, ret->vx);
|
155 |
sprite_set_angle(ret->b, angle-M_PI_2); |
156 |
return ret;
|
157 |
} |
158 |
|
159 |
void (bullet_dtor)(bullet_t *p){
|
160 |
if(p == NULL) return; |
161 |
sprite_dtor(p->b); |
162 |
free(p); |
163 |
} |
164 |
double (bullet_get_x) (const bullet_t *p){ return p->x; } |
165 |
double (bullet_get_y) (const bullet_t *p){ return p->y; } |
166 |
double (bullet_get_vx) (const bullet_t *p){ return p->vx; } |
167 |
double (bullet_get_vy) (const bullet_t *p){ return p->vy; } |
168 |
int16_t (bullet_get_x_screen)(const bullet_t *p){ return (p->x-x_origin)*scale; } |
169 |
int16_t (bullet_get_y_screen)(const bullet_t *p){ return (p->y-y_origin)*scale; } |
170 |
double (bullet_get_damage) (const bullet_t *p){ return p->damage; } |
171 |
void (bullet_set_damage) (bullet_t *p, double damage) { |
172 |
if (damage < 0) damage = 0; |
173 |
p->damage = damage; |
174 |
} |
175 |
const gunner_t* (bullet_get_shooter)(const bullet_t *p){ return p->shooter; } |
176 |
void (bullet_update_movement)(bullet_t *p){
|
177 |
p->x += p->vx; |
178 |
p->y += p->vy; |
179 |
} |
180 |
|
181 |
void (bullet_update_movement_list)(list_t *bullet_list){
|
182 |
if (list_size(bullet_list) == 0) return; |
183 |
|
184 |
list_node_t *it = list_begin(bullet_list); |
185 |
while (it != list_end(bullet_list)) {
|
186 |
bullet_update_movement(*(bullet_t**)list_node_val(it)); |
187 |
it = list_node_next(it); |
188 |
} |
189 |
} |
190 |
|
191 |
void (bullet_draw)(bullet_t *p){
|
192 |
const int16_t x_screen = bullet_get_x_screen(p);
|
193 |
const int16_t y_screen = bullet_get_y_screen(p);
|
194 |
sprite_set_pos (p->b, x_screen, y_screen); |
195 |
sprite_set_scale(p->b, scale); |
196 |
sprite_draw (p->b); |
197 |
} |
198 |
|
199 |
void (gunner_draw_list)(list_t *shooter_list) {
|
200 |
if (list_size(shooter_list) == 0) return; |
201 |
|
202 |
list_node_t *it = list_begin(shooter_list); |
203 |
while (it != list_end(shooter_list)) {
|
204 |
gunner_draw(*(gunner_t**)list_node_val(it)); |
205 |
it = list_node_next(it); |
206 |
} |
207 |
} |
208 |
|
209 |
void (bullet_draw_list)(list_t *bullet_list) {
|
210 |
if (list_size(bullet_list) == 0) return; |
211 |
|
212 |
list_node_t *it = list_begin(bullet_list); |
213 |
while (it != list_end(bullet_list)) {
|
214 |
bullet_draw(*(bullet_t**)list_node_val(it)); |
215 |
it = list_node_next(it); |
216 |
} |
217 |
} |
218 |
|
219 |
struct map{
|
220 |
basic_sprite_t *bsp_background; |
221 |
sprite_t *background; |
222 |
uint8_t *collide; |
223 |
uint8_t *collide_gunner; |
224 |
int32_t *prev; |
225 |
uint8_t *visited; |
226 |
}; |
227 |
static int (map_collides_gunner_pos)(const map_t *p, double shooter_x, double shooter_y, double radius) { |
228 |
for (double x = -radius; x <= radius; x += 1) { |
229 |
double y1 = sqrt(radius*radius - x*x);
|
230 |
double y2 = -y1;
|
231 |
if (map_collides_point(p, shooter_x + x, shooter_y + y1) || map_collides_point(p, shooter_x + x, shooter_y + y2)) return 1; |
232 |
} |
233 |
return 0; |
234 |
} |
235 |
map_t* (map_ctor)(const char **background, const char **collide){ |
236 |
map_t *ret = malloc(sizeof(map_t));
|
237 |
if(ret == NULL) return NULL; |
238 |
|
239 |
ret->bsp_background = NULL;
|
240 |
ret->background = NULL;
|
241 |
ret->collide = NULL;
|
242 |
ret->prev = NULL;
|
243 |
ret->visited = NULL;
|
244 |
|
245 |
ret->bsp_background = basic_sprite_ctor(background, 0, 0); |
246 |
ret->background = sprite_ctor(ret->bsp_background); |
247 |
if(ret->bsp_background == NULL || |
248 |
ret->background == NULL){ map_dtor(ret); return NULL; } |
249 |
|
250 |
basic_sprite_t *bsp_collide = basic_sprite_ctor(collide, 0, 0); |
251 |
if(bsp_collide == NULL){ map_dtor(ret); return NULL; } |
252 |
const uint16_t W = basic_sprite_get_w(bsp_collide);
|
253 |
const uint16_t H = basic_sprite_get_h(bsp_collide);
|
254 |
ret->collide = malloc(W*H*sizeof(uint8_t));
|
255 |
if(ret->collide == NULL){ map_dtor(ret); return NULL; } |
256 |
const uint8_t *m = basic_sprite_get_map(bsp_collide);
|
257 |
for(unsigned i = 0; i < W*H; ++i){ |
258 |
ret->collide[i] = (m[4*i+3] < ALPHA_THRESHOLD ? 1 : 0); |
259 |
} |
260 |
basic_sprite_dtor(bsp_collide); |
261 |
|
262 |
ret->collide_gunner = malloc(W*H*sizeof(uint8_t));
|
263 |
if(ret->collide_gunner == NULL){ map_dtor(ret); return NULL; } |
264 |
for(size_t i = 0; i < W*H; ++i){ |
265 |
int16_t x = i%W, y = i/W; |
266 |
ret->collide_gunner[i] = (map_collides_gunner_pos(ret, x, y, 36) ? 1 : 0); |
267 |
} |
268 |
|
269 |
ret->prev = malloc(W*H*sizeof(int32_t));
|
270 |
ret->visited = malloc(W*H*sizeof(uint8_t));
|
271 |
|
272 |
if(ret->prev == NULL || ret->visited == NULL){ |
273 |
map_dtor(ret); |
274 |
return NULL; |
275 |
} |
276 |
return ret;
|
277 |
} |
278 |
void (map_dtor)(map_t *p){
|
279 |
if(p == NULL) return; |
280 |
sprite_dtor(p->background); |
281 |
basic_sprite_dtor(p->bsp_background); |
282 |
free(p->collide); |
283 |
free(p->prev); |
284 |
free(p->visited); |
285 |
free(p); |
286 |
} |
287 |
int16_t (map_get_x_screen)(const map_t *p){ return (-x_origin)*scale; } |
288 |
int16_t (map_get_y_screen)(const map_t *p){ return (-y_origin)*scale; } |
289 |
int16_t (map_get_width) (const map_t *p){ return sprite_get_w(p->background); } |
290 |
int16_t (map_get_height) (const map_t *p){ return sprite_get_h(p->background); } |
291 |
int (map_collides_point)(const map_t *p, double x, double y){ |
292 |
const uint16_t w = sprite_get_w(p->background), h = sprite_get_h(p->background);
|
293 |
int16_t x_ = x, y_ = y; |
294 |
if(x_ < 0 || w <= x_ || y_ < 0 || h <= y_) return 0; |
295 |
uint32_t pos = x_ + y_*w; |
296 |
return p->collide[pos];
|
297 |
} |
298 |
int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) { |
299 |
double radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0; |
300 |
return map_collides_gunner_pos(p, gunner_get_x(shooter), gunner_get_y(shooter), radius);
|
301 |
} |
302 |
int (map_make_dijkstra)(map_t *p, int16_t x, int16_t y){
|
303 |
|
304 |
const uint16_t W = basic_sprite_get_w(p->bsp_background),
|
305 |
H = basic_sprite_get_h(p->bsp_background); |
306 |
|
307 |
static uint8_t first_time = true; |
308 |
if(first_time){
|
309 |
for(size_t i = 0; i < W*H; ++i) p->prev[i] = -1; |
310 |
first_time = false;
|
311 |
} |
312 |
/// ACTUAL DIJKSTRA
|
313 |
queue_t *q = queue_ctor(); |
314 |
|
315 |
memset(p->visited, false, W*H*sizeof(uint8_t)); |
316 |
|
317 |
int *ptr;
|
318 |
int32_t c, pos; |
319 |
c = y*W+x; p->prev[c] = c; ptr = malloc(sizeof(int)); *ptr = c; queue_push(q, ptr); |
320 |
|
321 |
while(!queue_empty(q)){
|
322 |
c = *(int*)queue_top(q); free(queue_top(q)); queue_pop(q);
|
323 |
x = c%W, y = c/W; |
324 |
if(p->visited[c]) continue; |
325 |
p->visited[c] = true;
|
326 |
if(p->collide_gunner[c]) continue; |
327 |
if(0 <= x-1){ pos = y*W+(x-1); if(!p->visited[pos] && p->prev[pos] != c){ p->prev[pos] = c; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }} |
328 |
if(x+1 < W ){ pos = y*W+(x+1); if(!p->visited[pos] && p->prev[pos] != c){ p->prev[pos] = c; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }} |
329 |
if(0 <= y-1){ pos = (y-1)*W+x; if(!p->visited[pos] && p->prev[pos] != c){ p->prev[pos] = c; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }} |
330 |
if(y+1 < H ){ pos = (y+1)*W+x; if(!p->visited[pos] && p->prev[pos] != c){ p->prev[pos] = c; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }} |
331 |
} |
332 |
|
333 |
queue_dtor(q); |
334 |
|
335 |
return SUCCESS;
|
336 |
} |
337 |
int (map_where_to_follow)(const map_t *p, float x, float y, float *theta){ |
338 |
const uint16_t W = basic_sprite_get_w(p->bsp_background);
|
339 |
int x_ = x, y_ = y;
|
340 |
int pos = y_*W+x_;
|
341 |
//printf("Is in %d,%d\n", x_, y_);
|
342 |
int newx = p->prev[pos]%W, newy = p->prev[pos]/W;
|
343 |
//printf("from %d,%d to %d,%d\n", x_, y_, newx, newy);
|
344 |
*theta = atan2(-(newy-y_), newx-x_); |
345 |
return SUCCESS;
|
346 |
} |
347 |
|
348 |
int (map_collides_bullet)(const map_t *p, const bullet_t *bull){ |
349 |
double radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0; |
350 |
double bullet_x = bullet_get_x(bull);
|
351 |
double bullet_y = bullet_get_y(bull);
|
352 |
for (double x = -radius; x < radius; x += 1){ |
353 |
double y1 = sqrt(radius*radius - x*x);
|
354 |
double y2 = -y1;
|
355 |
if (map_collides_point(p, bullet_x + x, bullet_y + y1) || map_collides_point(p, bullet_x + x, bullet_y + y2)) return 1; |
356 |
} |
357 |
return 0; |
358 |
} |
359 |
|
360 |
int (gunner_collides_bullet)(const gunner_t *shooter, const bullet_t *bull){ |
361 |
if(bull->shooter == shooter) return false; |
362 |
|
363 |
double shooter_radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0; |
364 |
double shooter_x = gunner_get_x(shooter);
|
365 |
double shooter_y = gunner_get_y(shooter);
|
366 |
|
367 |
double bullet_radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0; |
368 |
double bullet_x = bullet_get_x(bull);
|
369 |
double bullet_y = bullet_get_y(bull);
|
370 |
|
371 |
double dx = shooter_x - bullet_x;
|
372 |
double dy = shooter_y - bullet_y;
|
373 |
double distance = sqrt(dx*dx + dy*dy);
|
374 |
return distance <= shooter_radius+bullet_radius;
|
375 |
} |
376 |
|
377 |
double (distance_gunners)(const gunner_t *shooter1, const gunner_t *shooter2) { |
378 |
double shooter1_x = gunner_get_x(shooter1);
|
379 |
double shooter1_y = gunner_get_y(shooter1);
|
380 |
|
381 |
double shooter2_x = gunner_get_x(shooter2);
|
382 |
double shooter2_y = gunner_get_y(shooter2);
|
383 |
|
384 |
double dx = shooter1_x - shooter2_x;
|
385 |
double dy = shooter1_y - shooter2_y;
|
386 |
return sqrt(dx*dx + dy*dy);
|
387 |
} |
388 |
|
389 |
int (gunner_collides_gunner)(const gunner_t *shooter1, const gunner_t *shooter2) { |
390 |
if (shooter1 == shooter2) return false; |
391 |
double shooter1_radius = max(sprite_get_w(shooter1->dude), sprite_get_h(shooter1->dude))/2.0; |
392 |
double shooter2_radius = max(sprite_get_w(shooter2->dude), sprite_get_h(shooter2->dude))/2.0; |
393 |
double distance = distance_gunners(shooter1, shooter2);
|
394 |
return distance <= shooter1_radius+shooter2_radius;
|
395 |
} |
396 |
|
397 |
void (map_draw)(map_t *p){
|
398 |
const int16_t x_screen = map_get_x_screen(p);
|
399 |
const int16_t y_screen = map_get_y_screen(p);
|
400 |
sprite_set_pos (p->background, x_screen, y_screen); |
401 |
sprite_set_scale(p->background, scale); |
402 |
sprite_draw (p->background); |
403 |
} |