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
@@ -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