feat: handle stacking in inventory and show item count and current/max ammo of ranged weapons
This commit is contained in:
@@ -66,33 +66,47 @@ export class ItemManager {
|
||||
|
||||
if (itemActor) {
|
||||
const item = itemActor.item;
|
||||
|
||||
// Stacking Logic
|
||||
if (item.stackable) {
|
||||
const existingItem = player.inventory.items.find(it => it.id === item.id);
|
||||
if (existingItem) {
|
||||
existingItem.quantity = (existingItem.quantity || 1) + (item.quantity || 1);
|
||||
console.log(`Stacked ${item.name}. New quantity: ${existingItem.quantity}`);
|
||||
|
||||
this.entityManager.removeActor(itemActor.id);
|
||||
return existingItem;
|
||||
}
|
||||
}
|
||||
|
||||
// Add to inventory
|
||||
item.quantity = item.quantity || 1;
|
||||
player.inventory.items.push(item);
|
||||
const result = this.addItem(player, item);
|
||||
|
||||
// Remove from world
|
||||
this.entityManager.removeActor(itemActor.id);
|
||||
|
||||
console.log("Picked up:", item.name);
|
||||
return item;
|
||||
return result;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an item to player inventory, handling stacking if applicable
|
||||
* @returns The added or modified item
|
||||
*/
|
||||
addItem(player: CombatantActor, item: Item): Item {
|
||||
if (!player.inventory) throw new Error("Player has no inventory");
|
||||
|
||||
// Deep clone item (crucial for items with mutable stats like ammo or when picking up from ground)
|
||||
const itemToAdd = { ...item } as Item;
|
||||
if ('stats' in itemToAdd && itemToAdd.stats) {
|
||||
(itemToAdd as any).stats = { ...itemToAdd.stats };
|
||||
}
|
||||
|
||||
// Stacking Logic
|
||||
if (itemToAdd.stackable) {
|
||||
const existingItem = player.inventory.items.find(it => it.id === itemToAdd.id);
|
||||
if (existingItem) {
|
||||
existingItem.quantity = (existingItem.quantity || 1) + (itemToAdd.quantity || 1);
|
||||
console.log(`Stacked ${itemToAdd.name}. New quantity: ${existingItem.quantity}`);
|
||||
return existingItem;
|
||||
}
|
||||
}
|
||||
|
||||
// Add to inventory
|
||||
itemToAdd.quantity = itemToAdd.quantity || 1;
|
||||
player.inventory.items.push(itemToAdd);
|
||||
return itemToAdd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle using an item from inventory
|
||||
* Returns information about what happened
|
||||
|
||||
Reference in New Issue
Block a user