Files
DungeonCrawlerWorld/scripts/presentation/shop_panel.gd
T

71 lines
2.3 KiB
GDScript

class_name ShopPanel
extends PanelContainer
@onready var title: Label = $Title
@onready var entries: RichTextLabel = $Entries
var _stock: Array[Dictionary] = []
var _inventory: InventoryState
var _selected_index := 0
var _gold := 0
var _colony: Dictionary = {}
var _selling := false
func present(stock: Array[Dictionary], inventory: InventoryState, gold: int, colony: Dictionary) -> void:
_stock = stock.duplicate(true)
_inventory = inventory
_gold = gold
_colony = colony.duplicate(true)
_selected_index = 0
_selling = false
refresh()
visible = true
func dismiss() -> void:
visible = false
func select_next(delta: int) -> void:
var count := _entry_count()
if count == 0:
return
_selected_index = posmod(_selected_index + delta, count)
refresh()
func toggle_mode() -> void:
_selling = not _selling
_selected_index = 0
refresh()
func is_selling() -> bool:
return _selling
func selected_item_id() -> StringName:
if _selling:
if _inventory == null or _selected_index < 0 or _selected_index >= _inventory.items.size():
return &""
return _inventory.items[_selected_index].instance_id
if _selected_index < 0 or _selected_index >= _stock.size():
return &""
return StringName(_stock[_selected_index].id)
func refresh(gold: int = _gold) -> void:
_gold = gold
var colony_id := StringName(_colony.get("id", "bourg"))
var mode := "VENDRE" if _selling else "ACHETER"
title.text = "%s — %s (Or : %d)" % [mode, String(_colony.get("label", "Bourg")), _gold]
var lines: PackedStringArray = []
if _selling and _inventory != null:
for index in range(_inventory.items.size()):
var item: ItemInstance = _inventory.items[index]
var cursor := "> " if index == _selected_index else " "
lines.append("%s%s — %d or" % [cursor, item.definition.name, Economy.sell_price(item.definition.raw, colony_id)])
else:
for index in range(_stock.size()):
var item: Dictionary = _stock[index]
var cursor := "> " if index == _selected_index else " "
lines.append("%s%s — %d or" % [cursor, String(item.get("name", item.get("id", ""))), Economy.buy_price(item, colony_id)])
entries.text = "\n".join(lines) if not lines.is_empty() else "Aucun objet disponible."
func _entry_count() -> int:
return _inventory.items.size() if _selling and _inventory != null else _stock.size()