refactor(movement): generalize movement and use vectors

This commit is contained in:
Squirrel Modeller 2026-04-09 14:28:41 +02:00
commit d01a54161d
No known key found for this signature in database
GPG key ID: C9FBA7B8C387BF70
9 changed files with 134 additions and 85 deletions

View file

@ -2,15 +2,13 @@
#include "combat.h"
#include "common.h"
#include "items.h"
#include "map.h"
#include "movement.h"
#include "settings.h"
#include "utils.h"
#include <stdlib.h>
#include <string.h>
void player_init(Player *p, int x, int y) {
p->x = x;
p->y = y;
p->position.x = x;
p->position.y = y;
p->hp = PLAYER_BASE_HP;
p->max_hp = PLAYER_BASE_HP;
p->attack = PLAYER_BASE_ATTACK;
@ -43,29 +41,18 @@ Enemy *player_find_enemy_at(Enemy *enemies, int count, int x, int y) {
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].position.x == x && enemies[i].position.y == y)
return &enemies[i];
}
return NULL;
}
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))
int player_move(Player *p, Vec2 direction, Map *map, Enemy *enemies, int enemy_count) {
MoveResult result = try_move_entity(&p->position, direction, map, p, enemies, enemy_count, true);
if (result != MOVE_RESULT_MOVED)
return 0;
// Check if walkable
if (!is_floor(map, new_x, new_y))
return 0;
// Move player
p->x = new_x;
p->y = new_y;
p->step_count += 1;
// Regen suppressed while poisoned, bleeding, or burning
if (p->step_count % REGEN_STEP_INTERVAL == 0 && p->hp < p->max_hp &&
!combat_has_effect(p->effects, p->effect_count, EFFECT_POISON) &&
!combat_has_effect(p->effects, p->effect_count, EFFECT_BLEED) &&
@ -75,6 +62,16 @@ int player_move(Player *p, int dx, int dy, Map *map) {
return 1;
}
void player_on_move(Player *p) {
p->step_count += 1;
if (p->step_count % REGEN_STEP_INTERVAL == 0 && p->hp < p->max_hp &&
!combat_has_effect(p->effects, p->effect_count, EFFECT_POISON) &&
!combat_has_effect(p->effects, p->effect_count, EFFECT_BLEED) &&
!combat_has_effect(p->effects, p->effect_count, EFFECT_BURN)) {
p->hp += 1;
}
}
void player_attack(Player *p, Enemy *e) {
// Use combat system
combat_player_attack(p, e);
@ -228,8 +225,8 @@ int player_drop_item(Player *p, int inv_index, Item *items, int item_count) {
if (items[i].picked_up) {
// Place dropped item at this position
items[i] = *item;
items[i].x = p->x;
items[i].y = p->y;
items[i].x = p->position.x;
items[i].y = p->position.y;
items[i].picked_up = 0;
// Remove from inventory
player_remove_inventory_item(p, inv_index);