Files
feuerwerk/src/core/graph/liveObjects.ts
T
adminandClaude Opus 5.5 bc688f3efc
game-maker / gate (push) Successful in 3m50s
game-maker / suite (push) Successful in 12m3s
Refactoring round 7: split BehaviorSystem, fewer copies (#85)
Recurring cleanup round over the code since round 6 (056a622), 105
commits. Behaviour is unchanged. Duplicated code in src went from 29
clones with 229 lines (0.32 %) to 14 clones with 109 lines (0.15 %),
measured with jscpd and the same settings as round 6.

- BehaviorSystem.ts shrinks from 941 to 589 lines. Collecting the
  pulses (keys, timers, collisions, animations) moved to
  behaviorPulses.ts. The NodeContext the nodes receive moved to
  behaviorContext.ts. Imports from outside stay the same, TimeScaled
  is still exported from BehaviorSystem.ts.
- PointKindButton and drawPointGrip replace the "Point:" button with
  its menu and the point and handle drawing that the curve and the
  vector art tab each had on their own.
- lerpVec, cubicAt and splitCubic in src/core/util/vec.ts replace four
  copies of the cubic curve and two de Casteljau splits.
- mapNested in Properties.ts is the one walk over lists and structs
  that liveObjects and objectRefs did separately. withFieldRenamed
  and withoutField replace three copies of renaming a struct field
  and two of removing one.
- jsonBody and staleRevision build the JSON body and turn a 409 into
  StaleRevisionError for all stores. The auth store turns a 401 into
  its error in one place.
- Smaller helpers: structAssetFrom for the two "Make struct type"
  commands, shownObject in the inspector, selectedItem for the size
  gizmo and the polygon corners, solidTiles in the tilemap collision,
  requireVariablesAsset and requireBehaviorVariable.
- Left as is on purpose: the plain forwarding from the collision and
  physics systems to Solids, the pointer wiring of the canvases and
  the identical last lines of the two make commands.
- Removed unused code: tilemapTileIds and tilemapsUsing were never
  called since #128 and #131. EditorState.runtimeProject became unused
  with GameRun in #266. The assetId getter of the make commands was
  read only by a test.
- 60 values used only in their own file lost their export. The shader
  and particle node catalogues stay exported as in round 6.
- Five comments that told history now describe the current state. The
  doc comment of BehaviorSystem sits on the class again.
- README lists the struct, clip area and curve assets and mentions the
  game link from #266.

Findings:
- Deleting a tile that is painted on a tilemap leaves its cells in
  place, and they draw nothing. The delete dialog does not mention the
  tilemaps. That was probably the job of the unused tilemapsUsing.
  Left as is, because fixing it changes behaviour.
- The solid filter for tile surfaces was untested. A bent filter
  stayed green. A new test in tests/core/surface.test.ts now fails
  against it.

Tests:
- ./check.sh: 195 files, 2815 tests, exit 0 (one test removed with
  tilemapsUsing, one added for tile surfaces).
- Full e2e run: ALL PASSED, 1580 checks.
- Smokes jumprun, tetris, actor: 24, 19 and 21 checks, all passed.
  The example project files were restored byte for byte afterwards.
- Counter-checks, 12, all fail as expected: 10 in unit tests (among
  them mapNested, splitCubic, cubicAt, staleRevision, the stay pulse,
  the late timer and the time scale), 2 in the browser (Point button
  always enabled fails 47, selectedItem without a hit fails 29).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 16:16:37 +02:00

121 lines
4.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {
isList,
isStruct,
mapNested,
NO_OBJECT,
type PropertyBag,
type PropertyValue,
sameValue,
tagOf,
} from '../model/Properties';
import { referencedId } from '../model/objectRefs';
import type { EntityHandle, NodeContext } from './NodeContext';
import type { PortDef } from './NodeRegistry';
import { isEntity, isObjectPlace, type Value, writeTyped } from './values';
/**
* During a run a bag may hold live objects – in an object variable, a struct field, a list item. None
* of it may leave the run: `World.syncToModel` writes a behavior's changed values into the scene, a
* save is written as JSON, the inspector and the test hooks show what they are handed. Each of those
* ways out passes the value through here, where every object becomes the empty one (`NO_OBJECT`).
*/
export function withoutObjects<T extends PropertyValue | undefined>(value: T): T {
return mapObjects(value, () => NO_OBJECT) as T;
}
/**
* Whether a run's value still says what the model's does. What a graph put into an object's place
* stays in the run, so once it holds one, the objects on either side – the model's references
* included (#258) – no longer count.
*/
export function sameAtRest(model: PropertyValue | undefined, live: PropertyValue | undefined): boolean {
if (!holdsObjects(live)) return sameValue(model, live);
const none = () => NO_OBJECT;
const blank = (value: PropertyValue | undefined) =>
mapObjects(value, none, none) as PropertyValue | undefined;
return sameValue(blank(model), blank(live));
}
function holdsObjects(value: unknown): boolean {
if (isEntity(value as Value)) return true;
if (isList(value)) return value.some(holdsObjects);
return isStruct(value) && tagOf(value) === undefined && Object.values(value).some(holdsObjects);
}
export function bagWithoutObjects(bag: PropertyBag): PropertyBag {
const out: PropertyBag = {};
for (const [key, value] of Object.entries(bag)) out[key] = withoutObjects(value);
return out;
}
/** How a run tells what a bag's objects are: whether one is still there, and which one a reference names. */
export interface ObjectLookup {
isAlive(target: EntityHandle): boolean;
/** The object `ref` names, looked up from where the bag sits (see `model/objectRefs.ts`); null when gone. */
referenced(ref: string): EntityHandle | null;
}
/**
* A bag's value as a graph reads it: an object destroyed since it was written is gone, so a graph
* never goes on acting on one that has left the game, and a reference picked in the editor (#258) is
* the object it names. Alone either reads as nothing (`null`) when there is none, inside a list or
* struct as the empty object.
*/
export function liveValue(value: PropertyValue | undefined, objects: ObjectLookup): Value {
if (value === undefined) return null;
if (tagOf(value) === 'object') return referencedObject(value, objects);
if (isEntity(value)) return objects.isAlive(value) ? value : null;
return mapObjects(
value,
(held) => (objects.isAlive(held) ? held : NO_OBJECT),
(place) => referencedObject(place, objects) ?? NO_OBJECT,
) as Value;
}
/** The lookup for a bag that sits on `owner`: its references are read from there. */
export function objectsOf(
ctx: Pick<NodeContext, 'isAlive' | 'findReferenced'>,
owner: EntityHandle,
): ObjectLookup {
return { isAlive: (target) => ctx.isAlive(target), referenced: (ref) => ctx.findReferenced(owner, ref) };
}
function referencedObject(place: PropertyValue, objects: ObjectLookup): Value {
const ref = referencedId(place);
const found = ref ? objects.referenced(ref) : null;
return found && objects.isAlive(found) ? found : null;
}
/** What `outputs` read of a bag, each value live as `liveValue` says. */
export function readLive(
bag: PropertyBag,
outputs: readonly PortDef[],
objects: ObjectLookup,
): Record<string, Value> {
const values: Record<string, Value> = {};
for (const port of outputs) if (port.key in bag) values[port.key] = liveValue(bag[port.key], objects);
return values;
}
/** Writes what arrives on `port` into the bag, typed as the slot is; an object port always makes an object's place. */
export function writePort(bag: PropertyBag, port: PortDef, value: Value | undefined): void {
if (port.type === 'object' && !isObjectPlace(bag[port.key])) bag[port.key] = NO_OBJECT;
writeTyped(bag, port.key, value);
}
/**
* `value` with each object replaced by what `swap` says, and each object's place by what `place`
* says when given; untouched parts keep their identity.
*/
function mapObjects(
value: unknown,
swap: (held: EntityHandle) => unknown,
place?: (at: PropertyValue) => unknown,
): unknown {
return mapNested(value, (part) => {
if (isEntity(part as Value)) return swap(part as EntityHandle);
if (place && tagOf(part) === 'object') return place(part as PropertyValue);
return undefined;
});
}