freak-engine/src/entity/move.c

44 lines
960 B
C

#include "entity.h"
#include "util.h"
/* only provide non-nul value on `move_x` OR `move_y` */
static int entity_move_single_axis(struct Entity *, int mask, int move_x,
int move_y);
int
entity_move(struct Entity *e, int mask, int move_x, int move_y)
{
int collided = 0;
collided |= entity_move_single_axis(e, mask, move_x, 0);
collided |= entity_move_single_axis(e, mask, 0, move_y) << 1;
return collided;
}
static int
entity_move_single_axis(struct Entity *e, int mask, int move_x, int move_y)
{
struct Entity *other;
int collided = 0;
const int sign_move_x = sign(move_x);
const int sign_move_y = sign(move_y);
if (!move_x && !move_y)
return 0;
e->x += move_x;
e->y += move_y;
do {
other = entity_collide(e, mask);
if (other != NULL)
collided = 1;
while (entity_collide_with(e, other, mask)) {
e->x -= sign_move_x;
e->y -= sign_move_y;
}
} while (other != NULL);
return collided;
}