2021-05-30 21:59:09 +02:00
|
|
|
#include "map.h"
|
2021-06-01 17:49:29 +02:00
|
|
|
#include <stdlib.h>
|
2021-05-30 21:59:09 +02:00
|
|
|
|
2021-06-10 22:48:27 +02:00
|
|
|
frect_t tile_shape(struct tile const *tile)
|
2021-05-30 21:59:09 +02:00
|
|
|
{
|
2021-06-10 22:48:27 +02:00
|
|
|
if(!tile->solid) return (frect_t){ 0 };
|
|
|
|
|
|
|
|
return (frect_t){
|
2021-07-16 18:49:58 +02:00
|
|
|
.l = -fix(0.5),
|
|
|
|
.r = fix(0.5),
|
|
|
|
.t = -fix(0.5),
|
|
|
|
.b = fix(0.5),
|
2021-06-10 22:48:27 +02:00
|
|
|
};
|
2021-06-01 17:49:29 +02:00
|
|
|
}
|
|
|
|
|
2021-06-04 15:14:12 +02:00
|
|
|
struct tile *map_tile(map_t const *m, int x, int y)
|
2021-06-01 17:49:29 +02:00
|
|
|
{
|
2021-06-15 17:27:30 +02:00
|
|
|
if((unsigned)x >= (unsigned)m->width || (unsigned)y >= (unsigned)m->height)
|
2021-05-30 21:59:09 +02:00
|
|
|
return NULL;
|
|
|
|
|
2021-06-01 17:49:29 +02:00
|
|
|
return &m->tiles[y * m->width + x];
|
|
|
|
}
|
|
|
|
|
2021-06-15 17:27:30 +02:00
|
|
|
bool map_collides(map_t const *m, frect_t hitbox)
|
2021-06-01 17:49:29 +02:00
|
|
|
{
|
2021-06-15 17:27:30 +02:00
|
|
|
int y_min = ffloor(hitbox.t);
|
|
|
|
int y_max = fceil(hitbox.b);
|
|
|
|
int x_min = ffloor(hitbox.l);
|
|
|
|
int x_max = fceil(hitbox.r);
|
|
|
|
|
2021-06-01 22:06:58 +02:00
|
|
|
/* Collisions against walls and static objects */
|
2021-06-15 17:27:30 +02:00
|
|
|
for(int y = y_min; y < y_max; y++)
|
|
|
|
for(int x = x_min; x < x_max; x++) {
|
2021-06-01 22:06:58 +02:00
|
|
|
struct tile *t = map_tile(m, x, y);
|
|
|
|
if(!t || !t->solid) continue;
|
|
|
|
|
2021-07-16 18:49:58 +02:00
|
|
|
fpoint_t center = { fix(x) + fix(0.5), fix(y) + fix(0.5) };
|
2021-06-10 22:48:27 +02:00
|
|
|
frect_t tile_hitbox = rect_translate(tile_shape(t), center);
|
2021-06-01 22:06:58 +02:00
|
|
|
|
2021-06-10 22:48:27 +02:00
|
|
|
if(rect_collide(hitbox, tile_hitbox))
|
2021-06-01 22:06:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|