Consolidate duplicated editor code, trim comments, refresh README
Split GraphCanvas (1076 -> 831 lines) into EdgeLayer and graphMenus; the port and marker menus were the same list and are now one function. Value specs of the inspector and the list tab share valueSpecs.ts, and the remaining copies (compound commands, type glyphs, typed asset lookup, name rule, play/editor world setup, marquee) are merged. Four copies had already drifted apart; merging them fixes: - "Disconnect N links" in the port menu is one undo step, not N - "+ Add collider" is hidden on a placed container instance, as in the hierarchy menu, instead of always failing - a bool property coerced from the text "false" becomes false - a port command on an unknown node type reports instead of crashing Comment-only edits leave the bundle byte-identical. README corrected in 35 places (step limit, jump-run clock, suite duration, 409 body, ...). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9212e61c4c
commit
8f92a06c84
96 files changed
+999
-1261
No files matched your search
@@ -2,12 +2,7 @@ import type { ENTITY_HANDLE } from '../core/graph/NodeContext';
|
||||
import type { AssetType } from '../core/model/AssetTypes';
|
||||
import type { PropertyBag } from '../core/model/Properties';
|
||||
|
||||
/**
|
||||
* A live instance created from a scene object when Play starts. Holds the *resolved*
|
||||
* property values as mutable runtime state. Systems change entities freely; the world
|
||||
* mirrors them into the play-mode copy of the project (never the saved model) so the
|
||||
* editor panels can show and edit them while playing.
|
||||
*/
|
||||
/** A live instance of a scene object; its resolved props are mutable runtime state the world mirrors back. */
|
||||
export interface Entity {
|
||||
/** What tells a graph this object is a live entity and not a struct value. */
|
||||
readonly [ENTITY_HANDLE]: true;
|
||||
|
||||
+5
-19
@@ -29,13 +29,8 @@ export interface ModelWrite {
|
||||
}
|
||||
|
||||
/**
|
||||
* The running game: entities instantiated from one scene plus the systems ticking them.
|
||||
*
|
||||
* The world mirrors the scene of the project it was built from: while playing that is a copy of
|
||||
* the model, thrown away on stop; the editor run builds on the saved one, where the writes stay.
|
||||
* `syncFromModel()` pulls editor changes (moved, placed, removed or re-parented objects, edited
|
||||
* properties) into the entities; `syncToModel()` pushes what the systems changed (positions,
|
||||
* variables, destroyed entities) back, so the panels show live values.
|
||||
* The running game: entities of one scene plus the systems ticking them. Built on the play copy
|
||||
* (discarded on stop) or, for the editor run, on the saved model, where its writes stay.
|
||||
*/
|
||||
export class World {
|
||||
readonly entities: Entity[] = [];
|
||||
@@ -82,12 +77,7 @@ export class World {
|
||||
return this.elapsed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the entities match the scene: one entity per placed object (created on demand,
|
||||
* dropped when the object is gone), positions, names and resolved properties taken from
|
||||
* the model, parent links re-established. Property bags are updated in place so systems
|
||||
* holding a reference keep seeing the current values.
|
||||
*/
|
||||
/** Makes the entities match the scene; props are updated in place so systems holding a bag stay current. */
|
||||
syncFromModel(): void {
|
||||
const scene = findScene(this.project, this.sceneId);
|
||||
if (!scene) return;
|
||||
@@ -145,11 +135,8 @@ export class World {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of an asset while running (Spawn node): the object is added to
|
||||
* the scene of the runtime copy – so the panels list it and a full sync keeps it – and its
|
||||
* entity is created right away, without touching the other entities (they may have moved
|
||||
* during this tick already). Position is local to `parent`, world when there is none.
|
||||
* Returns null for unknown or unplaceable assets.
|
||||
* Adds the object to the scene and creates only its entity, leaving the others alone (they may have
|
||||
* moved this tick already). Position is local to `parent`, world when there is none.
|
||||
*/
|
||||
spawn(
|
||||
assetId: string,
|
||||
@@ -279,7 +266,6 @@ export class World {
|
||||
return this.entities.filter((e) => e.parent === entity);
|
||||
}
|
||||
|
||||
/** Everything parented below an entity, at any depth. */
|
||||
descendantsOf(entity: Entity): Entity[] {
|
||||
return descendantsBelow(
|
||||
entity,
|
||||
|
||||
@@ -10,10 +10,10 @@ import {
|
||||
import { stepKeyRepeat } from '../../core/graph/keyRepeat';
|
||||
import { behaviorLookup } from '../../core/model/behaviorPrograms';
|
||||
import { type PropertyBag, textOf } from '../../core/model/Properties';
|
||||
import { findAsset, type Project } from '../../core/model/Project';
|
||||
import { findAssetOf, type Project } from '../../core/model/Project';
|
||||
import type { Entity } from '../Entity';
|
||||
import type { World } from '../World';
|
||||
import { bodyOf, type CollisionPair, type CollisionSystem } from './CollisionSystem';
|
||||
import { bodyOf, type CollisionPair, CollisionSystem } from './CollisionSystem';
|
||||
import type { Input } from './Input';
|
||||
import type { System } from './System';
|
||||
|
||||
@@ -36,6 +36,27 @@ interface Instance {
|
||||
* Runs the graph of every behavior entity. Each instance has its own interpreter (and
|
||||
* therefore its own timers and variable state); the project is only read to find graphs.
|
||||
*/
|
||||
const ERROR_PREFIX = 'Error in behavior';
|
||||
|
||||
/** Collisions are detected first, so behaviors see this frame's enter/stay/exit events. */
|
||||
export function addGameSystems(
|
||||
world: World,
|
||||
project: Project,
|
||||
input: Input,
|
||||
shouldRun?: (entity: Entity) => boolean,
|
||||
): BehaviorSystem {
|
||||
const collisions = new CollisionSystem();
|
||||
const behaviors = new BehaviorSystem(project, input, collisions, shouldRun);
|
||||
world.addSystem(collisions);
|
||||
world.addSystem(behaviors);
|
||||
return behaviors;
|
||||
}
|
||||
|
||||
/** The behavior failure that stopped a run, if that is why it stopped. */
|
||||
export function behaviorError(world: World): string | undefined {
|
||||
return world.log.find((line) => line.startsWith(ERROR_PREFIX));
|
||||
}
|
||||
|
||||
export class BehaviorSystem implements System {
|
||||
private instances: Instance[] = [];
|
||||
private world: World | null = null;
|
||||
@@ -64,10 +85,8 @@ export class BehaviorSystem implements System {
|
||||
}
|
||||
|
||||
/**
|
||||
* One instance per behavior entity: entities that appeared (placed while playing) get an
|
||||
* interpreter and their `On start`; instances whose entity is gone are dropped. A behavior
|
||||
* that is switched off has no instance yet – it gets one, and its `On start`, the moment it
|
||||
* is switched on. That is what lets a hidden screen wake up when it is shown.
|
||||
* One instance per behavior entity. A switched-off behavior gets its instance and `On start` only when
|
||||
* switched on, which is what lets a hidden screen wake up when shown.
|
||||
*/
|
||||
private syncInstances(world: World): void {
|
||||
this.instances = this.instances.filter((i) => world.entities.includes(i.entity));
|
||||
@@ -79,8 +98,8 @@ export class BehaviorSystem implements System {
|
||||
this.instances.some((i) => i.entity === entity)
|
||||
)
|
||||
continue;
|
||||
const asset = findAsset(this.project, entity.assetId);
|
||||
if (!asset || asset.type !== 'behavior') continue;
|
||||
const asset = findAssetOf(this.project, entity.assetId, 'behavior');
|
||||
if (!asset) continue;
|
||||
const trace: Trace = { edges: new Map(), nodes: new Map() };
|
||||
const interpreter = new GraphInterpreter(asset.graph, behaviorLookup(this.project));
|
||||
interpreter.trace = {
|
||||
@@ -175,7 +194,6 @@ export class BehaviorSystem implements System {
|
||||
fire('OnCollisionExit', this.collisions.exits);
|
||||
}
|
||||
|
||||
/** Object ids of the behaviors this system is currently running. */
|
||||
runningIds(): string[] {
|
||||
return this.instances.map((i) => i.entity.objectId);
|
||||
}
|
||||
@@ -210,7 +228,7 @@ export class BehaviorSystem implements System {
|
||||
body(instance.interpreter, ctx);
|
||||
} catch (error) {
|
||||
world.log.push(
|
||||
`Error in behavior "${instance.entity.name}": ${error instanceof Error ? error.message : String(error)}`,
|
||||
`${ERROR_PREFIX} "${instance.entity.name}": ${error instanceof Error ? error.message : String(error)}`,
|
||||
);
|
||||
world.stopRequested = true;
|
||||
}
|
||||
|
||||
@@ -24,10 +24,8 @@ export interface CollisionPair {
|
||||
}
|
||||
|
||||
/**
|
||||
* Detects overlaps between collider entities once per tick and sorts them into enter /
|
||||
* stay / exit lists for the frame. Detection only – nothing is pushed apart. Colliders
|
||||
* of the same body (same parent) never collide with each other. Runs before the
|
||||
* BehaviorSystem so graphs see this frame's events.
|
||||
* Sorts collider overlaps into enter / stay / exit per tick, detection only; colliders of one body never
|
||||
* collide. Runs before the BehaviorSystem so graphs see this frame's events.
|
||||
*/
|
||||
export class CollisionSystem implements System {
|
||||
enters: CollisionPair[] = [];
|
||||
@@ -78,13 +76,8 @@ export class CollisionSystem implements System {
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves `entity` by (dx, dy) in its local units, one axis at a time (x, then y): after
|
||||
* each axis step its solid colliders are pushed back to just touch whatever solid collider
|
||||
* they ran into, so the remaining axis still moves – the object slides along walls.
|
||||
* An overlap that already existed is never resolved (no jolt) but also never deepened, so
|
||||
* an object inside a wall can leave it without being able to burrow further in. Long moves
|
||||
* are split into sub-steps so nothing jumps across a thin wall. Triggers, the entity's own
|
||||
* colliders and colliders that travel with it (descendants) never block.
|
||||
* Moves one axis at a time, stopping each at contact so the object slides along walls. An existing
|
||||
* overlap is never resolved (no jolt) but never deepened, so an object can leave a wall but not burrow in.
|
||||
*/
|
||||
moveAndSlide(
|
||||
world: World,
|
||||
@@ -114,7 +107,6 @@ export class CollisionSystem implements System {
|
||||
delta: number,
|
||||
): boolean {
|
||||
if (delta === 0 || own.length === 0) return false;
|
||||
// Long steps are split so a fast mover cannot jump across a thin obstacle in one frame.
|
||||
const steps = Math.min(
|
||||
MAX_STEPS,
|
||||
Math.max(1, Math.ceil(Math.abs(delta) / stepLimit(own, obstacles, axis))),
|
||||
|
||||
@@ -66,12 +66,10 @@ export class Input {
|
||||
return this.down.has(key);
|
||||
}
|
||||
|
||||
/** Keys that went down since the last frame. */
|
||||
get justPressed(): ReadonlySet<string> {
|
||||
return this.pressed;
|
||||
}
|
||||
|
||||
/** Keys that went up since the last frame. */
|
||||
get justReleased(): ReadonlySet<string> {
|
||||
return this.released;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user