Files
tekton/scripts/ui/shop_panel.gd
T

92 lines
2.9 KiB
GDScript

extends Control
signal closed
@onready var tab_container = $Panel/VBoxContainer/TabContainer
@onready var head_grid = $Panel/VBoxContainer/TabContainer/Head/GridContainer
@onready var costume_grid = $Panel/VBoxContainer/TabContainer/Costume/GridContainer
@onready var glove_grid = $Panel/VBoxContainer/TabContainer/Glove/GridContainer
@onready var accessory_grid = $Panel/VBoxContainer/TabContainer/Accessory/GridContainer
@onready var status_label = $Panel/VBoxContainer/StatusLabel
@onready var back_btn = $Panel/VBoxContainer/Header/BackBtn
var shop_items = {
"head": [
{"id": "head_hat1", "name": "Cap", "gold": 100, "star": 0},
{"id": "head_crown", "name": "Crown", "gold": 0, "star": 50}
],
"costume": [
{"id": "costume_red", "name": "Red Suit", "gold": 200, "star": 0},
{"id": "costume_gold", "name": "Gold Suit", "gold": 0, "star": 100}
],
"glove": [
{"id": "glove_leather", "name": "Leather Gloves", "gold": 50, "star": 0}
],
"accessory": [
{"id": "acc_glasses", "name": "Sunglasses", "gold": 80, "star": 0}
]
}
func _ready():
back_btn.pressed.connect(_on_close)
_build_shop()
func show_panel():
show()
_refresh_shop()
func _build_shop():
_populate_grid(head_grid, shop_items["head"])
_populate_grid(costume_grid, shop_items["costume"])
_populate_grid(glove_grid, shop_items["glove"])
_populate_grid(accessory_grid, shop_items["accessory"])
func _populate_grid(grid, items):
for child in grid.get_children():
child.queue_free()
for item in items:
var btn = Button.new()
btn.custom_minimum_size = Vector2(120, 80)
btn.add_theme_font_size_override("font_size", 14)
btn.text = "%s\nGold: %d\nStar: %d" % [item.name, item.gold, item.star]
btn.pressed.connect(_on_buy_pressed.bind(item))
grid.add_child(btn)
func _refresh_shop():
# Visual update to show which items are owned
# (For simplicity, not disabling buttons directly, relying on backend check)
status_label.text = "Welcome to the Shop!"
func _on_buy_pressed(item: Dictionary):
if UserProfileManager.inventory.has(item.id):
status_label.text = "Already owned: " + item.name
return
var price_gold = item.gold
var price_star = item.star
if UserProfileManager.wallet.get("gold", 0) < price_gold or UserProfileManager.wallet.get("star", 0) < price_star:
status_label.text = "Not enough currency for " + item.name
return
status_label.text = "Purchasing " + item.name + "..."
# Determine category
var category = ""
if shop_items["head"].has(item): category = "head"
elif shop_items["costume"].has(item): category = "costume"
elif shop_items["glove"].has(item): category = "glove"
elif shop_items["accessory"].has(item): category = "accessory"
var success = await UserProfileManager.purchase_item(item.id, price_gold, price_star, category)
if success:
status_label.text = "Successfully purchased: " + item.name
_refresh_shop()
else:
status_label.text = "Failed to purchase. Backend error."
func _on_close():
hide()
emit_signal("closed")