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:
@@ -544,29 +544,48 @@ func _update_tile_affinity(x: int, y: int, affinity: String) -> void:
|
|||||||
# ── Unit meshes ───────────────────────────────────────────────────────────────
|
# ── Unit meshes ───────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
func _rebuild_units(state: Dictionary) -> void:
|
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:
|
for iid : String in _move_anims:
|
||||||
var t := _move_anims[iid] as Tween
|
var t := _move_anims[iid] as Tween
|
||||||
if t != null: t.kill()
|
if t != null: t.kill()
|
||||||
_move_anims.clear()
|
_move_anims.clear()
|
||||||
|
|
||||||
for c in _units_root.get_children(): c.queue_free()
|
# ── Incremental rebuild: only remove/add; never recreate unchanged units ──
|
||||||
_unit_meshes.clear()
|
# Recreating a unit body interrupts its running animation (idle/move/attack),
|
||||||
_unit_bodies.clear()
|
# causing a one-frame bind-pose flash on every state echo.
|
||||||
_anim_players.clear()
|
|
||||||
_unit_def_ids.clear()
|
# 1. Collect the desired unit set from the new state.
|
||||||
# _prev_unit_hp and _prev_unit_pos are updated in _update_prev_state,
|
|
||||||
# called immediately in _on_state_updated — do NOT clear them here.
|
|
||||||
var my_id := Auth.user_id
|
var my_id := Auth.user_id
|
||||||
|
var desired : Dictionary = {} # iid → {unit, color, is_enemy}
|
||||||
for player: Dictionary in state.get("players", []):
|
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 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", {})
|
var arch : Dictionary = player.get("archSpirit", {})
|
||||||
if not arch.is_empty() and arch.get("position") != null:
|
for unit: Dictionary in ([arch] + player.get("field", [])):
|
||||||
_add_unit_mesh(arch, color, not is_mine)
|
if unit.is_empty() or unit.get("position") == null: continue
|
||||||
for unit: Dictionary in player.get("field", []):
|
var iid : String = str(unit.get("instanceId", ""))
|
||||||
if unit.get("position") != null:
|
if iid.is_empty(): continue
|
||||||
_add_unit_mesh(unit, color, not is_mine)
|
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:
|
func _add_unit_mesh(unit: Dictionary, team_color: Color, is_enemy: bool = false) -> void:
|
||||||
|
|||||||
Reference in New Issue
Block a user