Stash
This commit is contained in:
@@ -0,0 +1,343 @@
|
||||
extends CanvasLayer
|
||||
|
||||
## Stash UI
|
||||
## VIP feature for secure item storage between dungeon runs
|
||||
|
||||
@onready var close_button = $PanelContainer/VBoxContainer/Header/CloseButton
|
||||
@onready var title_label = $PanelContainer/VBoxContainer/Header/Title
|
||||
@onready var vip_message = $PanelContainer/VBoxContainer/VIPMessage
|
||||
@onready var content = $PanelContainer/VBoxContainer/Content
|
||||
@onready var inventory_grid = $PanelContainer/VBoxContainer/Content/InventorySection/InventoryGrid
|
||||
@onready var stash_grid = $PanelContainer/VBoxContainer/Content/StashSection/StashGrid
|
||||
|
||||
const INVENTORY_DISPLAY_SLOTS = 30
|
||||
const STASH_SLOTS = 50
|
||||
|
||||
const RARITY_COLORS = {
|
||||
"Common": Color(0.7, 0.7, 0.7),
|
||||
"Uncommon": Color(0.18, 0.8, 0.44),
|
||||
"Rare": Color(0.2, 0.6, 0.86),
|
||||
"Epic": Color(0.61, 0.35, 0.71),
|
||||
"Legendary": Color(0.95, 0.61, 0.07)
|
||||
}
|
||||
|
||||
var inventory_slots: Array = []
|
||||
var stash_slots: Array = []
|
||||
var tooltip: PanelContainer = null
|
||||
var is_vip: bool = false
|
||||
|
||||
func _ready():
|
||||
visible = false
|
||||
|
||||
if close_button:
|
||||
close_button.pressed.connect(_on_close)
|
||||
|
||||
_setup_inventory_slots()
|
||||
_setup_stash_slots()
|
||||
_create_tooltip()
|
||||
|
||||
call_deferred("_connect_gamestate")
|
||||
|
||||
func _connect_gamestate():
|
||||
if GameState:
|
||||
GameState.inventory_updated.connect(_refresh_inventory)
|
||||
GameState.stash_updated.connect(_on_stash_updated)
|
||||
GameState.stash_result.connect(_on_stash_result)
|
||||
|
||||
func _setup_inventory_slots():
|
||||
if not inventory_grid:
|
||||
return
|
||||
for i in range(INVENTORY_DISPLAY_SLOTS):
|
||||
var slot = _create_slot_button()
|
||||
slot.set_meta("slot_type", "inventory")
|
||||
slot.set_meta("slot_index", i)
|
||||
slot.set_meta("item", null)
|
||||
slot.gui_input.connect(_on_inventory_slot_input.bind(i))
|
||||
slot.mouse_entered.connect(_on_slot_hover.bind(slot))
|
||||
slot.mouse_exited.connect(_hide_tooltip)
|
||||
inventory_grid.add_child(slot)
|
||||
inventory_slots.append(slot)
|
||||
|
||||
func _setup_stash_slots():
|
||||
if not stash_grid:
|
||||
return
|
||||
for i in range(STASH_SLOTS):
|
||||
var slot = _create_slot_button()
|
||||
slot.set_meta("slot_type", "stash")
|
||||
slot.set_meta("slot_index", i)
|
||||
slot.set_meta("item", null)
|
||||
slot.gui_input.connect(_on_stash_slot_input.bind(i))
|
||||
slot.mouse_entered.connect(_on_slot_hover.bind(slot))
|
||||
slot.mouse_exited.connect(_hide_tooltip)
|
||||
stash_grid.add_child(slot)
|
||||
stash_slots.append(slot)
|
||||
|
||||
func _create_slot_button() -> Button:
|
||||
var btn = Button.new()
|
||||
btn.custom_minimum_size = Vector2(40, 40)
|
||||
btn.add_theme_stylebox_override("normal", _create_slot_style(Color(0.15, 0.15, 0.2)))
|
||||
btn.add_theme_stylebox_override("hover", _create_slot_style(Color(0.25, 0.25, 0.35)))
|
||||
return btn
|
||||
|
||||
func _create_slot_style(color: Color) -> StyleBoxFlat:
|
||||
var style = StyleBoxFlat.new()
|
||||
style.bg_color = color
|
||||
style.border_color = Color(0.3, 0.3, 0.4)
|
||||
style.set_border_width_all(2)
|
||||
style.set_corner_radius_all(4)
|
||||
return style
|
||||
|
||||
func _create_tooltip():
|
||||
tooltip = PanelContainer.new()
|
||||
tooltip.z_index = 100
|
||||
|
||||
var style = StyleBoxFlat.new()
|
||||
style.bg_color = Color(0.1, 0.1, 0.15, 0.95)
|
||||
style.border_color = Color(0.4, 0.4, 0.5)
|
||||
style.set_border_width_all(1)
|
||||
style.set_corner_radius_all(4)
|
||||
style.set_content_margin_all(8)
|
||||
tooltip.add_theme_stylebox_override("panel", style)
|
||||
|
||||
var vbox = VBoxContainer.new()
|
||||
vbox.name = "VBox"
|
||||
|
||||
var name_label = Label.new()
|
||||
name_label.name = "ItemName"
|
||||
name_label.add_theme_font_size_override("font_size", 14)
|
||||
vbox.add_child(name_label)
|
||||
|
||||
var type_label = Label.new()
|
||||
type_label.name = "ItemType"
|
||||
type_label.add_theme_font_size_override("font_size", 11)
|
||||
type_label.modulate = Color(0.7, 0.7, 0.7)
|
||||
vbox.add_child(type_label)
|
||||
|
||||
var stats_label = Label.new()
|
||||
stats_label.name = "ItemStats"
|
||||
stats_label.add_theme_font_size_override("font_size", 12)
|
||||
vbox.add_child(stats_label)
|
||||
|
||||
tooltip.add_child(vbox)
|
||||
tooltip.visible = false
|
||||
add_child(tooltip)
|
||||
|
||||
func show_stash():
|
||||
visible = true
|
||||
# Request stash from server
|
||||
if GameState:
|
||||
GameState.request_stash()
|
||||
|
||||
func _on_stash_updated(stash_data: Dictionary):
|
||||
is_vip = true
|
||||
_update_vip_state(true)
|
||||
_refresh_stash(stash_data)
|
||||
|
||||
func _on_stash_result(result: Dictionary):
|
||||
var success = result.get("success", false)
|
||||
var error = result.get("error", "")
|
||||
var message = result.get("message", "")
|
||||
|
||||
if error == "vip_required":
|
||||
is_vip = false
|
||||
_update_vip_state(false)
|
||||
|
||||
# Could show a toast/notification here
|
||||
if not success and message:
|
||||
print("[Stash] Error: ", message)
|
||||
|
||||
func _update_vip_state(is_vip_status: bool):
|
||||
if vip_message:
|
||||
vip_message.visible = not is_vip_status
|
||||
if content:
|
||||
content.visible = is_vip_status
|
||||
if title_label:
|
||||
title_label.text = "🔐 Stash (VIP Feature)" if is_vip_status else "🔒 Stash (VIP Required)"
|
||||
|
||||
func _refresh_inventory():
|
||||
for i in range(INVENTORY_DISPLAY_SLOTS):
|
||||
if i >= inventory_slots.size():
|
||||
break
|
||||
var item = GameState.inventory[i] if i < GameState.inventory.size() else null
|
||||
_update_slot(inventory_slots[i], item)
|
||||
|
||||
func _refresh_stash(stash_data: Dictionary):
|
||||
var items = stash_data.get("items", [])
|
||||
|
||||
# Clear all stash slots first
|
||||
for slot in stash_slots:
|
||||
_update_slot(slot, null)
|
||||
|
||||
# Fill in items at their slots
|
||||
for item_data in items:
|
||||
var slot_index = int(item_data.get("slotIndex", item_data.get("SlotIndex", -1)))
|
||||
if slot_index >= 0 and slot_index < stash_slots.size():
|
||||
# Merge definition data into item_data for display
|
||||
var definition = item_data.get("definition", item_data.get("Definition", {}))
|
||||
var merged_item = item_data.duplicate()
|
||||
if definition:
|
||||
for key in definition:
|
||||
merged_item[key] = definition[key]
|
||||
_update_slot(stash_slots[slot_index], merged_item)
|
||||
|
||||
func _update_slot(slot: Button, item):
|
||||
# Clear any existing children
|
||||
for child in slot.get_children():
|
||||
child.queue_free()
|
||||
|
||||
if item == null or (item is Dictionary and item.is_empty()):
|
||||
slot.text = ""
|
||||
slot.set_meta("item", null)
|
||||
slot.add_theme_stylebox_override("normal", _create_slot_style(Color(0.15, 0.15, 0.2)))
|
||||
else:
|
||||
slot.text = ""
|
||||
slot.set_meta("item", item)
|
||||
|
||||
# Try to load sprite from CDN
|
||||
var sprite_path = item.get("spritePath", item.get("SpritePath", ""))
|
||||
var has_sprite = false
|
||||
|
||||
if sprite_path and not sprite_path.is_empty() and UpdateManager:
|
||||
var texture = UpdateManager.load_texture(sprite_path)
|
||||
if texture:
|
||||
var sprite = TextureRect.new()
|
||||
sprite.texture = texture
|
||||
sprite.expand_mode = TextureRect.EXPAND_FIT_WIDTH_PROPORTIONAL
|
||||
sprite.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED
|
||||
sprite.custom_minimum_size = Vector2(28, 28)
|
||||
sprite.position = Vector2(6, 6)
|
||||
slot.add_child(sprite)
|
||||
has_sprite = true
|
||||
|
||||
if not has_sprite:
|
||||
slot.text = _get_item_emoji(item)
|
||||
|
||||
# Add stack count label if quantity > 1
|
||||
var quantity = item.get("quantity", item.get("Quantity", 1))
|
||||
if quantity > 1:
|
||||
var count_label = Label.new()
|
||||
count_label.text = str(quantity)
|
||||
count_label.add_theme_font_size_override("font_size", 9)
|
||||
count_label.add_theme_color_override("font_color", Color.WHITE)
|
||||
count_label.add_theme_color_override("font_shadow_color", Color.BLACK)
|
||||
count_label.position = Vector2(slot.custom_minimum_size.x - 16, slot.custom_minimum_size.y - 14)
|
||||
slot.add_child(count_label)
|
||||
|
||||
# Style based on rarity
|
||||
var rarity = item.get("rarity", item.get("Rarity", "Common"))
|
||||
var style = _create_slot_style(Color(0.15, 0.15, 0.2))
|
||||
style.border_color = RARITY_COLORS.get(rarity, Color.GRAY)
|
||||
style.border_width_bottom = 3
|
||||
slot.add_theme_stylebox_override("normal", style)
|
||||
|
||||
func _get_item_emoji(item: Dictionary) -> String:
|
||||
var item_type = item.get("type", item.get("Type", "")).to_lower()
|
||||
match item_type:
|
||||
"consumable": return "🧪"
|
||||
"material": return "📦"
|
||||
"weapon": return "⚔️"
|
||||
"armor": return "🛡️"
|
||||
"accessory": return "💍"
|
||||
_: return "❓"
|
||||
|
||||
func _on_inventory_slot_input(event: InputEvent, slot_index: int):
|
||||
if not event is InputEventMouseButton:
|
||||
return
|
||||
if not event.pressed:
|
||||
return
|
||||
|
||||
var item = GameState.inventory[slot_index] if slot_index < GameState.inventory.size() else null
|
||||
if item == null or (item is Dictionary and item.is_empty()):
|
||||
return
|
||||
|
||||
if event.button_index == MOUSE_BUTTON_RIGHT:
|
||||
# Right-click to deposit to stash
|
||||
var quantity = item.get("quantity", item.get("Quantity", 1))
|
||||
GameState.stash_deposit(slot_index, quantity)
|
||||
|
||||
func _on_stash_slot_input(event: InputEvent, slot_index: int):
|
||||
if not event is InputEventMouseButton:
|
||||
return
|
||||
if not event.pressed:
|
||||
return
|
||||
|
||||
var slot = stash_slots[slot_index] if slot_index < stash_slots.size() else null
|
||||
if slot == null:
|
||||
return
|
||||
|
||||
var item = slot.get_meta("item")
|
||||
if item == null:
|
||||
return
|
||||
|
||||
if event.button_index == MOUSE_BUTTON_RIGHT:
|
||||
# Right-click to withdraw from stash
|
||||
var quantity = item.get("quantity", item.get("Quantity", 1))
|
||||
GameState.stash_withdraw(slot_index, quantity)
|
||||
|
||||
func _on_slot_hover(slot: Button):
|
||||
if not slot.has_meta("item"):
|
||||
_hide_tooltip()
|
||||
return
|
||||
var item = slot.get_meta("item")
|
||||
if item == null:
|
||||
_hide_tooltip()
|
||||
return
|
||||
|
||||
_show_tooltip(item, slot)
|
||||
|
||||
func _show_tooltip(item: Dictionary, slot: Button):
|
||||
if not tooltip:
|
||||
return
|
||||
|
||||
var vbox = tooltip.get_node_or_null("VBox")
|
||||
if not vbox:
|
||||
return
|
||||
|
||||
var name_label = vbox.get_node_or_null("ItemName")
|
||||
var type_label = vbox.get_node_or_null("ItemType")
|
||||
var stats_label = vbox.get_node_or_null("ItemStats")
|
||||
|
||||
if not name_label:
|
||||
return
|
||||
|
||||
var rarity = item.get("rarity", item.get("Rarity", "Common"))
|
||||
var item_name = item.get("name", item.get("Name", "Unknown"))
|
||||
var item_type = item.get("type", item.get("Type", ""))
|
||||
var quantity = item.get("quantity", item.get("Quantity", 1))
|
||||
|
||||
if quantity > 1:
|
||||
name_label.text = "%s (x%d)" % [item_name, quantity]
|
||||
else:
|
||||
name_label.text = item_name
|
||||
name_label.modulate = RARITY_COLORS.get(rarity, Color.WHITE)
|
||||
|
||||
if type_label:
|
||||
type_label.text = "%s %s" % [rarity, item_type]
|
||||
|
||||
if stats_label:
|
||||
stats_label.text = ""
|
||||
|
||||
# Position tooltip
|
||||
await get_tree().process_frame
|
||||
var screen_size = get_viewport().get_visible_rect().size
|
||||
var pos = slot.global_position + Vector2(slot.size.x + 10, 0)
|
||||
|
||||
if pos.x + tooltip.size.x > screen_size.x:
|
||||
pos.x = slot.global_position.x - tooltip.size.x - 10
|
||||
if pos.y + tooltip.size.y > screen_size.y:
|
||||
pos.y = screen_size.y - tooltip.size.y - 10
|
||||
|
||||
tooltip.global_position = pos
|
||||
tooltip.visible = true
|
||||
|
||||
func _hide_tooltip():
|
||||
if tooltip:
|
||||
tooltip.visible = false
|
||||
|
||||
func _on_close():
|
||||
visible = false
|
||||
|
||||
func _input(event):
|
||||
if visible and event.is_action_pressed("escape_menu"):
|
||||
visible = false
|
||||
get_viewport().set_input_as_handled()
|
||||
Reference in New Issue
Block a user