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.
- package/dist/agent-prompts.d.ts +1 -0
- package/dist/agent-prompts.js +14 -5
- package/dist/agent.js +101 -4
- package/dist/api.d.ts +1 -0
- package/dist/api.js +5 -0
- package/dist/bundle.js +2 -1
- package/dist/castleJson.d.ts +1 -2
- package/dist/castleJson.js +0 -39
- package/dist/check.js +14 -6
- package/dist/devSessionFrameBudget.d.ts +14 -0
- package/dist/devSessionFrameBudget.js +64 -0
- package/dist/devSessionServer.d.ts +6 -0
- package/dist/devSessionServer.js +38 -1
- package/dist/editorConfig.d.ts +1 -1
- package/dist/editorConfig.js +12 -4
- package/dist/filesChanged.js +7 -3
- package/dist/ide.js +28 -59
- package/dist/importUpdateHook.d.ts +31 -0
- package/dist/importUpdateHook.js +135 -0
- package/dist/importUpdateHookRunner.d.ts +2 -0
- package/dist/importUpdateHookRunner.js +24 -0
- package/dist/imports.js +58 -10
- package/dist/index.js +58 -7
- package/dist/init.d.ts +1 -0
- package/dist/init.js +15 -9
- package/dist/multiplayerDoc.d.ts +1 -0
- package/dist/multiplayerDoc.js +228 -0
- package/dist/native/loop.js +29 -9
- package/dist/platformDoc.d.ts +1 -0
- package/dist/platformDoc.js +25 -6
- package/dist/remix.js +4 -1
- package/dist/scriptErrors.d.ts +23 -0
- package/dist/scriptErrors.js +147 -0
- package/dist/serve.js +366 -36
- package/dist/serverBundle.js +1 -1
- package/dist/shell/assets/index-kMH1qj6D.js +449 -0
- package/dist/shell/index.html +1 -1
- package/dist/versions.d.ts +14 -0
- package/dist/versions.js +21 -0
- package/dist/viteClientStub.d.ts +18 -0
- package/dist/viteClientStub.js +193 -0
- package/dist/vitePlugins.d.ts +1 -0
- package/dist/vitePlugins.js +18 -0
- package/kits/base/CLAUDE.md +3 -3
- package/kits/base/castle.json +1 -3
- package/kits/base/sdk/README.md +17 -0
- package/kits/multiplayer-2d/CLAUDE.md +9 -2
- package/kits/multiplayer-2d/blueprints/crate.scene +32 -4
- package/kits/multiplayer-2d/blueprints/other-player.scene +31 -4
- package/kits/multiplayer-2d/blueprints/player.scene +38 -6
- package/kits/multiplayer-2d/blueprints/wall.scene +25 -3
- package/kits/multiplayer-2d/castle.json +3 -12
- package/kits/multiplayer-2d/test/two-client-test.mjs +4 -4
- package/kits/multiplayer-3d/CLAUDE.md +5 -2
- package/kits/multiplayer-3d/castle.json +3 -11
- package/kits/multiplayer-3d/scenes/main.scene +128 -0
- package/kits/physics-2d/CLAUDE.md +16 -11
- package/kits/physics-2d/behaviors/Camera.jsx +5 -6
- package/kits/physics-2d/behaviors/Goal.jsx +5 -2
- package/kits/physics-2d/behaviors/Joints.jsx +4 -3
- package/kits/physics-2d/behaviors/Layout.jsx +120 -3
- package/kits/physics-2d/behaviors/Sprite.jsx +9 -9
- package/kits/physics-2d/behaviors/Style.jsx +22 -15
- package/kits/physics-2d/behaviors/Text.jsx +7 -6
- package/kits/physics-2d/blueprints/ball.scene +27 -5
- package/kits/physics-2d/blueprints/block.scene +16 -3
- package/kits/physics-2d/blueprints/cauldron.scene +3 -1
- package/kits/physics-2d/blueprints/crate.scene +23 -4
- package/kits/physics-2d/blueprints/goal.scene +12 -2
- package/kits/physics-2d/blueprints/text.scene +13 -3
- package/kits/physics-2d/castle.json +2 -11
- package/kits/physics-2d/editors/SceneEditor.jsx +68 -5
- package/kits/physics-2d/editors/SelectionOverlay.jsx +27 -20
- package/kits/physics-2d/editors/behaviorRegistry.js +8 -2
- package/kits/physics-2d/engine/ErrorOverlay.jsx +69 -0
- package/kits/physics-2d/engine/ScenePlayer.jsx +2 -67
- package/kits/physics-2d/engine/blueprint.js +111 -54
- package/kits/physics-2d/engine/blueprintCore.js +59 -0
- package/kits/physics-2d/engine/collider.js +28 -17
- package/kits/physics-2d/engine/layout.js +61 -0
- package/kits/physics-2d/engine/layoutOriginMigration.js +223 -0
- package/kits/physics-2d/engine/liveReload.js +5 -4
- package/kits/physics-2d/engine/physics/PhysicsSystem.js +11 -7
- package/kits/physics-2d/engine/physics/controls.js +4 -8
- package/kits/physics-2d/engine/physics/matterBridge.js +13 -8
- package/kits/physics-2d/engine/scene.js +38 -34
- package/kits/physics-2d/engine/spriteGeometry.js +17 -9
- package/kits/physics-2d/engine/tap.js +4 -3
- package/kits/physics-2d/scenes/main.scene +59 -6
- package/kits/physics-2d/scenes/sandbox.scene +69 -7
- package/kits/physics-2d/scripts/onUpdate.mjs +199 -0
- package/kits/physics-3d/castle.json +0 -11
- package/kits/real-time/castle.json +2 -8
- package/kits/real-time/code/server/persist.js +14 -0
- package/kits/turn-based/CLAUDE.md +18 -10
- package/kits/turn-based/blueprints/board.scene +3 -1
- package/kits/turn-based/blueprints/square.scene +3 -1
- package/kits/turn-based/castle.json +3 -14
- package/kits/turn-based/code/behaviors/Board.jsx +33 -21
- package/package.json +1 -1
- package/dist/shell/assets/index-ColHKJYj.js +0 -449
|
@@ -2,6 +2,7 @@ import { resolveDeckFile } from 'castle-web-sdk';
|
|
|
2
2
|
import { initialFiles, parseJsonFile } from './files';
|
|
3
3
|
import { actorUsesBlueprint, getBlueprintTemplate, mergeComponents } from './blueprint';
|
|
4
4
|
import { getColliderRect, intersects, spriteIsEmpty } from './collider';
|
|
5
|
+
import { layoutCenter, layoutRect } from './layout';
|
|
5
6
|
import { systemInstallers } from './systemRegistry';
|
|
6
7
|
import { deckFontFamily } from './fonts';
|
|
7
8
|
import { wrapText } from './text';
|
|
@@ -215,6 +216,15 @@ export class SceneRuntime {
|
|
|
215
216
|
return getColliderRect(actor, this.sprites);
|
|
216
217
|
}
|
|
217
218
|
|
|
219
|
+
// The actor's Layout box in world units with `originX/originY` applied -- the
|
|
220
|
+
// rect it actually occupies. Deck code asks for this instead of doing
|
|
221
|
+
// `layout.x + width / 2` by hand, which is only right for a top-left origin
|
|
222
|
+
// (and deck behaviors can't import `engine/layout.js`).
|
|
223
|
+
layoutRect(actorOrId) {
|
|
224
|
+
const actor = typeof actorOrId === 'string' ? this.getActor(actorOrId) : actorOrId;
|
|
225
|
+
return layoutRect(getLayout(actor));
|
|
226
|
+
}
|
|
227
|
+
|
|
218
228
|
overlaps(first, second) {
|
|
219
229
|
const a = this.colliderRect(first);
|
|
220
230
|
const b = this.colliderRect(second);
|
|
@@ -524,14 +534,9 @@ export class SceneRuntime {
|
|
|
524
534
|
const actors = this.getActors().slice().reverse();
|
|
525
535
|
const hits = [];
|
|
526
536
|
for (const actor of actors) {
|
|
527
|
-
const
|
|
528
|
-
if (!
|
|
529
|
-
if (
|
|
530
|
-
x >= layout.x &&
|
|
531
|
-
x <= layout.x + layout.width &&
|
|
532
|
-
y >= layout.y &&
|
|
533
|
-
y <= layout.y + layout.height
|
|
534
|
-
) {
|
|
537
|
+
const box = layoutRect(getLayout(actor));
|
|
538
|
+
if (!box) continue;
|
|
539
|
+
if (x >= box.x && x <= box.x + box.width && y >= box.y && y <= box.y + box.height) {
|
|
535
540
|
hits.push(actor);
|
|
536
541
|
}
|
|
537
542
|
}
|
|
@@ -545,13 +550,13 @@ export class SceneRuntime {
|
|
|
545
550
|
const maxY = rect.y + rect.height;
|
|
546
551
|
const ids = [];
|
|
547
552
|
for (const actor of this.getActors()) {
|
|
548
|
-
const
|
|
549
|
-
if (!
|
|
553
|
+
const box = layoutRect(getLayout(actor));
|
|
554
|
+
if (!box) continue;
|
|
550
555
|
if (
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
556
|
+
box.x + box.width >= minX &&
|
|
557
|
+
box.x <= maxX &&
|
|
558
|
+
box.y + box.height >= minY &&
|
|
559
|
+
box.y <= maxY
|
|
555
560
|
) {
|
|
556
561
|
ids.push(actor.id);
|
|
557
562
|
}
|
|
@@ -711,9 +716,7 @@ function applyActorOrder(sceneData, ordered) {
|
|
|
711
716
|
// Ignores Layout.rotation -- v1 selection bounds are axis-aligned (the canvas
|
|
712
717
|
// `drawSelection` still draws the rotated outline for per-actor feedback).
|
|
713
718
|
export function getLayoutRect(actor) {
|
|
714
|
-
|
|
715
|
-
if (!layout) return null;
|
|
716
|
-
return { x: layout.x, y: layout.y, width: layout.width, height: layout.height };
|
|
719
|
+
return layoutRect(getLayout(actor));
|
|
717
720
|
}
|
|
718
721
|
|
|
719
722
|
// Union axis-aligned bounding box of the given actors in world units, or null
|
|
@@ -852,9 +855,9 @@ function shouldDrawEditPlaceholder(actor, sprites) {
|
|
|
852
855
|
// Wraps to two lines when the actor is too narrow for one -- a label that always
|
|
853
856
|
// overflows its actor stops reading as belonging to it.
|
|
854
857
|
function drawDisabledSpriteLabel(ctx, actor) {
|
|
855
|
-
const
|
|
856
|
-
if (!
|
|
857
|
-
const fontSize = Math.max(8, Math.min(12,
|
|
858
|
+
const box = layoutRect(getLayout(actor));
|
|
859
|
+
if (!box) return;
|
|
860
|
+
const fontSize = Math.max(8, Math.min(12, box.width / 9));
|
|
858
861
|
ctx.save();
|
|
859
862
|
ctx.font = `600 ${fontSize}px -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif`;
|
|
860
863
|
ctx.textAlign = 'center';
|
|
@@ -863,13 +866,13 @@ function drawDisabledSpriteLabel(ctx, actor) {
|
|
|
863
866
|
const padY = fontSize * 0.4;
|
|
864
867
|
|
|
865
868
|
const single = 'Sprite disabled';
|
|
866
|
-
const lines = wrapText(ctx, single, Math.max(0,
|
|
869
|
+
const lines = wrapText(ctx, single, Math.max(0, box.width - padX * 2));
|
|
867
870
|
const lineHeight = fontSize * 1.15;
|
|
868
871
|
const textWidth = Math.max(...lines.map((line) => ctx.measureText(line).width));
|
|
869
872
|
const width = textWidth + padX * 2;
|
|
870
873
|
const height = lineHeight * lines.length + padY * 2;
|
|
871
|
-
const cx =
|
|
872
|
-
const cy =
|
|
874
|
+
const cx = box.x + box.width / 2;
|
|
875
|
+
const cy = box.y + box.height / 2;
|
|
873
876
|
const x = cx - width / 2;
|
|
874
877
|
const y = cy - height / 2;
|
|
875
878
|
const r = Math.min(4, height / 2);
|
|
@@ -888,14 +891,14 @@ function drawDisabledSpriteLabel(ctx, actor) {
|
|
|
888
891
|
}
|
|
889
892
|
|
|
890
893
|
function drawEditPlaceholder(ctx, actor) {
|
|
891
|
-
const
|
|
892
|
-
if (!
|
|
894
|
+
const box = layoutRect(getLayout(actor));
|
|
895
|
+
if (!box) return;
|
|
893
896
|
ctx.save();
|
|
894
897
|
ctx.fillStyle = 'rgba(255, 255, 255, 0.04)';
|
|
895
|
-
ctx.fillRect(
|
|
898
|
+
ctx.fillRect(box.x, box.y, box.width, box.height);
|
|
896
899
|
ctx.strokeStyle = 'rgba(255, 255, 255, 0.25)';
|
|
897
900
|
ctx.lineWidth = 1;
|
|
898
|
-
ctx.strokeRect(
|
|
901
|
+
ctx.strokeRect(box.x + 0.5, box.y + 0.5, box.width - 1, box.height - 1);
|
|
899
902
|
ctx.restore();
|
|
900
903
|
}
|
|
901
904
|
|
|
@@ -942,24 +945,25 @@ function roundedRect(ctx, x, y, width, height, radius) {
|
|
|
942
945
|
|
|
943
946
|
function drawSelection(ctx, actor) {
|
|
944
947
|
const layout = getLayout(actor);
|
|
945
|
-
|
|
948
|
+
const box = layoutRect(layout);
|
|
949
|
+
if (!box) return;
|
|
946
950
|
ctx.save();
|
|
947
951
|
applyLayoutRotation(ctx, layout);
|
|
948
952
|
ctx.strokeStyle = '#fff';
|
|
949
953
|
ctx.lineWidth = 2;
|
|
950
954
|
ctx.setLineDash([6, 4]);
|
|
951
|
-
ctx.strokeRect(
|
|
955
|
+
ctx.strokeRect(box.x + 1, box.y + 1, box.width - 2, box.height - 2);
|
|
952
956
|
ctx.restore();
|
|
953
957
|
}
|
|
954
958
|
|
|
959
|
+
// Rotation pivots on the box CENTER whatever the origin is (see engine/layout.js).
|
|
955
960
|
function applyLayoutRotation(ctx, layout) {
|
|
956
961
|
const rotation = layout?.rotation ?? 0;
|
|
957
|
-
if (!rotation
|
|
958
|
-
const
|
|
959
|
-
|
|
960
|
-
ctx.translate(cx, cy);
|
|
962
|
+
if (!rotation) return;
|
|
963
|
+
const center = layoutCenter(layout);
|
|
964
|
+
ctx.translate(center.x, center.y);
|
|
961
965
|
ctx.rotate((rotation * Math.PI) / 180);
|
|
962
|
-
ctx.translate(-
|
|
966
|
+
ctx.translate(-center.x, -center.y);
|
|
963
967
|
}
|
|
964
968
|
|
|
965
969
|
function getLayout(actor) {
|
|
@@ -2,30 +2,38 @@
|
|
|
2
2
|
// Sprite.jsx (rendering) and the Collider rect logic (engine/collider.js) can
|
|
3
3
|
// use the same math without behaviors/ <-> engine/ import cycles.
|
|
4
4
|
|
|
5
|
-
// Destination rect for a draw mode
|
|
5
|
+
// Destination rect for a draw mode, inside the content `box` (a real world-space
|
|
6
|
+
// rect -- `layoutRect` inset by Style.padding, see behaviors/Style.jsx, NOT the
|
|
7
|
+
// raw Layout component): the whole box for `stretch`/`tile`, or an
|
|
6
8
|
// aspect-ratio-preserving centered rect for `fit` and `cover`:
|
|
7
9
|
// - `fit` (CSS object-fit: contain): the largest rect with the art's aspect
|
|
8
10
|
// ratio that fits INSIDE the box -- letterboxed sides just aren't drawn.
|
|
9
11
|
// - `cover` (CSS object-fit: cover): the smallest rect with the art's aspect
|
|
10
12
|
// ratio that fully COVERS the box -- the overflow is cropped by the caller
|
|
11
|
-
// clipping to the
|
|
13
|
+
// clipping to the content box (see Sprite.draw).
|
|
12
14
|
// `artSize` only needs `.width`/`.height` -- a rendered canvas or a sprite's
|
|
13
15
|
// `.resolution` both work.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
//
|
|
17
|
+
// Always a FRESH rect, never the `box` argument itself: callers treat the result
|
|
18
|
+
// as their own to read past the box's lifetime, and handing back the input made
|
|
19
|
+
// `dest === box` for two of the four modes -- true only by accident.
|
|
20
|
+
export function spriteDestRect(mode, box, artSize) {
|
|
21
|
+
if (mode !== 'fit' && mode !== 'cover') {
|
|
22
|
+
return { x: box.x, y: box.y, width: box.width, height: box.height };
|
|
23
|
+
}
|
|
16
24
|
const aspect = artSize.width / artSize.height;
|
|
17
|
-
let width =
|
|
25
|
+
let width = box.width;
|
|
18
26
|
let height = width / aspect;
|
|
19
27
|
// `fit` shrinks until the art fits inside the box; `cover` grows until it
|
|
20
28
|
// fills the box. The comparison flips between the two.
|
|
21
|
-
const overflows = mode === 'fit' ? height >
|
|
29
|
+
const overflows = mode === 'fit' ? height > box.height : height < box.height;
|
|
22
30
|
if (overflows) {
|
|
23
|
-
height =
|
|
31
|
+
height = box.height;
|
|
24
32
|
width = height * aspect;
|
|
25
33
|
}
|
|
26
34
|
return {
|
|
27
|
-
x:
|
|
28
|
-
y:
|
|
35
|
+
x: box.x + (box.width - width) / 2,
|
|
36
|
+
y: box.y + (box.height - height) / 2,
|
|
29
37
|
width,
|
|
30
38
|
height,
|
|
31
39
|
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { layoutRect } from './layout';
|
|
1
2
|
import { TAP_CLAIM } from './physics/controls';
|
|
2
3
|
|
|
3
4
|
// Built-in tap. Any enabled behavior may implement `onTap(actor, scene)`; the
|
|
@@ -84,7 +85,7 @@ function hits(actor, scene, pointer) {
|
|
|
84
85
|
if (actor.components?.Collider && scene.physics?.containsPoint) {
|
|
85
86
|
return scene.physics.containsPoint(actor, x, y);
|
|
86
87
|
}
|
|
87
|
-
const
|
|
88
|
-
if (!
|
|
89
|
-
return x >=
|
|
88
|
+
const box = layoutRect(actor.components?.Layout);
|
|
89
|
+
if (!box) return false;
|
|
90
|
+
return x >= box.x && x <= box.x + box.width && y >= box.y && y <= box.y + box.height;
|
|
90
91
|
}
|
|
@@ -1,12 +1,65 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "Main",
|
|
3
3
|
"background": "#1a1932",
|
|
4
|
-
"physics": {
|
|
4
|
+
"physics": {
|
|
5
|
+
"gravity": 1
|
|
6
|
+
},
|
|
5
7
|
"actors": [
|
|
6
|
-
{
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
{
|
|
9
|
+
"id": "ball",
|
|
10
|
+
"blueprint": "blueprints/ball.scene",
|
|
11
|
+
"components": {
|
|
12
|
+
"Layout": {
|
|
13
|
+
"x": 92,
|
|
14
|
+
"y": 582
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"id": "floor",
|
|
20
|
+
"blueprint": "blueprints/block.scene",
|
|
21
|
+
"components": {
|
|
22
|
+
"Layout": {
|
|
23
|
+
"x": 250,
|
|
24
|
+
"y": 670,
|
|
25
|
+
"width": 500,
|
|
26
|
+
"height": 60
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"id": "leftWall",
|
|
32
|
+
"blueprint": "blueprints/block.scene",
|
|
33
|
+
"components": {
|
|
34
|
+
"Layout": {
|
|
35
|
+
"x": 25,
|
|
36
|
+
"y": 350,
|
|
37
|
+
"width": 50,
|
|
38
|
+
"height": 700
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
"id": "rightWall",
|
|
44
|
+
"blueprint": "blueprints/block.scene",
|
|
45
|
+
"components": {
|
|
46
|
+
"Layout": {
|
|
47
|
+
"x": 475,
|
|
48
|
+
"y": 350,
|
|
49
|
+
"width": 50,
|
|
50
|
+
"height": 700
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"id": "goal",
|
|
56
|
+
"blueprint": "blueprints/goal.scene",
|
|
57
|
+
"components": {
|
|
58
|
+
"Layout": {
|
|
59
|
+
"x": 355,
|
|
60
|
+
"y": 437
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
11
64
|
]
|
|
12
65
|
}
|
|
@@ -1,13 +1,75 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "Sandbox",
|
|
3
3
|
"background": "#161a2b",
|
|
4
|
-
"physics": {
|
|
4
|
+
"physics": {
|
|
5
|
+
"gravity": 1
|
|
6
|
+
},
|
|
5
7
|
"actors": [
|
|
6
|
-
{
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
{
|
|
9
|
+
"id": "floor",
|
|
10
|
+
"blueprint": "blueprints/block.scene",
|
|
11
|
+
"components": {
|
|
12
|
+
"Layout": {
|
|
13
|
+
"x": 250,
|
|
14
|
+
"y": 670,
|
|
15
|
+
"width": 500,
|
|
16
|
+
"height": 60
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"id": "leftWall",
|
|
22
|
+
"blueprint": "blueprints/block.scene",
|
|
23
|
+
"components": {
|
|
24
|
+
"Layout": {
|
|
25
|
+
"x": 25,
|
|
26
|
+
"y": 350,
|
|
27
|
+
"width": 50,
|
|
28
|
+
"height": 700
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
"id": "rightWall",
|
|
34
|
+
"blueprint": "blueprints/block.scene",
|
|
35
|
+
"components": {
|
|
36
|
+
"Layout": {
|
|
37
|
+
"x": 475,
|
|
38
|
+
"y": 350,
|
|
39
|
+
"width": 50,
|
|
40
|
+
"height": 700
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"id": "crate1",
|
|
46
|
+
"blueprint": "blueprints/crate.scene",
|
|
47
|
+
"components": {
|
|
48
|
+
"Layout": {
|
|
49
|
+
"x": 174,
|
|
50
|
+
"y": 324
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"id": "crate2",
|
|
56
|
+
"blueprint": "blueprints/crate.scene",
|
|
57
|
+
"components": {
|
|
58
|
+
"Layout": {
|
|
59
|
+
"x": 254,
|
|
60
|
+
"y": 204
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
"id": "crate3",
|
|
66
|
+
"blueprint": "blueprints/crate.scene",
|
|
67
|
+
"components": {
|
|
68
|
+
"Layout": {
|
|
69
|
+
"x": 324,
|
|
70
|
+
"y": 324
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
12
74
|
]
|
|
13
75
|
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// This kit's `onUpdate` hook: the CLI runs it in a deck whose pin on the kit has
|
|
3
|
+
// just moved (castle.json `onUpdate`, see the CLI's `importUpdateHook.ts`).
|
|
4
|
+
//
|
|
5
|
+
// The one thing it does today is state a pre-origin deck's top-left anchoring
|
|
6
|
+
// outright, so the center-origin default can't move anything --
|
|
7
|
+
// `engine/layoutOriginMigration.js` holds the whole decision, this file is only
|
|
8
|
+
// the disk. The version the deck was pinned at is the gate: an omitted origin is
|
|
9
|
+
// ambiguous on disk, and the pin is the only thing that says which way it was
|
|
10
|
+
// meant.
|
|
11
|
+
//
|
|
12
|
+
// Also runnable by hand, which is how a deck on a `builtin` pin (never
|
|
13
|
+
// auto-updated) or a deck someone hand-synced gets migrated:
|
|
14
|
+
//
|
|
15
|
+
// node <repo>/kits/physics-2d/scripts/onUpdate.mjs --deck . --from <ISO> [--to <ISO>] [--dry-run] [--force]
|
|
16
|
+
|
|
17
|
+
import * as fs from 'fs';
|
|
18
|
+
import * as path from 'path';
|
|
19
|
+
import { pathToFileURL } from 'url';
|
|
20
|
+
import {
|
|
21
|
+
LAST_TOP_LEFT_PUBLISHED_VERSION,
|
|
22
|
+
needsTopLeftStamp,
|
|
23
|
+
stampTopLeftOrigin,
|
|
24
|
+
} from '../engine/layoutOriginMigration.js';
|
|
25
|
+
|
|
26
|
+
const IMPORTS_DIR = 'imports';
|
|
27
|
+
const FORKS_DIR = 'forks';
|
|
28
|
+
// Not deck content: build output, dependencies, runtime state, history.
|
|
29
|
+
const SKIP_DIRS = new Set(['node_modules', '.castle', '.git', 'dist']);
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Every `.scene` in the deck as whole-deck key -> text, plus the map of forked
|
|
33
|
+
* imported paths.
|
|
34
|
+
*
|
|
35
|
+
* The runtime globs every `.scene` in the deck, so a scene is wherever the deck
|
|
36
|
+
* put it (`levels/2.scene` is as real as `scenes/main.scene`) and every one of
|
|
37
|
+
* them has to be walked. Imported scenes are read too, unwritable as they are: a
|
|
38
|
+
* template's text is what says whether an instance inherits an origin from it.
|
|
39
|
+
*
|
|
40
|
+
* A fork's text is keyed at the imported path it OVERLAYS, because that is the
|
|
41
|
+
* path every ref in the deck names; `forks` remembers where it is actually
|
|
42
|
+
* written.
|
|
43
|
+
*/
|
|
44
|
+
function collectSceneFiles(deckDir) {
|
|
45
|
+
const files = {};
|
|
46
|
+
const forks = {};
|
|
47
|
+
const walk = (absDir, relDir) => {
|
|
48
|
+
let entries;
|
|
49
|
+
try {
|
|
50
|
+
entries = fs.readdirSync(absDir, { withFileTypes: true });
|
|
51
|
+
} catch {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
for (const entry of entries) {
|
|
55
|
+
if (SKIP_DIRS.has(entry.name)) continue;
|
|
56
|
+
const rel = relDir ? `${relDir}/${entry.name}` : entry.name;
|
|
57
|
+
if (entry.isDirectory()) {
|
|
58
|
+
walk(path.join(absDir, entry.name), rel);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (!entry.name.endsWith('.scene')) continue;
|
|
62
|
+
const text = fs.readFileSync(path.join(absDir, entry.name), 'utf-8');
|
|
63
|
+
if (rel.startsWith(`${FORKS_DIR}/`)) {
|
|
64
|
+
const imported = `${IMPORTS_DIR}/${rel.slice(FORKS_DIR.length + 1)}`;
|
|
65
|
+
files[imported] = text;
|
|
66
|
+
forks[imported] = rel;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
files[rel] = text;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
walk(deckDir, '');
|
|
73
|
+
return { files, forks };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// The writes, applied. `imports/` is refused here as well as excluded upstream:
|
|
77
|
+
// a hook writing into somebody else's deck is the one mistake worth being unable
|
|
78
|
+
// to make by accident.
|
|
79
|
+
function applyWrites(deckDir, writes) {
|
|
80
|
+
for (const { path: rel, text } of writes) {
|
|
81
|
+
if (rel.startsWith(`${IMPORTS_DIR}/`)) {
|
|
82
|
+
throw new Error(`refusing to write ${rel}: files under ${IMPORTS_DIR}/ belong to their own deck`);
|
|
83
|
+
}
|
|
84
|
+
fs.writeFileSync(path.join(deckDir, rel), text, 'utf-8');
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// A `Layout` built in code has no file to stamp, so it takes the new default at
|
|
89
|
+
// runtime and this migration cannot help it. Read-only: point at them and let a
|
|
90
|
+
// person read the code.
|
|
91
|
+
function inlineLayoutFiles(deckDir) {
|
|
92
|
+
const dir = path.join(deckDir, 'behaviors');
|
|
93
|
+
let names;
|
|
94
|
+
try {
|
|
95
|
+
names = fs.readdirSync(dir);
|
|
96
|
+
} catch {
|
|
97
|
+
return [];
|
|
98
|
+
}
|
|
99
|
+
return names
|
|
100
|
+
.filter((name) => name.endsWith('.jsx') || name.endsWith('.js'))
|
|
101
|
+
.filter((name) => {
|
|
102
|
+
try {
|
|
103
|
+
return fs.readFileSync(path.join(dir, name), 'utf-8').includes('spawnActor(');
|
|
104
|
+
} catch {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
})
|
|
108
|
+
.map((name) => `behaviors/${name}`);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function summarize(stats, writes) {
|
|
112
|
+
const parts = [
|
|
113
|
+
`${writes.length} file${writes.length === 1 ? '' : 's'}`,
|
|
114
|
+
`${stats.templates} blueprint template${stats.templates === 1 ? '' : 's'}`,
|
|
115
|
+
`${stats.instances} instance${stats.instances === 1 ? '' : 's'}`,
|
|
116
|
+
];
|
|
117
|
+
if (stats.orphans > 0) parts.push(`${stats.orphans} un-migrated actor${stats.orphans === 1 ? '' : 's'}`);
|
|
118
|
+
return parts.join(', ');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The hook. Idempotent by construction: a stamped deck has an explicit origin
|
|
123
|
+
* everywhere the stamp goes, so a second run finds nothing to write -- which is
|
|
124
|
+
* what makes it safe after an `update-import --revert` and after a rolled-back
|
|
125
|
+
* failure.
|
|
126
|
+
*/
|
|
127
|
+
export default async function onUpdate({ deckDir, alias = 'castle.physics-2d', from, to, log = console.log, dryRun = false, force = false } = {}) {
|
|
128
|
+
if (!deckDir) throw new Error('onUpdate needs a deckDir');
|
|
129
|
+
if (!force && !needsTopLeftStamp({ from, to })) {
|
|
130
|
+
log(
|
|
131
|
+
`layout origin: nothing to do (${from} -> ${to}; the default changed after ${LAST_TOP_LEFT_PUBLISHED_VERSION}).`,
|
|
132
|
+
);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const { files, forks } = collectSceneFiles(deckDir);
|
|
136
|
+
const { writes, stats } = stampTopLeftOrigin(files, { forks });
|
|
137
|
+
for (const skipped of stats.unparseable) log(`layout origin: skipped ${skipped} (not valid JSON).`);
|
|
138
|
+
if (writes.length === 0) {
|
|
139
|
+
log(`layout origin: already explicit in all ${stats.considered} scene file(s); nothing written.`);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (dryRun) {
|
|
143
|
+
log(`layout origin: WOULD write ${summarize(stats, writes)}:`);
|
|
144
|
+
for (const { path: rel } of writes) log(`layout origin: ${rel}`);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
applyWrites(deckDir, writes);
|
|
148
|
+
log(
|
|
149
|
+
`layout origin: wrote explicit originX/originY 0 into ${summarize(stats, writes)} -- ` +
|
|
150
|
+
`nothing moved, ${alias} now reads them the same way it always did. ` +
|
|
151
|
+
'Convert a blueprint to a center origin from the inspector when you want to; ' +
|
|
152
|
+
'`castle-web list-versions` names the version saved just before this, and ' +
|
|
153
|
+
'`castle-web restore-version <id>` puts these files back.',
|
|
154
|
+
);
|
|
155
|
+
const inline = inlineLayoutFiles(deckDir);
|
|
156
|
+
if (inline.length > 0) {
|
|
157
|
+
log(
|
|
158
|
+
`layout origin: check inline Layouts in ${inline.join(', ')} -- a Layout built in code has no ` +
|
|
159
|
+
'file to stamp and takes the new center default.',
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// -- direct CLI ---------------------------------------------------------------
|
|
165
|
+
|
|
166
|
+
function flag(name) {
|
|
167
|
+
return process.argv.includes(`--${name}`);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function value(name, fallback) {
|
|
171
|
+
const at = process.argv.indexOf(`--${name}`);
|
|
172
|
+
return at >= 0 && process.argv[at + 1] ? process.argv[at + 1] : fallback;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async function main() {
|
|
176
|
+
const from = value('from');
|
|
177
|
+
if (!from && !flag('force')) {
|
|
178
|
+
console.error(
|
|
179
|
+
'Usage: node scripts/onUpdate.mjs [--deck DIR] --from ISO [--to ISO] [--dry-run] [--force]\n' +
|
|
180
|
+
'--from is the kit version the deck was pinned at; it is what decides whether an absent ' +
|
|
181
|
+
'origin meant top-left. --force runs anyway.',
|
|
182
|
+
);
|
|
183
|
+
process.exit(1);
|
|
184
|
+
}
|
|
185
|
+
await onUpdate({
|
|
186
|
+
deckDir: path.resolve(value('deck', '.')),
|
|
187
|
+
from,
|
|
188
|
+
to: value('to', new Date().toISOString()),
|
|
189
|
+
dryRun: flag('dry-run'),
|
|
190
|
+
force: flag('force'),
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (process.argv[1] && pathToFileURL(process.argv[1]).href === import.meta.url) {
|
|
195
|
+
main().catch((e) => {
|
|
196
|
+
console.error(e instanceof Error ? e.message : String(e));
|
|
197
|
+
process.exit(1);
|
|
198
|
+
});
|
|
199
|
+
}
|
|
@@ -18,17 +18,6 @@
|
|
|
18
18
|
"file": "scenes/main.scene"
|
|
19
19
|
}
|
|
20
20
|
],
|
|
21
|
-
"hiddenPaths": [],
|
|
22
|
-
"visiblePaths": [
|
|
23
|
-
"drawings/**",
|
|
24
|
-
"models/**",
|
|
25
|
-
"scenes/**",
|
|
26
|
-
"blueprints/**",
|
|
27
|
-
"behaviors/**",
|
|
28
|
-
"assets/**",
|
|
29
|
-
"theme.style",
|
|
30
|
-
"fonts.generated.js"
|
|
31
|
-
],
|
|
32
21
|
"fileTypes": [
|
|
33
22
|
{
|
|
34
23
|
"ext": ".scene",
|
|
@@ -1,19 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"title": "real-time",
|
|
3
|
-
"editor": {
|
|
4
|
-
"hiddenPaths": [],
|
|
5
|
-
"visiblePaths": [
|
|
6
|
-
"code/**"
|
|
7
|
-
]
|
|
8
|
-
},
|
|
9
3
|
"imports": {
|
|
10
4
|
"castle.base": {
|
|
11
5
|
"deckId": "yRcmH4_aYllE",
|
|
12
|
-
"version": "2026-09-
|
|
6
|
+
"version": "2026-09-11T20:21:04.335Z"
|
|
13
7
|
}
|
|
14
8
|
},
|
|
15
9
|
"autoUpdateWhenImported": true,
|
|
16
10
|
"deckId": "xFVr-afOLuUQ",
|
|
17
11
|
"cardId": "w6G45sy3_P_m",
|
|
18
|
-
"publishedVersion": "2026-09-
|
|
12
|
+
"publishedVersion": "2026-09-16T21:18:06.448Z"
|
|
19
13
|
}
|
|
@@ -91,12 +91,26 @@ async function write(session, values, operation) {
|
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
let announced = false;
|
|
94
|
+
let unpublishedStorageAnnounced = false;
|
|
94
95
|
|
|
95
96
|
// Operations that have already reported a failure, so a broken setup is named
|
|
96
97
|
// once.
|
|
97
98
|
const reported = new Set();
|
|
98
99
|
|
|
99
100
|
function report(operation, error) {
|
|
101
|
+
if (
|
|
102
|
+
error?.code === 'CAULDRON_STORAGE_UNAVAILABLE' &&
|
|
103
|
+
String(error?.message ?? '').startsWith('Storage needs a published deck.')
|
|
104
|
+
) {
|
|
105
|
+
if (!unpublishedStorageAnnounced) {
|
|
106
|
+
unpublishedStorageAnnounced = true;
|
|
107
|
+
console.info(
|
|
108
|
+
'[persist] deck storage is off until the deck is published (save-deck); ' +
|
|
109
|
+
'the room will not persist across serve restarts.'
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
100
114
|
if (reported.has(operation)) {
|
|
101
115
|
return;
|
|
102
116
|
}
|