Merge pull request 'dev: added step_count and health recovery' (#1) from amr/rogged:recover-health into main
Reviewed-on: #1 Reviewed-by: raf <raf@notashelf.dev>
This commit is contained in:
commit
4908a32661
2 changed files with 126 additions and 113 deletions
|
|
@ -44,6 +44,7 @@ typedef struct {
|
|||
int attack;
|
||||
int defense;
|
||||
int floor;
|
||||
int step_count;
|
||||
Item inventory[MAX_INVENTORY];
|
||||
int inventory_count;
|
||||
} Player;
|
||||
|
|
|
|||
54
src/player.c
54
src/player.c
|
|
@ -1,12 +1,12 @@
|
|||
#include "player.h"
|
||||
#include "combat.h"
|
||||
#include "common.h"
|
||||
#include "items.h"
|
||||
#include "map.h"
|
||||
#include "utils.h"
|
||||
#include "combat.h"
|
||||
#include "items.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->y = y;
|
||||
p->hp = PLAYER_BASE_HP;
|
||||
|
|
@ -14,6 +14,7 @@ void player_init(Player* p, int x, int y) {
|
|||
p->attack = PLAYER_BASE_ATTACK;
|
||||
p->defense = 0;
|
||||
p->floor = 1;
|
||||
p->step_count = 0;
|
||||
p->inventory_count = 0;
|
||||
|
||||
// Initialize inventory to empty
|
||||
|
|
@ -23,7 +24,7 @@ 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) {
|
||||
static Enemy *get_enemy_at(Enemy *enemies, int count, int x, int y) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (enemies[i].alive && enemies[i].x == x && enemies[i].y == y) {
|
||||
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
|
||||
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++) {
|
||||
if (!items[i].picked_up && items[i].x == x && items[i].y == y) {
|
||||
return &items[i];
|
||||
|
|
@ -42,7 +43,8 @@ static Item* get_item_at(Item* items, int count, int x, int y) {
|
|||
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_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
|
||||
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) {
|
||||
// Attack the 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
|
||||
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) {
|
||||
// Pick up the 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
|
||||
p->x = new_x;
|
||||
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;
|
||||
}
|
||||
|
||||
void player_attack(Player* p, Enemy* e) {
|
||||
void player_attack(Player *p, Enemy *e) {
|
||||
// Use combat system
|
||||
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) {
|
||||
return; // inventory full
|
||||
}
|
||||
|
|
@ -97,9 +103,11 @@ void player_pickup(Player* p, Item* i) {
|
|||
p->inventory_count++;
|
||||
}
|
||||
|
||||
void player_use_item(Player* p, Item* i) {
|
||||
if (p == NULL || i == NULL) return;
|
||||
if (i->picked_up) return; // invalid item
|
||||
void player_use_item(Player *p, Item *i) {
|
||||
if (p == NULL || i == NULL)
|
||||
return;
|
||||
if (i->picked_up)
|
||||
return; // invalid item
|
||||
|
||||
// Apply item effect
|
||||
item_use(p, i);
|
||||
|
|
@ -108,13 +116,14 @@ void player_use_item(Player* p, Item* i) {
|
|||
i->picked_up = 1;
|
||||
}
|
||||
|
||||
int player_use_first_item(Player* p) {
|
||||
if (p == NULL || p->inventory_count == 0) return 0;
|
||||
int player_use_first_item(Player *p) {
|
||||
if (p == NULL || p->inventory_count == 0)
|
||||
return 0;
|
||||
|
||||
// Find first valid item in inventory
|
||||
for (int i = 0; i < MAX_INVENTORY; i++) {
|
||||
if (!p->inventory[i].picked_up) {
|
||||
Item* item = &p->inventory[i];
|
||||
Item *item = &p->inventory[i];
|
||||
|
||||
// Apply item effect
|
||||
item_use(p, item);
|
||||
|
|
@ -134,9 +143,12 @@ int player_use_first_item(Player* p) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
Item* player_get_inventory_item(Player* p, int index) {
|
||||
if (p == NULL) return NULL;
|
||||
if (index < 0 || index >= MAX_INVENTORY) return NULL;
|
||||
if (p->inventory[index].picked_up) return NULL; // invalid/empty
|
||||
Item *player_get_inventory_item(Player *p, int index) {
|
||||
if (p == NULL)
|
||||
return NULL;
|
||||
if (index < 0 || index >= MAX_INVENTORY)
|
||||
return NULL;
|
||||
if (p->inventory[index].picked_up)
|
||||
return NULL; // invalid/empty
|
||||
return &p->inventory[index];
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue