46 lines
1.1 KiB
C
46 lines
1.1 KiB
C
#include "map.h"
|
|
#include <stdlib.h>
|
|
|
|
frect_t tile_shape(struct tile const *tile)
|
|
{
|
|
if(!tile->solid) return (frect_t){ 0 };
|
|
|
|
return (frect_t){
|
|
.l = -fix(0.5),
|
|
.r = fix(0.5),
|
|
.t = -fix(0.5),
|
|
.b = fix(0.5),
|
|
};
|
|
}
|
|
|
|
struct tile *map_tile(map_t const *m, int x, int y)
|
|
{
|
|
if((unsigned)x >= (unsigned)m->width || (unsigned)y >= (unsigned)m->height)
|
|
return NULL;
|
|
|
|
return &m->tiles[y * m->width + x];
|
|
}
|
|
|
|
bool map_collides(map_t const *m, frect_t hitbox)
|
|
{
|
|
int y_min = ffloor(hitbox.t);
|
|
int y_max = fceil(hitbox.b);
|
|
int x_min = ffloor(hitbox.l);
|
|
int x_max = fceil(hitbox.r);
|
|
|
|
/* Collisions against walls and static objects */
|
|
for(int y = y_min; y < y_max; y++)
|
|
for(int x = x_min; x < x_max; x++) {
|
|
struct tile *t = map_tile(m, x, y);
|
|
if(!t || !t->solid) continue;
|
|
|
|
fpoint_t center = { fix(x) + fix(0.5), fix(y) + fix(0.5) };
|
|
frect_t tile_hitbox = rect_translate(tile_shape(t), center);
|
|
|
|
if(rect_collide(hitbox, tile_hitbox))
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|