Crafting v2
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
extends Node
|
||||
|
||||
## DragManager - handles drag and drop between UI elements
|
||||
|
||||
signal drag_started(data: Dictionary)
|
||||
signal drag_ended
|
||||
signal item_dropped(data: Dictionary, target_type: String, target_slot: int)
|
||||
|
||||
var is_dragging: bool = false
|
||||
var drag_data: Dictionary = {}
|
||||
var drag_preview: Control = null
|
||||
|
||||
func _ready():
|
||||
_create_drag_preview()
|
||||
|
||||
func _create_drag_preview():
|
||||
drag_preview = PanelContainer.new()
|
||||
drag_preview.name = "DragPreview"
|
||||
drag_preview.visible = false
|
||||
drag_preview.z_index = 1000
|
||||
drag_preview.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
||||
|
||||
var style = StyleBoxFlat.new()
|
||||
style.bg_color = Color(0.2, 0.2, 0.25, 0.9)
|
||||
style.set_corner_radius_all(4)
|
||||
drag_preview.add_theme_stylebox_override("panel", style)
|
||||
|
||||
var label = Label.new()
|
||||
label.name = "Label"
|
||||
label.add_theme_font_size_override("font_size", 12)
|
||||
drag_preview.add_child(label)
|
||||
|
||||
# Add to scene tree when ready
|
||||
call_deferred("_add_preview_to_tree")
|
||||
|
||||
func _add_preview_to_tree():
|
||||
var root = get_tree().root
|
||||
if root:
|
||||
root.add_child(drag_preview)
|
||||
|
||||
func start_drag(item: Dictionary, source_type: String, source_slot: int):
|
||||
is_dragging = true
|
||||
drag_data = {
|
||||
"item": item.duplicate(),
|
||||
"source_type": source_type,
|
||||
"source_slot": source_slot
|
||||
}
|
||||
|
||||
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_started.emit(drag_data)
|
||||
|
||||
func end_drag():
|
||||
is_dragging = false
|
||||
drag_data = {}
|
||||
if drag_preview:
|
||||
drag_preview.visible = false
|
||||
drag_ended.emit()
|
||||
|
||||
func drop_on(target_type: String, target_slot: int):
|
||||
if not is_dragging:
|
||||
return
|
||||
|
||||
item_dropped.emit(drag_data, target_type, target_slot)
|
||||
end_drag()
|
||||
|
||||
func get_drag_item() -> Dictionary:
|
||||
return drag_data.get("item", {})
|
||||
|
||||
func _process(_delta):
|
||||
if is_dragging and drag_preview:
|
||||
drag_preview.global_position = get_viewport().get_mouse_position() + Vector2(15, 10)
|
||||
|
||||
func _input(event):
|
||||
# Cancel drag on right click or escape
|
||||
if is_dragging:
|
||||
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_RIGHT:
|
||||
end_drag()
|
||||
elif event.is_action_pressed("ui_cancel"):
|
||||
end_drag()
|
||||
@@ -14,6 +14,10 @@ signal loot_chest_spawned(chest_data: Dictionary)
|
||||
signal loot_chest_state_received(chest_data: Dictionary)
|
||||
signal death_loot_lost(data: Dictionary)
|
||||
signal dungeon_list_received(data: Dictionary)
|
||||
signal crafting_skills_updated(skills: Dictionary)
|
||||
signal crafting_recipes_updated(recipes: Array)
|
||||
signal crafting_result_received(result: Dictionary)
|
||||
signal recipe_discovered(recipe: Dictionary)
|
||||
|
||||
# Use constants instead of enum to avoid type issues
|
||||
const STATE_AUTH = 0
|
||||
@@ -138,6 +142,16 @@ func _on_message_received(opcode: int, data: Dictionary):
|
||||
_handle_dungeon_list(data)
|
||||
OpCodes.ASSET_MANIFEST_RESPONSE:
|
||||
_handle_asset_manifest(data)
|
||||
OpCodes.CRAFTING_SKILLS_STATE:
|
||||
_handle_crafting_skills(data)
|
||||
OpCodes.CRAFTING_RECIPES_STATE:
|
||||
_handle_crafting_recipes(data)
|
||||
OpCodes.CRAFTING_RESULT:
|
||||
_handle_crafting_result(data)
|
||||
OpCodes.RECIPE_DISCOVERED:
|
||||
_handle_recipe_discovered(data)
|
||||
OpCodes.CRAFTING_SKILL_LEVELUP:
|
||||
_handle_crafting_skill_levelup(data)
|
||||
_:
|
||||
print("[GameState] Unhandled opcode: ", opcode)
|
||||
|
||||
@@ -534,3 +548,64 @@ func create_dungeon(tier: int):
|
||||
|
||||
func join_dungeon(dungeon_id: String):
|
||||
Network.send_message(OpCodes.DUNGEON_JOIN, {"DungeonId": dungeon_id})
|
||||
|
||||
# ===== CRAFTING SYSTEM =====
|
||||
|
||||
var crafting_skills: Dictionary = {} # skillType -> {level, xp, xpForNext}
|
||||
var known_recipes: Array = []
|
||||
|
||||
func _handle_crafting_skills(data: Dictionary):
|
||||
print("[GameState] Received crafting skills")
|
||||
crafting_skills = data.get("skills", {})
|
||||
crafting_skills_updated.emit(crafting_skills)
|
||||
|
||||
func _handle_crafting_recipes(data: Dictionary):
|
||||
print("[GameState] Received crafting recipes")
|
||||
known_recipes = data.get("recipes", [])
|
||||
crafting_recipes_updated.emit(known_recipes)
|
||||
|
||||
func _handle_crafting_result(data: Dictionary):
|
||||
print("[GameState] Received crafting result: %s" % str(data))
|
||||
crafting_result_received.emit(data)
|
||||
|
||||
# Refresh inventory if successful
|
||||
if data.get("success", false):
|
||||
request_inventory()
|
||||
|
||||
func _handle_recipe_discovered(data: Dictionary):
|
||||
print("[GameState] Discovered new recipe: %s" % data.get("name", "Unknown"))
|
||||
recipe_discovered.emit(data)
|
||||
# Refresh recipes
|
||||
request_crafting_recipes()
|
||||
|
||||
func _handle_crafting_skill_levelup(data: Dictionary):
|
||||
print("[GameState] Crafting skill level up: %s to level %d" % [
|
||||
data.get("skillName", "Unknown"),
|
||||
data.get("newLevel", 1)
|
||||
])
|
||||
# Refresh skills
|
||||
request_crafting_skills()
|
||||
|
||||
func request_crafting_skills():
|
||||
Network.send_message(OpCodes.CRAFTING_SKILLS_REQUEST, {})
|
||||
|
||||
func request_crafting_recipes():
|
||||
Network.send_message(OpCodes.CRAFTING_RECIPES_REQUEST, {})
|
||||
|
||||
func request_inventory():
|
||||
Network.send_message(OpCodes.INVENTORY_REQUEST, {})
|
||||
|
||||
func send_craft_request(recipe_id: int):
|
||||
Network.send_message(OpCodes.CRAFTING_ATTEMPT, {"RecipeId": recipe_id})
|
||||
|
||||
func send_discover_recipe(ingredients: Array):
|
||||
# ingredients is an array of {itemDefinitionId, quantity}
|
||||
Network.send_message(OpCodes.CRAFTING_ATTEMPT, {"Ingredients": ingredients, "Discover": true})
|
||||
|
||||
func send_discover_recipe_with_skill(ingredients: Array, skill_type: int):
|
||||
# Discovery with specific skill type
|
||||
Network.send_message(OpCodes.CRAFTING_ATTEMPT, {
|
||||
"Ingredients": ingredients,
|
||||
"Discover": true,
|
||||
"SkillType": skill_type
|
||||
})
|
||||
|
||||
@@ -34,6 +34,17 @@ const LOOT_CHEST_SPAWNED = 275
|
||||
# Player Death
|
||||
const PLAYER_DEATH_LOOT_LOST = 280
|
||||
|
||||
# Crafting
|
||||
const CRAFTING_SKILLS_REQUEST = 330
|
||||
const CRAFTING_SKILLS_STATE = 331
|
||||
const CRAFTING_RECIPES_REQUEST = 332
|
||||
const CRAFTING_RECIPES_STATE = 333
|
||||
const CRAFTING_ATTEMPT = 334
|
||||
const CRAFTING_RESULT = 335
|
||||
const CRAFTING_SKILL_LEVELUP = 336
|
||||
const RECIPE_DISCOVERED = 337
|
||||
const RECIPE_SCROLL_USE = 338
|
||||
|
||||
# Dungeon
|
||||
const DUNGEON_ENTER = 300
|
||||
const DUNGEON_ENTER_RESULT = 301
|
||||
|
||||
@@ -0,0 +1,628 @@
|
||||
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
|
||||
|
||||
var slot = inventory_slots[slot_index]
|
||||
var item = slot.get_meta("item")
|
||||
|
||||
if event.button_index == MOUSE_BUTTON_LEFT:
|
||||
if event.pressed:
|
||||
# Start drag if has item
|
||||
if item and not item.is_empty():
|
||||
_start_drag(item, slot_index, "inventory")
|
||||
else:
|
||||
# End drag
|
||||
if is_dragging:
|
||||
_end_drag()
|
||||
|
||||
func _on_crafting_slot_input(event: InputEvent, slot_index: int):
|
||||
if not event is InputEventMouseButton:
|
||||
return
|
||||
|
||||
var slot = crafting_slots[slot_index]
|
||||
|
||||
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
|
||||
if is_dragging:
|
||||
# Drop item here
|
||||
_drop_on_crafting_slot(slot_index)
|
||||
else:
|
||||
# Pick up from crafting slot
|
||||
var item = slot.get_meta("item")
|
||||
if item and not item.is_empty():
|
||||
# Just clear the slot (item is still in inventory)
|
||||
_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
|
||||
|
||||
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", {})
|
||||
|
||||
# Place in crafting slot
|
||||
slot.set_meta("item", item.duplicate())
|
||||
_update_slot_display(slot, item)
|
||||
|
||||
# Green border to show it's in crafting
|
||||
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)
|
||||
|
||||
_end_drag()
|
||||
|
||||
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)))
|
||||
|
||||
func _clear_crafting_grid():
|
||||
for i in range(crafting_slots.size()):
|
||||
_clear_crafting_slot(i)
|
||||
|
||||
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
|
||||
var ingredient_counts = {}
|
||||
for item in items:
|
||||
var def_id = item.get("definitionId", 0)
|
||||
if not ingredient_counts.has(def_id):
|
||||
ingredient_counts[def_id] = 0
|
||||
ingredient_counts[def_id] += 1
|
||||
|
||||
var ingredients = []
|
||||
for def_id in ingredient_counts:
|
||||
ingredients.append({
|
||||
"itemDefinitionId": def_id,
|
||||
"quantity": ingredient_counts[def_id]
|
||||
})
|
||||
|
||||
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):
|
||||
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:
|
||||
_show_result(result.get("error", "Failed!"), Color.RED)
|
||||
|
||||
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]
|
||||
|
||||
func _input(event):
|
||||
if not visible:
|
||||
return
|
||||
|
||||
if event.is_action_pressed("escape_menu"):
|
||||
_on_close()
|
||||
get_viewport().set_input_as_handled()
|
||||
|
||||
# Cancel drag on escape or right click
|
||||
if is_dragging:
|
||||
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_RIGHT:
|
||||
_end_drag()
|
||||
@@ -0,0 +1 @@
|
||||
uid://dht62hol3uhdp
|
||||
@@ -16,6 +16,11 @@ extends CanvasLayer
|
||||
@onready var browse_btn = $Control/HBoxContainer/RightPanel/BrowseDungeonsBtn
|
||||
@onready var dungeon_browser = $DungeonBrowser
|
||||
|
||||
@onready var inventory_btn = $Control/HBoxContainer/LeftPanel/MenuButtons/InventoryBtn
|
||||
@onready var crafting_btn = $Control/HBoxContainer/LeftPanel/MenuButtons/CraftingBtn
|
||||
@onready var inventory_ui = $InventoryUI
|
||||
@onready var crafting_ui = $CraftingUI
|
||||
|
||||
var selected_tier: int = 1
|
||||
|
||||
func _ready():
|
||||
@@ -25,6 +30,12 @@ func _ready():
|
||||
if browse_btn:
|
||||
browse_btn.pressed.connect(_on_browse_dungeons)
|
||||
|
||||
# Setup inventory and crafting buttons
|
||||
if inventory_btn:
|
||||
inventory_btn.pressed.connect(_on_inventory_pressed)
|
||||
if crafting_btn:
|
||||
crafting_btn.pressed.connect(_on_crafting_pressed)
|
||||
|
||||
# Setup tier buttons
|
||||
for i in range(1, 6):
|
||||
var btn_path = "Control/HBoxContainer/RightPanel/TierSelection/Tier%dBtn" % i
|
||||
@@ -45,6 +56,10 @@ func _ready():
|
||||
func _connect_gamestate():
|
||||
if GameState:
|
||||
GameState.character_updated.connect(_update_ui)
|
||||
|
||||
# Request inventory when hub loads
|
||||
if GameState:
|
||||
GameState.request_inventory()
|
||||
|
||||
func _update_ui():
|
||||
var c = GameState.character
|
||||
@@ -93,3 +108,14 @@ func _on_enter_dungeon():
|
||||
func _on_browse_dungeons():
|
||||
if dungeon_browser:
|
||||
dungeon_browser.show_browser()
|
||||
|
||||
func _on_inventory_pressed():
|
||||
if inventory_ui:
|
||||
inventory_ui.visible = true
|
||||
# Request fresh inventory data
|
||||
if GameState:
|
||||
GameState.request_inventory()
|
||||
|
||||
func _on_crafting_pressed():
|
||||
if crafting_ui:
|
||||
crafting_ui.show_crafting()
|
||||
|
||||
@@ -37,6 +37,18 @@ func _connect_gamestate():
|
||||
GameState.inventory_updated.connect(_update_hotbar)
|
||||
GameState.dungeon_entered.connect(_on_dungeon_entered)
|
||||
GameState.loot_received.connect(_on_loot_received)
|
||||
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 hotbar to show it
|
||||
if file_path.begins_with("textures/items/"):
|
||||
_update_hotbar()
|
||||
|
||||
func _on_updates_completed(_count: int):
|
||||
# Refresh hotbar after all updates complete
|
||||
_update_hotbar()
|
||||
|
||||
func _on_dungeon_entered():
|
||||
_generate_minimap()
|
||||
@@ -49,6 +61,7 @@ func _setup_hotbar():
|
||||
var slot = Button.new()
|
||||
slot.custom_minimum_size = Vector2(48, 48)
|
||||
slot.text = str(i + 1)
|
||||
slot.set_meta("hotbar_slot", i)
|
||||
|
||||
var style = StyleBoxFlat.new()
|
||||
style.bg_color = Color(0.15, 0.15, 0.2, 0.9)
|
||||
@@ -58,8 +71,45 @@ func _setup_hotbar():
|
||||
slot.add_theme_stylebox_override("normal", style)
|
||||
|
||||
slot.pressed.connect(_on_hotbar_pressed.bind(i))
|
||||
slot.gui_input.connect(_on_hotbar_slot_input.bind(i))
|
||||
hotbar_container.add_child(slot)
|
||||
hotbar_slots.append(slot)
|
||||
|
||||
# Connect to DragManager for drops
|
||||
if DragManager:
|
||||
DragManager.item_dropped.connect(_on_item_dropped)
|
||||
|
||||
func _on_hotbar_slot_input(event: InputEvent, slot_index: int):
|
||||
if not event is InputEventMouseButton:
|
||||
return
|
||||
|
||||
# Check if dropping from DragManager
|
||||
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
|
||||
if DragManager and DragManager.is_dragging:
|
||||
DragManager.drop_on("hotbar", slot_index)
|
||||
|
||||
# Right click to clear hotbar slot
|
||||
elif event.button_index == MOUSE_BUTTON_RIGHT and event.pressed:
|
||||
GameState.assign_hotbar(slot_index, -1) # -1 to clear
|
||||
_update_hotbar()
|
||||
|
||||
func _on_item_dropped(data: Dictionary, target_type: String, target_slot: int):
|
||||
if target_type != "hotbar":
|
||||
return
|
||||
|
||||
var item = data.get("item", {})
|
||||
var source_slot = data.get("source_slot", -1)
|
||||
|
||||
# Only consumables can go in hotbar
|
||||
var item_type = item.get("type", item.get("Type", ""))
|
||||
if item_type != "Consumable":
|
||||
print("Only consumables can be assigned to hotbar")
|
||||
return
|
||||
|
||||
if source_slot >= 0:
|
||||
GameState.assign_hotbar(target_slot, source_slot)
|
||||
_update_hotbar()
|
||||
print("Assigned inventory slot %d to hotbar slot %d" % [source_slot, target_slot])
|
||||
|
||||
func _setup_minimap():
|
||||
minimap_image = Image.create(MINIMAP_SIZE, MINIMAP_SIZE, false, Image.FORMAT_RGBA8)
|
||||
|
||||
+43
-16
@@ -46,6 +46,18 @@ func _ready():
|
||||
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:
|
||||
@@ -242,23 +254,38 @@ func _get_item_emoji(item: Dictionary) -> String:
|
||||
_: 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 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:
|
||||
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 should equip or use
|
||||
var drag_item = DragManager.get_drag_item()
|
||||
var item_type = drag_item.get("type", drag_item.get("Type", ""))
|
||||
|
||||
# If released over same slot or no target, treat as click
|
||||
# For now, just end the drag
|
||||
DragManager.end_drag()
|
||||
|
||||
elif event.button_index == MOUSE_BUTTON_RIGHT and event.pressed:
|
||||
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)
|
||||
# Right click: context action
|
||||
var item_type = item.get("type", item.get("Type", ""))
|
||||
if item_type == "Consumable":
|
||||
# Use consumable on right click
|
||||
GameState.send_use_item(slot_index)
|
||||
elif item_type in ["Weapon", "Armor", "Accessory"]:
|
||||
# Equip on right click
|
||||
GameState.send_equip_item(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:
|
||||
@@ -267,7 +294,7 @@ func _on_equipment_gui_input(event: InputEvent, slot_name: String):
|
||||
GameState.send_unequip_item(slot_name)
|
||||
|
||||
func _show_hotbar_menu(inv_slot: int):
|
||||
# Simple approach: assign to next empty hotbar slot or show popup
|
||||
# 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):
|
||||
|
||||
Reference in New Issue
Block a user