feat(repair): make fragmented -> scanning event-driven via onSplitSettled

The fragmented -> scanning transition used to fire on a blind
setTimeout of REPAIR_FRAGMENTATION_SEQUENCE_SECONDS regardless of
whether the explode lerp had actually finished. With the new sphere-
reveal flow this got out of sync (sphere grows for 2.5s before
fragmented even mounts), so the timer could fire too early or while
the parts were still flying out.

Now the ExplodedModel emits a single 'settled' callback when its
internal lerp converges on its target (1 = fully exploded, 0 = fully
reassembled). RepairGame listens for settled-at-1 on the fragmented
ExplodableModel and advances to scanning on that event.

The legacy timer is kept as a generous safety net
(REPAIR_FRAGMENTATION_SEQUENCE_SECONDS + 2 seconds) so that if the
model fails to load (no parts -> no settled event ever fires) the
flow can never get stuck on the fragmented step.

Changes:
- ExplodedModel.ts:
  - new ExplodedModelOptions.onSettled: (settledAt: 0 | 1) => void
  - track settledAtTarget to ensure the callback fires exactly once
    per lerp (re-armed when setSplit() flips the target).
- ExplodableModel.tsx: new onSplitSettled prop, forwarded to the
  underlying ExplodedModel via a stable useCallback that reads the
  latest prop through a ref so the instance is not recreated mid-anim.
- RepairGame.tsx:
  - wire onSplitSettled on the fragmented ExplodableModel to
    setMissionStep(mission, 'scanning').
  - keep the existing setTimeout but extend it as a fallback only.

Pylon and farm benefit from the same fix automatically since they
share the same RepairGame fragmented branch.
This commit is contained in:
Tom Boullay
2026-06-03 04:18:10 +02:00
parent fe30596a5a
commit 317db48bcc
3 changed files with 65 additions and 7 deletions
+19 -1
View File
@@ -9,6 +9,13 @@ export interface ExplodedPart {
interface ExplodedModelOptions {
distance?: number;
speed?: number;
/**
* Fired exactly once each time the lerp converges on a target value
* (1 = fully exploded, 0 = fully reassembled). Useful for chaining
* the next mission step on actual animation completion rather than a
* blind timer.
*/
onSettled?: (settledAt: 0 | 1) => void;
}
const _center = new THREE.Vector3();
@@ -18,17 +25,24 @@ export class ExplodedModel {
private readonly parts: ExplodedPart[] = [];
private readonly distance: number;
private readonly speed: number;
private readonly onSettled?: (settledAt: 0 | 1) => void;
private progress = 0;
private targetProgress = 0;
private settledAtTarget = true;
constructor(model: THREE.Object3D, options: ExplodedModelOptions = {}) {
this.distance = options.distance ?? 1.2;
this.speed = options.speed ?? 6;
if (options.onSettled) this.onSettled = options.onSettled;
this.parts = this.createParts(model);
}
setSplit(split: boolean): void {
this.targetProgress = split ? 1 : 0;
const next = split ? 1 : 0;
if (next !== this.targetProgress) {
this.targetProgress = next;
this.settledAtTarget = false;
}
}
getParts(): readonly ExplodedPart[] {
@@ -39,6 +53,10 @@ export class ExplodedModel {
const diff = this.targetProgress - this.progress;
if (Math.abs(diff) < 0.001) {
this.progress = this.targetProgress;
if (!this.settledAtTarget) {
this.settledAtTarget = true;
this.onSettled?.(this.targetProgress === 1 ? 1 : 0);
}
} else {
this.progress += diff * Math.min(delta * this.speed, 1);
}