Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I1802469f3baff4576f61accfb5a197d86a6a6964
29 lines
812 B
C
29 lines
812 B
C
#include "movement.h"
|
|
#include "enemy.h"
|
|
#include "map/map.h"
|
|
#include <stdbool.h>
|
|
|
|
// Check if position is occupied by player
|
|
static int is_player_at(Player *p, int x, int y) {
|
|
return (p->position.x == x && p->position.y == y);
|
|
}
|
|
|
|
MoveResult try_move_entity(Vec2 *p, Vec2 direction, Map *map, Player *player, Enemy *enemies, int enemy_count,
|
|
bool moving_is_player) {
|
|
int new_x = p->x + direction.x;
|
|
int new_y = p->y + direction.y;
|
|
|
|
if (!is_floor(map, new_x, new_y))
|
|
return MOVE_RESULT_BLOCKED_WALL;
|
|
|
|
if (is_enemy_at(enemies, enemy_count, new_x, new_y))
|
|
return MOVE_RESULT_BLOCKED_ENEMY;
|
|
if (!moving_is_player) {
|
|
if (is_player_at(player, new_x, new_y))
|
|
return MOVE_RESULT_BLOCKED_PLAYER;
|
|
}
|
|
|
|
p->x = new_x;
|
|
p->y = new_y;
|
|
return MOVE_RESULT_MOVED;
|
|
}
|