init
This commit is contained in:
88
gui/pause/inventory/scripts/inventory_data.gd
Normal file
88
gui/pause/inventory/scripts/inventory_data.gd
Normal file
@@ -0,0 +1,88 @@
|
||||
class_name InventoryData extends Resource
|
||||
|
||||
@export var slots: Array[SlotData]
|
||||
|
||||
func _init() -> void:
|
||||
connect_slots()
|
||||
|
||||
func add_item(item: ItemData, count := 1) -> bool:
|
||||
var slot = get_slot_by_item(item)
|
||||
if slot:
|
||||
slot.quantity += count
|
||||
return true
|
||||
var empty_slot_index = get_empty_slot_index()
|
||||
if empty_slot_index > -1:
|
||||
var new_slot = SlotData.new()
|
||||
new_slot.item_data = item
|
||||
new_slot.quantity = count
|
||||
slots[empty_slot_index] = new_slot
|
||||
new_slot.changed.connect(on_slot_changed)
|
||||
return true
|
||||
print("inventory full")
|
||||
return false
|
||||
|
||||
func get_slot_by_item(item: ItemData) -> SlotData:
|
||||
var index = slots.find_custom(func(slot: SlotData): return slot != null and slot.item_data == item)
|
||||
if index > -1:
|
||||
return slots[index]
|
||||
return null
|
||||
|
||||
func get_empty_slot_index() -> int:
|
||||
return slots.find_custom(func(slot: SlotData): return !slot)
|
||||
|
||||
func get_valid_slots() -> Array[SlotData]:
|
||||
return slots.filter(func(slot: SlotData): return slot);
|
||||
|
||||
func connect_slots() -> void:
|
||||
for slot: SlotData in get_valid_slots():
|
||||
slot.changed.connect(on_slot_changed)
|
||||
|
||||
func on_slot_changed() -> void:
|
||||
for slot_index in slots.size():
|
||||
var slot: SlotData = slots[slot_index]
|
||||
if slot and slot.quantity < 1:
|
||||
slot.changed.disconnect(on_slot_changed)
|
||||
slots[slot_index] = null
|
||||
emit_changed()
|
||||
|
||||
func get_save_data() -> Array:
|
||||
var item_save := []
|
||||
for slot in slots:
|
||||
item_save.append(item_to_save(slot))
|
||||
return item_save
|
||||
|
||||
func item_to_save(slot: SlotData) -> Dictionary:
|
||||
var result := {
|
||||
item = "",
|
||||
quantity = 0
|
||||
}
|
||||
if !slot:
|
||||
return result
|
||||
result.quantity = slot.quantity
|
||||
if !slot.item_data:
|
||||
return result
|
||||
result.item = slot.item_data.resource_path
|
||||
return result
|
||||
|
||||
func load_save_date(save_data: Array) -> void:
|
||||
var slots_size = slots.size()
|
||||
slots.clear()
|
||||
slots.resize(slots_size)
|
||||
for save_index in save_data.size():
|
||||
slots[save_index] = save_to_item(save_data[save_index])
|
||||
connect_slots()
|
||||
|
||||
func save_to_item(save_item: Dictionary) -> SlotData:
|
||||
if save_item.item == "":
|
||||
return null
|
||||
var new_slot := SlotData.new()
|
||||
new_slot.item_data = load(save_item.item)
|
||||
new_slot.quantity = int(save_item.quantity)
|
||||
return new_slot
|
||||
|
||||
func use_item(item: ItemData, count := 1) -> bool:
|
||||
for slot in slots:
|
||||
if slot and slot.item_data == item and slot.quantity >= count:
|
||||
slot.quantity -= count
|
||||
return true
|
||||
return false
|
||||
Reference in New Issue
Block a user