add caching for inventory searching
This commit is contained in:
parent
6df47a294b
commit
70c9c1ad16
1 changed files with 13 additions and 1 deletions
|
@ -43,12 +43,24 @@ class ItemInstance {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class Inventory extends Node {
|
export default class Inventory extends Node {
|
||||||
|
private static cache = new Map()
|
||||||
|
|
||||||
items: Map<StringName, ItemInstance> = new Map()
|
items: Map<StringName, ItemInstance> = new Map()
|
||||||
|
|
||||||
static find_inventory(root: Node): Inventory | null | undefined {
|
static find_inventory(root: Node): Inventory | null | undefined {
|
||||||
|
if (Inventory.cache.has(root)) {
|
||||||
|
return Inventory.cache.get(root)
|
||||||
|
}
|
||||||
|
|
||||||
const child_enumerator: GArrayEnumerator<Node> = new GArrayEnumerator(root.get_children())
|
const child_enumerator: GArrayEnumerator<Node> = new GArrayEnumerator(root.get_children())
|
||||||
const children = Enumerable.from(child_enumerator)
|
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) {
|
add(item: ItemData, quantity: number = 1) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue