1
0
Fork 0
forked from NotAShelf/rogged

Merge pull request 'dev: added step_count and health recovery' (#1) from amr/rogged:recover-health into main

Reviewed-on: NotAShelf/rogged#1
Reviewed-by: raf <raf@notashelf.dev>
This commit is contained in:
raf 2026-03-20 19:52:19 +00:00
commit 4908a32661
2 changed files with 126 additions and 113 deletions

View file

@ -44,6 +44,7 @@ typedef struct {
int attack; int attack;
int defense; int defense;
int floor; int floor;
int step_count;
Item inventory[MAX_INVENTORY]; Item inventory[MAX_INVENTORY];
int inventory_count; int inventory_count;
} Player; } Player;

View file

@ -1,12 +1,12 @@
#include "player.h" #include "player.h"
#include "combat.h"
#include "common.h" #include "common.h"
#include "items.h"
#include "map.h" #include "map.h"
#include "utils.h" #include "utils.h"
#include "combat.h"
#include "items.h"
#include <stdlib.h> #include <stdlib.h>
void player_init(Player* p, int x, int y) { void player_init(Player *p, int x, int y) {
p->x = x; p->x = x;
p->y = y; p->y = y;
p->hp = PLAYER_BASE_HP; p->hp = PLAYER_BASE_HP;
@ -14,6 +14,7 @@ void player_init(Player* p, int x, int y) {
p->attack = PLAYER_BASE_ATTACK; p->attack = PLAYER_BASE_ATTACK;
p->defense = 0; p->defense = 0;
p->floor = 1; p->floor = 1;
p->step_count = 0;
p->inventory_count = 0; p->inventory_count = 0;
// Initialize inventory to empty // Initialize inventory to empty
@ -23,7 +24,7 @@ void player_init(Player* p, int x, int y) {
} }
// Check if position has an enemy // Check if position has an enemy
static Enemy* get_enemy_at(Enemy* enemies, int count, int x, int y) { static Enemy *get_enemy_at(Enemy *enemies, int count, int x, int y) {
for (int i = 0; i < count; i++) { 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 &enemies[i];
@ -33,7 +34,7 @@ static Enemy* get_enemy_at(Enemy* enemies, int count, int x, int y) {
} }
// Check if position has an item // Check if position has an item
static Item* get_item_at(Item* items, int count, int x, int y) { static Item *get_item_at(Item *items, int count, int x, int y) {
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
if (!items[i].picked_up && items[i].x == x && items[i].y == y) { if (!items[i].picked_up && items[i].x == x && items[i].y == y) {
return &items[i]; return &items[i];
@ -42,7 +43,8 @@ static Item* get_item_at(Item* items, int count, int x, int y) {
return NULL; return NULL;
} }
int player_move(Player* p, int dx, int dy, Map* map, Enemy* enemies, int enemy_count, Item* items, int item_count) { int player_move(Player *p, int dx, int dy, Map *map, Enemy *enemies,
int enemy_count, Item *items, int item_count) {
int new_x = p->x + dx; int new_x = p->x + dx;
int new_y = p->y + dy; int new_y = p->y + dy;
@ -57,7 +59,7 @@ int player_move(Player* p, int dx, int dy, Map* map, Enemy* enemies, int enemy_c
} }
// Check for enemy at target position // Check for enemy at target position
Enemy* enemy = get_enemy_at(enemies, enemy_count, new_x, new_y); Enemy *enemy = get_enemy_at(enemies, enemy_count, new_x, new_y);
if (enemy != NULL) { if (enemy != NULL) {
// Attack the enemy // Attack the enemy
player_attack(p, enemy); player_attack(p, enemy);
@ -65,7 +67,7 @@ int player_move(Player* p, int dx, int dy, Map* map, Enemy* enemies, int enemy_c
} }
// Check for item at target position // Check for item at target position
Item* item = get_item_at(items, item_count, new_x, new_y); Item *item = get_item_at(items, item_count, new_x, new_y);
if (item != NULL) { if (item != NULL) {
// Pick up the item // Pick up the item
player_pickup(p, item); player_pickup(p, item);
@ -74,16 +76,20 @@ int player_move(Player* p, int dx, int dy, Map* map, Enemy* enemies, int enemy_c
// Move player // Move player
p->x = new_x; p->x = new_x;
p->y = new_y; p->y = new_y;
p->step_count += 1;
if (p->step_count % 15 == 0 && p->hp < p->max_hp) {
p->hp += 1;
// p->hunger -= 0.1; ??
}
return 1; return 1;
} }
void player_attack(Player* p, Enemy* e) { void player_attack(Player *p, Enemy *e) {
// Use combat system // Use combat system
combat_player_attack(p, e); combat_player_attack(p, e);
} }
void player_pickup(Player* p, Item* i) { void player_pickup(Player *p, Item *i) {
if (p->inventory_count >= MAX_INVENTORY) { if (p->inventory_count >= MAX_INVENTORY) {
return; // inventory full return; // inventory full
} }
@ -97,9 +103,11 @@ void player_pickup(Player* p, Item* i) {
p->inventory_count++; p->inventory_count++;
} }
void player_use_item(Player* p, Item* i) { void player_use_item(Player *p, Item *i) {
if (p == NULL || i == NULL) return; if (p == NULL || i == NULL)
if (i->picked_up) return; // invalid item return;
if (i->picked_up)
return; // invalid item
// Apply item effect // Apply item effect
item_use(p, i); item_use(p, i);
@ -108,13 +116,14 @@ void player_use_item(Player* p, Item* i) {
i->picked_up = 1; i->picked_up = 1;
} }
int player_use_first_item(Player* p) { int player_use_first_item(Player *p) {
if (p == NULL || p->inventory_count == 0) return 0; if (p == NULL || p->inventory_count == 0)
return 0;
// Find first valid item in inventory // Find first valid item in inventory
for (int i = 0; i < MAX_INVENTORY; i++) { for (int i = 0; i < MAX_INVENTORY; i++) {
if (!p->inventory[i].picked_up) { if (!p->inventory[i].picked_up) {
Item* item = &p->inventory[i]; Item *item = &p->inventory[i];
// Apply item effect // Apply item effect
item_use(p, item); item_use(p, item);
@ -134,9 +143,12 @@ int player_use_first_item(Player* p) {
return 0; return 0;
} }
Item* player_get_inventory_item(Player* p, int index) { Item *player_get_inventory_item(Player *p, int index) {
if (p == NULL) return NULL; if (p == NULL)
if (index < 0 || index >= MAX_INVENTORY) return NULL; return NULL;
if (p->inventory[index].picked_up) return NULL; // invalid/empty if (index < 0 || index >= MAX_INVENTORY)
return NULL;
if (p->inventory[index].picked_up)
return NULL; // invalid/empty
return &p->inventory[index]; return &p->inventory[index];
} }