Mob looting fix and dungeon closure.

This commit is contained in:
dingo
2026-01-25 18:04:47 -05:00
parent 85c95e9f6a
commit c1a97b219e
8 changed files with 157 additions and 65 deletions
+20 -6
View File
@@ -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")
+11
View File
@@ -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)
+21 -14
View File
@@ -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
+3 -1
View File
@@ -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")
+16 -4
View File
@@ -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):
"""
+78 -32
View File
@@ -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()
+2 -2
View File
@@ -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: