diff --git a/Client/origin-client/scenes/game/game_screen.gd b/Client/origin-client/scenes/game/game_screen.gd index 1bcdf59..994ea41 100644 --- a/Client/origin-client/scenes/game/game_screen.gd +++ b/Client/origin-client/scenes/game/game_screen.gd @@ -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: