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