From 2a5f4b9b7871f15824fc73d3e9638b695ac1d208 Mon Sep 17 00:00:00 2001 From: dingo Date: Tue, 23 Dec 2025 13:41:30 -0500 Subject: [PATCH] Crafting v2 --- scenes/hub/hub_screen.tscn | 19 +- scenes/ui/crafting_ui.tscn | 155 ++++++++ scripts/autoload/drag_manager.gd | 84 +++++ scripts/autoload/game_state.gd | 75 ++++ scripts/autoload/opcodes.gd | 11 + scripts/ui/crafting_ui.gd | 628 +++++++++++++++++++++++++++++++ scripts/ui/crafting_ui.gd.uid | 1 + scripts/ui/hub_ui.gd | 26 ++ scripts/ui/hud.gd | 50 +++ scripts/ui/inventory_ui.gd | 59 ++- 10 files changed, 1091 insertions(+), 17 deletions(-) create mode 100644 scenes/ui/crafting_ui.tscn create mode 100644 scripts/autoload/drag_manager.gd create mode 100644 scripts/ui/crafting_ui.gd create mode 100644 scripts/ui/crafting_ui.gd.uid diff --git a/scenes/hub/hub_screen.tscn b/scenes/hub/hub_screen.tscn index da37ebb..4f2353f 100644 --- a/scenes/hub/hub_screen.tscn +++ b/scenes/hub/hub_screen.tscn @@ -1,7 +1,9 @@ -[gd_scene load_steps=3 format=3 uid="uid://linskq5jx4uh"] +[gd_scene load_steps=5 format=3 uid="uid://linskq5jx4uh"] [ext_resource type="Script" uid="uid://c4v4vw32gyxbl" path="res://scripts/ui/hub_ui.gd" id="1_u7qci"] [ext_resource type="PackedScene" uid="uid://ctsfevsx6nnv2" path="res://scenes/hub/dungeon_browser.tscn" id="2_browser"] +[ext_resource type="PackedScene" path="res://scenes/ui/inventory.tscn" id="3_inventory"] +[ext_resource type="PackedScene" path="res://scenes/ui/crafting_ui.tscn" id="4_crafting"] [node name="HubScreen" type="CanvasLayer"] script = ExtResource("1_u7qci") @@ -110,6 +112,17 @@ text = "+" [node name="PointsRemaining" type="Label" parent="Control/HBoxContainer/LeftPanel/StatsPanel"] layout_mode = 2 +[node name="MenuButtons" type="HBoxContainer" parent="Control/HBoxContainer/LeftPanel"] +layout_mode = 2 + +[node name="InventoryBtn" type="Button" parent="Control/HBoxContainer/LeftPanel/MenuButtons"] +layout_mode = 2 +text = "Inventory" + +[node name="CraftingBtn" type="Button" parent="Control/HBoxContainer/LeftPanel/MenuButtons"] +layout_mode = 2 +text = "Crafting" + [node name="RightPanel" type="VBoxContainer" parent="Control/HBoxContainer"] layout_mode = 2 alignment = 2 @@ -146,3 +159,7 @@ layout_mode = 2 text = "Browse Dungeons" [node name="DungeonBrowser" parent="." instance=ExtResource("2_browser")] + +[node name="InventoryUI" parent="." instance=ExtResource("3_inventory")] + +[node name="CraftingUI" parent="." instance=ExtResource("4_crafting")] diff --git a/scenes/ui/crafting_ui.tscn b/scenes/ui/crafting_ui.tscn new file mode 100644 index 0000000..e2530c9 --- /dev/null +++ b/scenes/ui/crafting_ui.tscn @@ -0,0 +1,155 @@ +[gd_scene load_steps=2 format=3 uid="uid://crafting_ui_scene"] + +[ext_resource type="Script" path="res://scripts/ui/crafting_ui.gd" id="1_crafting"] + +[node name="CraftingUI" type="CanvasLayer"] +layer = 10 +script = ExtResource("1_crafting") + +[node name="Panel" type="PanelContainer" parent="."] +anchors_preset = 8 +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +offset_left = -450.0 +offset_top = -280.0 +offset_right = 450.0 +offset_bottom = 280.0 +grow_horizontal = 2 +grow_vertical = 2 + +[node name="VBox" type="VBoxContainer" parent="Panel"] +layout_mode = 2 + +[node name="Header" type="HBoxContainer" parent="Panel/VBox"] +layout_mode = 2 + +[node name="Title" type="Label" parent="Panel/VBox/Header"] +layout_mode = 2 +size_flags_horizontal = 3 +text = "Crafting" +horizontal_alignment = 1 + +[node name="CloseButton" type="Button" parent="Panel/VBox/Header"] +layout_mode = 2 +text = "X" + +[node name="Content" type="HBoxContainer" parent="Panel/VBox"] +layout_mode = 2 +size_flags_vertical = 3 +theme_override_constants/separation = 10 + +[node name="LeftPanel" type="VBoxContainer" parent="Panel/VBox/Content"] +layout_mode = 2 +size_flags_horizontal = 3 +size_flags_stretch_ratio = 0.4 + +[node name="SkillsPanel" type="VBoxContainer" parent="Panel/VBox/Content/LeftPanel"] +layout_mode = 2 + +[node name="SkillsLabel" type="Label" parent="Panel/VBox/Content/LeftPanel/SkillsPanel"] +layout_mode = 2 +text = "Select Skill:" + +[node name="SkillButtons" type="HBoxContainer" parent="Panel/VBox/Content/LeftPanel/SkillsPanel"] +layout_mode = 2 +theme_override_constants/separation = 2 + +[node name="SkillInfo" type="Label" parent="Panel/VBox/Content/LeftPanel/SkillsPanel"] +layout_mode = 2 +text = "Select a crafting skill" + +[node name="Sep1" type="HSeparator" parent="Panel/VBox/Content/LeftPanel"] +layout_mode = 2 + +[node name="RecipesLabel" type="Label" parent="Panel/VBox/Content/LeftPanel"] +layout_mode = 2 +text = "Known Recipes:" + +[node name="RecipeScroll" type="ScrollContainer" parent="Panel/VBox/Content/LeftPanel"] +layout_mode = 2 +size_flags_vertical = 3 + +[node name="RecipeList" type="VBoxContainer" parent="Panel/VBox/Content/LeftPanel/RecipeScroll"] +layout_mode = 2 +size_flags_horizontal = 3 + +[node name="MiddlePanel" type="VBoxContainer" parent="Panel/VBox/Content"] +layout_mode = 2 +size_flags_horizontal = 3 +size_flags_stretch_ratio = 0.35 + +[node name="CraftingLabel" type="Label" parent="Panel/VBox/Content/MiddlePanel"] +layout_mode = 2 +text = "Crafting Grid (4x4):" +horizontal_alignment = 1 + +[node name="CraftingGrid" type="GridContainer" parent="Panel/VBox/Content/MiddlePanel"] +layout_mode = 2 +size_flags_horizontal = 4 +columns = 4 + +[node name="ButtonRow" type="HBoxContainer" parent="Panel/VBox/Content/MiddlePanel"] +layout_mode = 2 +size_flags_horizontal = 4 + +[node name="CraftButton" type="Button" parent="Panel/VBox/Content/MiddlePanel/ButtonRow"] +layout_mode = 2 +text = "Craft" + +[node name="DiscoverButton" type="Button" parent="Panel/VBox/Content/MiddlePanel/ButtonRow"] +layout_mode = 2 +text = "Discover" + +[node name="ClearButton" type="Button" parent="Panel/VBox/Content/MiddlePanel/ButtonRow"] +layout_mode = 2 +text = "Clear" + +[node name="ResultLabel" type="RichTextLabel" parent="Panel/VBox/Content/MiddlePanel"] +layout_mode = 2 +size_flags_vertical = 3 +bbcode_enabled = true +text = "Select a skill, then craft or discover recipes" +fit_content = true + +[node name="RightPanel" type="VBoxContainer" parent="Panel/VBox/Content"] +layout_mode = 2 +size_flags_horizontal = 3 +size_flags_stretch_ratio = 0.45 + +[node name="InventoryLabel" type="Label" parent="Panel/VBox/Content/RightPanel"] +layout_mode = 2 +text = "Inventory (drag to grid):" + +[node name="InventoryScroll" type="ScrollContainer" parent="Panel/VBox/Content/RightPanel"] +layout_mode = 2 +size_flags_vertical = 3 + +[node name="InventoryGrid" type="GridContainer" parent="Panel/VBox/Content/RightPanel/InventoryScroll"] +layout_mode = 2 +size_flags_horizontal = 3 +columns = 5 + +[node name="Tooltip" type="PanelContainer" parent="."] +visible = false +z_index = 100 +offset_right = 200.0 +offset_bottom = 120.0 + +[node name="VBox" type="VBoxContainer" parent="Tooltip"] +layout_mode = 2 + +[node name="ItemName" type="Label" parent="Tooltip/VBox"] +layout_mode = 2 +text = "Item Name" + +[node name="ItemType" type="Label" parent="Tooltip/VBox"] +layout_mode = 2 + +[node name="ItemStats" type="Label" parent="Tooltip/VBox"] +layout_mode = 2 + +[node name="ItemDesc" type="Label" parent="Tooltip/VBox"] +layout_mode = 2 +autowrap_mode = 2 diff --git a/scripts/autoload/drag_manager.gd b/scripts/autoload/drag_manager.gd new file mode 100644 index 0000000..353fc2c --- /dev/null +++ b/scripts/autoload/drag_manager.gd @@ -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() diff --git a/scripts/autoload/game_state.gd b/scripts/autoload/game_state.gd index 453c668..1822eaa 100644 --- a/scripts/autoload/game_state.gd +++ b/scripts/autoload/game_state.gd @@ -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 + }) diff --git a/scripts/autoload/opcodes.gd b/scripts/autoload/opcodes.gd index 87cda31..f3af291 100644 --- a/scripts/autoload/opcodes.gd +++ b/scripts/autoload/opcodes.gd @@ -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 diff --git a/scripts/ui/crafting_ui.gd b/scripts/ui/crafting_ui.gd new file mode 100644 index 0000000..cf1da59 --- /dev/null +++ b/scripts/ui/crafting_ui.gd @@ -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() diff --git a/scripts/ui/crafting_ui.gd.uid b/scripts/ui/crafting_ui.gd.uid new file mode 100644 index 0000000..1b5896f --- /dev/null +++ b/scripts/ui/crafting_ui.gd.uid @@ -0,0 +1 @@ +uid://dht62hol3uhdp diff --git a/scripts/ui/hub_ui.gd b/scripts/ui/hub_ui.gd index 806ac73..66fb2d7 100644 --- a/scripts/ui/hub_ui.gd +++ b/scripts/ui/hub_ui.gd @@ -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() diff --git a/scripts/ui/hud.gd b/scripts/ui/hud.gd index ca10e09..5eb8391 100644 --- a/scripts/ui/hud.gd +++ b/scripts/ui/hud.gd @@ -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) diff --git a/scripts/ui/inventory_ui.gd b/scripts/ui/inventory_ui.gd index 0b95765..35e39c8 100644 --- a/scripts/ui/inventory_ui.gd +++ b/scripts/ui/inventory_ui.gd @@ -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):