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
|
||||
1
gui/pause/inventory/scripts/inventory_data.gd.uid
Normal file
1
gui/pause/inventory/scripts/inventory_data.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://b6ol0ja8441tv
|
||||
38
gui/pause/inventory/scripts/inventory_slot_ui.gd
Normal file
38
gui/pause/inventory/scripts/inventory_slot_ui.gd
Normal file
@@ -0,0 +1,38 @@
|
||||
class_name InventorySlotUi extends Button
|
||||
|
||||
var slot_data: SlotData :
|
||||
set = set_slot_data
|
||||
|
||||
@onready var texture_rect: TextureRect = $TextureRect
|
||||
@onready var label: Label = $Label
|
||||
|
||||
func _ready() -> void:
|
||||
texture_rect.texture = null
|
||||
label.text = ""
|
||||
focus_entered.connect(on_item_focus)
|
||||
focus_exited.connect(on_item_unfocus)
|
||||
pressed.connect(item_pressed)
|
||||
|
||||
func set_slot_data(value: SlotData) -> void:
|
||||
slot_data = value
|
||||
if slot_data == null:
|
||||
return
|
||||
texture_rect.texture = slot_data.item_data.texture
|
||||
label.text = str(slot_data.quantity)
|
||||
|
||||
func on_item_focus() -> void:
|
||||
if slot_data == null or slot_data.item_data == null:
|
||||
return
|
||||
PauseMenu.update_item_description(slot_data.item_data.description)
|
||||
|
||||
func on_item_unfocus() -> void:
|
||||
PauseMenu.update_item_description("")
|
||||
pass
|
||||
|
||||
func item_pressed() -> void:
|
||||
if !slot_data or !slot_data.item_data:
|
||||
return
|
||||
if !slot_data.item_data.use():
|
||||
return
|
||||
slot_data.quantity -= 1
|
||||
label.text = str(slot_data.quantity)
|
||||
1
gui/pause/inventory/scripts/inventory_slot_ui.gd.uid
Normal file
1
gui/pause/inventory/scripts/inventory_slot_ui.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cvbri6j1mbq07
|
||||
37
gui/pause/inventory/scripts/inventory_ui.gd
Normal file
37
gui/pause/inventory/scripts/inventory_ui.gd
Normal file
@@ -0,0 +1,37 @@
|
||||
class_name InventoryUi extends Control
|
||||
|
||||
const INVENTORY_SLOT = preload("res://gui/pause/inventory/inventory_slot.tscn")
|
||||
|
||||
@export var data: InventoryData
|
||||
|
||||
var focus_index := 1
|
||||
|
||||
func _ready() -> void:
|
||||
PauseMenu.shown.connect(update)
|
||||
PauseMenu.hidden.connect(clear)
|
||||
clear()
|
||||
data.changed.connect(on_inventory_changed)
|
||||
|
||||
func clear() -> void:
|
||||
for child in get_children():
|
||||
child.queue_free()
|
||||
|
||||
func update() -> void:
|
||||
for slot in data.slots:
|
||||
var new_slot = INVENTORY_SLOT.instantiate()
|
||||
add_child(new_slot)
|
||||
new_slot.slot_data = slot
|
||||
new_slot.focus_entered.connect(on_item_focused)
|
||||
if !data.slots.is_empty():
|
||||
await get_tree().process_frame
|
||||
get_child(focus_index).grab_focus()
|
||||
|
||||
func on_inventory_changed() -> void:
|
||||
clear()
|
||||
update()
|
||||
|
||||
func on_item_focused() -> void:
|
||||
for child_index in get_child_count():
|
||||
if get_child(child_index).has_focus():
|
||||
focus_index = child_index
|
||||
return
|
||||
1
gui/pause/inventory/scripts/inventory_ui.gd.uid
Normal file
1
gui/pause/inventory/scripts/inventory_ui.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://b7a5u5kcishd4
|
||||
9
gui/pause/inventory/scripts/slot_data.gd
Normal file
9
gui/pause/inventory/scripts/slot_data.gd
Normal file
@@ -0,0 +1,9 @@
|
||||
class_name SlotData extends Resource
|
||||
|
||||
@export var item_data: ItemData
|
||||
@export var quantity := 0 : set = set_quantity
|
||||
|
||||
func set_quantity(new_quantity: int) -> void:
|
||||
quantity = new_quantity
|
||||
if quantity < 1:
|
||||
emit_changed()
|
||||
1
gui/pause/inventory/scripts/slot_data.gd.uid
Normal file
1
gui/pause/inventory/scripts/slot_data.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://d0boqgd4kawwt
|
||||
Reference in New Issue
Block a user