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
@@ -1,3 +1,35 @@
1
+ import React from 'react';
2
+ import {
3
+ DEFAULT_ORIGIN_X,
4
+ DEFAULT_ORIGIN_Y,
5
+ layoutOriginX,
6
+ layoutOriginY,
7
+ layoutRect,
8
+ } from '../engine/layout';
9
+ import { Panel, SelectField } from '../engine/ui';
10
+ import { AutoFields } from '../engine/autoInspector';
11
+
12
+ // Origin picker: the two common anchors. Custom fractions still work in data;
13
+ // SelectField prepends an off-list value so it stays visible if one shows up.
14
+ const ORIGIN_PRESETS = [
15
+ {
16
+ value: `${DEFAULT_ORIGIN_X},${DEFAULT_ORIGIN_Y}`,
17
+ label: 'Center',
18
+ x: DEFAULT_ORIGIN_X,
19
+ y: DEFAULT_ORIGIN_Y,
20
+ },
21
+ { value: '0,0', label: 'Top left', x: 0, y: 0 },
22
+ ];
23
+
24
+ function originKey(x, y) {
25
+ return `${x},${y}`;
26
+ }
27
+
28
+ function findPreset(x, y) {
29
+ const key = originKey(x, y);
30
+ return ORIGIN_PRESETS.find((preset) => preset.value === key) ?? null;
31
+ }
32
+
1
33
  export class Layout {
2
34
  static behaviorName = 'Layout';
3
35
 
@@ -8,6 +40,13 @@ export class Layout {
8
40
  rotation: 0,
9
41
  width: 50,
10
42
  height: 50,
43
+ // Which point of the box `x`/`y` names, as a fraction of the box from its
44
+ // own top-left: `0, 0` top-left, `0.5, 0.5` center (the default, and what
45
+ // an absent origin means -- see DEFAULT_ORIGIN_* in engine/layout.js),
46
+ // `1, 1` bottom-right. Not clamped. Rotation still pivots on the box
47
+ // CENTER -- the origin anchors position only.
48
+ originX: DEFAULT_ORIGIN_X,
49
+ originY: DEFAULT_ORIGIN_Y,
11
50
  // Show the Layout box (the invisible frame the sprite fills and the collider
12
51
  // derives from) as a dashed outline, in editor and play. Per-actor toggle.
13
52
  debug: false,
@@ -25,7 +64,10 @@ export class Layout {
25
64
  // on spawn), and a blueprint edit to its own template x/y/z/rotation only
26
65
  // affects newly placed instances, never moves ones already placed. Because
27
66
  // they're always instance-local, they're also never surfaced as blueprint
28
- // "overrides" in the inspector. width/height default to inherited (absent).
67
+ // "overrides" in the inspector. width/height default to inherited (absent),
68
+ // and so does the origin: it describes the KIND of thing (a ball is placed by
69
+ // its center), so it belongs on the blueprint -- with a per-instance override
70
+ // available like any other inherited prop.
29
71
  //
30
72
  // Size is width/height in card units -- the same space you compose in. A
31
73
  // user-facing scaleX/scaleY (relative to the sprite's native pixels) was
@@ -49,12 +91,87 @@ export class Layout {
49
91
  // so it reads separately from the solid collider debug and the selection box.
50
92
  draw(actor, scene, ctx) {
51
93
  if (!this.props.debug) return;
52
- const { x, y, width, height } = this.props;
94
+ const rect = layoutRect(this.props);
53
95
  ctx.save();
54
96
  ctx.strokeStyle = 'rgba(190, 178, 255, 0.75)';
55
97
  ctx.setLineDash([4, 3]);
56
98
  ctx.lineWidth = 1.5;
57
- ctx.strokeRect(x + 0.5, y + 0.5, width - 1, height - 1);
99
+ ctx.strokeRect(rect.x + 0.5, rect.y + 0.5, rect.width - 1, rect.height - 1);
58
100
  ctx.restore();
59
101
  }
102
+
103
+ // Hand-written rather than AutoInspector's default, only for the origin: a
104
+ // Top left / Center select is clearer than two bare fraction fields, and it
105
+ // has to rewrite x/y alongside the origin (see OriginFields).
106
+ static Inspector({ component, setComponent, override }) {
107
+ return (
108
+ <Panel title="Layout" overridden={override?.anyOverridden()}>
109
+ <AutoFields
110
+ defaultProps={Layout.defaultProps}
111
+ meta={Layout.propertyMeta}
112
+ component={component}
113
+ setComponent={setComponent}
114
+ exclude={['originX', 'originY']}
115
+ override={override}
116
+ />
117
+ <OriginFields component={component} setComponent={setComponent} override={override} />
118
+ </Panel>
119
+ );
120
+ }
121
+ }
122
+
123
+ // Changing the origin moves x/y by exactly as much as it moved the anchor, so
124
+ // the box stays where it is on the card -- picking "Center" on a placed actor is
125
+ // a re-anchoring, not a move.
126
+ function OriginFields({ component, setComponent, override }) {
127
+ const originX = layoutOriginX(component);
128
+ const originY = layoutOriginY(component);
129
+ const preset = findPreset(originX, originY);
130
+ const value = preset?.value ?? originKey(originX, originY);
131
+ const setOrigin = (nextX, nextY) => {
132
+ const width = component.width ?? 0;
133
+ const height = component.height ?? 0;
134
+ setComponent({
135
+ originX: nextX,
136
+ originY: nextY,
137
+ x: (component.x ?? 0) + (nextX - originX) * width,
138
+ y: (component.y ?? 0) + (nextY - originY) * height,
139
+ });
140
+ };
141
+ const originOverridden = Boolean(
142
+ override?.isOverridden?.('originX') || override?.isOverridden?.('originY')
143
+ );
144
+ const baselineX = layoutOriginX({ originX: override?.baseline?.('originX') });
145
+ const baselineY = layoutOriginY({ originY: override?.baseline?.('originY') });
146
+ const baselinePreset = findPreset(baselineX, baselineY);
147
+ return (
148
+ <SelectField
149
+ label="Origin"
150
+ value={value}
151
+ options={ORIGIN_PRESETS}
152
+ onChange={(next) => {
153
+ const chosen = ORIGIN_PRESETS.find((preset) => preset.value === next);
154
+ if (!chosen) return;
155
+ setOrigin(chosen.x, chosen.y);
156
+ }}
157
+ overridden={originOverridden}
158
+ defaultValue={baselinePreset?.label ?? originKey(baselineX, baselineY)}
159
+ onReset={
160
+ originOverridden
161
+ ? () => {
162
+ const width = component.width ?? 0;
163
+ const height = component.height ?? 0;
164
+ // Keep the box put, then drop the origin overrides so the
165
+ // blueprint's values show through again.
166
+ setComponent({
167
+ x: (component.x ?? 0) + (baselineX - originX) * width,
168
+ y: (component.y ?? 0) + (baselineY - originY) * height,
169
+ });
170
+ override.reset('originX');
171
+ override.reset('originY');
172
+ }
173
+ : undefined
174
+ }
175
+ />
176
+ );
60
177
  }
@@ -215,13 +215,13 @@ function validTileSize(value) {
215
215
  // using the same per-edge-rounding discipline as the box snap in `draw`, so
216
216
  // neighboring tiles within this actor land on identical device pixels with
217
217
  // no hairline seams.
218
- function drawTilesSnapped(ctx, canvas, artSize, tileSize, layout, transform, x0, y0, rx0, ry0, rx1, ry1) {
218
+ function drawTilesSnapped(ctx, canvas, artSize, tileSize, dest, transform, x0, y0, rx0, ry0, rx1, ry1) {
219
219
  const cellHeight = validTileSize(tileSize);
220
220
  const cellWidth = cellHeight * (artSize.width / artSize.height);
221
221
  const cellDeviceWidth = cellWidth * transform.a;
222
222
  const cellDeviceHeight = cellHeight * transform.d;
223
- const cols = Math.ceil(layout.width / cellWidth);
224
- const rows = Math.ceil(layout.height / cellHeight);
223
+ const cols = Math.ceil(dest.width / cellWidth);
224
+ const rows = Math.ceil(dest.height / cellHeight);
225
225
  ctx.save();
226
226
  ctx.beginPath();
227
227
  ctx.rect(rx0, ry0, rx1 - rx0, ry1 - ry0);
@@ -240,19 +240,19 @@ function drawTilesSnapped(ctx, canvas, artSize, tileSize, layout, transform, x0,
240
240
 
241
241
  // Rotated actors have no axis-aligned device edges to snap to, so tile
242
242
  // unsnapped in card units, matching the unsnapped stretch fallback in `draw`.
243
- function drawTilesUnsnapped(ctx, canvas, artSize, tileSize, layout) {
243
+ function drawTilesUnsnapped(ctx, canvas, artSize, tileSize, dest) {
244
244
  const cellHeight = validTileSize(tileSize);
245
245
  const cellWidth = cellHeight * (artSize.width / artSize.height);
246
- const cols = Math.ceil(layout.width / cellWidth);
247
- const rows = Math.ceil(layout.height / cellHeight);
246
+ const cols = Math.ceil(dest.width / cellWidth);
247
+ const rows = Math.ceil(dest.height / cellHeight);
248
248
  ctx.save();
249
249
  ctx.beginPath();
250
- ctx.rect(layout.x, layout.y, layout.width, layout.height);
250
+ ctx.rect(dest.x, dest.y, dest.width, dest.height);
251
251
  ctx.clip();
252
252
  for (let row = 0; row < rows; row++) {
253
- const ty = layout.y + row * cellHeight;
253
+ const ty = dest.y + row * cellHeight;
254
254
  for (let col = 0; col < cols; col++) {
255
- const tx = layout.x + col * cellWidth;
255
+ const tx = dest.x + col * cellWidth;
256
256
  ctx.drawImage(canvas, tx, ty, cellWidth, cellHeight);
257
257
  }
258
258
  }
@@ -1,4 +1,5 @@
1
1
  import React from 'react';
2
+ import { layoutRect } from '../engine/layout';
2
3
  import { PaletteColorField } from '../engine/paletteField';
3
4
  import { NumberField, Panel } from '../engine/ui';
4
5
  import { AutoFields, overrideProps } from '../engine/autoInspector';
@@ -153,12 +154,12 @@ function contentPaintsStyle(actor) {
153
154
  }
154
155
 
155
156
  function paintStyleChrome(actor, ctx, props) {
156
- const layout = actor.components.Layout;
157
- if (!layout || !props) return;
157
+ const box = layoutRect(actor.components.Layout);
158
+ if (!box || !props) return;
158
159
  const { background, border, borderWidth, borderRadius, opacity } = props;
159
160
  const alpha = Number.isFinite(opacity) ? Math.min(1, Math.max(0, opacity)) : 1;
160
161
  const bw = Math.max(0, Number(borderWidth) || 0);
161
- const radius = clampRadius(borderRadius, layout.width, layout.height);
162
+ const radius = clampRadius(borderRadius, box.width, box.height);
162
163
  const shadow = resolveShadow(props);
163
164
  const hasFill = Boolean(background);
164
165
  const hasBorder = Boolean(border) && bw > 0;
@@ -174,12 +175,12 @@ function paintStyleChrome(actor, ctx, props) {
174
175
  }
175
176
  if (hasFill) {
176
177
  ctx.fillStyle = background;
177
- fillRoundRect(ctx, layout.x, layout.y, layout.width, layout.height, radius);
178
+ fillRoundRect(ctx, box.x, box.y, box.width, box.height, radius);
178
179
  } else if (shadow) {
179
180
  // Shadow needs a filled path; use a near-invisible fill when there's no
180
181
  // background so the drop shadow still casts.
181
182
  ctx.fillStyle = 'rgba(0, 0, 0, 0.01)';
182
- fillRoundRect(ctx, layout.x, layout.y, layout.width, layout.height, radius);
183
+ fillRoundRect(ctx, box.x, box.y, box.width, box.height, radius);
183
184
  }
184
185
  if (shadow) {
185
186
  ctx.shadowColor = 'transparent';
@@ -195,10 +196,10 @@ function paintStyleChrome(actor, ctx, props) {
195
196
  // the same amount so the outer curve still matches the fill's corner.
196
197
  strokeRoundRect(
197
198
  ctx,
198
- layout.x + inset,
199
- layout.y + inset,
200
- Math.max(0, layout.width - bw),
201
- Math.max(0, layout.height - bw),
199
+ box.x + inset,
200
+ box.y + inset,
201
+ Math.max(0, box.width - bw),
202
+ Math.max(0, box.height - bw),
202
203
  Math.max(0, radius - inset)
203
204
  );
204
205
  }
@@ -251,15 +252,21 @@ export function stylePadding(actor) {
251
252
  return Number.isFinite(pad) ? Math.max(0, pad) : 0;
252
253
  }
253
254
 
254
- /** Layout box inset by Style.padding, or the layout itself when padding is 0. */
255
+ /**
256
+ * The world-space rect content draws into: the actor's Layout box (origin
257
+ * applied) inset by Style.padding. Always a real rect, never the `layout`
258
+ * component itself -- callers read `.x`/`.y` as a top-left, which a Layout with
259
+ * a non-zero origin is not.
260
+ */
255
261
  export function styleContentBox(actor, layout) {
262
+ const box = layoutRect(layout);
256
263
  const pad = stylePadding(actor);
257
- if (!(pad > 0) || !layout) return layout;
264
+ if (!box || !(pad > 0)) return box;
258
265
  return {
259
- x: layout.x + pad,
260
- y: layout.y + pad,
261
- width: Math.max(0, layout.width - pad * 2),
262
- height: Math.max(0, layout.height - pad * 2),
266
+ x: box.x + pad,
267
+ y: box.y + pad,
268
+ width: Math.max(0, box.width - pad * 2),
269
+ height: Math.max(0, box.height - pad * 2),
263
270
  };
264
271
  }
265
272
 
@@ -1,5 +1,6 @@
1
1
  import { writeFile } from 'castle-web-sdk';
2
2
  import React from 'react';
3
+ import { layoutRect } from '../engine/layout';
3
4
  import { drawTextBlock } from '../engine/text';
4
5
  import { PaletteColorField } from '../engine/paletteField';
5
6
  import {
@@ -56,8 +57,8 @@ export class Text {
56
57
  }
57
58
 
58
59
  paint(actor, scene, ctx) {
59
- const layout = actor.components.Layout;
60
- if (!layout) return;
60
+ const box = layoutRect(actor.components.Layout);
61
+ if (!box) return;
61
62
  const {
62
63
  text,
63
64
  font,
@@ -73,10 +74,10 @@ export class Text {
73
74
  ctx.save();
74
75
  ctx.globalAlpha *= styleOpacity(actor);
75
76
  drawTextBlock(ctx, {
76
- x: layout.x,
77
- y: layout.y,
78
- width: layout.width,
79
- height: layout.height,
77
+ x: box.x,
78
+ y: box.y,
79
+ width: box.width,
80
+ height: box.height,
80
81
  text,
81
82
  size,
82
83
  font: resolveTextFont(font),
@@ -3,11 +3,33 @@
3
3
  "actors": [
4
4
  {
5
5
  "components": {
6
- "Layout": { "width": 44, "height": 44, "z": 2 },
7
- "Sprite": { "file": "drawings/cauldron.sprite", "mode": "fit" },
8
- "Collider": { "shape": "circle", "width": 40, "height": 40, "bounciness": 0.6, "friction": 0.05 },
9
- "RigidBody": { "bodyType": "dynamic", "gravityScale": 1, "drag": 0.005 },
10
- "Slingshot": { "speed": 0.1, "maxDrag": 200 }
6
+ "Layout": {
7
+ "width": 44,
8
+ "height": 44,
9
+ "z": 2,
10
+ "originX": 0.5,
11
+ "originY": 0.5
12
+ },
13
+ "Sprite": {
14
+ "file": "drawings/cauldron.sprite",
15
+ "mode": "fit"
16
+ },
17
+ "Collider": {
18
+ "shape": "circle",
19
+ "width": 40,
20
+ "height": 40,
21
+ "bounciness": 0.6,
22
+ "friction": 0.05
23
+ },
24
+ "RigidBody": {
25
+ "bodyType": "dynamic",
26
+ "gravityScale": 1,
27
+ "drag": 0.005
28
+ },
29
+ "Slingshot": {
30
+ "speed": 0.1,
31
+ "maxDrag": 200
32
+ }
11
33
  }
12
34
  }
13
35
  ]
@@ -3,9 +3,22 @@
3
3
  "actors": [
4
4
  {
5
5
  "components": {
6
- "Layout": { "width": 100, "height": 40, "z": 1 },
7
- "Sprite": { "file": "drawings/block.sprite", "mode": "tile", "tileSize": 20 },
8
- "Collider": { "bounciness": 0.4, "friction": 0.2 }
6
+ "Layout": {
7
+ "width": 100,
8
+ "height": 40,
9
+ "z": 1,
10
+ "originX": 0.5,
11
+ "originY": 0.5
12
+ },
13
+ "Sprite": {
14
+ "file": "drawings/block.sprite",
15
+ "mode": "tile",
16
+ "tileSize": 20
17
+ },
18
+ "Collider": {
19
+ "bounciness": 0.4,
20
+ "friction": 0.2
21
+ }
9
22
  }
10
23
  }
11
24
  ]
@@ -6,7 +6,9 @@
6
6
  "Layout": {
7
7
  "width": 100,
8
8
  "height": 100,
9
- "z": 1
9
+ "z": 1,
10
+ "originX": 0.5,
11
+ "originY": 0.5
10
12
  },
11
13
  "Sprite": {
12
14
  "file": "drawings/cauldron.sprite"
@@ -3,10 +3,29 @@
3
3
  "actors": [
4
4
  {
5
5
  "components": {
6
- "Layout": { "width": 48, "height": 48, "z": 2 },
7
- "Sprite": { "file": "drawings/block.sprite", "mode": "fit" },
8
- "Collider": { "shape": "box", "width": 46, "height": 46, "bounciness": 0.2, "friction": 0.3 },
9
- "RigidBody": { "bodyType": "dynamic", "gravityScale": 1, "drag": 0.02 },
6
+ "Layout": {
7
+ "width": 48,
8
+ "height": 48,
9
+ "z": 2,
10
+ "originX": 0.5,
11
+ "originY": 0.5
12
+ },
13
+ "Sprite": {
14
+ "file": "drawings/block.sprite",
15
+ "mode": "fit"
16
+ },
17
+ "Collider": {
18
+ "shape": "box",
19
+ "width": 46,
20
+ "height": 46,
21
+ "bounciness": 0.2,
22
+ "friction": 0.3
23
+ },
24
+ "RigidBody": {
25
+ "bodyType": "dynamic",
26
+ "gravityScale": 1,
27
+ "drag": 0.02
28
+ },
10
29
  "Draggable": {}
11
30
  }
12
31
  }
@@ -3,8 +3,18 @@
3
3
  "actors": [
4
4
  {
5
5
  "components": {
6
- "Layout": { "width": 110, "height": 34, "z": 3 },
7
- "Collider": { "width": 110, "height": 34, "isTrigger": true },
6
+ "Layout": {
7
+ "width": 110,
8
+ "height": 34,
9
+ "z": 3,
10
+ "originX": 0.5,
11
+ "originY": 0.5
12
+ },
13
+ "Collider": {
14
+ "width": 110,
15
+ "height": 34,
16
+ "isTrigger": true
17
+ },
8
18
  "Goal": {}
9
19
  }
10
20
  }
@@ -3,9 +3,19 @@
3
3
  "actors": [
4
4
  {
5
5
  "components": {
6
- "Layout": { "width": 280, "height": 50, "z": 5 },
7
- "Style": { "padding": 8 },
8
- "Text": { "text": "Your text goes here" }
6
+ "Layout": {
7
+ "width": 280,
8
+ "height": 50,
9
+ "z": 5,
10
+ "originX": 0.5,
11
+ "originY": 0.5
12
+ },
13
+ "Style": {
14
+ "padding": 8
15
+ },
16
+ "Text": {
17
+ "text": "Your text goes here"
18
+ }
9
19
  }
10
20
  }
11
21
  ]
@@ -17,16 +17,6 @@
17
17
  "file": "scenes/main.scene"
18
18
  }
19
19
  ],
20
- "hiddenPaths": [],
21
- "visiblePaths": [
22
- "drawings/**",
23
- "scenes/**",
24
- "blueprints/**",
25
- "behaviors/**",
26
- "assets/**",
27
- "theme.style",
28
- "fonts.generated.js"
29
- ],
30
20
  "fileTypes": [
31
21
  {
32
22
  "ext": ".scene",
@@ -75,8 +65,9 @@
75
65
  "cardId": "_oBFbAW6DxsO",
76
66
  "main": "main.jsx",
77
67
  "autoUpdateWhenImported": true,
68
+ "onUpdate": "scripts/onUpdate.mjs",
78
69
  "title": "physics-2d",
79
- "publishedVersion": "2026-09-13T17:33:26.446Z",
70
+ "publishedVersion": "2026-09-17T20:55:45.193Z",
80
71
  "imports": {
81
72
  "castle.base": {
82
73
  "deckId": "yRcmH4_aYllE",
@@ -28,6 +28,7 @@ import {
28
28
  setActorComponent,
29
29
  } from '../engine/scene';
30
30
  import { fontsReady, ensureOfficialFace } from '../engine/fonts';
31
+ import { ErrorOverlay } from '../engine/ErrorOverlay';
31
32
  import {
32
33
  addActorWithBlueprint,
33
34
  blueprintDropXY,
@@ -45,6 +46,7 @@ import {
45
46
  placeBlueprintInstance,
46
47
  renameBlueprintCascade,
47
48
  setBlueprintName,
49
+ setBlueprintOriginCascade,
48
50
  setBlueprintTemplateComponents,
49
51
  setInstanceComponent,
50
52
  } from '../engine/blueprint';
@@ -308,7 +310,7 @@ export function SceneEditor({
308
310
  const recordSceneSnapshot = readOnly ? () => {} : history.recordSnapshot;
309
311
  const commitScene = readOnly ? () => {} : (next) => history.commit(serialize(next));
310
312
  useMigrateOrphanActors({ isBlueprintFile, sceneData, files, text, onChange, readOnly });
311
- useScenePlayLoop({
313
+ const { loopError, restartLoop } = useScenePlayLoop({
312
314
  sceneData,
313
315
  sprites,
314
316
  files,
@@ -453,11 +455,34 @@ export function SceneEditor({
453
455
  if (onChangeFile) onChangeFile(selectedBlueprintPath, nextText);
454
456
  else void writeFile(selectedBlueprintPath, nextText);
455
457
  };
458
+ // The Layout origin is the one template prop whose edit MOVES existing
459
+ // instances: it changes what their own stored x/y means. So it goes through
460
+ // the cascade, which re-anchors every instance in every scene of this deck in
461
+ // the same gesture, and the writes land the way `renameCommit`'s do -- the
462
+ // open scene through history (so undo covers it), the rest as file writes.
463
+ const commitBlueprintOrigin = (nextProps) => {
464
+ if (blueprintLocked || !selectedBlueprintPath || !hotbarBlueprint) return;
465
+ const result = setBlueprintOriginCascade(files, selectedBlueprintPath, nextProps);
466
+ if (!result) return;
467
+ // Instances here move too. If this scene can't be written, refuse rather
468
+ // than shift the other scenes' instances and leave these behind.
469
+ if (readOnly && result.writes.some((write) => write.path === path)) return;
470
+ for (const write of result.writes) {
471
+ if (write.path === path) history.commit(write.text);
472
+ else if (onChangeFile) onChangeFile(write.path, write.text);
473
+ else void writeFile(write.path, write.text);
474
+ }
475
+ };
456
476
  const blueprintActions = {
457
- setComponent: (behaviorName, nextProps) =>
477
+ setComponent: (behaviorName, nextProps) => {
478
+ if (behaviorName === 'Layout' && ('originX' in nextProps || 'originY' in nextProps)) {
479
+ commitBlueprintOrigin(nextProps);
480
+ return;
481
+ }
458
482
  updateBlueprintTemplate((components) => {
459
483
  components[behaviorName] = { ...(components[behaviorName] ?? {}), ...nextProps };
460
- }),
484
+ });
485
+ },
461
486
  // Brings expected siblings along, as the actor path does -- a lone Draggable
462
487
  // is silently inert without a Collider.
463
488
  addBehavior: (behaviorName) => {
@@ -802,6 +827,13 @@ export function SceneEditor({
802
827
  recordSnapshot={recordSceneSnapshot}
803
828
  />
804
829
  )}
830
+ {loopError ? (
831
+ <ErrorOverlay
832
+ error={loopError}
833
+ onRestart={restartLoop}
834
+ title={isPlaying ? 'Play error' : 'Scene error'}
835
+ />
836
+ ) : null}
805
837
  </div>
806
838
  {!isPlaying && (
807
839
  <div style={ZOOM_CONTROLS_STYLE}>
@@ -1691,6 +1723,17 @@ function useScenePlayLoop({
1691
1723
  selectedBlueprintPathRef,
1692
1724
  colliderShapeSelRef,
1693
1725
  }) {
1726
+ // A throw from inside the loop below (a behavior's update/draw, or makeScene
1727
+ // itself on a behavior whose module is broken) kills the loop -- it cannot
1728
+ // schedule the next frame -- and the canvas simply stops updating. Captured
1729
+ // so the editor shows the same redbox the play surface does instead of a
1730
+ // blank scene. `loopRunId` bumps to remount the loop on Restart.
1731
+ const [loopError, setLoopError] = useState(null);
1732
+ const [loopRunId, setLoopRunId] = useState(0);
1733
+ const restartLoop = useCallback(() => {
1734
+ setLoopError(null);
1735
+ setLoopRunId((n) => n + 1);
1736
+ }, []);
1694
1737
  // Spin up / tear down the play-mode runtime as the user toggles play. `text`
1695
1738
  // is the stable identity for `sceneData` (which is re-parsed every render);
1696
1739
  // depending on `sceneData` here would reset the runtime — and any
@@ -1706,8 +1749,10 @@ function useScenePlayLoop({
1706
1749
  // Pressing Stop has to stop what the runtime started -- a sound keeps
1707
1750
  // playing on its own otherwise, with no scene left to stop it.
1708
1751
  return () => runtime.dispose();
1752
+ // `loopRunId` too: Restart has to dispose and rebuild the runtime, not hand
1753
+ // the fresh loop back the one that just threw.
1709
1754
  // eslint-disable-next-line react-hooks/exhaustive-deps
1710
- }, [sprites, isPlaying, text, runtimeRef]);
1755
+ }, [sprites, isPlaying, text, runtimeRef, loopRunId]);
1711
1756
  // Animation loop -- draws edit-mode previews and ticks the play-mode runtime.
1712
1757
  useEffect(() => {
1713
1758
  if (!sceneData || !canvasRef.current) return undefined;
@@ -1735,6 +1780,20 @@ function useScenePlayLoop({
1735
1780
  // Recompute each frame so the edit zoom (mutated on the camera ref by the
1736
1781
  // pinch gesture) takes effect without restarting the loop.
1737
1782
  const frameViewport = isPlaying ? undefined : editViewportForZoom(editCameraRef.current.zoom);
1783
+ try {
1784
+ drawFrame(dt, frameViewport);
1785
+ } catch (error) {
1786
+ // Stop scheduling frames (it would otherwise rethrow 60x/second) and
1787
+ // surface it, the same way the play loop does. Restart remounts a
1788
+ // fresh loop.
1789
+ // eslint-disable-next-line no-console
1790
+ console.error('[castle] scene editor loop error', error);
1791
+ setLoopError(error);
1792
+ return;
1793
+ }
1794
+ raf = requestAnimationFrame(frame);
1795
+ };
1796
+ const drawFrame = (dt, frameViewport) => {
1738
1797
  const scene = isPlaying
1739
1798
  ? runtimeRef.current
1740
1799
  : makeScene(sceneData, behaviorClasses, sprites, files, path);
@@ -1765,8 +1824,10 @@ function useScenePlayLoop({
1765
1824
  colliderShapeSel: isPlaying ? -1 : (colliderShapeSelRef?.current ?? -1),
1766
1825
  });
1767
1826
  }
1768
- raf = requestAnimationFrame(frame);
1769
1827
  };
1828
+ // A loop that is starting is not a loop that is broken -- an edit that
1829
+ // fixes the file resets this effect, and the stale redbox must go with it.
1830
+ setLoopError(null);
1770
1831
  raf = requestAnimationFrame(frame);
1771
1832
  return () => cancelAnimationFrame(raf);
1772
1833
  // eslint-disable-next-line react-hooks/exhaustive-deps -- `text` proxies `sceneData` identity; resetting on every parse churns the loop and wipes runtime state.
@@ -1775,6 +1836,7 @@ function useScenePlayLoop({
1775
1836
  files,
1776
1837
  isPlaying,
1777
1838
  text,
1839
+ loopRunId,
1778
1840
  canvasRef,
1779
1841
  runtimeRef,
1780
1842
  marqueeRef,
@@ -1782,6 +1844,7 @@ function useScenePlayLoop({
1782
1844
  selectedBlueprintPathRef,
1783
1845
  colliderShapeSelRef,
1784
1846
  ]);
1847
+ return { loopError, restartLoop };
1785
1848
  }
1786
1849
  function useScenePlayKeys(runtimeRef) {
1787
1850
  useEffect(() => {