66 lines
2.3 KiB
GDScript
66 lines
2.3 KiB
GDScript
extends PanelContainer
|
||
## Envanter paneli — açıldığında /world/inventory yükler, listede gösterir.
|
||
|
||
@onready var list: VBoxContainer = $Margin/VBox/Scroll/List
|
||
@onready var title: Label = $Margin/VBox/Title
|
||
@onready var close_btn: Button = $Margin/VBox/CloseBtn
|
||
|
||
const RARITY_COLOR := {
|
||
"common": Color(0.85, 0.85, 0.85),
|
||
"uncommon": Color(0.4, 0.85, 0.4),
|
||
"rare": Color(0.4, 0.6, 1.0),
|
||
"epic": Color(0.7, 0.4, 1.0),
|
||
"legendary": Color(1.0, 0.7, 0.2),
|
||
}
|
||
|
||
func _ready() -> void:
|
||
visible = false
|
||
close_btn.pressed.connect(func(): visible = false)
|
||
GameState.inventory_changed.connect(_render)
|
||
|
||
func open() -> void:
|
||
visible = true
|
||
await GameState.load_inventory()
|
||
_render()
|
||
|
||
func _render() -> void:
|
||
for c in list.get_children():
|
||
c.queue_free()
|
||
title.text = "Envanter (%d)" % GameState.inventory.size()
|
||
if GameState.inventory.is_empty():
|
||
var l := Label.new()
|
||
l.text = "Boş. Bir node'dan kaynak topla."
|
||
l.add_theme_color_override("font_color", Color(0.6, 0.6, 0.6))
|
||
list.add_child(l)
|
||
return
|
||
for it in GameState.inventory:
|
||
var row := HBoxContainer.new()
|
||
row.add_theme_constant_override("separation", 12)
|
||
var name := Label.new()
|
||
name.text = str(it.get("item_name", ""))
|
||
name.add_theme_color_override("font_color", RARITY_COLOR.get(str(it.get("rarity", "common")), Color.WHITE))
|
||
name.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
row.add_child(name)
|
||
var qty := Label.new()
|
||
qty.text = "x%d" % int(it.get("qty", 1))
|
||
row.add_child(qty)
|
||
# Consumable ise "Kullan" butonu (effect backend'den gelir, örn. +50 adım)
|
||
if str(it.get("item_type", "")) == "consumable" and it.get("effect") != null:
|
||
var effect: Dictionary = it["effect"]
|
||
var btn := Button.new()
|
||
btn.text = "Kullan (+%d)" % int(effect.get("amount", 0))
|
||
var code: String = str(it.get("item_code", ""))
|
||
btn.pressed.connect(_on_use_item.bind(btn, code))
|
||
row.add_child(btn)
|
||
list.add_child(row)
|
||
|
||
func _on_use_item(btn: Button, code: String) -> void:
|
||
btn.disabled = true
|
||
var res: Dictionary = await GameState.use_item(code, 1)
|
||
if not res.ok:
|
||
var msg := "Kullanılamadı"
|
||
if res.body is Dictionary and res.body.has("detail"):
|
||
msg = str(res.body["detail"])
|
||
print("[use-item fail] ", msg)
|
||
btn.disabled = false
|
||
# Başarıda inventory_changed sinyali paneli zaten yeniden çizer
|