castle-web-cli 0.4.78 → 0.4.80
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent-prompts.d.ts +4 -1
- package/dist/agent-prompts.js +28 -7
- package/dist/agent.d.ts +7 -2
- package/dist/agent.js +655 -51
- package/dist/native/loop.d.ts +2 -0
- package/dist/native/loop.js +698 -0
- package/dist/native/openrouter.d.ts +55 -0
- package/dist/native/openrouter.js +354 -0
- package/dist/native/playtest-browser.d.ts +34 -0
- package/dist/native/playtest-browser.js +354 -0
- package/dist/native/playtest-executor.d.ts +3 -0
- package/dist/native/playtest-executor.js +156 -0
- package/dist/native/playtest.d.ts +131 -0
- package/dist/native/playtest.js +314 -0
- package/dist/native/tools.d.ts +38 -0
- package/dist/native/tools.js +690 -0
- package/dist/native/types.d.ts +40 -0
- package/dist/native/types.js +41 -0
- package/dist/serve.js +12 -0
- package/dist/shell/assets/{index-yGdKhgfZ.js → index-D3unT7do.js} +37 -37
- package/dist/shell/assets/{index-WE24qX3d.css → index-RZrw5gQ2.css} +1 -1
- package/dist/shell/index.html +2 -2
- package/kits/basic-2d/CLAUDE.md +29 -3
- package/kits/basic-2d/behaviors/Layout.jsx +10 -0
- package/kits/basic-2d/behaviors/Sprite.jsx +1 -1
- package/kits/basic-2d/blueprints/cauldron.scene +22 -0
- package/kits/basic-2d/castle.json +5 -7
- package/kits/basic-2d/docs/pxart-format.md +4 -3
- package/kits/basic-2d/drawings/cauldron.pxart +113 -0
- package/kits/basic-2d/editors/BlueprintLibrary.jsx +247 -0
- package/kits/basic-2d/editors/PlayOnly.jsx +1 -0
- package/kits/basic-2d/editors/SceneEditor.jsx +399 -411
- package/kits/basic-2d/editors/SelectionOverlay.jsx +125 -63
- package/kits/basic-2d/editors/SingleEditor.jsx +11 -2
- package/kits/basic-2d/editors/editorHistory.js +8 -2
- package/kits/basic-2d/editors/inspectorSheet.js +5 -19
- package/kits/basic-2d/engine/ScenePlayer.jsx +2 -2
- package/kits/basic-2d/engine/blueprint.js +423 -0
- package/kits/basic-2d/engine/files.js +1 -1
- package/kits/basic-2d/engine/scene.js +29 -29
- package/kits/basic-2d/engine/ui.jsx +160 -21
- package/kits/basic-2d/engine/ui.module.css +155 -13
- package/kits/basic-2d/pnpm-workspace.yaml +3 -0
- package/kits/basic-2d/scenes/main.scene +3 -13
- package/package.json +2 -1
- package/kits/basic-2d/drawings/pig.pxart +0 -26
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
// Blueprint system: every actor in a scene is an instance of a blueprint (a
|
|
2
|
+
// `.scene` file under `blueprints/` whose `actors[0]` is the template, with no
|
|
3
|
+
// `id`). An instance stores only sparse per-property overrides in its own
|
|
4
|
+
// `components`; blueprint template values are the base, merged live from the
|
|
5
|
+
// current `files` map (never a load-time snapshot) so a blueprint edit in one
|
|
6
|
+
// panel propagates to every scene that places it. Shared by the runtime
|
|
7
|
+
// (`scene.js`, so play mode and `PlayOnly` resolve blueprints too) and the
|
|
8
|
+
// editors (which also mint/fork/migrate/cascade-delete blueprint files).
|
|
9
|
+
import { formatJson } from './files';
|
|
10
|
+
import { DEFAULT_RESOLUTION, TRANSPARENT, serializeCompact } from './pxart';
|
|
11
|
+
import { cardSize, mintActorId } from './scene';
|
|
12
|
+
|
|
13
|
+
export const BLUEPRINTS_DIR = 'blueprints';
|
|
14
|
+
export const DRAWINGS_DIR = 'drawings';
|
|
15
|
+
|
|
16
|
+
export function isBlueprintPath(path) {
|
|
17
|
+
return typeof path === 'string' && path.startsWith(`${BLUEPRINTS_DIR}/`) && path.endsWith('.scene');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Per-property inherit metadata for a behavior prop. Behaviors opt a prop out
|
|
21
|
+
// of inheritance via `static propertyMeta = { propName: { inherit: false } }`
|
|
22
|
+
// (see behaviors/Layout.jsx for x/y/rotation); anything absent defaults to
|
|
23
|
+
// inherited. A non-inherited prop is always written as an instance value at
|
|
24
|
+
// placement time -- the blueprint's value for it is only a default for newly
|
|
25
|
+
// placed instances, and future blueprint edits never move an existing
|
|
26
|
+
// instance's copy of that prop.
|
|
27
|
+
export function getPropertyMeta(Behavior, prop) {
|
|
28
|
+
return Behavior?.propertyMeta?.[prop] ?? { inherit: true };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function isInheritedProp(Behavior, prop) {
|
|
32
|
+
return getPropertyMeta(Behavior, prop).inherit !== false;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Parse a blueprint file's text into { name, components } (the template
|
|
36
|
+
// actor's components), or null if the text isn't a valid blueprint (used to
|
|
37
|
+
// treat a tombstoned/deleted blueprint file as absent).
|
|
38
|
+
function parseBlueprintText(text) {
|
|
39
|
+
if (typeof text !== 'string') return null;
|
|
40
|
+
let data;
|
|
41
|
+
try {
|
|
42
|
+
data = JSON.parse(text);
|
|
43
|
+
} catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
const actor = data?.actors?.[0];
|
|
47
|
+
if (!actor || typeof actor !== 'object' || !actor.components) return null;
|
|
48
|
+
const name = typeof data.name === 'string' && data.name.trim() ? data.name : 'Blueprint';
|
|
49
|
+
return { name, components: actor.components };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Resolve a blueprint's template from the LIVE files map -- the load-bearing
|
|
53
|
+
// call for live propagation. Returns null when the file is missing, deleted,
|
|
54
|
+
// or malformed (a dangling `blueprint` ref degrades to "no template" rather
|
|
55
|
+
// than throwing).
|
|
56
|
+
export function getBlueprintTemplate(files, blueprintPath) {
|
|
57
|
+
return parseBlueprintText(files?.[blueprintPath]);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Every blueprint currently in the deck, for the library/hotbar. Skips
|
|
61
|
+
// unparseable / tombstoned files (see `cascadeDeleteBlueprint`).
|
|
62
|
+
export function listBlueprints(files) {
|
|
63
|
+
const out = [];
|
|
64
|
+
for (const path of Object.keys(files ?? {})) {
|
|
65
|
+
if (!isBlueprintPath(path)) continue;
|
|
66
|
+
const template = parseBlueprintText(files[path]);
|
|
67
|
+
if (!template) continue;
|
|
68
|
+
out.push({ path, name: template.name, components: template.components });
|
|
69
|
+
}
|
|
70
|
+
out.sort((a, b) => a.path.localeCompare(b.path));
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Per-property merge: the blueprint template is the base, instance overrides
|
|
75
|
+
// win per-property within each component (not whole-component replacement). A
|
|
76
|
+
// component present on only one side appears whole.
|
|
77
|
+
export function mergeComponents(templateComponents, instanceComponents) {
|
|
78
|
+
const merged = {};
|
|
79
|
+
const names = new Set([
|
|
80
|
+
...Object.keys(templateComponents ?? {}),
|
|
81
|
+
...Object.keys(instanceComponents ?? {}),
|
|
82
|
+
]);
|
|
83
|
+
for (const name of names) {
|
|
84
|
+
const templateProps = templateComponents?.[name];
|
|
85
|
+
const instanceProps = instanceComponents?.[name];
|
|
86
|
+
if (templateProps === undefined) merged[name] = { ...instanceProps };
|
|
87
|
+
else if (instanceProps === undefined) merged[name] = { ...templateProps };
|
|
88
|
+
else merged[name] = { ...templateProps, ...instanceProps };
|
|
89
|
+
}
|
|
90
|
+
return merged;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// The fully resolved components for one actor: its blueprint's template
|
|
94
|
+
// merged with its own sparse overrides, or its raw components when it carries
|
|
95
|
+
// no `blueprint` ref (the blueprint's own template actor, or a not-yet
|
|
96
|
+
// migrated legacy actor).
|
|
97
|
+
export function resolveActorComponents(files, actor) {
|
|
98
|
+
if (!actor?.blueprint) return actor?.components ?? {};
|
|
99
|
+
const template = getBlueprintTemplate(files, actor.blueprint);
|
|
100
|
+
return mergeComponents(template?.components, actor.components);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function countMatches(text, regex) {
|
|
104
|
+
let max = 0;
|
|
105
|
+
const re = new RegExp(regex);
|
|
106
|
+
const m = re.exec(text ?? '');
|
|
107
|
+
return m ? Number(m[1]) : max;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// "Blueprint N" -- N is one more than the highest existing "Blueprint <n>"
|
|
111
|
+
// name (not just existing.length + 1, so a gap left by a deleted blueprint
|
|
112
|
+
// doesn't get reused and collide with intent elsewhere).
|
|
113
|
+
function mintBlueprintName(existingNames) {
|
|
114
|
+
let max = 0;
|
|
115
|
+
for (const name of existingNames) max = Math.max(max, countMatches(name, /^Blueprint (\d+)$/));
|
|
116
|
+
return `Blueprint ${max + 1}`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// `blueprints/blueprint-N.scene` -- the filename is a stable slug minted once
|
|
120
|
+
// at creation; nothing ever renames it afterward (renaming edits only `name`).
|
|
121
|
+
function mintBlueprintPath(existingPaths) {
|
|
122
|
+
let max = 0;
|
|
123
|
+
for (const path of existingPaths) {
|
|
124
|
+
max = Math.max(max, countMatches(path, new RegExp(`^${BLUEPRINTS_DIR}/blueprint-(\\d+)\\.scene$`)));
|
|
125
|
+
}
|
|
126
|
+
return `${BLUEPRINTS_DIR}/blueprint-${max + 1}.scene`;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function formatBlueprintFileText(name, components) {
|
|
130
|
+
return formatJson({ name, actors: [{ components }] });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Rename a blueprint: replace its top-level `name`, preserving actors and any
|
|
134
|
+
// hand-added extras. Returns the next file text, or null when the file is
|
|
135
|
+
// missing/unparseable (caller should treat that as "blueprint gone" and skip
|
|
136
|
+
// the write). Empty/whitespace names are stored as-is; readers fall back to
|
|
137
|
+
// "Blueprint" for display (see getBlueprintTemplate).
|
|
138
|
+
export function setBlueprintName(files, blueprintPath, name) {
|
|
139
|
+
let data;
|
|
140
|
+
try {
|
|
141
|
+
data = JSON.parse(files?.[blueprintPath] ?? '');
|
|
142
|
+
} catch {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
if (!data || typeof data !== 'object') return null;
|
|
146
|
+
data.name = name;
|
|
147
|
+
return formatJson(data);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Replace a blueprint file's template components, preserving every other
|
|
151
|
+
// top-level field (name, hand-added extras). Returns the next file text, or
|
|
152
|
+
// null when the file is missing/unparseable/tombstoned (caller should treat
|
|
153
|
+
// that as "blueprint gone" and skip the write).
|
|
154
|
+
export function setBlueprintTemplateComponents(files, blueprintPath, components) {
|
|
155
|
+
let data;
|
|
156
|
+
try {
|
|
157
|
+
data = JSON.parse(files?.[blueprintPath] ?? '');
|
|
158
|
+
} catch {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
if (!data?.actors?.[0]) return null;
|
|
162
|
+
data.actors[0].components = components;
|
|
163
|
+
return formatJson(data);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Mint a brand new blueprint file descriptor ({ path, name, text }) for the
|
|
167
|
+
// given template components. Does not write the file -- callers write it
|
|
168
|
+
// (via the SDK's `writeFile`) and, for the current scene, place an instance
|
|
169
|
+
// referencing `path`.
|
|
170
|
+
export function mintBlueprintFile(files, components) {
|
|
171
|
+
const existing = listBlueprints(files);
|
|
172
|
+
const name = mintBlueprintName(existing.map((blueprint) => blueprint.name));
|
|
173
|
+
const path = mintBlueprintPath(existing.map((blueprint) => blueprint.path));
|
|
174
|
+
return { path, name, text: formatBlueprintFileText(name, components) };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Split a resolved component's props into { inherited, overridden } by each
|
|
178
|
+
// prop's propertyMeta. Used by migration and fork to decide what belongs in a
|
|
179
|
+
// new blueprint template vs. what must stay an explicit instance value.
|
|
180
|
+
function splitByInherit(Behavior, props) {
|
|
181
|
+
const inherited = {};
|
|
182
|
+
const overridden = {};
|
|
183
|
+
for (const [prop, value] of Object.entries(props ?? {})) {
|
|
184
|
+
if (isInheritedProp(Behavior, prop)) inherited[prop] = value;
|
|
185
|
+
else overridden[prop] = value;
|
|
186
|
+
}
|
|
187
|
+
return { inherited, overridden };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function findBehavior(behaviors, name) {
|
|
191
|
+
return behaviors.find((candidate) => candidate.behaviorName === name);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Apply an inspector-style partial patch (`{ propName: value, ... }`) to one
|
|
195
|
+
// actor's component, IN PLACE. For a blueprint's own template actor (no
|
|
196
|
+
// `blueprint` ref) this is a plain merge -- there's no baseline to diff
|
|
197
|
+
// against. For an instance, each patched prop is diffed against the
|
|
198
|
+
// blueprint's current value (falling back to the behavior's defaultProps):
|
|
199
|
+
// an inherited prop that becomes equal to the blueprint again drops its
|
|
200
|
+
// override (so it goes back to tracking blueprint edits); anything else is
|
|
201
|
+
// recorded as an override. Never writes a full component copy.
|
|
202
|
+
export function applyComponentPatch(files, behaviors, actor, behaviorName, nextProps) {
|
|
203
|
+
actor.components ??= {};
|
|
204
|
+
if (!actor.blueprint) {
|
|
205
|
+
actor.components[behaviorName] = { ...(actor.components[behaviorName] ?? {}), ...nextProps };
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
const template = getBlueprintTemplate(files, actor.blueprint);
|
|
209
|
+
const templateProps = template?.components?.[behaviorName] ?? {};
|
|
210
|
+
const Behavior = findBehavior(behaviors, behaviorName);
|
|
211
|
+
const defaultProps = Behavior?.defaultProps ?? {};
|
|
212
|
+
const overrides = { ...(actor.components[behaviorName] ?? {}) };
|
|
213
|
+
for (const [prop, value] of Object.entries(nextProps)) {
|
|
214
|
+
const baseline = prop in templateProps ? templateProps[prop] : defaultProps[prop];
|
|
215
|
+
if (isInheritedProp(Behavior, prop) && Object.is(value, baseline)) delete overrides[prop];
|
|
216
|
+
else overrides[prop] = value;
|
|
217
|
+
}
|
|
218
|
+
if (Object.keys(overrides).length === 0) delete actor.components[behaviorName];
|
|
219
|
+
else actor.components[behaviorName] = overrides;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Single-actor convenience wrapper around `applyComponentPatch` for the
|
|
223
|
+
// inspector: clones the scene, patches the one actor's component, returns the
|
|
224
|
+
// next scene (or the original when the actor is missing).
|
|
225
|
+
export function setInstanceComponent(files, behaviors, sceneData, actorId, behaviorName, nextProps) {
|
|
226
|
+
const next = structuredClone(sceneData);
|
|
227
|
+
const actor = next.actors?.find((candidate) => candidate.id === actorId);
|
|
228
|
+
if (!actor) return sceneData;
|
|
229
|
+
applyComponentPatch(files, behaviors, actor, behaviorName, nextProps);
|
|
230
|
+
return next;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function snapValue(value, snap) {
|
|
234
|
+
if (!snap?.enabled) return Math.round(value);
|
|
235
|
+
return Math.round(value / snap.gridSize) * snap.gridSize;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// "Add actor" = mint a new blueprint AND place its first instance in one
|
|
239
|
+
// gesture. Returns the next scene, the new instance id, and the new
|
|
240
|
+
// blueprint's { path, name, text } for the caller to write to disk.
|
|
241
|
+
// A blank 16x16 (DEFAULT_RESOLUTION) compact `.pxart`: all-transparent grid,
|
|
242
|
+
// empty palette. This is what a brand-new blueprint's sprite points at, so the
|
|
243
|
+
// "add actor" button always yields a fresh empty canvas to draw into rather
|
|
244
|
+
// than reusing whatever art happened to be first in drawings/.
|
|
245
|
+
export function blankPxArtText() {
|
|
246
|
+
const { width, height } = DEFAULT_RESOLUTION;
|
|
247
|
+
const grid = Array.from({ length: height }, () => TRANSPARENT.repeat(width));
|
|
248
|
+
return serializeCompact({ palette: {}, grid });
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// `drawings/drawing-N.pxart` -- next free numbered slug, so repeated adds don't
|
|
252
|
+
// collide and a deleted drawing's number isn't reused.
|
|
253
|
+
function mintDrawingPath(files) {
|
|
254
|
+
const re = new RegExp(`^${DRAWINGS_DIR}/drawing-(\\d+)\\.pxart$`);
|
|
255
|
+
let max = 0;
|
|
256
|
+
for (const path of Object.keys(files ?? {})) {
|
|
257
|
+
const m = path.match(re);
|
|
258
|
+
if (m) max = Math.max(max, Number(m[1]));
|
|
259
|
+
}
|
|
260
|
+
return `${DRAWINGS_DIR}/drawing-${max + 1}.pxart`;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
export function addActorWithBlueprint(sceneData, files) {
|
|
264
|
+
const width = 64;
|
|
265
|
+
const height = 64;
|
|
266
|
+
const drawingFile = { path: mintDrawingPath(files), text: blankPxArtText() };
|
|
267
|
+
const components = {
|
|
268
|
+
Layout: { width, height, z: 0 },
|
|
269
|
+
Sprite: { file: drawingFile.path },
|
|
270
|
+
};
|
|
271
|
+
const blueprintFile = mintBlueprintFile(files, components);
|
|
272
|
+
const next = structuredClone(sceneData);
|
|
273
|
+
const existingIds = new Set(next.actors.map((actor) => actor.id));
|
|
274
|
+
const newId = mintActorId(existingIds);
|
|
275
|
+
const x = Math.round((cardSize.width - width) / 2);
|
|
276
|
+
const y = Math.round((cardSize.height - height) / 2);
|
|
277
|
+
next.actors.push({ id: newId, blueprint: blueprintFile.path, components: { Layout: { x, y } } });
|
|
278
|
+
return { sceneData: next, newId, blueprintFile, drawingFile };
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Centered, grid-snapped Layout x/y for a blueprint's template at a
|
|
282
|
+
// card-space point. Shared by initial drag-in placement and the subsequent
|
|
283
|
+
// moves while the drag is still in progress, so both snap identically.
|
|
284
|
+
export function blueprintDropXY(files, blueprintPath, position, snap) {
|
|
285
|
+
const template = getBlueprintTemplate(files, blueprintPath);
|
|
286
|
+
const layout = template?.components?.Layout ?? {};
|
|
287
|
+
const width = layout.width ?? 64;
|
|
288
|
+
const height = layout.height ?? 64;
|
|
289
|
+
const dropX = position?.x ?? cardSize.width / 2;
|
|
290
|
+
const dropY = position?.y ?? cardSize.height / 2;
|
|
291
|
+
return {
|
|
292
|
+
x: snapValue(dropX - width / 2, snap),
|
|
293
|
+
y: snapValue(dropY - height / 2, snap),
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Place a new instance of an existing blueprint at a drop point (card units,
|
|
298
|
+
// pre-snap), grid-snapped and centered on the point using the template's
|
|
299
|
+
// Layout size. Only Layout x/y are written on the instance -- everything else
|
|
300
|
+
// inherits from the blueprint.
|
|
301
|
+
export function placeBlueprintInstance(sceneData, files, blueprintPath, position, snap) {
|
|
302
|
+
const next = structuredClone(sceneData);
|
|
303
|
+
const existingIds = new Set(next.actors.map((actor) => actor.id));
|
|
304
|
+
const newId = mintActorId(existingIds);
|
|
305
|
+
const { x, y } = blueprintDropXY(files, blueprintPath, position, snap);
|
|
306
|
+
next.actors.push({ id: newId, blueprint: blueprintPath, components: { Layout: { x, y } } });
|
|
307
|
+
return { sceneData: next, newId };
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// "New blueprint from this actor": mint a blueprint whose template is the
|
|
311
|
+
// instance's fully merged components, with non-inherited props reset to their
|
|
312
|
+
// behavior defaults in the TEMPLATE (a fresh blueprint has no instances yet to
|
|
313
|
+
// carry position, so its own template position is a neutral default).
|
|
314
|
+
// Reparent the instance to the new blueprint, keeping only its non-inherited
|
|
315
|
+
// prop values (Layout position et al) as instance overrides -- everything
|
|
316
|
+
// else now matches the new template exactly, so no override is needed for it.
|
|
317
|
+
export function forkActorToBlueprint(files, behaviors, sceneData, actorId) {
|
|
318
|
+
const next = structuredClone(sceneData);
|
|
319
|
+
const actor = next.actors?.find((candidate) => candidate.id === actorId);
|
|
320
|
+
if (!actor) return null;
|
|
321
|
+
const merged = resolveActorComponents(files, actor);
|
|
322
|
+
const templateComponents = {};
|
|
323
|
+
const instanceOverrides = {};
|
|
324
|
+
for (const [behaviorName, props] of Object.entries(merged)) {
|
|
325
|
+
if (!props) continue;
|
|
326
|
+
const Behavior = findBehavior(behaviors, behaviorName);
|
|
327
|
+
const { inherited, overridden } = splitByInherit(Behavior, props);
|
|
328
|
+
if (Object.keys(overridden).length > 0) instanceOverrides[behaviorName] = overridden;
|
|
329
|
+
// The new template's non-inherited props reset to the behavior's default
|
|
330
|
+
// (a fresh blueprint has no instances yet to carry a real position); its
|
|
331
|
+
// inherited props keep the actor's current (merged) values verbatim.
|
|
332
|
+
const defaultProps = Behavior?.defaultProps ?? {};
|
|
333
|
+
const resetOverridden = Object.fromEntries(
|
|
334
|
+
Object.keys(overridden).map((prop) => [prop, prop in defaultProps ? defaultProps[prop] : overridden[prop]])
|
|
335
|
+
);
|
|
336
|
+
if (Object.keys(inherited).length > 0 || Object.keys(resetOverridden).length > 0) {
|
|
337
|
+
templateComponents[behaviorName] = { ...inherited, ...resetOverridden };
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
const blueprintFile = mintBlueprintFile(files, templateComponents);
|
|
341
|
+
actor.blueprint = blueprintFile.path;
|
|
342
|
+
actor.components = instanceOverrides;
|
|
343
|
+
return { sceneData: next, blueprintFile };
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// Every `scenes/*.scene` file's parsed data, for callers that need to scan
|
|
347
|
+
// every scene in the deck (cascade delete, the instance-count badge/confirm).
|
|
348
|
+
// Skips unparseable files rather than throwing -- a mid-edit scene with
|
|
349
|
+
// invalid JSON just doesn't count towards these scans.
|
|
350
|
+
function parseSceneFiles(files) {
|
|
351
|
+
const out = [];
|
|
352
|
+
for (const [path, text] of Object.entries(files ?? {})) {
|
|
353
|
+
if (!path.startsWith('scenes/') || !path.endsWith('.scene')) continue;
|
|
354
|
+
try {
|
|
355
|
+
out.push({ path, data: JSON.parse(text) });
|
|
356
|
+
} catch {
|
|
357
|
+
// skip
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return out;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// Every `scenes/*.scene` file's actor count referencing a blueprint, for the
|
|
364
|
+
// delete-confirm dialog and the library's per-blueprint badge.
|
|
365
|
+
export function countBlueprintInstances(files, blueprintPath) {
|
|
366
|
+
let count = 0;
|
|
367
|
+
for (const { data } of parseSceneFiles(files)) {
|
|
368
|
+
for (const actor of data?.actors ?? []) {
|
|
369
|
+
if (actor?.blueprint === blueprintPath) count += 1;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
return count;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// Cascade-delete a blueprint: every instance in every scene is removed, and
|
|
376
|
+
// the blueprint file itself is tombstoned (overwritten with an empty scene --
|
|
377
|
+
// `listBlueprints` skips it and it stops resolving). Returns the list of
|
|
378
|
+
// { path, text } writes for the caller to apply (the currently open scene
|
|
379
|
+
// should go through that editor's own history/onChange so undo still works
|
|
380
|
+
// for it; every other file is a direct `writeFile`).
|
|
381
|
+
export function cascadeDeleteBlueprint(files, blueprintPath) {
|
|
382
|
+
const writes = [];
|
|
383
|
+
for (const { path, data } of parseSceneFiles(files)) {
|
|
384
|
+
const actors = data.actors ?? [];
|
|
385
|
+
const filtered = actors.filter((actor) => actor?.blueprint !== blueprintPath);
|
|
386
|
+
if (filtered.length !== actors.length) {
|
|
387
|
+
writes.push({ path, text: formatJson({ ...data, actors: filtered }) });
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
writes.push({ path: blueprintPath, text: formatJson({}) });
|
|
391
|
+
return writes;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// Auto-migrate a scene's orphan actors (no `blueprint` field -- the pre-v1
|
|
395
|
+
// shape) into blueprint instances: mint one blueprint per orphan (its
|
|
396
|
+
// authored components split by propertyMeta, same as fork), rewrite the actor
|
|
397
|
+
// to reference it with only the non-inherited props as overrides. Mechanical
|
|
398
|
+
// and safe: never touches an actor that already has a `blueprint`.
|
|
399
|
+
export function migrateOrphanActors(files, behaviors, sceneData) {
|
|
400
|
+
const next = structuredClone(sceneData);
|
|
401
|
+
const newBlueprintFiles = [];
|
|
402
|
+
let knownBlueprints = listBlueprints(files);
|
|
403
|
+
for (const actor of next.actors ?? []) {
|
|
404
|
+
if (actor.blueprint) continue;
|
|
405
|
+
const templateComponents = {};
|
|
406
|
+
const instanceOverrides = {};
|
|
407
|
+
for (const [behaviorName, props] of Object.entries(actor.components ?? {})) {
|
|
408
|
+
if (!props) continue;
|
|
409
|
+
const Behavior = findBehavior(behaviors, behaviorName);
|
|
410
|
+
const { inherited, overridden } = splitByInherit(Behavior, props);
|
|
411
|
+
if (Object.keys(inherited).length > 0) templateComponents[behaviorName] = inherited;
|
|
412
|
+
if (Object.keys(overridden).length > 0) instanceOverrides[behaviorName] = overridden;
|
|
413
|
+
}
|
|
414
|
+
const name = mintBlueprintName(knownBlueprints.map((blueprint) => blueprint.name));
|
|
415
|
+
const path = mintBlueprintPath(knownBlueprints.map((blueprint) => blueprint.path));
|
|
416
|
+
const text = formatBlueprintFileText(name, templateComponents);
|
|
417
|
+
newBlueprintFiles.push({ path, text });
|
|
418
|
+
knownBlueprints = [...knownBlueprints, { path, name, components: templateComponents }];
|
|
419
|
+
actor.blueprint = path;
|
|
420
|
+
actor.components = instanceOverrides;
|
|
421
|
+
}
|
|
422
|
+
return { sceneData: next, newBlueprintFiles };
|
|
423
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Seed the file map by scanning the deck dir. Vite module-cache is invalidated
|
|
2
2
|
// on restart, so a newly-created file just shows up on the next reload.
|
|
3
3
|
const rawModules = import.meta.glob(
|
|
4
|
-
['../scenes/*.scene', '../drawings/*.pxart', '../behaviors/*.jsx'],
|
|
4
|
+
['../scenes/*.scene', '../blueprints/*.scene', '../drawings/*.pxart', '../behaviors/*.jsx'],
|
|
5
5
|
{ query: '?raw', import: 'default', eager: true }
|
|
6
6
|
);
|
|
7
7
|
export const initialFiles = Object.fromEntries(
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { initialFiles, parseJsonFile } from './files';
|
|
2
|
+
import { getBlueprintTemplate, mergeComponents } from './blueprint';
|
|
2
3
|
|
|
3
4
|
const CARD_WIDTH = 500;
|
|
4
5
|
const CARD_HEIGHT = 700;
|
|
@@ -25,9 +26,14 @@ function resolveSceneFileKey(name) {
|
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
export class SceneRuntime {
|
|
28
|
-
|
|
29
|
+
// `files` is the live deck files map (path -> text), used to resolve every
|
|
30
|
+
// actor's `blueprint` ref against the CURRENT blueprint file content -- not
|
|
31
|
+
// a load-time snapshot -- so a blueprint edit propagates to every scene that
|
|
32
|
+
// places it, in both the editor and play mode / `PlayOnly`.
|
|
33
|
+
constructor(sceneData, behaviors, sprites, files) {
|
|
29
34
|
this.behaviors = new Map(behaviors.map((Behavior) => [Behavior.behaviorName, Behavior]));
|
|
30
35
|
this.sprites = sprites ?? {};
|
|
36
|
+
this.files = files ?? {};
|
|
31
37
|
this.time = 0;
|
|
32
38
|
this.keys = new Set();
|
|
33
39
|
this.pointer = { x: 0, y: 0, down: false };
|
|
@@ -43,6 +49,10 @@ export class SceneRuntime {
|
|
|
43
49
|
this.actors = new Map();
|
|
44
50
|
for (const actor of this.data.actors ?? []) {
|
|
45
51
|
actor.runtime = {};
|
|
52
|
+
if (actor.blueprint) {
|
|
53
|
+
const template = getBlueprintTemplate(this.files, actor.blueprint);
|
|
54
|
+
actor.components = mergeComponents(template?.components, actor.components);
|
|
55
|
+
}
|
|
46
56
|
this.actors.set(actor.id, actor);
|
|
47
57
|
for (const [behaviorName, Behavior] of this.behaviors) {
|
|
48
58
|
const component = actor.components[behaviorName];
|
|
@@ -77,7 +87,7 @@ export class SceneRuntime {
|
|
|
77
87
|
}
|
|
78
88
|
|
|
79
89
|
clone() {
|
|
80
|
-
return new SceneRuntime(this.serialize(), [...this.behaviors.values()], this.sprites);
|
|
90
|
+
return new SceneRuntime(this.serialize(), [...this.behaviors.values()], this.sprites, this.files);
|
|
81
91
|
}
|
|
82
92
|
|
|
83
93
|
serialize() {
|
|
@@ -126,6 +136,10 @@ export class SceneRuntime {
|
|
|
126
136
|
return Boolean(a && b && intersects(a, b));
|
|
127
137
|
}
|
|
128
138
|
|
|
139
|
+
// Raw spawn: `actor.components` is used as-is (already-resolved values),
|
|
140
|
+
// just filled out with each behavior's defaultProps. Internal use -- prefer
|
|
141
|
+
// `spawnFromBlueprint` so a runtime-spawned actor is a blueprint instance
|
|
142
|
+
// like every other actor.
|
|
129
143
|
spawnActor(actor) {
|
|
130
144
|
const id = actor.id ?? mintActorId(new Set(this.actors.keys()));
|
|
131
145
|
const components = {};
|
|
@@ -139,6 +153,16 @@ export class SceneRuntime {
|
|
|
139
153
|
return next;
|
|
140
154
|
}
|
|
141
155
|
|
|
156
|
+
// The blessed spawn path: resolve `blueprintPath` from the live `files`,
|
|
157
|
+
// merge with `overrides.components` (sparse, same shape as a placed
|
|
158
|
+
// instance's file overrides), and spawn the result. `overrides.id`, when
|
|
159
|
+
// given, is used as the new actor's id.
|
|
160
|
+
spawnFromBlueprint(blueprintPath, overrides = {}) {
|
|
161
|
+
const template = getBlueprintTemplate(this.files, blueprintPath);
|
|
162
|
+
const components = mergeComponents(template?.components, overrides.components);
|
|
163
|
+
return this.spawnActor({ id: overrides.id, blueprint: blueprintPath, components });
|
|
164
|
+
}
|
|
165
|
+
|
|
142
166
|
despawnActor(actorId) {
|
|
143
167
|
const actor = this.actors.get(actorId);
|
|
144
168
|
if (!actor) return false;
|
|
@@ -261,8 +285,8 @@ export class SceneRuntime {
|
|
|
261
285
|
}
|
|
262
286
|
}
|
|
263
287
|
|
|
264
|
-
export function makeScene(sceneData, behaviors, sprites) {
|
|
265
|
-
return new SceneRuntime(sceneData, behaviors, sprites);
|
|
288
|
+
export function makeScene(sceneData, behaviors, sprites, files) {
|
|
289
|
+
return new SceneRuntime(sceneData, behaviors, sprites, files);
|
|
266
290
|
}
|
|
267
291
|
|
|
268
292
|
export function setActorComponent(sceneData, actorId, behaviorName, nextProps) {
|
|
@@ -437,31 +461,7 @@ export function getSelectionBounds(sceneData, actorIds) {
|
|
|
437
461
|
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
438
462
|
}
|
|
439
463
|
|
|
440
|
-
export function
|
|
441
|
-
const next = structuredClone(sceneData);
|
|
442
|
-
const existingIds = new Set(next.actors.map((actor) => actor.id));
|
|
443
|
-
const id = mintActorId(existingIds);
|
|
444
|
-
const width = 64;
|
|
445
|
-
const height = 64;
|
|
446
|
-
const layout = {
|
|
447
|
-
x: Math.round((CARD_WIDTH - width) / 2),
|
|
448
|
-
y: Math.round((CARD_HEIGHT - height) / 2),
|
|
449
|
-
width,
|
|
450
|
-
height,
|
|
451
|
-
z: 0,
|
|
452
|
-
rotation: 0,
|
|
453
|
-
};
|
|
454
|
-
const components = { Layout: layout };
|
|
455
|
-
// Give a fresh actor the first available .pxart sprite so it shows art.
|
|
456
|
-
const spritePath = Object.keys(sprites ?? {}).sort()[0];
|
|
457
|
-
if (spritePath) {
|
|
458
|
-
components.Sprite = { file: spritePath };
|
|
459
|
-
}
|
|
460
|
-
next.actors.push({ id, components });
|
|
461
|
-
return { sceneData: next, newId: id };
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
function mintActorId(existing) {
|
|
464
|
+
export function mintActorId(existing) {
|
|
465
465
|
for (let attempt = 0; attempt < 64; attempt++) {
|
|
466
466
|
const candidate = Math.floor(Math.random() * 0xffffffff)
|
|
467
467
|
.toString(16)
|