Fix: incremental unit rebuild stops idle animation restarts

_rebuild_units was destroying and recreating ALL unit bodies on every
state update, including echo broadcasts (delay=0, no animation needed).
Each recreation restarted idle from frame 0 — causing the bind-pose
flash the user sees as "T-pose then playing idle again."

Now _rebuild_units is incremental:
  - Remove only units that left the board (died / discarded).
  - Add only units newly present (just played / first connect).
  - Leave existing unit bodies untouched so their animations continue
    without interruption.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-03-15 21:29:30 -04:00
co-authored by Claude Happy
parent 192a2b2380
commit 48a12ed14b
+33 -14
View File
@@ -544,29 +544,48 @@ func _update_tile_affinity(x: int, y: int, affinity: String) -> void:
# ── Unit meshes ───────────────────────────────────────────────────────────────
func _rebuild_units(state: Dictionary) -> void:
# Kill any in-flight movement tweens before destroying nodes.
# Kill any in-flight movement tweens.
for iid : String in _move_anims:
var t := _move_anims[iid] as Tween
if t != null: t.kill()
_move_anims.clear()
for c in _units_root.get_children(): c.queue_free()
_unit_meshes.clear()
_unit_bodies.clear()
_anim_players.clear()
_unit_def_ids.clear()
# _prev_unit_hp and _prev_unit_pos are updated in _update_prev_state,
# called immediately in _on_state_updated — do NOT clear them here.
# ── Incremental rebuild: only remove/add; never recreate unchanged units ──
# Recreating a unit body interrupts its running animation (idle/move/attack),
# causing a one-frame bind-pose flash on every state echo.
# 1. Collect the desired unit set from the new state.
var my_id := Auth.user_id
var desired : Dictionary = {} # iid → {unit, color, is_enemy}
for player: Dictionary in state.get("players", []):
var is_mine : bool = str(player.get("accountId", "")).to_lower() == my_id.to_lower()
var is_mine : bool = str(player.get("accountId", "")).to_lower() == my_id.to_lower()
var color : Color = Color(0.3, 0.5, 1.0) if is_mine else Color(1.0, 0.35, 0.35)
var arch : Dictionary = player.get("archSpirit", {})
if not arch.is_empty() and arch.get("position") != null:
_add_unit_mesh(arch, color, not is_mine)
for unit: Dictionary in player.get("field", []):
if unit.get("position") != null:
_add_unit_mesh(unit, color, not is_mine)
for unit: Dictionary in ([arch] + player.get("field", [])):
if unit.is_empty() or unit.get("position") == null: continue
var iid : String = str(unit.get("instanceId", ""))
if iid.is_empty(): continue
desired[iid] = {"unit": unit, "color": color, "is_enemy": not is_mine}
# 2. Remove units that are no longer on the board.
var to_remove : Array[String] = []
for iid : String in _unit_bodies:
if not desired.has(iid):
to_remove.append(iid)
for iid : String in to_remove:
var body := _unit_bodies[iid] as StaticBody3D
if is_instance_valid(body):
body.queue_free()
_unit_bodies.erase(iid)
_unit_meshes.erase(iid)
_anim_players.erase(iid)
_unit_def_ids.erase(iid)
# 3. Add units not yet present (newly played, or first render after connect).
for iid : String in desired:
if not _unit_bodies.has(iid):
var d := desired[iid] as Dictionary
_add_unit_mesh(d["unit"] as Dictionary, d["color"] as Color, d["is_enemy"] as bool)
func _add_unit_mesh(unit: Dictionary, team_color: Color, is_enemy: bool = false) -> void: