castle-web-cli 0.4.193 → 0.4.195

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.
Files changed (101) hide show
  1. package/dist/agent-prompts.d.ts +1 -0
  2. package/dist/agent-prompts.js +14 -5
  3. package/dist/agent.js +101 -4
  4. package/dist/api.d.ts +1 -0
  5. package/dist/api.js +5 -0
  6. package/dist/bundle.js +2 -1
  7. package/dist/castleJson.d.ts +1 -2
  8. package/dist/castleJson.js +0 -39
  9. package/dist/check.js +14 -6
  10. package/dist/devSessionFrameBudget.d.ts +14 -0
  11. package/dist/devSessionFrameBudget.js +64 -0
  12. package/dist/devSessionServer.d.ts +6 -0
  13. package/dist/devSessionServer.js +38 -1
  14. package/dist/editorConfig.d.ts +1 -1
  15. package/dist/editorConfig.js +12 -4
  16. package/dist/filesChanged.js +7 -3
  17. package/dist/ide.js +28 -59
  18. package/dist/importUpdateHook.d.ts +31 -0
  19. package/dist/importUpdateHook.js +135 -0
  20. package/dist/importUpdateHookRunner.d.ts +2 -0
  21. package/dist/importUpdateHookRunner.js +24 -0
  22. package/dist/imports.js +58 -10
  23. package/dist/index.js +58 -7
  24. package/dist/init.d.ts +1 -0
  25. package/dist/init.js +15 -9
  26. package/dist/multiplayerDoc.d.ts +1 -0
  27. package/dist/multiplayerDoc.js +228 -0
  28. package/dist/native/loop.js +29 -9
  29. package/dist/platformDoc.d.ts +1 -0
  30. package/dist/platformDoc.js +25 -6
  31. package/dist/remix.js +4 -1
  32. package/dist/scriptErrors.d.ts +23 -0
  33. package/dist/scriptErrors.js +147 -0
  34. package/dist/serve.js +366 -36
  35. package/dist/serverBundle.js +1 -1
  36. package/dist/shell/assets/index-kMH1qj6D.js +449 -0
  37. package/dist/shell/index.html +1 -1
  38. package/dist/versions.d.ts +14 -0
  39. package/dist/versions.js +21 -0
  40. package/dist/viteClientStub.d.ts +18 -0
  41. package/dist/viteClientStub.js +193 -0
  42. package/dist/vitePlugins.d.ts +1 -0
  43. package/dist/vitePlugins.js +18 -0
  44. package/kits/base/CLAUDE.md +3 -3
  45. package/kits/base/castle.json +1 -3
  46. package/kits/base/sdk/README.md +17 -0
  47. package/kits/multiplayer-2d/CLAUDE.md +9 -2
  48. package/kits/multiplayer-2d/blueprints/crate.scene +32 -4
  49. package/kits/multiplayer-2d/blueprints/other-player.scene +31 -4
  50. package/kits/multiplayer-2d/blueprints/player.scene +38 -6
  51. package/kits/multiplayer-2d/blueprints/wall.scene +25 -3
  52. package/kits/multiplayer-2d/castle.json +3 -12
  53. package/kits/multiplayer-2d/test/two-client-test.mjs +4 -4
  54. package/kits/multiplayer-3d/CLAUDE.md +5 -2
  55. package/kits/multiplayer-3d/castle.json +3 -11
  56. package/kits/multiplayer-3d/scenes/main.scene +128 -0
  57. package/kits/physics-2d/CLAUDE.md +16 -11
  58. package/kits/physics-2d/behaviors/Camera.jsx +5 -6
  59. package/kits/physics-2d/behaviors/Goal.jsx +5 -2
  60. package/kits/physics-2d/behaviors/Joints.jsx +4 -3
  61. package/kits/physics-2d/behaviors/Layout.jsx +120 -3
  62. package/kits/physics-2d/behaviors/Sprite.jsx +9 -9
  63. package/kits/physics-2d/behaviors/Style.jsx +22 -15
  64. package/kits/physics-2d/behaviors/Text.jsx +7 -6
  65. package/kits/physics-2d/blueprints/ball.scene +27 -5
  66. package/kits/physics-2d/blueprints/block.scene +16 -3
  67. package/kits/physics-2d/blueprints/cauldron.scene +3 -1
  68. package/kits/physics-2d/blueprints/crate.scene +23 -4
  69. package/kits/physics-2d/blueprints/goal.scene +12 -2
  70. package/kits/physics-2d/blueprints/text.scene +13 -3
  71. package/kits/physics-2d/castle.json +2 -11
  72. package/kits/physics-2d/editors/SceneEditor.jsx +68 -5
  73. package/kits/physics-2d/editors/SelectionOverlay.jsx +27 -20
  74. package/kits/physics-2d/editors/behaviorRegistry.js +8 -2
  75. package/kits/physics-2d/engine/ErrorOverlay.jsx +69 -0
  76. package/kits/physics-2d/engine/ScenePlayer.jsx +2 -67
  77. package/kits/physics-2d/engine/blueprint.js +111 -54
  78. package/kits/physics-2d/engine/blueprintCore.js +59 -0
  79. package/kits/physics-2d/engine/collider.js +28 -17
  80. package/kits/physics-2d/engine/layout.js +61 -0
  81. package/kits/physics-2d/engine/layoutOriginMigration.js +223 -0
  82. package/kits/physics-2d/engine/liveReload.js +5 -4
  83. package/kits/physics-2d/engine/physics/PhysicsSystem.js +11 -7
  84. package/kits/physics-2d/engine/physics/controls.js +4 -8
  85. package/kits/physics-2d/engine/physics/matterBridge.js +13 -8
  86. package/kits/physics-2d/engine/scene.js +38 -34
  87. package/kits/physics-2d/engine/spriteGeometry.js +17 -9
  88. package/kits/physics-2d/engine/tap.js +4 -3
  89. package/kits/physics-2d/scenes/main.scene +59 -6
  90. package/kits/physics-2d/scenes/sandbox.scene +69 -7
  91. package/kits/physics-2d/scripts/onUpdate.mjs +199 -0
  92. package/kits/physics-3d/castle.json +0 -11
  93. package/kits/real-time/castle.json +2 -8
  94. package/kits/real-time/code/server/persist.js +14 -0
  95. package/kits/turn-based/CLAUDE.md +18 -10
  96. package/kits/turn-based/blueprints/board.scene +3 -1
  97. package/kits/turn-based/blueprints/square.scene +3 -1
  98. package/kits/turn-based/castle.json +3 -14
  99. package/kits/turn-based/code/behaviors/Board.jsx +33 -21
  100. package/package.json +1 -1
  101. package/dist/shell/assets/index-ColHKJYj.js +0 -449
@@ -14,9 +14,15 @@
14
14
  // side (keeps circles round under non-uniform box scaling). Legacy single-shape
15
15
  // colliders (shape/width/height/radius/offset[/mode]) normalize to one box shape.
16
16
  //
17
+ // The box itself is `layoutRect(layout)` -- Layout's own `originX/originY` decides
18
+ // where `layout.x/y` sits in it (see engine/layout.js), and these fractions are
19
+ // measured from that box's top-left either way. So a shape list is unaffected by
20
+ // an origin change, exactly as it is by a resize.
21
+ //
17
22
  // Shapes are in the UNROTATED box frame; the draw pipeline (scene.js) and the
18
23
  // matter body (matterBridge) apply Layout.rotation, so nothing here rotates.
19
24
  import { artFrameCount, artReady, renderArtFrame } from './art';
25
+ import { layoutRect } from './layout';
20
26
  import { spriteDestRect } from './spriteGeometry';
21
27
 
22
28
  // ---------------------------------------------------------------------------
@@ -75,18 +81,19 @@ export function spriteIsEmpty(sprite) {
75
81
  }
76
82
 
77
83
  // The sprite's opaque-pixel rect in world units through the draw mode, or null.
78
- function autoRect(layout, spriteProps, sprite) {
84
+ // `box` is the actor's Layout box (origin already applied).
85
+ function autoRect(box, spriteProps, sprite) {
79
86
  if (!sprite || spriteProps.mode === 'tile') return null;
80
87
  const bounds = opaqueBoundsFraction(sprite);
81
88
  if (!bounds) return null;
82
- const dest = spriteDestRect(spriteProps.mode, layout, sprite.resolution);
89
+ const dest = spriteDestRect(spriteProps.mode, box, sprite.resolution);
83
90
  const rect = {
84
91
  x: dest.x + bounds.x * dest.width,
85
92
  y: dest.y + bounds.y * dest.height,
86
93
  width: bounds.width * dest.width,
87
94
  height: bounds.height * dest.height,
88
95
  };
89
- if (spriteProps.mode === 'cover') return intersectRect(rect, layout);
96
+ if (spriteProps.mode === 'cover') return intersectRect(rect, box);
90
97
  return rect;
91
98
  }
92
99
 
@@ -129,11 +136,13 @@ function legacyShapeFraction(collider, layout) {
129
136
  }
130
137
 
131
138
  // Resolve one box-fraction shape to world coordinates (unrotated box frame).
132
- function resolveShape(shape, layout) {
133
- const bx = layout.x;
134
- const by = layout.y;
135
- const bw = layout.width;
136
- const bh = layout.height;
139
+ // `box` is the actor's Layout box from `layoutRect` -- the origin is applied
140
+ // there, so everything here is measured from a real top-left.
141
+ function resolveShape(shape, box) {
142
+ const bx = box.x;
143
+ const by = box.y;
144
+ const bw = box.width;
145
+ const bh = box.height;
137
146
  const wx = (fx) => bx + fx * bw;
138
147
  const wy = (fy) => by + fy * bh;
139
148
  if (shape.type === 'circle') {
@@ -157,7 +166,8 @@ export function getColliderShapes(actor) {
157
166
  const layout = actor?.components?.Layout;
158
167
  const collider = actor?.components?.Collider;
159
168
  if (!layout || !collider) return null;
160
- return colliderShapeFractions(collider, layout).map((s) => resolveShape(s, layout));
169
+ const box = layoutRect(layout);
170
+ return colliderShapeFractions(collider, layout).map((s) => resolveShape(s, box));
161
171
  }
162
172
 
163
173
  // Back-compat single-shape accessor (the first shape).
@@ -220,16 +230,17 @@ export function getColliderRect(actor) {
220
230
  // draw mode, or null when there's nothing to fit to. `asCircle` returns a circle
221
231
  // that encloses those bounds. Powers the inspector's "Auto-fit to sprite".
222
232
  export function computeAutoFit(actor, sprites, asCircle = false) {
223
- const rawLayout = actor?.components?.Layout;
224
233
  const spriteProps = actor?.components?.Sprite;
225
- if (!rawLayout || !spriteProps) return null;
226
- const layout = { ...rawLayout, x: rawLayout.x ?? 0, y: rawLayout.y ?? 0 };
227
- const rect = autoRect(layout, spriteProps, sprites?.[spriteProps.file]);
234
+ const box = layoutRect(actor?.components?.Layout);
235
+ if (!box || !spriteProps || !(box.width > 0) || !(box.height > 0)) return null;
236
+ const rect = autoRect(box, spriteProps, sprites?.[spriteProps.file]);
228
237
  if (!rect || rect.width <= 0 || rect.height <= 0) return null;
229
- const x = (rect.x + rect.width / 2 - layout.x) / layout.width;
230
- const y = (rect.y + rect.height / 2 - layout.y) / layout.height;
231
- const w = rect.width / layout.width;
232
- const h = rect.height / layout.height;
238
+ // Back to box fractions -- the inverse of `resolveShape`, so the fit survives
239
+ // a later resize or origin change like any other shape.
240
+ const x = (rect.x + rect.width / 2 - box.x) / box.width;
241
+ const y = (rect.y + rect.height / 2 - box.y) / box.height;
242
+ const w = rect.width / box.width;
243
+ const h = rect.height / box.height;
233
244
  if (asCircle) return { type: 'circle', x, y, r: Math.max(w, h) / 2 };
234
245
  return { type: 'box', x, y, w, h };
235
246
  }
@@ -0,0 +1,61 @@
1
+ // The one place the Layout origin is applied.
2
+ //
3
+ // `Layout.x/y` is not the box's top-left: it is the point of the box named by
4
+ // `originX`/`originY`, stored as FRACTIONS of the box from its own top-left --
5
+ // `0,0` top-left, `0.5,0.5` center, `1,1` bottom-right, not clamped (the same
6
+ // model as Collider shape fractions, so an origin survives a resize and a
7
+ // per-instance size override). Absent means CENTER (`DEFAULT_ORIGIN_*` below) --
8
+ // the same value `Layout.defaultProps` and editor-minted blueprints write.
9
+ //
10
+ // `rotation` is unaffected: it still pivots on the box CENTER. The origin
11
+ // anchors position only.
12
+ //
13
+ // Engine-internal, like `engine/physics/controls.js` -- behaviors import it,
14
+ // deck code goes through `scene.layoutRect(actorOrId)`.
15
+
16
+ // Single source of truth for "no origin written". Every reader goes through
17
+ // `layoutOriginX` / `layoutOriginY` (or these constants) -- never a bare `?? 0`.
18
+ export const DEFAULT_ORIGIN_X = 0.5;
19
+ export const DEFAULT_ORIGIN_Y = 0.5;
20
+
21
+ export function layoutOriginX(layout) {
22
+ return layout?.originX ?? DEFAULT_ORIGIN_X;
23
+ }
24
+
25
+ export function layoutOriginY(layout) {
26
+ return layout?.originY ?? DEFAULT_ORIGIN_Y;
27
+ }
28
+
29
+ // The unrotated axis-aligned box an actor occupies, or null with no Layout.
30
+ export function layoutRect(layout) {
31
+ if (!layout) return null;
32
+ const width = layout.width ?? 0;
33
+ const height = layout.height ?? 0;
34
+ return {
35
+ x: (layout.x ?? 0) - layoutOriginX(layout) * width,
36
+ y: (layout.y ?? 0) - layoutOriginY(layout) * height,
37
+ width,
38
+ height,
39
+ };
40
+ }
41
+
42
+ // Center of the Layout box -- the rotation pivot, and where a matter body and
43
+ // the joint overlay anchor.
44
+ export function layoutCenter(layout) {
45
+ const rect = layoutRect(layout);
46
+ if (!rect) return null;
47
+ return { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 };
48
+ }
49
+
50
+ // Inverse of `layoutRect`: the `x`/`y` to store so the box's top-left lands at
51
+ // `rect.x`/`rect.y` under this origin. `rect`'s own size wins when it has one,
52
+ // so a gesture that resizes and repositions in one step (scale handles) can
53
+ // hand over the whole next rect.
54
+ export function layoutXYForRect(rect, layout) {
55
+ const width = rect?.width ?? layout?.width ?? 0;
56
+ const height = rect?.height ?? layout?.height ?? 0;
57
+ return {
58
+ x: (rect?.x ?? 0) + layoutOriginX(layout) * width,
59
+ y: (rect?.y ?? 0) + layoutOriginY(layout) * height,
60
+ };
61
+ }
@@ -0,0 +1,223 @@
1
+ // Stamping `originX: 0, originY: 0` into a deck authored before the Layout
2
+ // origin defaulted to CENTER.
3
+ //
4
+ // `Layout.originX/originY` say which point of the box `x`/`y` names, and an
5
+ // absent origin now means the center (`engine/layout.js`). Every actor in a deck
6
+ // written before that change omits the origin and means the TOP-LEFT, so the new
7
+ // engine would move all of them by half their size.
8
+ //
9
+ // The migration writes the old meaning down. It is deliberately NOT a conversion
10
+ // to center: deck code reads `Layout.x` as the top-left (clamps, bounce tests,
11
+ // `x + width / 2`) and rewriting that is not something a migration can do. So
12
+ // nothing moves, every deck keeps rendering exactly as it did, and a creator
13
+ // converts a blueprint to center when they want to, from the inspector's Origin
14
+ // select (`setBlueprintOriginCascade`, which compensates the instances).
15
+ //
16
+ // Pure: `stampTopLeftOrigin` takes a file map and hands back the writes.
17
+ // `scripts/onUpdate.mjs` is the side that reads and writes the disk, and it is
18
+ // what the CLI runs as this kit's `onUpdate` hook.
19
+ //
20
+ // Known residual: a `scene.spawnActor({ components: { Layout: {...} } })` in
21
+ // deck code has no file to stamp and takes the center default at runtime. There
22
+ // is no file to fix, so the hook only points at them.
23
+ import { isBlueprintPath, parseBlueprintText } from './blueprintCore.js';
24
+
25
+ // The last kit version published with a top-left default -- the kit's
26
+ // `publishedVersion` as committed on `main` before the center-origin release.
27
+ // RE-CONFIRM THIS AT RELEASE: it is the whole gate, and a value from the wrong
28
+ // side of the release either misses every old deck or stamps top-left onto data
29
+ // somebody authored as center. See `.claude/skills/release/SKILL.md`.
30
+ export const LAST_TOP_LEFT_PUBLISHED_VERSION = '2026-09-13T17:33:26.446Z';
31
+
32
+ // Kit versions are the server's `updatedAt`, so a plain string compare orders
33
+ // them -- but only for a real ISO timestamp, which is why the shape is checked
34
+ // rather than assumed.
35
+ const ISO_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z$/;
36
+
37
+ function isIsoVersion(value) {
38
+ return typeof value === 'string' && ISO_RE.test(value);
39
+ }
40
+
41
+ /**
42
+ * Whether this particular update is the one that changes what an absent origin
43
+ * means for this deck.
44
+ *
45
+ * The gate is on `from` and it is load-bearing: an omitted origin is ambiguous
46
+ * on disk -- in an old deck it means top-left, in a new one it means center on
47
+ * purpose -- so the pin the deck was sitting at is the only thing that can tell
48
+ * them apart. A deck already on a post-release kit is never touched, and neither
49
+ * is a deck moving between two pre-release versions.
50
+ */
51
+ export function needsTopLeftStamp({ from, to } = {}) {
52
+ if (!isIsoVersion(from) || !isIsoVersion(to)) return false;
53
+ return from <= LAST_TOP_LEFT_PUBLISHED_VERSION && to > LAST_TOP_LEFT_PUBLISHED_VERSION;
54
+ }
55
+
56
+ // An 8-line mirror of the SDK's `resolveDeckFile` / `deckPrefixOf`
57
+ // (`sdk/src/files.ts`), which stays canonical -- this module has to be readable
58
+ // by plain Node, and the SDK is a browser package. Change it there first.
59
+ const IMPORTS_PREFIX = '@imports/';
60
+ const IMPORTS_DIR = 'imports';
61
+
62
+ function deckPrefixOf(filePath) {
63
+ const match = new RegExp(`^(${IMPORTS_DIR}/[^/]+/)`).exec(filePath ?? '');
64
+ return match ? match[1] : '';
65
+ }
66
+
67
+ function resolveRef(ref, from) {
68
+ const raw = String(ref ?? '').replace(/^\.\//, '');
69
+ if (raw.startsWith(IMPORTS_PREFIX)) return `${IMPORTS_DIR}/${raw.slice(IMPORTS_PREFIX.length)}`;
70
+ return deckPrefixOf(String(from ?? '').replace(/^\.\//, '')) + raw;
71
+ }
72
+
73
+ // Whether the deck may write this file at all. Everything under `imports/`
74
+ // belongs to the deck it came from -- unless this deck forked it, in which case
75
+ // the deck's own `forks/<alias>/<inner>` is what gets written and the imported
76
+ // path is only where the text is keyed.
77
+ function isOwned(path, forks) {
78
+ return !path.startsWith(`${IMPORTS_DIR}/`) || forks[path] !== undefined;
79
+ }
80
+
81
+ function writePathFor(path, forks) {
82
+ return forks[path] ?? path;
83
+ }
84
+
85
+ // One axis at a time, and only where nothing is written: an instance that
86
+ // already states `originX` means it, and its own axis stays whatever it says.
87
+ // Returns whether anything was added.
88
+ function stampLayout(layout) {
89
+ let stamped = false;
90
+ if (layout.originX === undefined) {
91
+ layout.originX = 0;
92
+ stamped = true;
93
+ }
94
+ if (layout.originY === undefined) {
95
+ layout.originY = 0;
96
+ stamped = true;
97
+ }
98
+ return stamped;
99
+ }
100
+
101
+ // A blueprint template: the origin is inherited, so stamping it here is what
102
+ // covers every instance that doesn't state one. A template with no `Layout` gets
103
+ // nothing -- there is no box to anchor.
104
+ function stampTemplate(data) {
105
+ const layout = data?.actors?.[0]?.components?.Layout;
106
+ if (!layout || typeof layout !== 'object') return false;
107
+ return stampLayout(layout);
108
+ }
109
+
110
+ // Whether an actor's origin comes from a template this migration is also
111
+ // stamping. That is the only case an instance can be left alone in: it inherits
112
+ // the `0` about to be written on the template, and a one-axis override of its
113
+ // own keeps that axis.
114
+ function inheritsStamp(actor, scenePath, files, forks) {
115
+ if (typeof actor?.blueprint !== 'string' || !actor.blueprint) return false;
116
+ const key = resolveRef(actor.blueprint, scenePath);
117
+ if (!isOwned(key, forks) || !isBlueprintPath(key)) return false;
118
+ // Dangling or tombstoned: nothing will be stamped there, so the instance's own
119
+ // Layout is all the origin it has.
120
+ return parseBlueprintText(files[key]) !== null;
121
+ }
122
+
123
+ // Every actor in one scene. An actor that inherits from a stamped template needs
124
+ // nothing; anything else -- an orphan (the pre-blueprint shape), an instance of
125
+ // an import's blueprint, an instance whose template is gone -- carries its own
126
+ // origin and gets it written down.
127
+ //
128
+ // An instance of an import's template may have no `Layout` at all, because it
129
+ // inherited every field of it. It gets one: the origin is the one thing about
130
+ // that box this deck now has to state, and it cannot state it on a file it
131
+ // doesn't own.
132
+ function stampSceneActors(data, scenePath, files, forks, stats) {
133
+ let stamped = false;
134
+ for (const actor of data?.actors ?? []) {
135
+ if (!actor || typeof actor !== 'object') continue;
136
+ if (inheritsStamp(actor, scenePath, files, forks)) continue;
137
+ const orphan = !actor.blueprint;
138
+ const layout = actor.components?.Layout;
139
+ // An orphan with no Layout has no box; an instance with no Layout inherits
140
+ // one from a template this migration cannot write.
141
+ if (orphan && (!layout || typeof layout !== 'object')) continue;
142
+ if (!layout || typeof layout !== 'object') {
143
+ actor.components = { ...(actor.components ?? {}), Layout: { originX: 0, originY: 0 } };
144
+ stats.instances += 1;
145
+ stamped = true;
146
+ continue;
147
+ }
148
+ if (!stampLayout(layout)) continue;
149
+ if (orphan) stats.orphans += 1;
150
+ else stats.instances += 1;
151
+ stamped = true;
152
+ }
153
+ return stamped;
154
+ }
155
+
156
+ // Re-serialize `data` the way `text` was written, so a migration shows up as
157
+ // added `originX`/`originY` lines and nothing else. A compact one-line scene
158
+ // stays one line; anything else keeps the indent it used and its trailing
159
+ // newline. Key order comes free from the JSON round trip -- the two new keys
160
+ // land at the end of the Layout object.
161
+ function reserializeLike(text, data) {
162
+ if (!text.includes('\n')) return JSON.stringify(data);
163
+ const indentMatch = /\n([ \t]+)/.exec(text);
164
+ const indent = indentMatch ? (indentMatch[1][0] === '\t' ? '\t' : indentMatch[1].length) : 2;
165
+ return JSON.stringify(data, null, indent) + (text.endsWith('\n') ? '\n' : '');
166
+ }
167
+
168
+ function emptyStats() {
169
+ return {
170
+ /** Deck-owned `.scene` files looked at. */
171
+ considered: 0,
172
+ /** Blueprint templates stamped. */
173
+ templates: 0,
174
+ /** Instance Layouts stamped or created. */
175
+ instances: 0,
176
+ /** Orphan-actor Layouts stamped. */
177
+ orphans: 0,
178
+ /** Paths under `imports/` this deck has not forked, so never written. */
179
+ skippedImported: [],
180
+ /** Paths whose JSON did not parse, so never written. */
181
+ unparseable: [],
182
+ };
183
+ }
184
+
185
+ /**
186
+ * The writes that state a pre-origin deck's top-left anchoring outright.
187
+ *
188
+ * @param files every `.scene` in the deck as whole-deck key -> text, INCLUDING
189
+ * the ones under `imports/` (a template has to be readable to know
190
+ * whether an instance inherits from it). A forked file's text is
191
+ * keyed at the imported path it stands in for.
192
+ * @param forks imported path -> the `forks/<alias>/<inner>` path that overlays
193
+ * it, for the ones this deck has forked.
194
+ * @returns `{ writes: [{ path, text }], stats }`. Pure -- nothing is read from
195
+ * or written to disk here.
196
+ */
197
+ export function stampTopLeftOrigin(files, { forks = {} } = {}) {
198
+ const writes = [];
199
+ const stats = emptyStats();
200
+ for (const path of Object.keys(files ?? {}).sort()) {
201
+ if (!path.endsWith('.scene')) continue;
202
+ if (!isOwned(path, forks)) {
203
+ stats.skippedImported.push(path);
204
+ continue;
205
+ }
206
+ let data;
207
+ try {
208
+ data = JSON.parse(files[path]);
209
+ } catch {
210
+ stats.unparseable.push(path);
211
+ continue;
212
+ }
213
+ stats.considered += 1;
214
+ const blueprint = isBlueprintPath(path);
215
+ const changed = blueprint
216
+ ? stampTemplate(data)
217
+ : stampSceneActors(data, path, files, forks, stats);
218
+ if (!changed) continue;
219
+ if (blueprint) stats.templates += 1;
220
+ writes.push({ path: writePathFor(path, forks), text: reserializeLike(files[path], data) });
221
+ }
222
+ return { writes, stats };
223
+ }
@@ -114,10 +114,11 @@ export async function hydrateDeclaredFiles({ knownExts, knownFiles, ensurePath }
114
114
  })
115
115
  );
116
116
  const extra = Object.fromEntries(entries.filter(Boolean));
117
- // `list` is the deck's CURATED listing, so a declared file the deck left out
118
- // of visiblePaths is not in it. The shell can still open that file by path,
119
- // and an editor handed '' for it would be back to the original bug -- so make
120
- // sure whatever was actually opened is present, curated or not.
117
+ // `list` leaves out whatever the deck hides from its Files tree
118
+ // (`editor.hiddenPaths`), so a declared file named there is not in it. The
119
+ // shell can still open that file by path, and an editor handed '' for it would
120
+ // be back to the original bug -- so make sure whatever was actually opened is
121
+ // present, listed or not.
121
122
  if (ensurePath && knownFiles[ensurePath] === undefined && extra[ensurePath] === undefined) {
122
123
  const one = await readDeclaredFile(ensurePath);
123
124
  if (one !== null) extra[ensurePath] = one;
@@ -11,6 +11,7 @@ import { isBehaviorEnabled } from '../scene';
11
11
 
12
12
  import Matter from 'matter-js';
13
13
  import { getColliderRect } from '../collider';
14
+ import { layoutCenter } from '../layout';
14
15
  import { buildJointConstraints, jointSignature, patchJoint } from './joints';
15
16
  import {
16
17
  DEFAULT_GRAVITY,
@@ -187,6 +188,10 @@ export class PhysicsSystem {
187
188
  const body = createBody(actor, rect);
188
189
  const center = rectCenter(rect);
189
190
  const layout = actor.components.Layout;
191
+ // A pure delta from the stored `x`/`y` to the collider AABB center, so the
192
+ // origin needs no mention here: the rect already had it applied, and
193
+ // `writeBack` subtracts the same delta again. `bodySignature` carries the
194
+ // origin so an origin edit mid-play rebuilds this cache.
190
195
  const offset = { dx: center.x - layout.x, dy: center.y - layout.y };
191
196
  // Cache what physics last wrote so a later Layout write can be detected
192
197
  // with !== (raw values: an omitted rotation stays undefined).
@@ -497,7 +502,10 @@ export class PhysicsSystem {
497
502
  const layout = this.layoutOf(actor);
498
503
  if (layout) layout.rotation = deg;
499
504
  },
500
- // Layout top-left convention (not body center).
505
+ // Layout convention (not body center): `x`/`y` are the point of the box
506
+ // named by `originX`/`originY` -- the box center when absent (see
507
+ // DEFAULT_ORIGIN_* in engine/layout.js). `scene.layoutRect(actor)` reads
508
+ // the box back.
501
509
  setPosition: (actor, { x, y }) => {
502
510
  const layout = this.layoutOf(actor);
503
511
  if (!layout) return;
@@ -541,11 +549,11 @@ export class PhysicsSystem {
541
549
  const joints = actor.components.Joints;
542
550
  const list = isBehaviorEnabled(joints) ? joints?.list : null;
543
551
  if (!Array.isArray(list)) continue;
544
- const a = centerOfLayout(actor.components.Layout);
552
+ const a = layoutCenter(actor.components.Layout);
545
553
  for (const joint of list) {
546
554
  const target = joint?.target && scene.getActor(joint.target);
547
555
  if (!target || !a) continue;
548
- const b = centerOfLayout(target.components.Layout);
556
+ const b = layoutCenter(target.components.Layout);
549
557
  if (!b) continue;
550
558
  out.push({
551
559
  ownerId: actor.id,
@@ -562,10 +570,6 @@ export class PhysicsSystem {
562
570
  }
563
571
  }
564
572
 
565
- function centerOfLayout(L) {
566
- return L ? { x: L.x + (L.width ?? 0) / 2, y: L.y + (L.height ?? 0) / 2 } : null;
567
- }
568
-
569
573
  function pairKey(a, b) {
570
574
  return a < b ? `${a}|${b}` : `${b}|${a}`;
571
575
  }
@@ -5,6 +5,7 @@
5
5
  // through scene.physics. (Controls are touch/pointer-first; keyboard, when
6
6
  // added, should only DUPLICATE an on-screen control, never be the only input.)
7
7
  // Stick force-mode math lives in AnalogStick.jsx — it has no other consumer.
8
+ import { layoutCenter } from '../layout';
8
9
 
9
10
  // Authored Draggable `stiffness` (0..1, 0 = floppy .. 1 = rigid) -> the matter
10
11
  // constraint values that hold the object. Geometric, like joints.js's rope map
@@ -21,10 +22,6 @@ export function dragHold(stiffness) {
21
22
  return { stiffness: 0.001 * Math.pow(400, s), damping: 0.02 + 0.25 * s };
22
23
  }
23
24
 
24
- export function centerOf(layout) {
25
- return { x: layout.x + layout.width / 2, y: layout.y + layout.height / 2 };
26
- }
27
-
28
25
  export function length(v) {
29
26
  return Math.hypot(v.x, v.y);
30
27
  }
@@ -81,11 +78,10 @@ export function inActorWorldSpace(ctx, layout, draw) {
81
78
  const rot = layout?.rotation ?? 0;
82
79
  ctx.save();
83
80
  if (rot) {
84
- const cx = layout.x + layout.width / 2;
85
- const cy = layout.y + layout.height / 2;
86
- ctx.translate(cx, cy);
81
+ const center = layoutCenter(layout);
82
+ ctx.translate(center.x, center.y);
87
83
  ctx.rotate((-rot * Math.PI) / 180);
88
- ctx.translate(-cx, -cy);
84
+ ctx.translate(-center.x, -center.y);
89
85
  }
90
86
  draw(ctx);
91
87
  ctx.restore();
@@ -1,3 +1,6 @@
1
+ import Matter from 'matter-js';
2
+ import { getColliderShapes, pointsBounds } from '../collider';
3
+ import { layoutOriginX, layoutOriginY } from '../layout';
1
4
  import { isBehaviorEnabled } from '../scene';
2
5
  // Pure helpers bridging Cauldron actors (Layout + Collider + RigidBody
3
6
  // components) to matter-js bodies. No engine state lives here -- the
@@ -6,19 +9,19 @@ import { isBehaviorEnabled } from '../scene';
6
9
  // collider geometry, including sprite-fit `auto` mode).
7
10
  //
8
11
  // Coordinate systems:
9
- // - Cauldron Layout: top-left origin, +y down, `rotation` in degrees CW.
12
+ // - Cauldron Layout: `x`/`y` is the box point named by `originX`/`originY`
13
+ // (center when absent -- see DEFAULT_ORIGIN_* in engine/layout.js), +y down,
14
+ // `rotation` in degrees CW about the box CENTER.
10
15
  // - matter-js Body: centroid origin, +y down, `angle` in radians.
11
- // Rotation converts by DEG<->RAD; position via the body's cached center-offset
12
- // from Layout's top-left (see PhysicsSystem).
16
+ // Rotation converts by DEG<->RAD; position via the body's cached offset from
17
+ // Layout's `x`/`y` to the collider AABB center, which folds the origin in (see
18
+ // PhysicsSystem.addBody).
13
19
  //
14
20
  // matter-js unit notes (see Body defaults): `frictionAir` is linear damping,
15
21
  // `restitution` is bounciness, `friction` is contact friction. There is no
16
22
  // native per-body gravity multiplier and no kinematic body type -- both are
17
23
  // emulated here (applyWorldGravity, and kinematic == static-moved-from-Layout).
18
24
 
19
- import Matter from 'matter-js';
20
- import { getColliderShapes, pointsBounds } from '../collider';
21
-
22
25
  export const DEG_TO_RAD = Math.PI / 180;
23
26
  export const RAD_TO_DEG = 180 / Math.PI;
24
27
 
@@ -92,8 +95,10 @@ function shapeToPart(s) {
92
95
  // Structural signature: when this changes we rebuild the body (shape set / sizes
93
96
  // / type) rather than live-patching material props in place. Resolved sizes are
94
97
  // included, so resizing the Layout box (which rescales every fraction shape)
95
- // triggers a rebuild.
98
+ // triggers a rebuild -- and so is the Layout origin, which changes the body's
99
+ // cached offset from `layout.x/y` without changing any shape's size.
96
100
  export function bodySignature(actor) {
101
+ const layout = actor.components?.Layout;
97
102
  const shapes = getColliderShapes(actor) ?? [];
98
103
  const sig = shapes
99
104
  .map((s) =>
@@ -104,7 +109,7 @@ export function bodySignature(actor) {
104
109
  : `${s.type[0]}${(s.points ?? []).length}`
105
110
  )
106
111
  .join(',');
107
- return `${sig}|${rigidBodyType(actor)}`;
112
+ return `${sig}|${rigidBodyType(actor)}|${layoutOriginX(layout)},${layoutOriginY(layout)}`;
108
113
  }
109
114
 
110
115
  // Build a fresh matter body -- a single part, or a compound of all collider