/** * Small factories for writing project documents by hand (the seed scripts). They produce the * exact shape `src/core/store/serialize.ts` reads; optional fields get their defaults. */ /** A scene object (also a container template entry). Position defaults to the origin. */ export const sceneObject = ( id, assetId, { name = '', x = 0, y = 0, z = 0, overrides = {}, parentId = '', templateId = '', editorOffset } = {}, ) => ({ id, assetId, name, position: { x, y, z }, overrides, parentId, templateId, ...(editorOffset ? { editorOffset } : {}), }); /** A graph edge from `from.fromPort` to `to.toPort`. */ export const edge = (id, from, fromPort, to, toPort) => ({ id, from: { node: from, port: fromPort }, to: { node: to, port: toPort }, }); /** A graph node; `params` holds settings and inline input values alike. */ export const node = (id, type, x, y, params = {}) => ({ id, type, x, y, params }); /** A behavior variable declaration. */ export const variable = (key, kind, value) => ({ key, label: key, kind, default: value }); /** * Instantiation of container templates the way the editor does it (`instantiateContainer`): * `templates` maps container asset ids to their template objects. * * expand(assetId, hostId) copies of the template, parented below `hostId` * instantiate(id, assetId, options) the container object plus its expanded content * * A template object that is itself a container is expanded in turn, unless the template already * spells its content out. Copies remember their template id. */ export function containerFactory(templates) { const expand = (assetId, hostId) => { const template = templates[assetId]; const objects = []; for (const t of template) { const id = `${hostId}-${t.id}`; objects.push({ ...t, id, parentId: t.parentId ? `${hostId}-${t.parentId}` : hostId, templateId: t.id }); if (templates[t.assetId] && !template.some((o) => o.parentId === t.id)) objects.push(...expand(t.assetId, id)); } return objects; }; const instantiate = (id, assetId, options = {}) => [ sceneObject(id, assetId, options), ...expand(assetId, id), ]; return { expand, instantiate }; }