Fix findUsages/undo/node-lookup bugs, split Viewport and Editor, share command and inspector helpers
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1eb28314cf
commit
f140c2fd77
58 files changed
+1969
-1171
No files matched your search
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
* Owns everything that only exists while the project is playing: the world, its systems, the
|
||||
* keyboard and the per-frame step.
|
||||
*
|
||||
* The key rule lives here: play never touches the saved model. `state.enterPlay()` swaps in a
|
||||
* runtime copy, the world is built from that copy, edits made while playing go into it, and
|
||||
* `exitPlay()` throws it away again.
|
||||
*/
|
||||
import { worldPositionOf } from '../core/graph';
|
||||
import { BehaviorSystem, type Trace } from '../runtime/systems/BehaviorSystem';
|
||||
import { CollisionSystem } from '../runtime/systems/CollisionSystem';
|
||||
import { Input } from '../runtime/systems/Input';
|
||||
import { World } from '../runtime/World';
|
||||
import { ui } from '../ui';
|
||||
import type { GameViewport } from '../viewport/GameViewport';
|
||||
import type { GraphEditor } from './graph/GraphEditor';
|
||||
import type { EditorState } from './state';
|
||||
|
||||
/** Facts about the running world for the check scripts. */
|
||||
export interface WorldSnapshot {
|
||||
entities: { objectId: string; assetId: string; name: string; x: number; y: number; worldX: number; worldY: number; parent: string; props: Record<string, unknown> }[];
|
||||
log: string[];
|
||||
}
|
||||
|
||||
/** What a play session needs from the surrounding editor, and nothing more. */
|
||||
export interface PlayHost {
|
||||
gameView: GameViewport;
|
||||
graphEditor: GraphEditor;
|
||||
/** Bring the game view to the front – play has started. */
|
||||
showGame(): void;
|
||||
/** Leave the game view if it is the one on screen – play has stopped. */
|
||||
leaveGame(): void;
|
||||
}
|
||||
|
||||
export class PlaySession {
|
||||
private world: World | null = null;
|
||||
private behaviors: BehaviorSystem | null = null;
|
||||
private readonly input = new Input();
|
||||
|
||||
constructor(private readonly state: EditorState, private readonly host: PlayHost) {}
|
||||
|
||||
get isPlaying(): boolean {
|
||||
return this.world !== null;
|
||||
}
|
||||
|
||||
/** Live execution data for the graph editor (null when not playing). */
|
||||
traceFor(assetId: string): Trace | null {
|
||||
return this.behaviors?.traceFor(assetId) ?? null;
|
||||
}
|
||||
|
||||
get time(): number {
|
||||
return this.world?.time ?? 0;
|
||||
}
|
||||
|
||||
/** Mirrors an edit made while playing into the running world at once. */
|
||||
syncFromModel(): void {
|
||||
this.world?.syncFromModel();
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a run: the state switches to a runtime copy, the world is built from it and shown
|
||||
* in the Game tab. All panels stay open and edit the copy.
|
||||
*/
|
||||
play(): void {
|
||||
const scene = this.state.activeScene;
|
||||
if (!scene) return;
|
||||
ui().focus.blur();
|
||||
const runtime = this.state.enterPlay();
|
||||
const world = World.instantiate(runtime, scene.id);
|
||||
this.world = world;
|
||||
this.input.attach();
|
||||
// Collisions are detected first so behaviors see this frame's enter/stay/exit events.
|
||||
const collisions = new CollisionSystem();
|
||||
this.behaviors = new BehaviorSystem(runtime, this.input, collisions);
|
||||
world.addSystem(collisions);
|
||||
world.addSystem(this.behaviors);
|
||||
this.host.gameView.setWorld(world);
|
||||
this.host.showGame();
|
||||
this.state.setNotice(world.firstOfType('camera') ? 'Playing · edits are not saved · Esc stops' : 'This scene has no camera – nothing is rendered · Esc stops');
|
||||
if (world.stopRequested) this.stop();
|
||||
}
|
||||
|
||||
/** Ends the run: the world and the runtime copy are discarded, the saved model returns untouched. */
|
||||
stop(): void {
|
||||
const world = this.world;
|
||||
this.world = null;
|
||||
this.behaviors = null;
|
||||
this.input.detach();
|
||||
ui().focus.blur();
|
||||
world?.destroy();
|
||||
this.host.gameView.setWorld(null);
|
||||
this.state.exitPlay();
|
||||
this.host.leaveGame();
|
||||
const lastError = world?.log.find((line) => line.startsWith('Error in behavior'));
|
||||
this.state.setNotice(lastError ?? '');
|
||||
}
|
||||
|
||||
/** One frame. Does nothing when not playing. */
|
||||
tick(dt: number): void {
|
||||
const world = this.world;
|
||||
if (!world) return;
|
||||
world.tick(Math.min(dt, 0.1));
|
||||
this.input.endFrame();
|
||||
if (world.stopRequested) {
|
||||
this.stop();
|
||||
return;
|
||||
}
|
||||
// Restart game: throw the run away and start the scene again from the saved model.
|
||||
if (world.restartRequested) {
|
||||
this.stop();
|
||||
this.play();
|
||||
return;
|
||||
}
|
||||
// Mirror what the systems did into the runtime copy so the panels show live values.
|
||||
// A destroyed entity removes its object – that is a structural change the lists must see.
|
||||
if (world.syncToModel()) this.state.preview();
|
||||
this.state.runtimeTick();
|
||||
if (this.host.graphEditor.visible) this.host.graphEditor.redraw();
|
||||
}
|
||||
|
||||
/** Read-only view of the running world for the check scripts. */
|
||||
snapshot(): WorldSnapshot | null {
|
||||
const world = this.world;
|
||||
if (!world) return null;
|
||||
return {
|
||||
entities: world.entities.map((e) => {
|
||||
const w = worldPositionOf(e);
|
||||
return { objectId: e.objectId, assetId: e.assetId, name: e.name, x: e.x, y: e.y, worldX: w.x, worldY: w.y, parent: e.parent?.objectId ?? '', props: { ...e.props } };
|
||||
}),
|
||||
log: [...world.log],
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user