Files
sor/.claude/plan.md
T
2026-03-16 16:04:30 -04:00

5.4 KiB

Plan: Deferred Rebuild + Movement Path Animation

Goal

  1. Let attack/damage/death animations play to completion before _rebuild_units destroys the nodes.
  2. Move units along a BFS path (avoiding occupied tiles) while playing the Moving animation, before the rebuild snaps them to their new positions.
  3. Hand and HUD update immediately; only the board (unit nodes) is delayed.

New constants (near ANIM_ALIASES)

const MOVE_STEP_SECS : float = 0.28   # seconds per grid step during move anim
const POST_ANIM_SECS : float = 1.2    # seconds to hold after one-shot anims

New state variables (near _prev_unit_hp)

var _prev_unit_pos     : Dictionary = {}  # instanceId → Vector2i
var _move_anims        : Dictionary = {}  # instanceId → Tween
var _pending_rebuild   : Dictionary = {}  # latest state awaiting delayed rebuild
var _rebuild_tween     : Tween = null

New functions

_update_prev_state(state) — replaces HP tracking in _rebuild_units

Called immediately in _on_state_updated (before the delayed rebuild) so the next state update has correct baselines for both HP and position.

func _update_prev_state(state: Dictionary) -> void:
    _prev_unit_hp.clear()
    _prev_unit_pos.clear()
    for player: Dictionary in state.get("players", []):
        var arch := player.get("archSpirit", {})
        for unit: Dictionary in ([arch] + player.get("field", [])):
            if unit.is_empty(): continue
            var iid := str(unit.get("instanceId", ""))
            if iid.is_empty(): continue
            _prev_unit_hp[iid]  = int(unit.get("currentHealth", 0))
            var pd = unit.get("position")
            if pd is Dictionary and not pd.is_empty():
                _prev_unit_pos[iid] = Vector2i(pd.get("x",0), pd.get("y",0))

_bfs_path(from, to, blocked) → Array[Vector2i]

4-directional BFS. blocked is Dictionary[Vector2i, true] of impassable tiles; the destination is always treated as passable. Returns the list of steps NOT including from, INCLUDING to. Falls back to [to] if no path found (shouldn't happen on a connected grid).

_animate_unit_move(iid, from_coord, to_coord, blocked) → float

  1. Look up _unit_bodies[iid]; guard with is_instance_valid.
  2. BFS path from from_coord to to_coord using blocked.
  3. Kill any existing tween in _move_anims[iid].
  4. Call _play_unit_anim(iid, "moving", true) (looping).
  5. Create Tween (EASE_IN_OUT / TRANS_SINE). For each step in path, tween body.position to Vector3(wp.x, body.position.y, wp.z) over MOVE_STEP_SECS. (Keep body's current Y so placeholder and model heights both work.)
  6. Chain a callback: erase from _move_anims, call _play_unit_anim(iid,"idle",true).
  7. Store tween in _move_anims[iid].
  8. Return path.size() * MOVE_STEP_SECS.

_play_move_animations(state) → float

Called before rebuild, using _prev_unit_pos as the "before" snapshot.

  • Build blocked: Dictionary from all values in _prev_unit_pos.
  • For each unit in state that has a position change vs _prev_unit_pos:
    • Erase the mover's old coord from blocked (they've left it).
    • Call _animate_unit_move(iid, old, new, blocked).
    • Track max duration.
  • Returns max duration across all movers.

_schedule_rebuild(state, delay) → void

  • Store state in _pending_rebuild.
  • Kill + null _rebuild_tween if one exists.
  • If delay <= 0: call _rebuild_units(state) synchronously and return.
  • Else: _rebuild_tween = create_tween(), .tween_callback(λ).set_delay(delay). The lambda: _rebuild_tween = null; _rebuild_units(_pending_rebuild).

Modified functions

_play_damage_animations — returns bool

Change return type from void to bool. Return true if any _play_unit_anim call was made (HP drop or missing unit).

_rebuild_units

  • Remove the HP tracking block at the bottom (moved to _update_prev_state).
  • Remove _prev_unit_hp.clear() — no longer owned here.
  • Add at the top: kill all tweens in _move_anims, then .clear() it.

_add_unit_mesh — model load callback

In the model load callback, before b.position = wp + Vector3(0,0.5,0), check:

if not _move_anims.has(iid):   # only reset pos if not currently moving
    b.position = wp + Vector3(0, 0.5, 0)

This prevents a loaded model from snapping back to the original tile mid-tween.

_on_state_updated — new structure

1. print / _rebuild_ownership / camera / board   [unchanged, immediate]
2. _play_move_animations(state)   → move_dur
3. _play_damage_animations(state) → dmg_played
4. [immediate UI] _rebuild_hand(Game.get_hand()), _update_hud(state), print
5. _update_prev_state(state)      [update tracking now, before delayed rebuild]
6. delay = move_dur; if dmg_played: delay = maxf(delay, POST_ANIM_SECS)
7. _schedule_rebuild(state, delay)
8. [selection restoration match block — immediate, same as before]
9. [auto-draw await — same as before]

What this achieves

  • Attack animation fires on click → plays for ~1-2s → state update arrives → damage anim plays → 1.2s delay → rebuild. Total: seamless.
  • Unit moves → move animation plays cell-by-cell along BFS path → rebuild fires after path completes. Hand/HUD update immediately.
  • If a new state arrives during the delay (rapid opponent actions), the pending rebuild is replaced with the latest state; old animations may be cut short, which is acceptable.