233 lines
7.2 KiB
GDScript
233 lines
7.2 KiB
GDScript
extends CanvasLayer
|
|
|
|
## Mob Loot UI
|
|
## Displays loot from a dead mob in a 5x4 grid with scrollbar
|
|
|
|
@onready var panel_container = $PanelContainer
|
|
@onready var close_button = $PanelContainer/VBoxContainer/Header/CloseButton
|
|
@onready var mob_name_label = $PanelContainer/VBoxContainer/Header/MobNameLabel
|
|
@onready var loot_grid = $PanelContainer/VBoxContainer/Content/ScrollContainer/LootGrid
|
|
@onready var scroll_container = $PanelContainer/VBoxContainer/Content/ScrollContainer
|
|
@onready var take_all_button = $PanelContainer/VBoxContainer/Footer/TakeAllButton
|
|
|
|
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 current_mob_id: int = -1
|
|
var loot_slots: Array = []
|
|
|
|
func _ready():
|
|
visible = false
|
|
|
|
if close_button:
|
|
close_button.pressed.connect(_on_close)
|
|
|
|
if take_all_button:
|
|
take_all_button.pressed.connect(_on_take_all)
|
|
|
|
_setup_loot_grid()
|
|
|
|
# Connect to GameState signal
|
|
if GameState:
|
|
GameState.mob_loot_state_received.connect(_on_mob_loot_state_received)
|
|
|
|
func _on_mob_loot_state_received(data: Dictionary):
|
|
var mob_id = data.get("mobId", -1)
|
|
var mob_name = data.get("mobName", "Mob")
|
|
var items = data.get("items", [])
|
|
|
|
show_loot(mob_id, mob_name, items)
|
|
|
|
func _setup_loot_grid():
|
|
if not loot_grid:
|
|
return
|
|
|
|
# Create 20 slots (5x4 grid) initially
|
|
for i in range(20):
|
|
var slot = _create_slot_button()
|
|
slot.set_meta("slot_index", i)
|
|
slot.set_meta("item", null)
|
|
slot.gui_input.connect(_on_slot_gui_input.bind(i))
|
|
loot_grid.add_child(slot)
|
|
loot_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 show_loot(mob_id: int, mob_name: String, loot_items: Array):
|
|
current_mob_id = mob_id
|
|
|
|
if mob_name_label:
|
|
mob_name_label.text = "%s's Loot" % mob_name
|
|
|
|
# Clear existing slots
|
|
for slot in loot_slots:
|
|
_clear_slot(slot)
|
|
|
|
# Add more slots if needed (expandable to show all loot)
|
|
var needed_slots = loot_items.size()
|
|
while loot_slots.size() < needed_slots:
|
|
var slot = _create_slot_button()
|
|
var slot_index = loot_slots.size()
|
|
slot.set_meta("slot_index", slot_index)
|
|
slot.set_meta("item", null)
|
|
slot.gui_input.connect(_on_slot_gui_input.bind(slot_index))
|
|
loot_grid.add_child(slot)
|
|
loot_slots.append(slot)
|
|
|
|
# Populate loot items
|
|
for i in range(loot_items.size()):
|
|
if i < loot_slots.size():
|
|
_update_slot(loot_slots[i], loot_items[i])
|
|
|
|
visible = true
|
|
|
|
func _clear_slot(slot: Button):
|
|
for child in slot.get_children():
|
|
child.queue_free()
|
|
slot.set_meta("item", null)
|
|
slot.text = ""
|
|
slot.add_theme_stylebox_override("normal", _create_slot_style(Color(0.15, 0.15, 0.2)))
|
|
|
|
func _update_slot(slot: Button, item: Dictionary):
|
|
_clear_slot(slot)
|
|
|
|
if item.is_empty():
|
|
return
|
|
|
|
slot.set_meta("item", item)
|
|
slot.text = ""
|
|
|
|
# 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)
|
|
|
|
# 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
|
|
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", 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_slot_gui_input(event: InputEvent, slot_index: int):
|
|
if not event is InputEventMouseButton:
|
|
return
|
|
|
|
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
|
|
var item = loot_slots[slot_index].get_meta("item", null)
|
|
if item and not item.is_empty():
|
|
_take_item(slot_index)
|
|
|
|
func _take_item(slot_index: int):
|
|
if current_mob_id < 0:
|
|
return
|
|
|
|
# Send message to server to take item from mob loot
|
|
GameState.take_mob_loot_item(current_mob_id, slot_index)
|
|
print("[MobLootUI] Taking item at index %d from mob %d" % [slot_index, current_mob_id])
|
|
|
|
# Don't clear locally - wait for server to send updated loot state
|
|
# The server will send MOB_LOOT_STATE with remaining items
|
|
|
|
func _on_take_all():
|
|
if current_mob_id < 0:
|
|
return
|
|
|
|
# Send message to server to take all items from mob loot
|
|
GameState.take_all_mob_loot(current_mob_id)
|
|
print("[MobLootUI] Taking all items from mob %d" % current_mob_id)
|
|
|
|
# Clear all slots locally (server will update inventory)
|
|
for slot in loot_slots:
|
|
_clear_slot(slot)
|
|
|
|
_on_close()
|
|
|
|
func _on_close():
|
|
visible = false
|
|
current_mob_id = -1
|
|
|
|
func _input(event):
|
|
if visible and event.is_action_pressed("escape_menu"):
|
|
_on_close()
|
|
get_viewport().set_input_as_handled()
|