Initial
This commit is contained in:
@@ -0,0 +1,341 @@
|
||||
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", "ring1", "ring2"]
|
||||
const EQUIPMENT_ICONS = {
|
||||
"weapon": "⚔️",
|
||||
"offhand": "🛡️",
|
||||
"helm": "🎩",
|
||||
"chest": "👕",
|
||||
"gloves": "🧤",
|
||||
"legs": "👖",
|
||||
"boots": "👢",
|
||||
"ring1": "💍",
|
||||
"ring2": "💍"
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
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.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 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, "?"))
|
||||
|
||||
func _update_slot(slot: Button, item, default_text: String = ""):
|
||||
if item == null or (item is Dictionary and item.is_empty()):
|
||||
slot.text = default_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 = _get_item_emoji(item)
|
||||
slot.set_meta("item", item)
|
||||
|
||||
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 event is InputEventMouseButton and event.pressed:
|
||||
var item = GameState.inventory[slot_index] if slot_index < GameState.inventory.size() else null
|
||||
if item == null:
|
||||
return
|
||||
|
||||
if event.button_index == MOUSE_BUTTON_LEFT:
|
||||
# Left click: use/equip
|
||||
var item_type = item.get("type", item.get("Type", ""))
|
||||
if item_type == "Consumable":
|
||||
GameState.send_use_item(slot_index)
|
||||
elif item_type in ["Weapon", "Armor", "Accessory"]:
|
||||
GameState.send_equip_item(slot_index)
|
||||
elif event.button_index == MOUSE_BUTTON_RIGHT:
|
||||
# Right click: assign to hotbar (for consumables)
|
||||
var item_type = item.get("type", item.get("Type", ""))
|
||||
if item_type == "Consumable":
|
||||
_show_hotbar_menu(slot_index)
|
||||
|
||||
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 _show_hotbar_menu(inv_slot: int):
|
||||
# Simple approach: assign to next empty hotbar slot or show popup
|
||||
# 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", ""))
|
||||
|
||||
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_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))
|
||||
|
||||
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()
|
||||
Reference in New Issue
Block a user