Update ranged weapon quickslot text to match stackable items
This commit is contained in:
113
src/engine/__tests__/inventory.test.ts
Normal file
113
src/engine/__tests__/inventory.test.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
import { describe, it, expect, beforeEach } from "vitest";
|
||||
import { ItemManager } from "../../scenes/systems/ItemManager";
|
||||
import type { World, CombatantActor, Item } from "../../core/types";
|
||||
import { EntityManager } from "../../engine/EntityManager";
|
||||
|
||||
describe("ItemManager - Stacking Logic", () => {
|
||||
let itemManager: ItemManager;
|
||||
let entityManager: EntityManager;
|
||||
let world: World;
|
||||
let player: CombatantActor;
|
||||
|
||||
beforeEach(() => {
|
||||
world = {
|
||||
width: 10,
|
||||
height: 10,
|
||||
tiles: [],
|
||||
actors: new Map(),
|
||||
exit: { x: 9, y: 9 }
|
||||
} as any;
|
||||
|
||||
entityManager = new EntityManager(world);
|
||||
itemManager = new ItemManager(world, entityManager);
|
||||
|
||||
player = {
|
||||
id: 0,
|
||||
pos: { x: 1, y: 1 },
|
||||
category: "combatant",
|
||||
isPlayer: true,
|
||||
type: "player",
|
||||
inventory: { gold: 0, items: [] },
|
||||
stats: {} as any,
|
||||
equipment: {} as any,
|
||||
speed: 1,
|
||||
energy: 0
|
||||
};
|
||||
world.actors.set(0, player);
|
||||
});
|
||||
|
||||
it("should stack stackable items when picked up", () => {
|
||||
const potion: Item = {
|
||||
id: "potion",
|
||||
name: "Potion",
|
||||
type: "Consumable",
|
||||
textureKey: "items",
|
||||
spriteIndex: 0,
|
||||
stackable: true,
|
||||
quantity: 1
|
||||
};
|
||||
|
||||
// First potion
|
||||
itemManager.spawnItem(potion, { x: 1, y: 1 });
|
||||
itemManager.tryPickup(player);
|
||||
|
||||
expect(player.inventory!.items.length).toBe(1);
|
||||
expect(player.inventory!.items[0].quantity).toBe(1);
|
||||
|
||||
// Second potion
|
||||
itemManager.spawnItem(potion, { x: 1, y: 1 });
|
||||
itemManager.tryPickup(player);
|
||||
|
||||
expect(player.inventory!.items.length).toBe(1);
|
||||
expect(player.inventory!.items[0].quantity).toBe(2);
|
||||
});
|
||||
|
||||
it("should NOT stack non-stackable items", () => {
|
||||
const sword: Item = {
|
||||
id: "sword",
|
||||
name: "Sword",
|
||||
type: "Weapon",
|
||||
weaponType: "melee",
|
||||
textureKey: "items",
|
||||
spriteIndex: 1,
|
||||
stackable: false,
|
||||
stats: { attack: 1 }
|
||||
} as any;
|
||||
|
||||
// First sword
|
||||
itemManager.spawnItem(sword, { x: 1, y: 1 });
|
||||
itemManager.tryPickup(player);
|
||||
|
||||
expect(player.inventory!.items.length).toBe(1);
|
||||
|
||||
// Second sword
|
||||
itemManager.spawnItem(sword, { x: 1, y: 1 });
|
||||
itemManager.tryPickup(player);
|
||||
|
||||
expect(player.inventory!.items.length).toBe(2);
|
||||
});
|
||||
|
||||
it("should sum quantities of stackable items correctly", () => {
|
||||
const ammo: Item = {
|
||||
id: "ammo",
|
||||
name: "Ammo",
|
||||
type: "Ammo",
|
||||
textureKey: "items",
|
||||
spriteIndex: 2,
|
||||
stackable: true,
|
||||
quantity: 10,
|
||||
ammoType: "9mm"
|
||||
};
|
||||
|
||||
itemManager.spawnItem(ammo, { x: 1, y: 1 });
|
||||
itemManager.tryPickup(player);
|
||||
|
||||
expect(player.inventory!.items[0].quantity).toBe(10);
|
||||
|
||||
const moreAmmo = { ...ammo, quantity: 5 };
|
||||
itemManager.spawnItem(moreAmmo, { x: 1, y: 1 });
|
||||
itemManager.tryPickup(player);
|
||||
|
||||
expect(player.inventory!.items[0].quantity).toBe(15);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user