State machines: asset, tab, State branch and Set state nodes (#51)
A state machine is an asset whose graph holds one `State` node per state (no ports, name as title), edited in a new "State machine" tab that reuses the graph canvas: right-click places "State n", snap toggle shared with the Behavior tab via SnapToggle. `statemachine` is a value kind like texture (port, literal node, variable, struct field, tagged list item). Behaviors use it through `State branch` (one exec output per state) and `Set state` (one exec input per state; the interpreter now hands `run` the entry port). States live per behavior instance in NodeContext.states, with no initial state. Ports derive from the inline picker or a wired `State machine` literal; BehaviorLookup became GraphLookup with behavior(id) and machine(id). The Graph tab is labelled "Behavior" (key unchanged). Unit tests (state-machine, kinds, catalog, ports), e2e step 26 and README added. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
ce42d16657
commit
e7d82bd1a9
60 files changed
+1269
-213
No files matched your search
@@ -10,7 +10,7 @@ import {
|
||||
type Value,
|
||||
} from '../../core/graph';
|
||||
import { stepKeyRepeat } from '../../core/graph/keyRepeat';
|
||||
import { behaviorLookup } from '../../core/model/behaviorPrograms';
|
||||
import { graphLookup } from '../../core/model/graphLookup';
|
||||
import { type PropertyBag, textOf } from '../../core/model/Properties';
|
||||
import { findAssetOf, type Project } from '../../core/model/Project';
|
||||
import { parentChain } from '../../core/model/walk';
|
||||
@@ -41,6 +41,8 @@ interface Instance {
|
||||
interpreter: GraphInterpreter;
|
||||
trace: Omit<Trace, 'values'>;
|
||||
values: Map<Graph, Map<string, Produced>>;
|
||||
/** Current state per state machine – this instance's alone, like its variables. */
|
||||
states: Record<string, string>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -125,7 +127,7 @@ export class BehaviorSystem implements System {
|
||||
if (!asset) continue;
|
||||
const trace = { edges: new Map<string, number>(), nodes: new Map<string, number>() };
|
||||
const values = new Map<Graph, Map<string, Produced>>();
|
||||
const interpreter = new GraphInterpreter(asset.graph, behaviorLookup(this.project));
|
||||
const interpreter = new GraphInterpreter(asset.graph, graphLookup(this.project));
|
||||
interpreter.trace = {
|
||||
onEdge: (id) => trace.edges.set(id, world.time),
|
||||
onNode: (id) => trace.nodes.set(id, world.time),
|
||||
@@ -135,7 +137,7 @@ export class BehaviorSystem implements System {
|
||||
byNode.set(id, { time: world.time, values: produced });
|
||||
},
|
||||
} satisfies TraceListener;
|
||||
const instance = { entity, interpreter, trace, values };
|
||||
const instance: Instance = { entity, interpreter, trace, values, states: {} };
|
||||
this.instances.push(instance);
|
||||
this.run(instance, (i, ctx) => i.fire('OnStart', ctx), 0);
|
||||
}
|
||||
@@ -275,7 +277,7 @@ export class BehaviorSystem implements System {
|
||||
): void {
|
||||
const world = this.world;
|
||||
if (!world) return;
|
||||
const ctx = this.context(instance.entity, world, dt);
|
||||
const ctx = this.context(instance, world, dt);
|
||||
try {
|
||||
body(instance.interpreter, ctx);
|
||||
} catch (error) {
|
||||
@@ -286,7 +288,7 @@ export class BehaviorSystem implements System {
|
||||
}
|
||||
}
|
||||
|
||||
private context(entity: Entity, world: World, dt: number): NodeContext {
|
||||
private context({ entity, states }: Instance, world: World, dt: number): NodeContext {
|
||||
// Handles arriving from the graph are the entities themselves, or test doubles with the same id.
|
||||
const resolve = (handle: EntityHandle | null): Entity | undefined =>
|
||||
handle ? world.entities.find((e) => e === handle || e.id === handle.id) : undefined;
|
||||
@@ -294,6 +296,7 @@ export class BehaviorSystem implements System {
|
||||
self: entity,
|
||||
owner: entity.parent,
|
||||
variables: entity.props,
|
||||
states,
|
||||
time: world.time,
|
||||
dt,
|
||||
findByName: (name) => world.findByName(name) ?? null,
|
||||
|
||||
Reference in new issue
Block a user