42 lines
1.1 KiB
GDScript
42 lines
1.1 KiB
GDScript
class_name InventoryState
|
|
extends RefCounted
|
|
|
|
signal changed
|
|
|
|
var seed_items: Array[Dictionary] = []
|
|
var fertilizer_items: Array[Dictionary] = []
|
|
var warehouse_items: Array[Dictionary] = []
|
|
|
|
|
|
func set_items(seeds: Array[Dictionary], fertilizers: Array[Dictionary], warehouse: Array[Dictionary]) -> void:
|
|
seed_items = seeds.duplicate(true)
|
|
fertilizer_items = fertilizers.duplicate(true)
|
|
warehouse_items = warehouse.duplicate(true)
|
|
changed.emit()
|
|
|
|
|
|
func decrease_seed(item_id: int) -> void:
|
|
_decrease_item(seed_items, item_id, 1)
|
|
|
|
|
|
func decrease_fertilizer(item_id: int) -> void:
|
|
_decrease_item(fertilizer_items, item_id, 1)
|
|
|
|
|
|
func decrease_warehouse(item_id: int, count: int) -> void:
|
|
_decrease_item(warehouse_items, item_id, count)
|
|
|
|
|
|
func _decrease_item(items: Array[Dictionary], item_id: int, count: int) -> void:
|
|
for index in range(items.size()):
|
|
if int(items[index].get("item_id", 0)) != item_id:
|
|
continue
|
|
var next_num := int(items[index].get("num", items[index].get("number", 0))) - count
|
|
if next_num <= 0:
|
|
items.remove_at(index)
|
|
else:
|
|
items[index]["num"] = next_num
|
|
items[index]["number"] = next_num
|
|
changed.emit()
|
|
return
|