refactor: split player_move and decompose handle_input

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Iaac0cda778dd541eb34980f3e902ca726a6a6964
This commit is contained in:
raf 2026-04-05 20:11:37 +03:00
commit ee116ef33f
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
3 changed files with 228 additions and 230 deletions

View file

@ -37,37 +37,29 @@ void player_init(Player *p, int x, int y) {
}
}
// Check if position has an enemy
static Enemy *get_enemy_at(Enemy *enemies, int count, int x, int y) {
Enemy *player_find_enemy_at(Enemy *enemies, int count, int x, int y) {
if (enemies == NULL || count <= 0)
return NULL;
if (count > MAX_ENEMIES)
count = MAX_ENEMIES;
for (int i = 0; i < count; i++) {
if (enemies[i].alive && enemies[i].x == x && enemies[i].y == y) {
if (enemies[i].alive && enemies[i].x == x && enemies[i].y == y)
return &enemies[i];
}
}
return NULL;
}
int player_move(Player *p, int dx, int dy, Map *map, Enemy *enemies, int enemy_count) {
int player_move(Player *p, int dx, int dy, Map *map) {
int new_x = p->x + dx;
int new_y = p->y + dy;
// Check bounds
if (!in_bounds(new_x, new_y, MAP_WIDTH, MAP_HEIGHT)) {
if (!in_bounds(new_x, new_y, MAP_WIDTH, MAP_HEIGHT))
return 0;
}
// Check if walkable
if (!is_floor(map, new_x, new_y)) {
if (!is_floor(map, new_x, new_y))
return 0;
}
// Check for enemy at target position
Enemy *enemy = get_enemy_at(enemies, enemy_count, new_x, new_y);
if (enemy != NULL) {
// Attack the enemy
player_attack(p, enemy);
return 1;
}
// Move player
p->x = new_x;