Mob fixes.

This commit is contained in:
dingo
2026-02-09 12:17:26 -05:00
parent 9a03cf89dd
commit 8ddbd3a231
3 changed files with 50 additions and 20 deletions
+11 -1
View File
@@ -300,10 +300,20 @@ func _handle_dungeon_enter(data: Dictionary):
var pid = p.get("id", p.get("Id")) var pid = p.get("id", p.get("Id"))
portals[pid] = p 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) # Prefetch mob textures early on web (mobs list is included in enter payload)
var mobs_list = data.get("mobs", data.get("Mobs", [])) var mobs_list = data.get("mobs", data.get("Mobs", []))
_prefetch_mob_textures_from_list(mobs_list) _prefetch_mob_textures_from_list(mobs_list)
set_state(STATE_PLAYING) set_state(STATE_PLAYING)
dungeon_entered.emit() dungeon_entered.emit()
+8 -6
View File
@@ -641,12 +641,7 @@ func _update_mob_entities():
mob_dead = hp <= 0 mob_dead = hp <= 0
var was_dead = entity_node.get_meta("is_dead", false) var was_dead = entity_node.get_meta("is_dead", false)
if mob_dead and not was_dead: if not mob_dead and 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:
# Dead → alive transition (respawn / reused ID) — reset stale state # Dead → alive transition (respawn / reused ID) — reset stale state
entity_node.set_meta("is_dead", false) entity_node.set_meta("is_dead", false)
if entity_node.has_method("reset_state"): 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)) var has_loot = mob.get("hasLoot", mob.get("HasLoot", false))
entity_node.set_meta("has_loot", has_loot) 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"): if entity_node.has_method("update_data"):
entity_node.update_data(mob) 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 # Remove entity nodes that are no longer tracked at all
for child in mobs_container.get_children(): for child in mobs_container.get_children():
if child.has_meta("entity_id"): if child.has_meta("entity_id"):
+31 -13
View File
@@ -153,14 +153,31 @@ func update_data(data: Dictionary):
_apply_tint(tint) _apply_tint(tint)
_initialized = true _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 current_anim = data.get("currentAnimation", data.get("CurrentAnimation", null))
var rotation = data.get("rotation", data.get("Rotation", 0.0)) 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(): 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 anim_lower = current_anim.to_lower()
var animation_state := _state_to_animation(current_anim) # Map through STATE_TO_ANIMATION if it's a known state name; otherwise use as-is
_update_animation_state(animation_state, rotation) 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": elif _current_state != "dead":
# Legacy path: use state field, but still skip if locally dead (no server override) # 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() 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: func _rotation_to_direction(rotation: float) -> String:
# Convert rotation (radians) to cardinal direction # Convert rotation (radians) to cardinal direction
var angle = fmod(rotation + TAU, TAU) var angle = fmod(rotation + TAU, TAU)
if angle < PI * 0.25 or angle >= PI * 1.75: if angle < PI * 0.25 or angle >= PI * 1.75:
return "right" return "right"
elif angle < PI * 0.75: 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, {}) var anim_config = _sprite_config["animations"].get(anim_name, {})
should_flip_x = anim_config.get("flipX", false) should_flip_x = anim_config.get("flipX", false)
should_flip_y = anim_config.get("flipY", false) should_flip_y = anim_config.get("flipY", false)
# Only log and play if animation changed # Only log and play if animation changed
if anim_name != _last_animation: if anim_name != _last_animation:
#print("[MobVisual] Playing animation: %s (state=%s, dir=%s)" % [anim_name, state, direction])
_last_animation = anim_name _last_animation = anim_name
_animated_sprite.play(anim_name) _animated_sprite.play(anim_name)
# Handle flipping # Handle horizontal flipping when playing opposite direction's animation.
# For left direction without a dedicated left animation, flip the right one # If facing right but playing a _left animation (or vice versa), flip.
if direction == "left" and not frames.has_animation("%s_left" % state): if direction == "right" and anim_name.ends_with("_left"):
should_flip_x = not should_flip_x # Toggle flip 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_h = should_flip_x
_animated_sprite.flip_v = should_flip_y _animated_sprite.flip_v = should_flip_y
return return