RogueLife/src/map.c

46 lines
1.1 KiB
C
Raw Normal View History

#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(1)/2,
.r = fix(1)/2,
.t = -fix(1)/2,
.b = fix(1)/2,
};
}
2021-06-04 15:14:12 +02:00
struct tile *map_tile(map_t const *m, int x, int y)
{
2021-06-15 17:27:30 +02:00
if((unsigned)x >= (unsigned)m->width || (unsigned)y >= (unsigned)m->height)
return NULL;
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-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);
/* 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++) {
struct tile *t = map_tile(m, x, y);
if(!t || !t->solid) continue;
fpoint_t center = { fix(x) + fix(1)/2, fix(y) + fix(1)/2 };
frect_t tile_hitbox = rect_translate(tile_shape(t), center);
if(rect_collide(hitbox, tile_hitbox))
return true;
}
return false;
}