Spawn node: place assets at runtime into the play copy, Spawner demo behavior

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
rootandClaude Fable 5.1 committed 2026-09-07 16:57:19 +02:00
1 parent c3a533c4e5
commit 91ee2feadd
13 files changed
+219 -37

No files matched your search

+40 -1
View File
@@ -4,7 +4,7 @@ import { CommandStack } from '../../src/core/commands/CommandStack';
import { AddNodeCommand, ConnectCommand, DisconnectCommand, RemoveNodeCommand, SetNodeParamCommand } from '../../src/core/commands/graphCommands';
import { PlaceAssetCommand, SetParentCommand } from '../../src/core/commands/sceneObjectCommands';
import { AddVariableCommand, RemoveVariableCommand, RenameVariableCommand } from '../../src/core/commands/variableCommands';
import { type EntityHandle, GraphInterpreter, type NodeContext, allNodeDefs, portsCompatible } from '../../src/core/graph';
import { type EntityHandle, type Graph, GraphInterpreter, type NodeContext, allNodeDefs, portsCompatible } from '../../src/core/graph';
import type { BehaviorAsset } from '../../src/core/model/Asset';
import { createEmptyProject } from '../../src/core/model/Project';
import { deserializeProject, serializeProject } from '../../src/core/store/serialize';
@@ -32,6 +32,7 @@ function context(overrides: Partial<NodeContext> = {}): NodeContext & { logs: st
return { blockedX: false, blockedY: false };
},
log: (m: string) => logs.push(m),
spawn: () => null,
destroy: () => {},
stop: () => { ctx.stopped = true; },
...overrides,
@@ -382,3 +383,41 @@ describe('behavior serialization', () => {
expect(cleaned.graph.edges.length).toBe(behavior.graph.edges.length);
});
});
describe('Spawn node', () => {
const graph: Graph = {
nodes: [
{ id: 'start', type: 'OnStart', x: 0, y: 0, params: {} },
{ id: 'spawn', type: 'Spawn', x: 0, y: 0, params: { asset: 'crate', x: 5, y: 7 } },
{ id: 'log', type: 'Log', x: 0, y: 0, params: {} },
],
edges: [
{ id: 'e1', from: { node: 'start', port: 'exec' }, to: { node: 'spawn', port: 'exec' } },
{ id: 'e2', from: { node: 'spawn', port: 'exec' }, to: { node: 'log', port: 'exec' } },
{ id: 'e3', from: { node: 'spawn', port: 'object' }, to: { node: 'log', port: 'message' } },
],
};
it('asks the context for a new instance at the inline position and outputs it', () => {
const calls: unknown[] = [];
const ctx = context({
spawn: (assetId, position, parent) => {
calls.push({ assetId, position, parent });
return entity('Crate', position.x);
},
});
new GraphInterpreter(graph).fire('OnStart', ctx);
expect(calls).toEqual([{ assetId: 'crate', position: { x: 5, y: 7, z: 0 }, parent: null }]);
expect(ctx.logs).toEqual(['Crate']); // the spawned object flowed into Log
});
it('logs a hint and continues when no asset is selected or the asset is unknown', () => {
const ctx = context({ spawn: () => null });
const noAsset: Graph = { ...graph, nodes: graph.nodes.map((n) => (n.id === 'spawn' ? { ...n, params: {} } : n)) };
new GraphInterpreter(noAsset).fire('OnStart', ctx);
expect(ctx.logs).toEqual(['Spawn: no asset selected', '']);
const ctx2 = context({ spawn: () => null });
new GraphInterpreter(graph).fire('OnStart', ctx2);
expect(ctx2.logs[0]).toBe('Spawn: asset crate not found');
});
});
+1 -1
View File
@@ -156,7 +156,7 @@ describe('runtime hierarchy', () => {
{ id: 'e9', from: { node: 'ppos', port: 'worldX' }, to: { node: 'log3', port: 'message' } },
] };
const logs: string[] = [];
const ctx: NodeContext = { self: child, owner: parent, variables: {}, time: 0, dt: 0, findByName: () => null, isKeyDown: () => false, isTouching: () => false, moveAndSlide: () => ({ blockedX: false, blockedY: false }), log: (m) => logs.push(m), destroy: () => {}, stop: () => {} };
const ctx: NodeContext = { self: child, owner: parent, variables: {}, time: 0, dt: 0, findByName: () => null, isKeyDown: () => false, isTouching: () => false, moveAndSlide: () => ({ blockedX: false, blockedY: false }), log: (m) => logs.push(m), spawn: () => null, destroy: () => {}, stop: () => {} };
new GraphInterpreter(graph).fire('OnStart', ctx);
expect(logs).toEqual(['105', '5', '100']);
});
+35
View File
@@ -82,6 +82,41 @@ describe('world ↔ model synchronisation (editing while playing)', () => {
expect(sceneOf().objects.map((o) => o.id)).toEqual([a]);
});
it('spawn adds an object to the runtime scene and an entity at once, without resetting others', () => {
const { project, sceneId, spriteId, a, sceneOf } = setup();
const world = World.instantiate(project, sceneId);
const entityA = world.findByObjectId(a)!;
entityA.x = 77; // moved earlier in this tick – a spawn must not pull the model value back
const child = world.spawn(spriteId, { x: 10, y: 20, z: 1 }, entityA)!;
expect(child).not.toBeNull();
expect(child.parent).toBe(entityA);
expect(child.type).toBe('sprite');
expect(child.name).toBe('Box');
expect(child.props.width).toBe(100);
expect(entityA.x).toBe(77);
const object = sceneOf().objects.find((o) => o.id === child.objectId)!;
expect(object.parentId).toBe(a);
expect(object.position).toEqual({ x: 10, y: 20, z: 1 });
// The spawn is a structural change the panels must see – reported once.
expect(world.syncToModel()).toBe(true);
expect(world.syncToModel()).toBe(false);
// A full sync from the model keeps the spawned entity (identity included).
world.syncFromModel();
expect(world.findByObjectId(child.objectId)).toBe(child);
expect(child.parent).toBe(entityA);
// Destroying the spawned entity drops its object again.
world.remove(child);
expect(world.syncToModel()).toBe(true);
expect(sceneOf().objects.some((o) => o.id === child.objectId)).toBe(false);
// Unknown or unplaceable assets spawn nothing.
expect(world.spawn('nope', { x: 0, y: 0, z: 0 }, null)).toBeNull();
expect(world.spawn(sceneId, { x: 0, y: 0, z: 0 }, null)).toBeNull();
});
it('a play-mode copy leaves the original model untouched', () => {
const { project, sceneId, a, object } = setup();
const copy = deserializeProject(serializeProject(project), project.id);