Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: Id81f32d86f70a7df99c2ad3d478646416a6a6964
52 lines
1.4 KiB
C
52 lines
1.4 KiB
C
#include "movement.h"
|
|
#include "enemy.h"
|
|
#include "map/map.h"
|
|
#include "map/utils.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);
|
|
}
|
|
|
|
static int direction_to_door_open_from(Vec2 dir) {
|
|
if (dir.y == -1)
|
|
return 0; // N
|
|
if (dir.y == 1)
|
|
return 1; // S
|
|
if (dir.x == 1)
|
|
return 2; // E
|
|
if (dir.x == -1)
|
|
return 3; // W
|
|
return 255;
|
|
}
|
|
|
|
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 (!in_bounds(new_x, new_y, MAP_WIDTH, MAP_HEIGHT))
|
|
return MOVE_RESULT_BLOCKED_WALL;
|
|
|
|
if (map->tiles[new_y][new_x] == TILE_DOOR_CLOSED) {
|
|
map->tiles[new_y][new_x] = TILE_DOOR_OPEN;
|
|
map->door_open_from[new_y][new_x] = direction_to_door_open_from(direction);
|
|
map->door_anim_target[new_y][new_x] = 1;
|
|
map->door_anim_timer[new_y][new_x] = DOOR_ANIM_FRAMES;
|
|
}
|
|
|
|
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;
|
|
}
|