feat: handle stacking in inventory and show item count and current/max ammo of ranged weapons
This commit is contained in:
@@ -52,7 +52,12 @@ export function generateWorld(floor: number, runState: RunState): { world: World
|
||||
items: [
|
||||
...runState.inventory.items,
|
||||
// Add starting items for testing if empty
|
||||
...(runState.inventory.items.length === 0 ? [ITEMS["health_potion"], ITEMS["health_potion"], ITEMS["iron_sword"], ITEMS["throwing_dagger"], ITEMS["throwing_dagger"], ITEMS["throwing_dagger"], ITEMS["pistol"]] : [])
|
||||
...(runState.inventory.items.length === 0 ? [
|
||||
{ ...ITEMS["health_potion"], quantity: 2 },
|
||||
ITEMS["iron_sword"],
|
||||
{ ...ITEMS["throwing_dagger"], quantity: 3 },
|
||||
ITEMS["pistol"]
|
||||
] : [])
|
||||
]
|
||||
},
|
||||
energy: 0
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -236,6 +236,26 @@ export class InventoryOverlay extends OverlayComponent {
|
||||
sprite.setScale(2.2); // Scale to fit nicely in 44px slots
|
||||
|
||||
slot.add(sprite);
|
||||
|
||||
// Add Count Label (Bottom-Right)
|
||||
let labelText = "";
|
||||
if (item.stackable) {
|
||||
labelText = `x${item.quantity || 1}`;
|
||||
} else if (item.type === "Weapon" && item.weaponType === "ranged" && item.stats) {
|
||||
labelText = `${item.stats.currentAmmo}/${item.stats.magazineSize}`;
|
||||
}
|
||||
|
||||
if (labelText) {
|
||||
const slotSize = 44;
|
||||
const display = this.scene.add.text(slotSize / 2 - 3, slotSize / 2 - 3, labelText, {
|
||||
fontSize: "11px",
|
||||
color: "#ffffff",
|
||||
fontStyle: "bold",
|
||||
stroke: "#000000",
|
||||
strokeThickness: 2
|
||||
}).setOrigin(1, 1);
|
||||
slot.add(display);
|
||||
}
|
||||
|
||||
// Add interactivity
|
||||
slot.setInteractive(new Phaser.Geom.Rectangle(-22, -22, 44, 44), Phaser.Geom.Rectangle.Contains);
|
||||
|
||||
Reference in New Issue
Block a user