43 lines
1016 B
GDScript
43 lines
1016 B
GDScript
class_name SimulationItems
|
|
extends RefCounted
|
|
|
|
static var _items: Dictionary = {}
|
|
static var _cache_valid := false
|
|
|
|
|
|
static func _ensure_cache() -> void:
|
|
if _cache_valid:
|
|
return
|
|
_items = {}
|
|
var catalog := ContentCatalog.create_core()
|
|
if catalog.is_valid():
|
|
for item in catalog.get_items():
|
|
_items[item.item_id] = item
|
|
_cache_valid = true
|
|
|
|
|
|
static func invalidate_cache() -> void:
|
|
_cache_valid = false
|
|
|
|
|
|
static func get_item(item_id: StringName) -> ItemDefinition:
|
|
_ensure_cache()
|
|
return _items.get(item_id) as ItemDefinition
|
|
|
|
|
|
static func get_items() -> Array[ItemDefinition]:
|
|
_ensure_cache()
|
|
var values: Array[ItemDefinition] = []
|
|
for item in _items.values():
|
|
values.append(item)
|
|
values.sort_custom(
|
|
func(first: ItemDefinition, second: ItemDefinition) -> bool:
|
|
return String(first.item_id) < String(second.item_id)
|
|
)
|
|
return values
|
|
|
|
|
|
static func get_weapon(item_id: StringName) -> ItemDefinition:
|
|
var item := get_item(item_id)
|
|
return item if item != null and item.is_weapon() else null
|