diff --git a/src/inventory.ts b/src/inventory.ts index 965e9dd..c58dfeb 100644 --- a/src/inventory.ts +++ b/src/inventory.ts @@ -43,12 +43,24 @@ class ItemInstance { } export default class Inventory extends Node { + private static cache = new Map() + items: Map = new Map() static find_inventory(root: Node): Inventory | null | undefined { + if (Inventory.cache.has(root)) { + return Inventory.cache.get(root) + } + const child_enumerator: GArrayEnumerator = new GArrayEnumerator(root.get_children()) const children = Enumerable.from(child_enumerator) - return children.find(child => child instanceof Inventory) as Inventory + const inventory = children.find(child => child instanceof Inventory) as Inventory + + if (inventory != null) { + Inventory.cache.set(root, inventory) + } + + return inventory } add(item: ItemData, quantity: number = 1) {