diff --git a/scenes/dungeon/loot_chest.tscn b/scenes/dungeon/loot_chest.tscn index c7b3d94..340f8c3 100644 --- a/scenes/dungeon/loot_chest.tscn +++ b/scenes/dungeon/loot_chest.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=3 format=3 uid="uid://dlootchest123"] -[ext_resource type="Script" path="res://scripts/dungeon/loot_chest.gd" id="1"] +[ext_resource type="Script" uid="uid://dw06v6bwbcuph" path="res://scripts/dungeon/loot_chest.gd" id="1"] [sub_resource type="CircleShape2D" id="CircleShape2D_chest"] radius = 20.0 @@ -17,9 +17,9 @@ scale = Vector2(2, 2) shape = SubResource("CircleShape2D_chest") [node name="Label" type="Label" parent="."] -offset_left = -30 -offset_top = -40 -offset_right = 30 -offset_bottom = -20 -horizontal_alignment = 1 +offset_left = -30.0 +offset_top = -40.0 +offset_right = 30.0 +offset_bottom = -20.0 text = "Loot" +horizontal_alignment = 1 diff --git a/scripts/autoload/game_state.gd b/scripts/autoload/game_state.gd index 7742092..a378c09 100644 --- a/scripts/autoload/game_state.gd +++ b/scripts/autoload/game_state.gd @@ -194,6 +194,8 @@ func _on_message_received(opcode: int, data: Dictionary): _handle_projectile_expired(data) OpCodes.EVENT_DAMAGE_NUMBER: _handle_damage_number(data) + OpCodes.ERROR: + _handle_error(data) _: print("[GameState] Unhandled opcode: ", opcode) @@ -361,12 +363,13 @@ func _handle_dungeon_state(data: Dictionary): # Check dungeon phase first var phase = data.get("phase", data.get("Phase", "active")) if phase == "cleared": - print("[GameState] Dungeon cleared! Waiting for hub state...") - # Server should send HUB_STATE next, but show a message - set_state(STATE_DUNGEONCLEARED) - dungeon_cleared.emit() - return - + # Only print once when transitioning to cleared state + if dungeon.phase != "cleared": + print("[GameState] Dungeon cleared! Use the portal to exit.") + dungeon_cleared.emit() + dungeon.phase = "cleared" + # Don't return - continue processing state so player can still move and loot + var ps = data.get("players", data.get("Players", [])) for p in ps: var pid = int(p.get("id", p.get("Id", 0))) @@ -549,6 +552,12 @@ func send_use_item(slot_index: int): func send_stack_items(source_slot: int, target_slot: int): Network.send_message(OpCodes.STACK_ITEMS, {"SourceSlot": source_slot, "TargetSlot": target_slot}) +func send_move_item(source_slot: int, target_slot: int): + Network.send_message(OpCodes.MOVE_ITEM, {"SourceSlot": source_slot, "TargetSlot": target_slot}) + +func send_auto_stack_inventory(): + Network.send_message(OpCodes.AUTO_STACK_INVENTORY, {}) + func send_chat(message: String): Network.send_message(OpCodes.CHAT_SEND, {"Message": message}) @@ -650,6 +659,11 @@ func take_mob_loot_item(mob_id: int, item_index: int): func take_all_mob_loot(mob_id: int): Network.send_message(OpCodes.MOB_LOOT_TAKE_ALL, {"mobId": mob_id}) +func _handle_error(data: Dictionary): + var error_message = data.get("error", "Unknown error") + push_error("[Server Error] " + error_message) + print("[GameState] Server error: " + error_message) + # Dungeon list handler func _handle_dungeon_list(data: Dictionary): print("[GameState] Received dungeon list") diff --git a/scripts/autoload/opcodes.gd b/scripts/autoload/opcodes.gd index a04053a..dff4dc9 100644 --- a/scripts/autoload/opcodes.gd +++ b/scripts/autoload/opcodes.gd @@ -84,6 +84,12 @@ const HOTBAR_ASSIGN = 256 ## Client -> Server: Stack two consumable items together const STACK_ITEMS = 257 +## Client -> Server: Move/swap item between inventory slots +const MOVE_ITEM = 258 + +## Client -> Server: Auto-stack all stackable items in inventory +const AUTO_STACK_INVENTORY = 259 + # ===== Chat (260-269) ===== ## Client -> Server: Send chat message @@ -290,6 +296,11 @@ const STASH_WITHDRAW = 613 ## Server -> Client: Stash operation result const STASH_RESULT = 614 +# ===== Errors (900-999) ===== + +## Server -> Client: Error message +const ERROR = 900 + # ===== Legacy / Deprecated ===== ## Legacy loot notification (consider consolidating with EVENT_LOOT) diff --git a/scripts/autoload/update_manager.gd b/scripts/autoload/update_manager.gd index 93fc9a4..5345a84 100644 --- a/scripts/autoload/update_manager.gd +++ b/scripts/autoload/update_manager.gd @@ -190,21 +190,23 @@ func _on_download_completed(result: int, response_code: int, _headers: PackedStr # Store in memory cache (works for both web and desktop) _texture_bytes[item.filePath] = body _local_hashes[item.filePath] = item.fileHash - + print("[UpdateManager] Stored in _texture_bytes: %s (%d bytes)" % [item.filePath, body.size()]) + # For desktop, also save to disk for persistence if not _is_web: var cache_path = _get_cache_path(item.filePath) var dir_path = cache_path.get_base_dir() DirAccess.make_dir_recursive_absolute(dir_path) - + var file = FileAccess.open(cache_path, FileAccess.WRITE) if file: file.store_buffer(body) file.close() else: push_warning("[UpdateManager] Failed to save file to disk: %s" % cache_path) - + _files_downloaded += 1 + print("[UpdateManager] Emitting file_downloaded signal for: %s" % item.filePath) file_downloaded.emit(item.filePath) print("[UpdateManager] Downloaded: %s (%d bytes)" % [item.filePath, body.size()]) @@ -419,32 +421,36 @@ func _find_file_recursive(dir: DirAccess, current_path: String, filename: String func load_texture(file_path: String) -> Texture2D: # Check already-loaded texture cache if _loaded_textures.has(file_path): + print("[UpdateManager] load_texture(%s) - Found in _loaded_textures cache" % file_path) return _loaded_textures[file_path] - + var buffer: PackedByteArray - + # Try memory cache first (works for web and desktop) if _texture_bytes.has(file_path): + print("[UpdateManager] load_texture(%s) - Found in _texture_bytes, creating texture..." % file_path) buffer = _texture_bytes[file_path] elif not _is_web: # Desktop: try disk cache var cache_path = _get_cache_path(file_path) if not FileAccess.file_exists(cache_path): - push_warning("[UpdateManager] Texture not in cache: %s" % file_path) + print("[UpdateManager] load_texture(%s) - Not in disk cache" % file_path) return null - + var file = FileAccess.open(cache_path, FileAccess.READ) if not file: - push_warning("[UpdateManager] Failed to open cached file: %s" % file_path) + print("[UpdateManager] load_texture(%s) - Failed to open cached file" % file_path) return null - + buffer = file.get_buffer(file.get_length()) file.close() - + # Store in memory cache for faster subsequent access _texture_bytes[file_path] = buffer else: - push_warning("[UpdateManager] Texture not in memory cache (web): %s" % file_path) + # Texture not in cache - this is normal for optional textures on web + # The calling code should handle null gracefully + print("[UpdateManager] load_texture(%s) - Not in memory cache (web build)" % file_path) return null var image = Image.new() @@ -453,11 +459,12 @@ func load_texture(file_path: String) -> Texture2D: err = image.load_jpg_from_buffer(buffer) if err != OK: err = image.load_webp_from_buffer(buffer) - + if err != OK: - push_error("[UpdateManager] Failed to load image: %s" % file_path) + push_error("[UpdateManager] Failed to load image from buffer: %s (buffer size: %d bytes)" % [file_path, buffer.size()]) return null - + + print("[UpdateManager] load_texture(%s) - Successfully created texture from buffer" % file_path) var texture = ImageTexture.create_from_image(image) _loaded_textures[file_path] = texture return texture diff --git a/scripts/dungeon/mob_visual.gd b/scripts/dungeon/mob_visual.gd index 12b52ed..5e0c5ef 100644 --- a/scripts/dungeon/mob_visual.gd +++ b/scripts/dungeon/mob_visual.gd @@ -546,7 +546,9 @@ func play_death_animation(): var frame_count = _animated_sprite.sprite_frames.get_frame_count(death_anim_name) if frame_count > 0: _animated_sprite.frame = frame_count - 1 - _animated_sprite.stop() + print("[Frame] - ", frame_count) + print("[Frame] - ", _animated_sprite.frame) + #_animated_sprite.stop() else: # No death animation, just fade out the fallback _update_fallback_state("dead") diff --git a/scripts/dungeon/player.gd b/scripts/dungeon/player.gd index 9423303..9b562f3 100644 --- a/scripts/dungeon/player.gd +++ b/scripts/dungeon/player.gd @@ -14,15 +14,21 @@ var current_direction: String = "down" # down, up, left, right func _ready(): # Sprite sheet will be loaded from CDN path: textures/player/Warrior-Blue.png if UpdateManager: + print("[Player] Checking if player sprite is available...") + print("[Player] UpdateManager._is_web: %s" % UpdateManager._is_web) + print("[Player] Has file in cache: %s" % UpdateManager.has_file("textures/player/Warrior-Blue.png")) + # Try to load immediately (may already be cached) var texture = UpdateManager.load_texture("textures/player/Warrior-Blue.png") if texture: - print("[Player] Loaded sprite sheet texture") + print("[Player] ✓ Loaded sprite sheet texture successfully") _setup_sprite_frames(texture) else: - print("[Player] Failed to load sprite sheet texture, waiting for download...") + print("[Player] ✗ Sprite sheet not available yet, connecting to file_downloaded signal") # Connect to file_downloaded signal to retry when file downloads - UpdateManager.file_downloaded.connect(_on_file_downloaded) + if not UpdateManager.file_downloaded.is_connected(_on_file_downloaded): + UpdateManager.file_downloaded.connect(_on_file_downloaded) + print("[Player] Connected to file_downloaded signal") # Setup aim line (will be hidden once sprites load) if aim_line: @@ -42,18 +48,24 @@ func _ready(): aim_line.visible = true func _on_file_downloaded(file_path: String): + print("[Player] file_downloaded signal received: %s" % file_path) + # Check if the player sprite was downloaded if file_path == "textures/player/Warrior-Blue.png" and UpdateManager: + print("[Player] Player sprite downloaded! Attempting to load...") var texture = UpdateManager.load_texture("textures/player/Warrior-Blue.png") if texture: - print("[Player] Loaded sprite sheet texture after download") + print("[Player] ✓ Loaded sprite sheet texture after download") _setup_sprite_frames(texture) # Start playing walk animation if animated_sprite and animated_sprite.sprite_frames: animated_sprite.play("walk_down") + print("[Player] Started walk_down animation") # Hide aim line since we have sprites if aim_line: aim_line.visible = false + else: + print("[Player] ✗ Failed to load texture even after download!") func _setup_sprite_frames(texture: Texture2D): """ diff --git a/scripts/ui/inventory_ui.gd b/scripts/ui/inventory_ui.gd index 033efd8..99eb072 100644 --- a/scripts/ui/inventory_ui.gd +++ b/scripts/ui/inventory_ui.gd @@ -49,6 +49,8 @@ var inventory_slots: Array = [] var equipment_slots: Dictionary = {} var tooltip: PanelContainer = null var selected_hotbar_slot: int = -1 # For hotbar assignment +var right_click_start_time: int = 0 # Track right-click duration +var right_click_slot: int = -1 func _ready(): visible = false @@ -305,38 +307,73 @@ func _on_inventory_gui_input(event: InputEvent, slot_index: int): var item = GameState.inventory[slot_index] if slot_index < GameState.inventory.size() else null + # Left click if event.button_index == MOUSE_BUTTON_LEFT and event.pressed: - if item == null: - return - # Left click: equip/unequip or assign to hotbar - var item_type = item.get("type", item.get("Type", "")) - if item_type == "Consumable": - # Show hotbar menu for consumables - _show_hotbar_menu(slot_index) - elif item_type in ["Weapon", "Armor", "Accessory"]: - # Equip on left click - GameState.send_equip_item(slot_index) + if DragManager and DragManager.is_dragging: + # Drop item here + _drop_item_on_slot(slot_index, item) + elif item != null: + var item_type = item.get("type", item.get("Type", "")) + if item_type == "Consumable": + # Use consumable on left click + GameState.send_use_item(slot_index) + elif item_type in ["Weapon", "Armor", "Accessory"]: + # Equip on left click + GameState.send_equip_item(slot_index) + # Right click elif event.button_index == MOUSE_BUTTON_RIGHT: if event.pressed: - # Start drag if has item - if item and not item.is_empty() and DragManager: + # Right click press - start drag if has item + if DragManager and DragManager.is_dragging: + # Already dragging, drop here + _drop_item_on_slot(slot_index, item) + elif item and not item.is_empty(): + # Start drag + right_click_start_time = Time.get_ticks_msec() + right_click_slot = slot_index DragManager.start_drag(item, "inventory", slot_index) else: - # End drag on release (if still dragging) + # Right click release if DragManager and DragManager.is_dragging: - # Check if we're dropping onto another inventory slot - var target_item = GameState.inventory[slot_index] if slot_index < GameState.inventory.size() else null - var drag_item = DragManager.get_drag_item() + var drag_time = Time.get_ticks_msec() - right_click_start_time + # If released quickly (< 200ms) without moving, it's a quick click + if drag_time < 200 and right_click_slot == slot_index and item: + DragManager.end_drag() + # Show hotbar menu for consumables + var item_type = item.get("type", item.get("Type", "")) + if item_type == "Consumable": + _show_hotbar_menu(slot_index) + else: + # It was a drag, drop here + _drop_item_on_slot(slot_index, item) - # Try to stack consumables - if target_item and drag_item and _can_stack_items(drag_item, target_item): - var source_slot = DragManager.drag_data.get("source_slot", -1) - if source_slot >= 0 and source_slot != slot_index: - GameState.send_stack_items(source_slot, slot_index) +func _drop_item_on_slot(slot_index: int, target_item): + if not DragManager or not DragManager.is_dragging: + return - # Just end the drag - drop handling is done by the drop target - DragManager.end_drag() + var drag_source_type = DragManager.drag_data.get("source_type", "") + var drag_source_slot = DragManager.drag_data.get("source_slot", -1) + var drag_item = DragManager.get_drag_item() + + # Can't drop onto the same slot + if drag_source_type == "inventory" and drag_source_slot == slot_index: + DragManager.end_drag() + return + + # Only handle inventory to inventory drops + if drag_source_type != "inventory" or drag_source_slot < 0: + DragManager.end_drag() + return + + # Try to stack if both items are stackable + if target_item and drag_item and _can_stack_items(drag_item, target_item): + GameState.send_stack_items(drag_source_slot, slot_index) + else: + # Move/swap items + GameState.send_move_item(drag_source_slot, slot_index) + + DragManager.end_drag() func _on_equipment_gui_input(event: InputEvent, slot_name: String): if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT: @@ -345,18 +382,19 @@ func _on_equipment_gui_input(event: InputEvent, slot_name: String): GameState.send_unequip_item(slot_name) func _can_stack_items(item1: Dictionary, item2: Dictionary) -> bool: - # Check if both items are consumables with the same name - var type1 = item1.get("type", item1.get("Type", "")) - var type2 = item2.get("type", item2.get("Type", "")) + # Check if both items are the same and stackable + var id1 = item1.get("id", item1.get("Id", 0)) + var id2 = item2.get("id", item2.get("Id", 0)) - if type1 != "Consumable" or type2 != "Consumable": + # Must have same ID + if id1 != id2 or id1 == 0: return false - # Check if same item type - var name1 = item1.get("name", item1.get("Name", "")) - var name2 = item2.get("name", item2.get("Name", "")) + # Must be stackable + var stackable1 = item1.get("stackable", item1.get("Stackable", false)) + var stackable2 = item2.get("stackable", item2.get("Stackable", false)) - if name1 != name2: + if not stackable1 or not stackable2: return false # Check if target has room (not already at max stack) @@ -518,6 +556,14 @@ func _on_close(): visible = false func _input(event): - if visible and event.is_action_pressed("escape_menu"): + if not visible: + return + + if event.is_action_pressed("escape_menu"): visible = false get_viewport().set_input_as_handled() + elif event is InputEventKey and event.pressed and not event.echo: + if event.keycode == KEY_S: + # Auto-stack inventory + GameState.send_auto_stack_inventory() + get_viewport().set_input_as_handled() diff --git a/scripts/ui/mob_loot_ui.gd b/scripts/ui/mob_loot_ui.gd index 9a1f924..f17ff85 100644 --- a/scripts/ui/mob_loot_ui.gd +++ b/scripts/ui/mob_loot_ui.gd @@ -205,8 +205,8 @@ func _take_item(slot_index: int): GameState.take_mob_loot_item(current_mob_id, slot_index) print("[MobLootUI] Taking item at index %d from mob %d" % [slot_index, current_mob_id]) - # Clear slot locally (server will update inventory) - _clear_slot(loot_slots[slot_index]) + # Don't clear locally - wait for server to send updated loot state + # The server will send MOB_LOOT_STATE with remaining items func _on_take_all(): if current_mob_id < 0: