#include "entity.h" static int entity_collide_with(struct Entity *restrict, struct Entity *restrict, int mask); struct Entity * entity_collide(void *restrict entity, int mask) { struct Entity *const e = entity; int i; i = ENTITY_GRID_SIZE; while (i-- > 0) if (entity_collide_with(e, g_entity_grid[i], mask)) return g_entity_grid[i]; return NULL; } /* axis aligned bounding box collision checking */ static int entity_collide_with(struct Entity *restrict self, struct Entity *restrict other, int mask) { const int sx = self->x + self->hb_x; const int sy = self->y + self->hb_y; const int ox = other->x + other->hb_x; const int oy = other->y + other->hb_y; if (self == NULL || other == NULL || self == other || !(mask & other->layer)) return 0; return sx < ox + other->hb_w && sx + self->hb_w > ox && sy < oy + other->hb_h && sy + self->hb_h > oy; }