forked from NotAShelf/rogged
player: correct item pickup order to copy before marking picked_up
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: Ie226b4a0ae6566ca77ab49b7d22f36726a6a6964
This commit is contained in:
parent
5fbe7c8c60
commit
c61e31f628
1 changed files with 11 additions and 9 deletions
20
src/player.c
20
src/player.c
|
|
@ -19,7 +19,7 @@ void player_init(Player *p, int x, int y) {
|
|||
|
||||
// Initialize inventory to empty
|
||||
for (int i = 0; i < MAX_INVENTORY; i++) {
|
||||
p->inventory[i].picked_up = 1; // mark as invalid
|
||||
p->inventory[i].picked_up = 1; // mark as invalid
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -43,8 +43,7 @@ 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;
|
||||
|
||||
|
|
@ -91,23 +90,26 @@ void player_attack(Player *p, Enemy *e) {
|
|||
|
||||
void player_pickup(Player *p, Item *i) {
|
||||
if (p->inventory_count >= MAX_INVENTORY) {
|
||||
return; // inventory full
|
||||
return; // inventory full
|
||||
}
|
||||
|
||||
if (i->picked_up) {
|
||||
return; // already picked up
|
||||
return; // already picked up
|
||||
}
|
||||
|
||||
i->picked_up = 1;
|
||||
p->inventory[p->inventory_count] = *i; // copy item to inventory
|
||||
// Copy item to inventory
|
||||
p->inventory[p->inventory_count] = *i;
|
||||
p->inventory_count++;
|
||||
|
||||
// Mark original as picked up
|
||||
i->picked_up = 1;
|
||||
}
|
||||
|
||||
void player_use_item(Player *p, Item *i) {
|
||||
if (p == NULL || i == NULL)
|
||||
return;
|
||||
if (i->picked_up)
|
||||
return; // invalid item
|
||||
return; // invalid item
|
||||
|
||||
// Apply item effect
|
||||
item_use(p, i);
|
||||
|
|
@ -149,6 +151,6 @@ Item *player_get_inventory_item(Player *p, int index) {
|
|||
if (index < 0 || index >= MAX_INVENTORY)
|
||||
return NULL;
|
||||
if (p->inventory[index].picked_up)
|
||||
return NULL; // invalid/empty
|
||||
return NULL; // invalid/empty
|
||||
return &p->inventory[index];
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue