Revision 340
more changes in ent.h, ent.c
ent.c | ||
---|---|---|
124 | 124 |
rectangle_draw(p->red_bar); |
125 | 125 |
text_draw(p->txt); |
126 | 126 |
} |
127 |
void (gunner_draw_list)(list_t *shooter_list) { |
|
128 |
if (list_size(shooter_list) == 0) return; |
|
127 | 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 |
} |
|
128 | 136 |
double (gunner_distance)(const gunner_t *p1, const gunner_t *p2){ |
129 | 137 |
double dx = gunner_get_x(p1) - gunner_get_x(p2); |
130 | 138 |
double dy = gunner_get_y(p1) - gunner_get_y(p2); |
131 | 139 |
return sqrt(dx*dx+dy*dy); |
132 | 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 |
} |
|
133 | 148 |
|
134 | 149 |
struct bullet{ |
135 | 150 |
const gunner_t *shooter; |
... | ... | |
156 | 171 |
sprite_set_angle(ret->b, angle-M_PI_2); |
157 | 172 |
return ret; |
158 | 173 |
} |
159 |
|
|
160 | 174 |
void (bullet_dtor)(bullet_t *p){ |
161 | 175 |
if(p == NULL) return; |
162 | 176 |
sprite_dtor(p->b); |
... | ... | |
178 | 192 |
p->x += p->vx; |
179 | 193 |
p->y += p->vy; |
180 | 194 |
} |
181 |
|
|
182 | 195 |
void (bullet_update_movement_list)(list_t *bullet_list){ |
183 | 196 |
if (list_size(bullet_list) == 0) return; |
184 | 197 |
|
... | ... | |
188 | 201 |
it = list_node_next(it); |
189 | 202 |
} |
190 | 203 |
} |
191 |
|
|
192 | 204 |
void (bullet_draw)(bullet_t *p){ |
193 | 205 |
const int16_t x_screen = bullet_get_x_screen(p); |
194 | 206 |
const int16_t y_screen = bullet_get_y_screen(p); |
... | ... | |
196 | 208 |
sprite_set_scale(p->b, scale); |
197 | 209 |
sprite_draw (p->b); |
198 | 210 |
} |
199 |
|
|
200 |
void (gunner_draw_list)(list_t *shooter_list) { |
|
201 |
if (list_size(shooter_list) == 0) return; |
|
202 |
|
|
203 |
list_node_t *it = list_begin(shooter_list); |
|
204 |
while (it != list_end(shooter_list)) { |
|
205 |
gunner_draw(*(gunner_t**)list_node_val(it)); |
|
206 |
it = list_node_next(it); |
|
207 |
} |
|
208 |
} |
|
209 |
|
|
210 | 211 |
void (bullet_draw_list)(list_t *bullet_list) { |
211 | 212 |
if (list_size(bullet_list) == 0) return; |
212 | 213 |
|
... | ... | |
233 | 234 |
} |
234 | 235 |
return 0; |
235 | 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); } |
|
236 | 239 |
map_t* (map_ctor)(const char *const *background, const char *const *collide){ |
237 | 240 |
map_t *ret = malloc(sizeof(map_t)); |
238 | 241 |
if(ret == NULL) return NULL; |
... | ... | |
285 | 288 |
free(p->visited); |
286 | 289 |
free(p); |
287 | 290 |
} |
288 |
static int16_t (map_get_x_screen)(const map_t *p){ (void)p; return (int16_t)((-x_origin)*scale); } |
|
289 |
static int16_t (map_get_y_screen)(const map_t *p){ (void)p; return (int16_t)((-y_origin)*scale); } |
|
290 | 291 |
uint16_t (map_get_width) (const map_t *p){ return sprite_get_w(p->background); } |
291 | 292 |
uint16_t (map_get_height) (const map_t *p){ return sprite_get_h(p->background); } |
292 |
int (map_collides_point)(const map_t *p, double x, double y){ |
|
293 |
const uint16_t w = sprite_get_w(p->background), h = sprite_get_h(p->background); |
|
294 |
int16_t x_ = (int16_t)x, y_ = (int16_t)y; |
|
295 |
if(x_ < 0 || w <= x_ || y_ < 0 || h <= y_) return 0; |
|
296 |
int32_t pos = x_ + y_*w; |
|
297 |
if(0 <= pos && pos < w*h) return p->collide[pos]; |
|
298 |
else return false; |
|
299 |
} |
|
300 |
int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) { |
|
301 |
double radius = max_d(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0; |
|
302 |
return map_collides_gunner_pos(p, gunner_get_x(shooter), gunner_get_y(shooter), radius); |
|
303 |
} |
|
304 | 293 |
int (map_make_dijkstra)(map_t *p, double x_, double y_){ |
305 | 294 |
int16_t x = (int16_t)x_, y = (int16_t)y_; |
306 | 295 |
|
... | ... | |
345 | 334 |
*theta = atan2(-(newy-y_), newx-x_); |
346 | 335 |
return SUCCESS; |
347 | 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 |
} |
|
348 | 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 |
} |
|
349 | 357 |
int (map_collides_bullet)(const map_t *p, const bullet_t *bull){ |
350 | 358 |
double radius = max_d(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0; |
351 | 359 |
double bullet_x = bullet_get_x(bull); |
... | ... | |
357 | 365 |
} |
358 | 366 |
return 0; |
359 | 367 |
} |
360 |
|
|
361 | 368 |
int (gunner_collides_bullet)(const gunner_t *shooter, const bullet_t *bull){ |
362 | 369 |
if(bull->shooter == shooter) return false; |
363 | 370 |
|
... | ... | |
374 | 381 |
double distance = sqrt(dx*dx + dy*dy); |
375 | 382 |
return distance <= shooter_radius+bullet_radius; |
376 | 383 |
} |
384 |
void (get_random_spawn)(const map_t *map, gunner_t *p, list_t *l) { |
|
385 |
uint16_t w = map_get_width(map), h = map_get_height(map); |
|
386 |
double x, y; |
|
377 | 387 |
|
378 |
int (gunner_collides_gunner)(const gunner_t *shooter1, const gunner_t *shooter2) { |
|
379 |
if (shooter1 == shooter2) return false; |
|
380 |
double shooter1_radius = max_d(sprite_get_w(shooter1->dude), sprite_get_h(shooter1->dude))/2.0; |
|
381 |
double shooter2_radius = max_d(sprite_get_w(shooter2->dude), sprite_get_h(shooter2->dude))/2.0; |
|
382 |
double distance = gunner_distance(shooter1, shooter2); |
|
383 |
return distance <= shooter1_radius+shooter2_radius; |
|
388 |
while(true){ |
|
389 |
x = rand() % w; |
|
390 |
y = rand() % h; |
|
391 |
gunner_set_pos(p, x, y); |
|
392 |
if(map_collides_gunner(map, p)) continue; |
|
393 |
int collides = false; |
|
394 |
list_node_t *it = list_begin(l); |
|
395 |
while(it != list_end(l)){ |
|
396 |
if(gunner_collides_gunner(p, *list_node_val(it))){ |
|
397 |
collides = true; |
|
398 |
break; |
|
399 |
} |
|
400 |
it = list_node_next(it); |
|
401 |
} |
|
402 |
if(!collides) return; |
|
403 |
} |
|
384 | 404 |
} |
385 |
|
|
386 |
void (map_draw)(map_t *p){ |
|
387 |
const int16_t x_screen = map_get_x_screen(p); |
|
388 |
const int16_t y_screen = map_get_y_screen(p); |
|
389 |
sprite_set_pos (p->background, x_screen, y_screen); |
|
390 |
sprite_set_scale(p->background, scale); |
|
391 |
sprite_draw (p->background); |
|
392 |
} |
Also available in: Unified diff