794 lines
23 KiB
GDScript
794 lines
23 KiB
GDScript
extends CanvasLayer
|
|
|
|
## Crafting UI with skill selection, drag & drop, tooltips, and recipe discovery
|
|
|
|
signal crafting_closed
|
|
|
|
const SKILL_NAMES = {
|
|
0: "Smithing",
|
|
1: "Fletching",
|
|
2: "Jewelry",
|
|
3: "Alchemy",
|
|
4: "Academic"
|
|
}
|
|
|
|
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 skills: Dictionary = {}
|
|
var recipes: Array = []
|
|
var selected_skill_type: int = -1 # -1 = none selected
|
|
var selected_recipe: Dictionary = {}
|
|
|
|
# Drag state
|
|
var is_dragging: bool = false
|
|
var drag_data: Dictionary = {}
|
|
var drag_preview: Control = null
|
|
|
|
# Slot arrays
|
|
var crafting_slots: Array = []
|
|
var inventory_slots: Array = []
|
|
var skill_buttons: Array = []
|
|
|
|
@onready var close_button = $Panel/VBox/Header/CloseButton
|
|
@onready var skills_container = $Panel/VBox/Content/LeftPanel/SkillsPanel/SkillButtons
|
|
@onready var skill_info_label = $Panel/VBox/Content/LeftPanel/SkillsPanel/SkillInfo
|
|
@onready var recipe_list = $Panel/VBox/Content/LeftPanel/RecipeScroll/RecipeList
|
|
@onready var crafting_grid = $Panel/VBox/Content/MiddlePanel/CraftingGrid
|
|
@onready var craft_button = $Panel/VBox/Content/MiddlePanel/ButtonRow/CraftButton
|
|
@onready var discover_button = $Panel/VBox/Content/MiddlePanel/ButtonRow/DiscoverButton
|
|
@onready var clear_button = $Panel/VBox/Content/MiddlePanel/ButtonRow/ClearButton
|
|
@onready var result_label = $Panel/VBox/Content/MiddlePanel/ResultLabel
|
|
@onready var inventory_grid = $Panel/VBox/Content/RightPanel/InventoryScroll/InventoryGrid
|
|
@onready var tooltip = $Tooltip
|
|
|
|
func _ready():
|
|
visible = false
|
|
|
|
if close_button:
|
|
close_button.pressed.connect(_on_close)
|
|
if craft_button:
|
|
craft_button.pressed.connect(_on_craft_pressed)
|
|
if discover_button:
|
|
discover_button.pressed.connect(_on_discover_pressed)
|
|
if clear_button:
|
|
clear_button.pressed.connect(_clear_crafting_grid)
|
|
|
|
_setup_skill_buttons()
|
|
_setup_crafting_grid()
|
|
_setup_inventory_grid()
|
|
_create_drag_preview()
|
|
_setup_tooltip()
|
|
|
|
call_deferred("_connect_signals")
|
|
|
|
func _connect_signals():
|
|
if GameState:
|
|
GameState.crafting_skills_updated.connect(_on_skills_updated)
|
|
GameState.crafting_recipes_updated.connect(_on_recipes_updated)
|
|
GameState.crafting_result_received.connect(_on_crafting_result)
|
|
GameState.inventory_updated.connect(_refresh_inventory_display)
|
|
|
|
func _setup_skill_buttons():
|
|
if not skills_container:
|
|
return
|
|
|
|
for skill_type in SKILL_NAMES:
|
|
var btn = Button.new()
|
|
btn.text = SKILL_NAMES[skill_type]
|
|
btn.toggle_mode = true
|
|
btn.custom_minimum_size = Vector2(90, 30)
|
|
btn.pressed.connect(_on_skill_selected.bind(skill_type))
|
|
skills_container.add_child(btn)
|
|
skill_buttons.append(btn)
|
|
|
|
func _setup_crafting_grid():
|
|
if not crafting_grid:
|
|
return
|
|
|
|
crafting_grid.columns = 4
|
|
|
|
for i in range(16):
|
|
var slot = _create_slot(40)
|
|
slot.set_meta("slot_index", i)
|
|
slot.set_meta("slot_type", "crafting")
|
|
slot.set_meta("item", null)
|
|
slot.gui_input.connect(_on_crafting_slot_input.bind(i))
|
|
slot.mouse_entered.connect(_on_slot_hover.bind(slot))
|
|
slot.mouse_exited.connect(_hide_tooltip)
|
|
crafting_grid.add_child(slot)
|
|
crafting_slots.append(slot)
|
|
|
|
func _setup_inventory_grid():
|
|
if not inventory_grid:
|
|
return
|
|
|
|
inventory_grid.columns = 5
|
|
|
|
for i in range(30):
|
|
var slot = _create_slot(40)
|
|
slot.set_meta("slot_index", i)
|
|
slot.set_meta("slot_type", "inventory")
|
|
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 _create_slot(size: int) -> Button:
|
|
var btn = Button.new()
|
|
btn.custom_minimum_size = Vector2(size, size)
|
|
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)))
|
|
btn.mouse_filter = Control.MOUSE_FILTER_STOP
|
|
btn.clip_text = true
|
|
return btn
|
|
|
|
func _create_slot_style(bg_color: Color, border_color: Color = Color(0.3, 0.3, 0.4)) -> StyleBoxFlat:
|
|
var style = StyleBoxFlat.new()
|
|
style.bg_color = bg_color
|
|
style.border_color = border_color
|
|
style.set_border_width_all(2)
|
|
style.set_corner_radius_all(4)
|
|
return style
|
|
|
|
func _create_drag_preview():
|
|
drag_preview = PanelContainer.new()
|
|
drag_preview.visible = false
|
|
drag_preview.z_index = 100
|
|
drag_preview.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
|
|
var label = Label.new()
|
|
label.name = "Label"
|
|
drag_preview.add_child(label)
|
|
|
|
add_child(drag_preview)
|
|
|
|
func _setup_tooltip():
|
|
if tooltip:
|
|
tooltip.visible = false
|
|
|
|
func show_crafting():
|
|
visible = true
|
|
selected_skill_type = -1
|
|
selected_recipe = {}
|
|
|
|
# Deselect all skill buttons
|
|
for btn in skill_buttons:
|
|
btn.button_pressed = false
|
|
|
|
if GameState:
|
|
GameState.request_crafting_skills()
|
|
GameState.request_crafting_recipes()
|
|
GameState.request_inventory()
|
|
|
|
_clear_crafting_grid()
|
|
_update_skill_info()
|
|
_refresh_recipe_list()
|
|
|
|
func _on_close():
|
|
_clear_crafting_grid()
|
|
visible = false
|
|
crafting_closed.emit()
|
|
|
|
func _on_skill_selected(skill_type: int):
|
|
# Toggle behavior - if clicking same skill, deselect
|
|
if selected_skill_type == skill_type:
|
|
selected_skill_type = -1
|
|
for btn in skill_buttons:
|
|
btn.button_pressed = false
|
|
else:
|
|
selected_skill_type = skill_type
|
|
# Update button states
|
|
for i in range(skill_buttons.size()):
|
|
skill_buttons[i].button_pressed = (i == skill_type)
|
|
|
|
selected_recipe = {}
|
|
_update_skill_info()
|
|
_refresh_recipe_list()
|
|
|
|
func _update_skill_info():
|
|
if not skill_info_label:
|
|
return
|
|
|
|
if selected_skill_type < 0:
|
|
skill_info_label.text = "Select a crafting skill"
|
|
return
|
|
|
|
var skill_name = SKILL_NAMES.get(selected_skill_type, "Unknown")
|
|
var skill_key = str(selected_skill_type)
|
|
var skill_data = skills.get(skill_key, skills.get(selected_skill_type, {}))
|
|
|
|
if skill_data.is_empty():
|
|
skill_info_label.text = "%s - Level 1" % skill_name
|
|
else:
|
|
var level = skill_data.get("level", 1)
|
|
var xp = skill_data.get("xp", 0)
|
|
var xp_next = skill_data.get("xpForNext", 100)
|
|
skill_info_label.text = "%s - Level %d (%d/%d XP)" % [skill_name, level, xp, xp_next]
|
|
|
|
func _on_skills_updated(new_skills: Dictionary):
|
|
skills = new_skills
|
|
_update_skill_info()
|
|
_refresh_recipe_list()
|
|
|
|
func _on_recipes_updated(new_recipes: Array):
|
|
recipes = new_recipes
|
|
_refresh_recipe_list()
|
|
|
|
func _refresh_recipe_list():
|
|
if not recipe_list:
|
|
return
|
|
|
|
for child in recipe_list.get_children():
|
|
child.queue_free()
|
|
|
|
if selected_skill_type < 0:
|
|
var label = Label.new()
|
|
label.text = "Select a skill to see recipes"
|
|
label.modulate = Color(0.6, 0.6, 0.6)
|
|
recipe_list.add_child(label)
|
|
return
|
|
|
|
var found_recipes = false
|
|
for recipe in recipes:
|
|
var recipe_skill = int(recipe.get("skillType", -1))
|
|
if recipe_skill == selected_skill_type:
|
|
found_recipes = true
|
|
var btn = Button.new()
|
|
btn.text = recipe.get("name", "Unknown")
|
|
btn.alignment = HORIZONTAL_ALIGNMENT_LEFT
|
|
btn.pressed.connect(_on_recipe_selected.bind(recipe))
|
|
|
|
var can_craft = _can_craft_recipe(recipe)
|
|
btn.modulate = Color.WHITE if can_craft else Color(0.5, 0.5, 0.5)
|
|
|
|
recipe_list.add_child(btn)
|
|
|
|
if not found_recipes:
|
|
var label = Label.new()
|
|
label.text = "No known recipes.\nTry discovering some!"
|
|
label.modulate = Color(0.6, 0.6, 0.6)
|
|
recipe_list.add_child(label)
|
|
|
|
func _on_recipe_selected(recipe: Dictionary):
|
|
selected_recipe = recipe
|
|
_show_result("[b]%s[/b]\n%s" % [recipe.get("name", ""), _get_recipe_info(recipe)], Color.WHITE)
|
|
|
|
func _get_recipe_info(recipe: Dictionary) -> String:
|
|
var text = ""
|
|
var ingredients = recipe.get("ingredients", [])
|
|
for ing in ingredients:
|
|
text += "%dx %s\n" % [ing.get("quantity", 1), ing.get("itemName", "?")]
|
|
text += "→ %dx %s" % [recipe.get("outputQuantity", 1), recipe.get("outputName", "?")]
|
|
return text
|
|
|
|
func _refresh_inventory_display():
|
|
if not GameState or inventory_slots.is_empty():
|
|
return
|
|
|
|
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_display(inventory_slots[i], item)
|
|
|
|
func _update_slot_display(slot: Button, item):
|
|
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.tooltip_text = ""
|
|
slot.add_theme_stylebox_override("normal", _create_slot_style(Color(0.15, 0.15, 0.2)))
|
|
return
|
|
|
|
slot.set_meta("item", item)
|
|
slot.text = ""
|
|
|
|
# Try to load sprite
|
|
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.position = Vector2(4, 4)
|
|
sprite.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
|
|
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
|
|
|
|
if not has_sprite:
|
|
slot.text = _get_item_emoji(item)
|
|
|
|
# Stack count
|
|
var qty = item.get("quantity", item.get("Quantity", 1))
|
|
if qty > 1:
|
|
var count_label = Label.new()
|
|
count_label.text = str(qty)
|
|
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.position = Vector2(24, 24)
|
|
count_label.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
slot.add_child(count_label)
|
|
|
|
# Rarity border
|
|
var rarity = item.get("rarity", item.get("Rarity", "Common"))
|
|
var style = _create_slot_style(Color(0.15, 0.15, 0.2), RARITY_COLORS.get(rarity, Color.GRAY))
|
|
slot.add_theme_stylebox_override("normal", style)
|
|
|
|
func _get_item_emoji(item: Dictionary) -> String:
|
|
var item_type = str(item.get("type", item.get("Type", ""))).to_lower()
|
|
var item_name = str(item.get("name", item.get("Name", ""))).to_lower()
|
|
|
|
if "health" in item_name: return "❤"
|
|
if "mana" in item_name: return "💙"
|
|
if "heart" in item_name: return "♥"
|
|
if "bone" in item_name: return "🦴"
|
|
if "essence" in item_name: return "✨"
|
|
if item_type == "weapon": return "⚔"
|
|
if item_type == "armor": return "🛡"
|
|
if item_type == "material": return "◆"
|
|
return "■"
|
|
|
|
# ===== DRAG AND DROP =====
|
|
|
|
func _on_inventory_slot_input(event: InputEvent, slot_index: int):
|
|
if not event is InputEventMouseButton:
|
|
return
|
|
|
|
if event.button_index != MOUSE_BUTTON_LEFT:
|
|
return
|
|
|
|
var slot = inventory_slots[slot_index]
|
|
var item = slot.get_meta("item")
|
|
|
|
if event.pressed:
|
|
# Start drag if has item
|
|
if item and not item.is_empty():
|
|
_start_drag(item, slot_index, "inventory")
|
|
print("[Crafting] Started drag: %s" % item.get("name", "?"))
|
|
|
|
func _on_crafting_slot_input(event: InputEvent, slot_index: int):
|
|
if not event is InputEventMouseButton:
|
|
return
|
|
|
|
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
|
|
var slot = crafting_slots[slot_index]
|
|
|
|
if is_dragging:
|
|
# Drop the dragged item here
|
|
_drop_on_crafting_slot(slot_index)
|
|
print("[Crafting] Dropped on slot %d" % slot_index)
|
|
else:
|
|
# Click to clear the slot
|
|
var item = slot.get_meta("item")
|
|
if item and not item.is_empty():
|
|
_clear_crafting_slot(slot_index)
|
|
|
|
elif event.button_index == MOUSE_BUTTON_RIGHT and event.pressed:
|
|
# Right click to clear
|
|
_clear_crafting_slot(slot_index)
|
|
|
|
func _start_drag(item: Dictionary, source_slot: int, source_type: String):
|
|
is_dragging = true
|
|
drag_data = {
|
|
"item": item.duplicate(),
|
|
"source_slot": source_slot,
|
|
"source_type": source_type
|
|
}
|
|
|
|
if drag_preview:
|
|
var label = drag_preview.get_node_or_null("Label")
|
|
if label:
|
|
label.text = item.get("name", "Item")
|
|
drag_preview.visible = true
|
|
drag_preview.global_position = get_viewport().get_mouse_position() + Vector2(15, 15)
|
|
|
|
func _drop_on_crafting_slot(slot_index: int):
|
|
if not is_dragging or drag_data.is_empty():
|
|
return
|
|
|
|
var slot = crafting_slots[slot_index]
|
|
var item = drag_data.get("item", {})
|
|
var source_slot = drag_data.get("source_slot", -1)
|
|
var max_qty = item.get("quantity", 1)
|
|
|
|
# If item has quantity > 1, show quantity selector
|
|
if max_qty > 1:
|
|
_show_quantity_popup(slot_index, item, source_slot, max_qty)
|
|
else:
|
|
# Just place 1
|
|
_place_in_crafting_slot(slot_index, item, source_slot, 1)
|
|
|
|
_end_drag()
|
|
|
|
func _show_quantity_popup(slot_index: int, item: Dictionary, source_slot: int, max_qty: int):
|
|
# Create a simple popup for quantity selection
|
|
var popup = PopupPanel.new()
|
|
popup.name = "QuantityPopup"
|
|
|
|
var vbox = VBoxContainer.new()
|
|
popup.add_child(vbox)
|
|
|
|
var label = Label.new()
|
|
label.text = "How many %s?" % item.get("name", "items")
|
|
vbox.add_child(label)
|
|
|
|
var hbox = HBoxContainer.new()
|
|
vbox.add_child(hbox)
|
|
|
|
var spin = SpinBox.new()
|
|
spin.min_value = 1
|
|
spin.max_value = max_qty
|
|
spin.value = 1
|
|
spin.name = "SpinBox"
|
|
hbox.add_child(spin)
|
|
|
|
var max_btn = Button.new()
|
|
max_btn.text = "Max"
|
|
max_btn.pressed.connect(func(): spin.value = max_qty)
|
|
hbox.add_child(max_btn)
|
|
|
|
var btn_row = HBoxContainer.new()
|
|
vbox.add_child(btn_row)
|
|
|
|
var ok_btn = Button.new()
|
|
ok_btn.text = "OK"
|
|
ok_btn.pressed.connect(func():
|
|
var qty = int(spin.value)
|
|
_place_in_crafting_slot(slot_index, item, source_slot, qty)
|
|
popup.queue_free()
|
|
)
|
|
btn_row.add_child(ok_btn)
|
|
|
|
var cancel_btn = Button.new()
|
|
cancel_btn.text = "Cancel"
|
|
cancel_btn.pressed.connect(func(): popup.queue_free())
|
|
btn_row.add_child(cancel_btn)
|
|
|
|
add_child(popup)
|
|
popup.popup_centered()
|
|
|
|
func _place_in_crafting_slot(slot_index: int, item: Dictionary, source_slot: int, quantity: int):
|
|
var slot = crafting_slots[slot_index]
|
|
|
|
# Create crafting slot item with selected quantity
|
|
var craft_item = item.duplicate()
|
|
craft_item["quantity"] = quantity
|
|
craft_item["_source_slot"] = source_slot
|
|
|
|
slot.set_meta("item", craft_item)
|
|
_update_slot_display(slot, craft_item)
|
|
|
|
# Green border to show it's in crafting grid
|
|
var style = _create_slot_style(Color(0.15, 0.2, 0.15), Color(0.3, 0.6, 0.3))
|
|
slot.add_theme_stylebox_override("normal", style)
|
|
|
|
# Update the visual quantity remaining in inventory display
|
|
_update_inventory_visual()
|
|
|
|
func _update_inventory_visual():
|
|
# Recalculate what's "used" in crafting grid and show reduced quantities
|
|
if not GameState or inventory_slots.is_empty():
|
|
return
|
|
|
|
# Count what's in crafting grid by source slot
|
|
var used_by_slot = {}
|
|
for slot in crafting_slots:
|
|
var item = slot.get_meta("item")
|
|
if item and not item.is_empty():
|
|
var src = item.get("_source_slot", -1)
|
|
if src >= 0:
|
|
if not used_by_slot.has(src):
|
|
used_by_slot[src] = 0
|
|
used_by_slot[src] += item.get("quantity", 1)
|
|
|
|
# Update inventory display with reduced quantities
|
|
for i in range(30):
|
|
if i >= inventory_slots.size():
|
|
break
|
|
var inv_slot = inventory_slots[i]
|
|
var item = GameState.inventory[i] if i < GameState.inventory.size() else null
|
|
|
|
if item and not item.is_empty():
|
|
var used = used_by_slot.get(i, 0)
|
|
var remaining = item.get("quantity", 1) - used
|
|
|
|
if remaining <= 0:
|
|
# All used in crafting, show empty/grayed
|
|
_update_slot_display(inv_slot, null)
|
|
inv_slot.modulate = Color(0.5, 0.5, 0.5, 0.5)
|
|
else:
|
|
# Show reduced quantity
|
|
var display_item = item.duplicate()
|
|
display_item["quantity"] = remaining
|
|
_update_slot_display(inv_slot, display_item)
|
|
inv_slot.modulate = Color.WHITE
|
|
else:
|
|
_update_slot_display(inv_slot, item)
|
|
inv_slot.modulate = Color.WHITE
|
|
|
|
func _end_drag():
|
|
is_dragging = false
|
|
drag_data = {}
|
|
if drag_preview:
|
|
drag_preview.visible = false
|
|
|
|
func _clear_crafting_slot(slot_index: int):
|
|
var slot = crafting_slots[slot_index]
|
|
slot.set_meta("item", null)
|
|
slot.text = ""
|
|
for child in slot.get_children():
|
|
child.queue_free()
|
|
slot.add_theme_stylebox_override("normal", _create_slot_style(Color(0.15, 0.15, 0.2)))
|
|
|
|
# Update inventory visuals
|
|
_update_inventory_visual()
|
|
|
|
func _clear_crafting_grid():
|
|
for i in range(crafting_slots.size()):
|
|
var slot = crafting_slots[i]
|
|
slot.set_meta("item", null)
|
|
slot.text = ""
|
|
for child in slot.get_children():
|
|
child.queue_free()
|
|
slot.add_theme_stylebox_override("normal", _create_slot_style(Color(0.15, 0.15, 0.2)))
|
|
|
|
# Reset inventory display
|
|
_refresh_inventory_display()
|
|
|
|
func _input(event):
|
|
if not visible:
|
|
return
|
|
|
|
# Handle drag release anywhere
|
|
if is_dragging and event is InputEventMouseButton:
|
|
if event.button_index == MOUSE_BUTTON_LEFT and not event.pressed:
|
|
# Check if we're over a crafting slot
|
|
var dominated = false
|
|
for i in range(crafting_slots.size()):
|
|
var slot = crafting_slots[i]
|
|
var rect = slot.get_global_rect()
|
|
if rect.has_point(get_viewport().get_mouse_position()):
|
|
_drop_on_crafting_slot(i)
|
|
dominated = true
|
|
break
|
|
|
|
if not dominated:
|
|
# Dropped elsewhere, cancel
|
|
_end_drag()
|
|
|
|
# Cancel drag on right click
|
|
if is_dragging and event is InputEventMouseButton:
|
|
if event.button_index == MOUSE_BUTTON_RIGHT and event.pressed:
|
|
_end_drag()
|
|
|
|
if event.is_action_pressed("escape_menu"):
|
|
_on_close()
|
|
get_viewport().set_input_as_handled()
|
|
|
|
func _process(_delta):
|
|
if is_dragging and drag_preview:
|
|
drag_preview.global_position = get_viewport().get_mouse_position() + Vector2(15, 15)
|
|
|
|
# ===== TOOLTIP =====
|
|
|
|
func _on_slot_hover(slot: Button):
|
|
var item = slot.get_meta("item")
|
|
if item == null or (item is Dictionary and item.is_empty()):
|
|
_hide_tooltip()
|
|
return
|
|
_show_tooltip(item, slot)
|
|
|
|
func _show_tooltip(item: Dictionary, slot: Button):
|
|
if not tooltip:
|
|
return
|
|
|
|
var name_label = tooltip.get_node_or_null("VBox/ItemName")
|
|
var type_label = tooltip.get_node_or_null("VBox/ItemType")
|
|
var stats_label = tooltip.get_node_or_null("VBox/ItemStats")
|
|
var desc_label = tooltip.get_node_or_null("VBox/ItemDesc")
|
|
|
|
if name_label:
|
|
var qty = item.get("quantity", 1)
|
|
var item_name = item.get("name", "Unknown")
|
|
name_label.text = item_name if qty <= 1 else "%s (x%d)" % [item_name, qty]
|
|
var rarity = item.get("rarity", "Common")
|
|
name_label.modulate = RARITY_COLORS.get(rarity, Color.WHITE)
|
|
|
|
if type_label:
|
|
type_label.text = item.get("type", "")
|
|
|
|
if stats_label:
|
|
var stats_text = ""
|
|
if item.get("minDamage", 0) > 0 or item.get("maxDamage", 0) > 0:
|
|
stats_text += "Damage: %d-%d\n" % [item.get("minDamage", 0), item.get("maxDamage", 0)]
|
|
if item.get("armor", 0) > 0:
|
|
stats_text += "Armor: %d\n" % item.get("armor", 0)
|
|
if item.get("healAmount", 0) > 0:
|
|
stats_text += "Heals: %d HP\n" % item.get("healAmount", 0)
|
|
stats_label.text = stats_text
|
|
|
|
if desc_label:
|
|
desc_label.text = item.get("description", "")
|
|
|
|
tooltip.visible = true
|
|
tooltip.global_position = slot.global_position + Vector2(slot.size.x + 5, 0)
|
|
|
|
func _hide_tooltip():
|
|
if tooltip:
|
|
tooltip.visible = false
|
|
|
|
# ===== CRAFTING =====
|
|
|
|
func _can_craft_recipe(recipe: Dictionary) -> bool:
|
|
if not GameState:
|
|
return false
|
|
|
|
var skill_key = str(recipe.get("skillType", 0))
|
|
var skill_data = skills.get(skill_key, skills.get(int(skill_key), {}))
|
|
var player_level = skill_data.get("level", 1) if skill_data else 1
|
|
|
|
if player_level < recipe.get("requiredLevel", 1):
|
|
return false
|
|
|
|
var ingredients = recipe.get("ingredients", [])
|
|
for ing in ingredients:
|
|
var def_id = ing.get("itemDefinitionId", 0)
|
|
var qty_needed = ing.get("quantity", 1)
|
|
var total_have = 0
|
|
for item in GameState.inventory:
|
|
if item and item.get("definitionId", 0) == def_id:
|
|
total_have += item.get("quantity", 1)
|
|
if total_have < qty_needed:
|
|
return false
|
|
|
|
return true
|
|
|
|
func _get_crafting_grid_items() -> Array:
|
|
var items = []
|
|
for slot in crafting_slots:
|
|
var item = slot.get_meta("item")
|
|
if item and not item.is_empty():
|
|
items.append(item)
|
|
return items
|
|
|
|
func _on_craft_pressed():
|
|
if selected_skill_type < 0:
|
|
_show_result("Select a crafting skill first!", Color.RED)
|
|
return
|
|
|
|
if selected_recipe.is_empty():
|
|
_show_result("Select a recipe to craft!", Color.RED)
|
|
return
|
|
|
|
if not _can_craft_recipe(selected_recipe):
|
|
_show_result("Missing ingredients or level!", Color.RED)
|
|
return
|
|
|
|
if GameState:
|
|
GameState.send_craft_request(selected_recipe.get("id", 0))
|
|
craft_button.disabled = true
|
|
|
|
func _on_discover_pressed():
|
|
if selected_skill_type < 0:
|
|
_show_result("Select a crafting skill first!", Color.YELLOW)
|
|
return
|
|
|
|
var items = _get_crafting_grid_items()
|
|
if items.is_empty():
|
|
_show_result("Place ingredients in the grid!", Color.YELLOW)
|
|
return
|
|
|
|
# Count ingredients by definition ID, using the quantity in each slot
|
|
var ingredient_counts = {}
|
|
for item in items:
|
|
# Server sends "definitionId" (camelCase)
|
|
var def_id = item.get("definitionId", 0)
|
|
var qty = item.get("quantity", 1)
|
|
|
|
print("[Crafting] Item in grid: name=%s, definitionId=%s, qty=%s, all_keys=%s" % [
|
|
item.get("name", "?"), def_id, qty, item.keys()])
|
|
|
|
if def_id == 0 or def_id == null:
|
|
print("[Crafting] ERROR: Item missing definitionId!")
|
|
_show_result("Error: Item has no definition ID", Color.RED)
|
|
return
|
|
|
|
if not ingredient_counts.has(def_id):
|
|
ingredient_counts[def_id] = 0
|
|
ingredient_counts[def_id] += qty
|
|
|
|
if ingredient_counts.is_empty():
|
|
_show_result("No valid ingredients found!", Color.RED)
|
|
return
|
|
|
|
var ingredients = []
|
|
for def_id in ingredient_counts:
|
|
ingredients.append({
|
|
"itemDefinitionId": int(def_id),
|
|
"quantity": int(ingredient_counts[def_id])
|
|
})
|
|
|
|
print("[Crafting] Sending discovery request: skill=%d, ingredients=%s" % [selected_skill_type, ingredients])
|
|
|
|
print("[Crafting] Attempting discovery with skill %d: %s" % [selected_skill_type, ingredients])
|
|
|
|
if GameState:
|
|
# Send with skill type for filtering
|
|
GameState.send_discover_recipe_with_skill(ingredients, selected_skill_type)
|
|
if discover_button:
|
|
discover_button.disabled = true
|
|
|
|
func _on_crafting_result(result: Dictionary):
|
|
print("[Crafting] Received result: %s" % result)
|
|
craft_button.disabled = false
|
|
if discover_button:
|
|
discover_button.disabled = false
|
|
|
|
if result.get("success", false):
|
|
var item_name = result.get("outputName", "item")
|
|
var qty = result.get("quantity", 1)
|
|
var xp = result.get("xpGained", 0)
|
|
var got_bonus = result.get("gotBonusOutput", false)
|
|
var discovered = result.get("discovered", false)
|
|
|
|
var text = ""
|
|
if discovered:
|
|
text = "[color=cyan]NEW RECIPE![/color]\n%s" % result.get("recipeName", item_name)
|
|
else:
|
|
text = "Crafted %dx %s\n+%d XP" % [qty, item_name, xp]
|
|
if got_bonus:
|
|
text = "[color=yellow]BONUS![/color] " + text
|
|
|
|
if result.get("leveledUp", false):
|
|
text += "\n[color=lime]LEVEL UP! → %d[/color]" % result.get("newSkillLevel", 1)
|
|
|
|
_show_result(text, Color.GREEN)
|
|
|
|
GameState.request_crafting_skills()
|
|
GameState.request_inventory()
|
|
_refresh_recipe_list()
|
|
_clear_crafting_grid()
|
|
else:
|
|
var error_text = result.get("error", "Failed!")
|
|
var materials_lost = result.get("materialsLost", false)
|
|
|
|
if materials_lost:
|
|
_show_result("[color=red]%s[/color]" % error_text, Color.RED)
|
|
# Refresh inventory since materials were lost
|
|
GameState.request_inventory()
|
|
else:
|
|
_show_result(error_text, Color.YELLOW)
|
|
|
|
_clear_crafting_grid()
|
|
|
|
func _show_result(text: String, color: Color):
|
|
if result_label:
|
|
result_label.bbcode_enabled = true
|
|
if color == Color.WHITE:
|
|
result_label.text = text
|
|
else:
|
|
result_label.text = "[color=#%s]%s[/color]" % [color.to_html(false), text]
|