70 lines
2.8 KiB
GDScript
70 lines
2.8 KiB
GDScript
class_name InventoryPanel
|
|
extends PanelContainer
|
|
|
|
@onready var entries: RichTextLabel = $Entries
|
|
@onready var details: RichTextLabel = $Details
|
|
|
|
var selected_index := 0
|
|
var _inventory: InventoryState
|
|
var _equipment: EquipmentState
|
|
var _identification: ItemIdentification
|
|
|
|
func present(inventory: InventoryState, equipment: EquipmentState, identification: ItemIdentification) -> void:
|
|
_inventory = inventory
|
|
_equipment = equipment
|
|
_identification = identification
|
|
selected_index = clampi(selected_index, 0, maxi(0, _item_count() - 1))
|
|
refresh()
|
|
visible = true
|
|
|
|
func dismiss() -> void:
|
|
visible = false
|
|
|
|
func select_next(delta: int) -> void:
|
|
if _item_count() == 0:
|
|
return
|
|
selected_index = posmod(selected_index + delta, _item_count())
|
|
refresh()
|
|
|
|
func selected_item() -> ItemInstance:
|
|
if _inventory == null or selected_index < 0 or selected_index >= _inventory.items.size():
|
|
return null
|
|
return _inventory.items[selected_index]
|
|
|
|
func refresh() -> void:
|
|
if _inventory == null:
|
|
entries.text = "Inventory is empty"
|
|
details.text = "Inventory is empty"
|
|
return
|
|
selected_index = clampi(selected_index, 0, maxi(0, _item_count() - 1))
|
|
var lines: PackedStringArray = []
|
|
for index in range(_inventory.items.size()):
|
|
var item: ItemInstance = _inventory.items[index]
|
|
var cursor := "> " if index == selected_index else " "
|
|
var name := _identification.display_name(item) if _identification != null else item.definition.name
|
|
lines.append("%s%s x%d" % [cursor, name, item.quantity])
|
|
entries.text = "\n".join(lines) if not lines.is_empty() else "Inventory is empty"
|
|
var equipment_summary := _equipment_summary()
|
|
var item := selected_item()
|
|
if item == null:
|
|
details.text = equipment_summary if not equipment_summary.is_empty() else "Inventory is empty"
|
|
return
|
|
var name := _identification.display_name(item) if _identification != null else item.definition.name
|
|
var stats := _identification.display_stats(item) if _identification != null else ""
|
|
var description := String(item.definition.raw.get("description", "")) if _identification == null or _identification.is_identified(item) else "Its properties are not yet known."
|
|
var comparison := _equipment.comparison_for(item, _identification) if _equipment != null else ""
|
|
details.text = "%s\n%s\n%s\n\n%s\n\n%s\n↑/↓ Select E Equip U Use D Drop I Close" % [name, stats, description, comparison, equipment_summary]
|
|
|
|
func _equipment_summary() -> String:
|
|
if _equipment == null or _identification == null:
|
|
return ""
|
|
var lines: PackedStringArray = []
|
|
for slot in EquipmentState.SLOTS:
|
|
var item := _equipment.item_in_slot(slot)
|
|
if item != null:
|
|
lines.append("%s: %s" % [slot, _identification.display_name(item)])
|
|
return "Equipped\n%s" % "\n".join(lines) if not lines.is_empty() else ""
|
|
|
|
func _item_count() -> int:
|
|
return _inventory.items.size() if _inventory != null else 0
|