524 lines
18 KiB
GDScript
524 lines
18 KiB
GDScript
extends CanvasLayer
|
|
|
|
@onready var close_button = $PanelContainer/VBoxContainer/Header/CloseButton
|
|
@onready var equipment_grid = $PanelContainer/VBoxContainer/Content/EquipmentSection/EquipmentGrid
|
|
@onready var inventory_grid = $PanelContainer/VBoxContainer/Content/BagSection/InventoryGrid
|
|
@onready var gold_label = $PanelContainer/VBoxContainer/Footer/GoldLabel
|
|
|
|
const EQUIPMENT_SLOTS = ["weapon", "offhand", "helm", "chest", "gloves", "legs", "boots", "necklace", "ring1", "ring2", "quiver"]
|
|
|
|
# Emoji fallbacks for equipment slots (used when CDN texture not available)
|
|
const EQUIPMENT_ICONS = {
|
|
"weapon": "⚔️",
|
|
"offhand": "🛡️",
|
|
"helm": "🎩",
|
|
"chest": "👕",
|
|
"gloves": "🧤",
|
|
"legs": "👖",
|
|
"boots": "👢",
|
|
"necklace": "📿",
|
|
"ring1": "💍",
|
|
"ring2": "💍",
|
|
"quiver": "🏹"
|
|
}
|
|
|
|
# CDN paths for equipment slot background icons
|
|
const EQUIPMENT_SLOT_TEXTURES = {
|
|
"weapon": "textures/ui/slots/mainhand.png",
|
|
"offhand": "textures/ui/slots/offhand.png",
|
|
"helm": "textures/ui/slots/head.png",
|
|
"chest": "textures/ui/slots/chest.png",
|
|
"gloves": "textures/ui/slots/gloves.png",
|
|
"legs": "textures/ui/slots/legs.png",
|
|
"boots": "textures/ui/slots/boots.png",
|
|
"necklace": "textures/ui/slots/neck.png",
|
|
"ring1": "textures/ui/slots/ring.png",
|
|
"ring2": "textures/ui/slots/ring.png",
|
|
"quiver": "textures/ui/slots/back.png"
|
|
}
|
|
|
|
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 equipment_slots: Dictionary = {}
|
|
var tooltip: PanelContainer = null
|
|
var selected_hotbar_slot: int = -1 # For hotbar assignment
|
|
|
|
func _ready():
|
|
visible = false
|
|
|
|
if close_button:
|
|
close_button.pressed.connect(_on_close)
|
|
|
|
_setup_equipment_slots()
|
|
_setup_inventory_slots()
|
|
_create_tooltip()
|
|
|
|
call_deferred("_connect_gamestate")
|
|
|
|
func _connect_gamestate():
|
|
if GameState:
|
|
GameState.inventory_updated.connect(_refresh_inventory)
|
|
if UpdateManager:
|
|
UpdateManager.file_downloaded.connect(_on_file_downloaded)
|
|
UpdateManager.update_completed.connect(_on_updates_completed)
|
|
|
|
func _on_file_downloaded(file_path: String):
|
|
# If an item texture was downloaded, refresh inventory to show it
|
|
if file_path.begins_with("textures/items/"):
|
|
_refresh_inventory()
|
|
|
|
func _on_updates_completed(_count: int):
|
|
# Refresh inventory after all updates complete
|
|
_refresh_inventory()
|
|
|
|
func _setup_equipment_slots():
|
|
if not equipment_grid:
|
|
return
|
|
for slot_name in EQUIPMENT_SLOTS:
|
|
var slot = _create_slot_button()
|
|
slot.set_meta("slot_type", "equipment")
|
|
slot.set_meta("slot_name", slot_name)
|
|
slot.set_meta("item", null)
|
|
slot.text = EQUIPMENT_ICONS.get(slot_name, "?")
|
|
slot.gui_input.connect(_on_equipment_gui_input.bind(slot_name))
|
|
slot.mouse_entered.connect(_on_slot_hover.bind(slot))
|
|
slot.mouse_exited.connect(_hide_tooltip)
|
|
equipment_grid.add_child(slot)
|
|
equipment_slots[slot_name] = slot
|
|
|
|
func _setup_inventory_slots():
|
|
if not inventory_grid:
|
|
return
|
|
for i in range(30):
|
|
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_gui_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 _create_slot_button() -> Button:
|
|
var btn = Button.new()
|
|
btn.custom_minimum_size = Vector2(48, 48)
|
|
btn.add_theme_stylebox_override("normal", _create_slot_style(Color(0.408, 0.408, 0.322, 1.0)))
|
|
btn.add_theme_stylebox_override("hover", _create_slot_style(Color(0.408, 0.408, 0.322, 0.902)))
|
|
return btn
|
|
|
|
func _create_slot_style(color: Color) -> StyleBoxFlat:
|
|
var style = StyleBoxFlat.new()
|
|
style.bg_color = color
|
|
style.border_color = Color(0.0, 0.0, 0.0, 1.0)
|
|
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 sep = HSeparator.new()
|
|
vbox.add_child(sep)
|
|
|
|
var stats_label = Label.new()
|
|
stats_label.name = "ItemStats"
|
|
stats_label.add_theme_font_size_override("font_size", 12)
|
|
vbox.add_child(stats_label)
|
|
|
|
var desc_label = Label.new()
|
|
desc_label.name = "ItemDesc"
|
|
desc_label.add_theme_font_size_override("font_size", 10)
|
|
desc_label.modulate = Color(0.6, 0.6, 0.6)
|
|
desc_label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
|
|
desc_label.custom_minimum_size.x = 200
|
|
vbox.add_child(desc_label)
|
|
|
|
var hint_label = Label.new()
|
|
hint_label.name = "Hint"
|
|
hint_label.add_theme_font_size_override("font_size", 10)
|
|
hint_label.modulate = Color(0.5, 0.7, 0.5)
|
|
vbox.add_child(hint_label)
|
|
|
|
tooltip.add_child(vbox)
|
|
tooltip.visible = false
|
|
add_child(tooltip)
|
|
|
|
func _refresh_inventory():
|
|
for i in range(30):
|
|
if i >= inventory_slots.size():
|
|
break
|
|
var item = GameState.inventory[i] if i < GameState.inventory.size() else null
|
|
_update_slot(inventory_slots[i], item)
|
|
|
|
for slot_name in EQUIPMENT_SLOTS:
|
|
if equipment_slots.has(slot_name):
|
|
var item = GameState.equipment.get(slot_name)
|
|
_update_slot(equipment_slots[slot_name], item, EQUIPMENT_ICONS.get(slot_name, "?"), slot_name)
|
|
|
|
func _update_slot(slot: Button, item, default_text: String = "", slot_name: String = ""):
|
|
# Clear any existing children (sprites, labels)
|
|
for child in slot.get_children():
|
|
child.queue_free()
|
|
|
|
if item == null or (item is Dictionary and item.is_empty()):
|
|
# Empty slot: try to load CDN slot texture, fall back to emoji
|
|
slot.set_meta("item", null)
|
|
slot.add_theme_stylebox_override("normal", _create_slot_style(Color(0.15, 0.15, 0.2)))
|
|
|
|
# Try to load slot icon from CDN if this is an equipment slot
|
|
if slot_name != "" and EQUIPMENT_SLOT_TEXTURES.has(slot_name) and UpdateManager:
|
|
var slot_texture_path = EQUIPMENT_SLOT_TEXTURES.get(slot_name)
|
|
var texture = UpdateManager.load_texture(slot_texture_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(32, 32)
|
|
sprite.anchors_preset = Control.PRESET_CENTER
|
|
sprite.position = Vector2(8, 8) # Center in 48x48 slot
|
|
sprite.modulate = Color(1, 1, 1, 0.3) # Dim the slot icon to show it's empty
|
|
slot.add_child(sprite)
|
|
slot.text = "" # Clear emoji text since we have a texture
|
|
return
|
|
|
|
# Fall back to emoji if CDN texture not available
|
|
slot.text = default_text
|
|
else:
|
|
# Equipped item: show the item's sprite, NOT the slot icon
|
|
slot.text = "" # Clear text, we'll use sprite instead
|
|
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(32, 32)
|
|
sprite.anchors_preset = Control.PRESET_CENTER
|
|
sprite.position = Vector2(8, 8) # Center in 48x48 slot
|
|
|
|
# Apply tint if specified
|
|
var tint = item.get("tintColor", item.get("TintColor", ""))
|
|
if tint and not tint.is_empty():
|
|
sprite.modulate = Color.from_string(tint, Color.WHITE)
|
|
|
|
slot.add_child(sprite)
|
|
has_sprite = true
|
|
|
|
# Fall back to emoji if no sprite
|
|
if not has_sprite:
|
|
slot.text = _get_item_emoji(item)
|
|
|
|
# Add stack count label if quantity > 1 OR if this is equipped ammunition/quiver
|
|
var quantity = item.get("quantity", item.get("Quantity", 1))
|
|
var show_count = quantity > 1
|
|
|
|
# Always show count for equipped quiver or ammunition slots
|
|
if slot_name == "quiver" and quantity >= 1:
|
|
show_count = true
|
|
|
|
if show_count:
|
|
var count_label = Label.new()
|
|
count_label.text = str(quantity)
|
|
count_label.add_theme_font_size_override("font_size", 10)
|
|
count_label.add_theme_color_override("font_color", Color.WHITE)
|
|
count_label.add_theme_color_override("font_shadow_color", Color.BLACK)
|
|
count_label.add_theme_constant_override("shadow_offset_x", 1)
|
|
count_label.add_theme_constant_override("shadow_offset_y", 1)
|
|
count_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
|
|
count_label.vertical_alignment = VERTICAL_ALIGNMENT_BOTTOM
|
|
count_label.anchors_preset = Control.PRESET_BOTTOM_RIGHT
|
|
count_label.position = Vector2(slot.custom_minimum_size.x - 20, slot.custom_minimum_size.y - 16)
|
|
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()
|
|
var item_name = item.get("name", item.get("Name", "")).to_lower()
|
|
|
|
match item_type:
|
|
"consumable":
|
|
if "health" in item_name: return "❤️"
|
|
if "mana" in item_name: return "💙"
|
|
if "escape" in item_name: return "🏃"
|
|
return "🧪"
|
|
"material":
|
|
if "bone" in item_name: return "🦴"
|
|
if "hide" in item_name or "leather" in item_name: return "🥩"
|
|
if "essence" in item_name or "soul" in item_name: return "👻"
|
|
if "shard" in item_name or "crystal" in item_name: return "💎"
|
|
return "📦"
|
|
"weapon": return "⚔️"
|
|
"armor":
|
|
if "helm" in item_name: return "🎩"
|
|
return "🛡️"
|
|
"accessory": return "💍"
|
|
_: return "❓"
|
|
|
|
func _on_inventory_gui_input(event: InputEvent, slot_index: int):
|
|
if not event is InputEventMouseButton:
|
|
return
|
|
|
|
var item = GameState.inventory[slot_index] if slot_index < GameState.inventory.size() else null
|
|
|
|
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
|
|
if item == null:
|
|
return
|
|
# Left click: equip/unequip or assign to hotbar
|
|
var item_type = item.get("type", item.get("Type", ""))
|
|
if item_type == "Consumable":
|
|
# Show hotbar menu for consumables
|
|
_show_hotbar_menu(slot_index)
|
|
elif item_type in ["Weapon", "Armor", "Accessory"]:
|
|
# Equip on left click
|
|
GameState.send_equip_item(slot_index)
|
|
|
|
elif event.button_index == MOUSE_BUTTON_RIGHT:
|
|
if event.pressed:
|
|
# Start drag if has item
|
|
if item and not item.is_empty() and DragManager:
|
|
DragManager.start_drag(item, "inventory", slot_index)
|
|
else:
|
|
# End drag on release (if still dragging)
|
|
if DragManager and DragManager.is_dragging:
|
|
# Check if we're dropping onto another inventory slot
|
|
var target_item = GameState.inventory[slot_index] if slot_index < GameState.inventory.size() else null
|
|
var drag_item = DragManager.get_drag_item()
|
|
|
|
# Try to stack consumables
|
|
if target_item and drag_item and _can_stack_items(drag_item, target_item):
|
|
var source_slot = DragManager.drag_data.get("source_slot", -1)
|
|
if source_slot >= 0 and source_slot != slot_index:
|
|
GameState.send_stack_items(source_slot, slot_index)
|
|
|
|
# Just end the drag - drop handling is done by the drop target
|
|
DragManager.end_drag()
|
|
|
|
func _on_equipment_gui_input(event: InputEvent, slot_name: String):
|
|
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
|
|
var item = GameState.equipment.get(slot_name)
|
|
if item != null:
|
|
GameState.send_unequip_item(slot_name)
|
|
|
|
func _can_stack_items(item1: Dictionary, item2: Dictionary) -> bool:
|
|
# Check if both items are consumables with the same name
|
|
var type1 = item1.get("type", item1.get("Type", ""))
|
|
var type2 = item2.get("type", item2.get("Type", ""))
|
|
|
|
if type1 != "Consumable" or type2 != "Consumable":
|
|
return false
|
|
|
|
# Check if same item type
|
|
var name1 = item1.get("name", item1.get("Name", ""))
|
|
var name2 = item2.get("name", item2.get("Name", ""))
|
|
|
|
if name1 != name2:
|
|
return false
|
|
|
|
# Check if target has room (not already at max stack)
|
|
var max_stack = item2.get("maxStack", item2.get("MaxStack", 99))
|
|
var current_quantity = item2.get("quantity", item2.get("Quantity", 1))
|
|
|
|
return current_quantity < max_stack
|
|
|
|
func _show_hotbar_menu(inv_slot: int):
|
|
# Simple approach: assign to next empty hotbar slot or use slot 0
|
|
# For now, find first empty slot or use slot 0
|
|
var target_slot = 0
|
|
for i in range(8):
|
|
if GameState.hotbar[i] == null:
|
|
target_slot = i
|
|
break
|
|
|
|
GameState.assign_hotbar(target_slot, inv_slot)
|
|
print("Assigned inventory slot %d to hotbar slot %d" % [inv_slot, target_slot])
|
|
|
|
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")
|
|
var desc_label = vbox.get_node_or_null("ItemDesc")
|
|
var hint_label = vbox.get_node_or_null("Hint")
|
|
|
|
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))
|
|
|
|
# Show quantity in name if > 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]
|
|
|
|
# Build stats text
|
|
var stats = []
|
|
var min_dmg = item.get("minDamage", item.get("MinDamage", 0))
|
|
var max_dmg = item.get("maxDamage", item.get("MaxDamage", 0))
|
|
if min_dmg > 0 and max_dmg > 0:
|
|
stats.append("Damage: %d-%d" % [int(min_dmg), int(max_dmg)])
|
|
|
|
var armor = item.get("armor", item.get("Armor", 0))
|
|
if armor > 0:
|
|
stats.append("Armor: +%d" % int(armor))
|
|
|
|
var bonus_str = item.get("bonusStrength", item.get("BonusStrength", 0))
|
|
if bonus_str > 0:
|
|
stats.append("Strength: +%d" % int(bonus_str))
|
|
|
|
var bonus_int = item.get("bonusIntelligence", item.get("BonusIntelligence", 0))
|
|
if bonus_int > 0:
|
|
stats.append("Intelligence: +%d" % int(bonus_int))
|
|
|
|
var bonus_wis = item.get("bonusWisdom", item.get("BonusWisdom", 0))
|
|
if bonus_wis > 0:
|
|
stats.append("Wisdom: +%d" % int(bonus_wis))
|
|
|
|
var bonus_end = item.get("bonusEndurance", item.get("BonusEndurance", 0))
|
|
if bonus_end > 0:
|
|
stats.append("Endurance: +%d" % int(bonus_end))
|
|
|
|
var bonus_dex = item.get("bonusDexterity", item.get("BonusDexterity", 0))
|
|
if bonus_dex > 0:
|
|
stats.append("Dexterity: +%d" % int(bonus_dex))
|
|
|
|
var bonus_hp = item.get("bonusHealth", item.get("BonusHealth", 0))
|
|
if bonus_hp > 0:
|
|
stats.append("Health: +%d" % int(bonus_hp))
|
|
|
|
var bonus_mp = item.get("bonusMana", item.get("BonusMana", 0))
|
|
if bonus_mp > 0:
|
|
stats.append("Mana: +%d" % int(bonus_mp))
|
|
|
|
var heal = item.get("healAmount", item.get("HealAmount", 0))
|
|
if heal > 0:
|
|
stats.append("Heals: %d HP" % int(heal))
|
|
|
|
var mana_restore = item.get("manaRestore", item.get("ManaRestore", 0))
|
|
if mana_restore > 0:
|
|
stats.append("Restores: %d MP" % int(mana_restore))
|
|
|
|
# Add crafted item info
|
|
var is_crafted = item.get("isCrafted", item.get("IsCrafted", false))
|
|
if is_crafted:
|
|
stats.append("") # Empty line separator
|
|
var crafter_name = item.get("crafterName", item.get("CrafterName", "Unknown"))
|
|
var quality = item.get("qualityRating", item.get("QualityRating", 0))
|
|
var crafted_at = item.get("craftedAt", item.get("CraftedAt", ""))
|
|
stats.append("Crafted by: %s" % crafter_name)
|
|
stats.append("Quality: %d%%" % int(quality))
|
|
|
|
if stats_label:
|
|
stats_label.text = "\n".join(stats) if stats.size() > 0 else ""
|
|
stats_label.visible = stats.size() > 0
|
|
|
|
if desc_label:
|
|
var desc = item.get("description", item.get("Description", ""))
|
|
desc_label.text = desc
|
|
desc_label.visible = desc != ""
|
|
|
|
# Hint for actions
|
|
if hint_label:
|
|
if item_type == "Consumable":
|
|
hint_label.text = "Left-click: Use | Right-click: Add to hotbar"
|
|
elif item_type in ["Weapon", "Armor", "Accessory"]:
|
|
hint_label.text = "Left-click: Equip"
|
|
else:
|
|
hint_label.text = ""
|
|
hint_label.visible = hint_label.text != ""
|
|
|
|
# Position tooltip near slot
|
|
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)
|
|
|
|
# Keep on screen
|
|
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()
|