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