From 8ddbd3a231533054ebb3869700b60fbcb254ba40 Mon Sep 17 00:00:00 2001 From: dingo Date: Mon, 9 Feb 2026 12:17:26 -0500 Subject: [PATCH] Mob fixes. --- scripts/autoload/game_state.gd | 12 +++++++- scripts/dungeon/dungeon_manager.gd | 14 ++++++---- scripts/dungeon/mob_visual.gd | 44 +++++++++++++++++++++--------- 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/scripts/autoload/game_state.gd b/scripts/autoload/game_state.gd index ec04cee..cf68e5c 100644 --- a/scripts/autoload/game_state.gd +++ b/scripts/autoload/game_state.gd @@ -300,10 +300,20 @@ func _handle_dungeon_enter(data: Dictionary): var pid = p.get("id", p.get("Id")) portals[pid] = p + # Parse mob catalog if included in the dungeon-enter response + var catalog = data.get("mobCatalog", data.get("MobCatalog", [])) + if catalog is Array and catalog.size() > 0: + for entry in catalog: + var cid = entry.get("id", entry.get("Id")) + if cid != null: + mob_catalog[cid] = entry + print("[GameState] Mob catalog from enter: %d entries" % catalog.size()) + _prefetch_mob_textures_from_list(catalog) + # Prefetch mob textures early on web (mobs list is included in enter payload) var mobs_list = data.get("mobs", data.get("Mobs", [])) _prefetch_mob_textures_from_list(mobs_list) - + set_state(STATE_PLAYING) dungeon_entered.emit() diff --git a/scripts/dungeon/dungeon_manager.gd b/scripts/dungeon/dungeon_manager.gd index 7faace2..72380c6 100644 --- a/scripts/dungeon/dungeon_manager.gd +++ b/scripts/dungeon/dungeon_manager.gd @@ -641,12 +641,7 @@ func _update_mob_entities(): mob_dead = hp <= 0 var was_dead = entity_node.get_meta("is_dead", false) - if mob_dead and not was_dead: - # Alive → dead transition - entity_node.set_meta("is_dead", true) - if entity_node.has_method("play_death_animation"): - entity_node.play_death_animation() - elif not mob_dead and was_dead: + if not mob_dead and was_dead: # Dead → alive transition (respawn / reused ID) — reset stale state entity_node.set_meta("is_dead", false) if entity_node.has_method("reset_state"): @@ -656,9 +651,16 @@ func _update_mob_entities(): var has_loot = mob.get("hasLoot", mob.get("HasLoot", false)) entity_node.set_meta("has_loot", has_loot) + # Update visual data BEFORE death transition so play_death_animation() has final say if entity_node.has_method("update_data"): entity_node.update_data(mob) + if mob_dead and not was_dead: + # Alive → dead transition — called AFTER update_data so it overrides animation + entity_node.set_meta("is_dead", true) + if entity_node.has_method("play_death_animation"): + entity_node.play_death_animation() + # Remove entity nodes that are no longer tracked at all for child in mobs_container.get_children(): if child.has_meta("entity_id"): diff --git a/scripts/dungeon/mob_visual.gd b/scripts/dungeon/mob_visual.gd index 21bbf29..8755255 100644 --- a/scripts/dungeon/mob_visual.gd +++ b/scripts/dungeon/mob_visual.gd @@ -153,14 +153,31 @@ func update_data(data: Dictionary): _apply_tint(tint) _initialized = true - # Resolve animation — prefer server-authoritative currentAnimation, fall back to state + # Resolve animation — prefer server-authoritative currentAnimation, fall back to state. + # If dungeon_manager already triggered play_death_animation() this tick (setting + # _current_state = "dead"), don't let a stale currentAnimation override it — unless + # the server explicitly says the mob is alive via a non-dead animation. var current_anim = data.get("currentAnimation", data.get("CurrentAnimation", null)) var rotation = data.get("rotation", data.get("Rotation", 0.0)) if current_anim != null and current_anim is String and not current_anim.is_empty(): - # Server says exactly what animation to play — always apply, even if currently "dead" - var animation_state := _state_to_animation(current_anim) - _update_animation_state(animation_state, rotation) + var anim_lower = current_anim.to_lower() + # Map through STATE_TO_ANIMATION if it's a known state name; otherwise use as-is + var animation_state: String + if STATE_TO_ANIMATION.has(anim_lower): + animation_state = STATE_TO_ANIMATION[anim_lower] + else: + animation_state = anim_lower + + if animation_state == "dead": + # Server confirms death — apply death animation + _update_animation_state(animation_state, rotation) + elif _current_state == "dead": + # Server says non-dead but we're locally dead — server override, mob is alive + _update_animation_state(animation_state, rotation) + else: + # Normal case: apply server animation + _update_animation_state(animation_state, rotation) elif _current_state != "dead": # Legacy path: use state field, but still skip if locally dead (no server override) var raw_state = data.get("state", data.get("State", "idle")).to_lower() @@ -480,7 +497,7 @@ func _update_animation_state(state: String, rotation: float): func _rotation_to_direction(rotation: float) -> String: # Convert rotation (radians) to cardinal direction var angle = fmod(rotation + TAU, TAU) - + if angle < PI * 0.25 or angle >= PI * 1.75: return "right" elif angle < PI * 0.75: @@ -522,18 +539,19 @@ func _play_animation(state: String, direction: String): var anim_config = _sprite_config["animations"].get(anim_name, {}) should_flip_x = anim_config.get("flipX", false) should_flip_y = anim_config.get("flipY", false) - + # Only log and play if animation changed if anim_name != _last_animation: - #print("[MobVisual] Playing animation: %s (state=%s, dir=%s)" % [anim_name, state, direction]) _last_animation = anim_name _animated_sprite.play(anim_name) - - # Handle flipping - # For left direction without a dedicated left animation, flip the right one - if direction == "left" and not frames.has_animation("%s_left" % state): - should_flip_x = not should_flip_x # Toggle flip - + + # Handle horizontal flipping when playing opposite direction's animation. + # If facing right but playing a _left animation (or vice versa), flip. + if direction == "right" and anim_name.ends_with("_left"): + should_flip_x = not should_flip_x + elif direction == "left" and anim_name.ends_with("_right"): + should_flip_x = not should_flip_x + _animated_sprite.flip_h = should_flip_x _animated_sprite.flip_v = should_flip_y return