/** * 格子背包系统:支持堆叠合并、拆分、交换、消耗、标签过滤与纯 Lua 表序列化/反序列化。 */ export const enum EItemRarity { Common = 0, Rare = 1, Epic = 2, Legendary = 3, } export interface ItemDef { itemId: string; name: string; category: "Weapon" | "Consumable" | "Material"; rarity: EItemRarity; maxStack: number; } export interface InventorySlot { itemId: string; count: number; } export interface SerializedInventory { capacity: number; slots: Record; } export class InventorySystem { private capacity: number; private defs: Record = {}; private slots: Record = {}; constructor(capacity: number) { this.capacity = capacity; } RegisterItemDef(def: ItemDef): void { this.defs[def.itemId] = def; } /** 添加物品,返回未放下的剩余数量(0 表示全部成功放入) */ AddItem(itemId: string, amount: number): number { const def = this.defs[itemId]; if (def === undefined || amount <= 0) { return amount; } let remaining = amount; // 1. 先合并到已有未满堆叠格 for (let i = 0; i < this.capacity && remaining > 0; i++) { const slot = this.slots[i]; if (slot !== undefined && slot.itemId === itemId && slot.count < def.maxStack) { const space = def.maxStack - slot.count; const add = math.min(space, remaining); slot.count += add; remaining -= add; } } // 2. 放入空格子 for (let i = 0; i < this.capacity && remaining > 0; i++) { if (this.slots[i] === undefined) { const add = math.min(def.maxStack, remaining); this.slots[i] = { itemId, count: add }; remaining -= add; } } return remaining; } ConsumeItem(itemId: string, amount: number): boolean { if (amount <= 0 || this.GetItemCount(itemId) < amount) { return false; } let remaining = amount; for (let i = this.capacity - 1; i >= 0 && remaining > 0; i--) { const slot = this.slots[i]; if (slot !== undefined && slot.itemId === itemId) { const take = math.min(slot.count, remaining); slot.count -= take; remaining -= take; if (slot.count <= 0) { this.slots[i] = undefined; } } } return true; } SwapSlots(slotA: number, slotB: number): boolean { if (slotA < 0 || slotA >= this.capacity || slotB < 0 || slotB >= this.capacity) { return false; } const tmp = this.slots[slotA]; this.slots[slotA] = this.slots[slotB]; this.slots[slotB] = tmp; return true; } SplitSlot(fromSlot: number, toSlot: number, splitCount: number): boolean { if ( fromSlot < 0 || fromSlot >= this.capacity || toSlot < 0 || toSlot >= this.capacity || fromSlot === toSlot ) { return false; } const src = this.slots[fromSlot]; if (src === undefined || this.slots[toSlot] !== undefined) { return false; } if (splitCount <= 0 || splitCount >= src.count) { return false; } src.count -= splitCount; this.slots[toSlot] = { itemId: src.itemId, count: splitCount }; return true; } GetItemCount(itemId: string): number { let total = 0; for (let i = 0; i < this.capacity; i++) { const slot = this.slots[i]; if (slot !== undefined && slot.itemId === itemId) { total += slot.count; } } return total; } GetSlot(index: number): InventorySlot | undefined { return this.slots[index]; } GetUsedSlotCount(): number { let used = 0; for (let i = 0; i < this.capacity; i++) { if (this.slots[i] !== undefined) used++; } return used; } FilterByCategory(category: ItemDef["category"]): InventorySlot[] { const out: InventorySlot[] = []; for (let i = 0; i < this.capacity; i++) { const slot = this.slots[i]; if (slot !== undefined) { const def = this.defs[slot.itemId]; if (def !== undefined && def.category === category) { out[out.length] = slot; } } } return out; } Serialize(): SerializedInventory { const copiedSlots: Record = {}; for (let i = 0; i < this.capacity; i++) { const s = this.slots[i]; if (s !== undefined) { copiedSlots[i] = { itemId: s.itemId, count: s.count }; } } return { capacity: this.capacity, slots: copiedSlots, }; } Deserialize(data: SerializedInventory): void { this.capacity = data.capacity; this.slots = {}; for (const [idx, s] of pairs(data.slots)) { this.slots[idx] = { itemId: s.itemId, count: s.count }; } } }