castle-web-cli 0.4.157 → 0.4.159
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-failures.d.ts +1 -0
- package/dist/agent-failures.js +6 -4
- package/dist/agent-prompts.js +4 -0
- package/dist/agent.d.ts +11 -0
- package/dist/agent.js +175 -15
- package/dist/bundle.js +8 -2
- package/dist/castle-host/host.js +4 -2
- package/dist/castleJson.js +6 -1
- package/dist/check.d.ts +1 -0
- package/dist/check.js +25 -0
- package/dist/devSessionServer.js +31 -7
- package/dist/imports.js +1 -19
- package/dist/index.js +15 -1
- package/dist/init.d.ts +2 -2
- package/dist/init.js +78 -42
- package/dist/mcpPlaytest.d.ts +1 -0
- package/dist/mcpPlaytest.js +182 -0
- package/dist/metering.d.ts +24 -0
- package/dist/metering.js +87 -0
- package/dist/sdkResolve.d.ts +3 -0
- package/dist/sdkResolve.js +55 -0
- package/dist/serve.js +50 -4
- package/dist/serverBundle.js +8 -3
- package/dist/shell/assets/{index-DsDkYFvP.js → index-DMSoTyXh.js} +64 -64
- package/dist/shell/assets/{index-DwezawBs.css → index-DfZmu3XC.css} +1 -1
- package/dist/shell/index.html +2 -2
- package/dist/vitePlugins.d.ts +1 -0
- package/dist/vitePlugins.js +20 -0
- package/kits/base/CLAUDE.md +196 -0
- package/kits/{blank → base}/castle.json +5 -2
- package/kits/base/editorHost.js +172 -0
- package/kits/{blank → base}/node_modules/.package-lock.json +3 -2
- package/kits/{blank → base}/package-lock.json +7 -4
- package/kits/{blank → base}/package.json +4 -2
- package/kits/base/sdk/README.md +569 -0
- package/kits/base/sdk/castle.d.ts +27 -0
- package/kits/base/sdk/castle.js +16 -0
- package/kits/base/sdk/commands.d.ts +181 -0
- package/kits/base/sdk/commands.js +16 -0
- package/kits/base/sdk/context.d.ts +17 -0
- package/kits/base/sdk/context.js +15 -0
- package/kits/base/sdk/errors.d.ts +22 -0
- package/kits/base/sdk/errors.js +16 -0
- package/kits/base/sdk/files.d.ts +15 -0
- package/kits/base/sdk/files.js +52 -0
- package/kits/base/sdk/haptics.d.ts +6 -0
- package/kits/base/sdk/haptics.js +36 -0
- package/kits/base/sdk/leaderboard.d.ts +26 -0
- package/kits/base/sdk/leaderboard.js +296 -0
- package/kits/base/sdk/lifecycle.d.ts +4 -0
- package/kits/base/sdk/lifecycle.js +13 -0
- package/kits/base/sdk/multiplayer.d.ts +43 -0
- package/kits/base/sdk/multiplayer.js +464 -0
- package/kits/base/sdk/multiplayerProtocol.d.ts +51 -0
- package/kits/base/sdk/multiplayerProtocol.js +5 -0
- package/kits/base/sdk/passes.d.ts +7 -0
- package/kits/base/sdk/passes.js +84 -0
- package/kits/base/sdk/portal.d.ts +7 -0
- package/kits/base/sdk/portal.js +35 -0
- package/kits/base/sdk/runtime.d.ts +62 -0
- package/kits/base/sdk/runtime.js +970 -0
- package/kits/base/sdk/saveQueue.d.ts +27 -0
- package/kits/base/sdk/saveQueue.js +205 -0
- package/kits/base/sdk/server/platformHandle.d.ts +25 -0
- package/kits/base/sdk/server/platformHandle.js +1 -0
- package/kits/base/sdk/server/wrapper.d.ts +28 -0
- package/kits/base/sdk/server/wrapper.js +109 -0
- package/kits/base/sdk/storage.d.ts +16 -0
- package/kits/base/sdk/storage.js +323 -0
- package/kits/base/sdk/time.d.ts +17 -0
- package/kits/base/sdk/time.js +120 -0
- package/kits/base/sdk/transport.d.ts +17 -0
- package/kits/base/sdk/transport.js +141 -0
- package/kits/base/sdk/types.d.ts +3 -0
- package/kits/base/sdk/types.js +1 -0
- package/kits/base/sdk/user.d.ts +10 -0
- package/kits/base/sdk/user.js +45 -0
- package/kits/multiplayer-2d/.prettierrc +5 -0
- package/kits/multiplayer-2d/CLAUDE.md +129 -0
- package/kits/multiplayer-2d/behaviors/Box.jsx +92 -0
- package/kits/multiplayer-2d/behaviors/Player.jsx +86 -0
- package/kits/multiplayer-2d/blueprints/crate.scene +13 -0
- package/kits/multiplayer-2d/blueprints/other-player.scene +13 -0
- package/kits/multiplayer-2d/blueprints/player.scene +15 -0
- package/kits/multiplayer-2d/blueprints/wall.scene +12 -0
- package/kits/multiplayer-2d/castle.json +39 -0
- package/kits/multiplayer-2d/code/client/avatars.js +93 -0
- package/kits/multiplayer-2d/code/client/poses.js +168 -0
- package/kits/multiplayer-2d/code/server/index.js +26 -0
- package/kits/multiplayer-2d/code/server/limits.js +99 -0
- package/kits/multiplayer-2d/code/server/ownership.js +177 -0
- package/kits/multiplayer-2d/code/server/players.js +127 -0
- package/kits/multiplayer-2d/code/server/sceneBodies.js +98 -0
- package/kits/multiplayer-2d/code/server/world.js +337 -0
- package/kits/multiplayer-2d/code/systems/multiplayer.js +424 -0
- package/kits/multiplayer-2d/eslint.config.js +21 -0
- package/kits/multiplayer-2d/fonts.generated.js +9 -0
- package/kits/multiplayer-2d/index.html +25 -0
- package/kits/multiplayer-2d/main.jsx +10 -0
- package/kits/multiplayer-2d/package-lock.json +351 -0
- package/kits/multiplayer-2d/package.json +30 -0
- package/kits/multiplayer-2d/scenes/main.scene +20 -0
- package/kits/multiplayer-2d/test/two-client-test.mjs +341 -0
- package/kits/multiplayer-2d/theme.style +4 -0
- package/kits/multiplayer-3d/.prettierrc +5 -0
- package/kits/multiplayer-3d/CLAUDE.md +215 -0
- package/kits/multiplayer-3d/blueprints/ball.scene +21 -0
- package/kits/multiplayer-3d/blueprints/other-player.scene +21 -0
- package/kits/multiplayer-3d/castle.json +43 -0
- package/kits/multiplayer-3d/code/client/avatars.js +136 -0
- package/kits/multiplayer-3d/code/client/nameTags.js +101 -0
- package/kits/multiplayer-3d/code/client/poses.js +220 -0
- package/kits/multiplayer-3d/code/server/index.js +23 -0
- package/kits/multiplayer-3d/code/server/limits.js +107 -0
- package/kits/multiplayer-3d/code/server/ownership.js +157 -0
- package/kits/multiplayer-3d/code/server/players.js +114 -0
- package/kits/multiplayer-3d/code/server/sceneBodies.js +88 -0
- package/kits/multiplayer-3d/code/server/world.js +306 -0
- package/kits/multiplayer-3d/code/systems/multiplayer.js +427 -0
- package/kits/multiplayer-3d/eslint.config.js +20 -0
- package/kits/multiplayer-3d/fonts.generated.js +9 -0
- package/kits/multiplayer-3d/index.html +22 -0
- package/kits/multiplayer-3d/main.jsx +9 -0
- package/kits/multiplayer-3d/package-lock.json +368 -0
- package/kits/multiplayer-3d/package.json +32 -0
- package/kits/multiplayer-3d/scenes/main.scene +409 -0
- package/kits/multiplayer-3d/theme.style +3 -0
- package/kits/physics-2d/CLAUDE.md +6 -0
- package/kits/physics-2d/castle.json +7 -1
- package/kits/physics-2d/editors/behaviorRegistry.js +5 -0
- package/kits/physics-2d/editors/editorRegistry.jsx +3 -1
- package/kits/physics-2d/engine/files.js +9 -5
- package/kits/physics-2d/engine/systemRegistry.js +2 -0
- package/kits/physics-2d/engine/ui.module.css +11 -0
- package/kits/physics-2d/package-lock.json +8 -2
- package/kits/physics-2d/package.json +2 -2
- package/kits/physics-3d/CLAUDE.md +4 -4
- package/kits/physics-3d/behaviors/Shape.jsx +1 -1
- package/kits/physics-3d/blueprints/crate.scene +1 -1
- package/kits/physics-3d/blueprints/door.scene +4 -4
- package/kits/physics-3d/blueprints/pillar.scene +1 -1
- package/kits/physics-3d/blueprints/platform.scene +1 -1
- package/kits/physics-3d/blueprints/statue.scene +1 -1
- package/kits/physics-3d/blueprints/wall.scene +1 -1
- package/kits/physics-3d/castle.json +8 -11
- package/kits/physics-3d/docs/pxmodel-format.md +1 -1
- package/kits/physics-3d/engine3d/materials.js +3 -3
- package/kits/physics-3d/engine3d/modelFiles.js +4 -2
- package/kits/physics-3d/engine3d/world3d.js +1 -1
- package/kits/physics-3d/fonts.generated.js +9 -0
- package/kits/physics-3d/main.jsx +1 -1
- package/kits/physics-3d/package-lock.json +85 -2
- package/kits/physics-3d/package.json +6 -2
- package/kits/physics-3d/scenes/main.scene +1 -1
- package/kits/physics-3d/scenes/model-lab.scene +1 -1
- package/kits/real-time/.prettierrc +5 -0
- package/kits/real-time/CLAUDE.md +60 -0
- package/kits/real-time/castle.json +19 -0
- package/kits/real-time/code/client/clock.js +68 -0
- package/kits/real-time/code/client/connection.js +134 -0
- package/kits/real-time/code/client/gameHooks.js +50 -0
- package/kits/real-time/code/client/interpolate.js +82 -0
- package/kits/real-time/code/client/messages.js +55 -0
- package/kits/real-time/code/client/ownership.js +94 -0
- package/kits/real-time/code/client/smooth.js +54 -0
- package/kits/real-time/code/server/blockDelta.js +48 -0
- package/kits/real-time/code/server/gameHooks.js +47 -0
- package/kits/real-time/code/server/limits.js +35 -0
- package/kits/real-time/code/server/session.js +251 -0
- package/kits/real-time/eslint.config.js +22 -0
- package/kits/real-time/index.html +45 -0
- package/kits/real-time/package.json +14 -0
- package/kits/{tabletop → turn-based}/CLAUDE.md +42 -49
- package/kits/turn-based/blueprints/board.scene +15 -0
- package/kits/turn-based/blueprints/square.scene +25 -0
- package/kits/turn-based/castle.json +37 -0
- package/kits/turn-based/code/behaviors/Board.jsx +169 -0
- package/kits/turn-based/code/behaviors/Square.jsx +26 -0
- package/kits/turn-based/code/server/index.js +152 -0
- package/kits/turn-based/drawings/square.sprite +25 -0
- package/kits/turn-based/eslint.config.js +12 -0
- package/kits/turn-based/fonts.generated.js +9 -0
- package/kits/{tabletop → turn-based}/index.html +5 -3
- package/kits/{tabletop → turn-based}/main.jsx +2 -2
- package/kits/{tabletop → turn-based}/package-lock.json +17 -4
- package/kits/{tabletop → turn-based}/package.json +5 -2
- package/kits/turn-based/room.js +289 -0
- package/kits/turn-based/scenes/main.scene +227 -0
- package/kits/{tabletop → turn-based}/testing.js +131 -99
- package/kits/turn-based/theme.style +3 -0
- package/package.json +1 -1
- package/kits/blank/CLAUDE.md +0 -198
- package/kits/tabletop/castle.json +0 -34
- package/kits/tabletop/room.js +0 -269
- /package/kits/{blank → base}/index.html +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/README.md +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/catalog.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/catalog.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/face.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/face.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Bore.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Bore.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/CourierPrime.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/CourierPrime.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/DMSans.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/DMSans.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Glacier.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Glacier.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/HelicoCentrica.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/HelicoCentrica.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Piazzolla.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Piazzolla.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Synco.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Synco.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Tektur.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Tektur.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/YatraOne.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/YatraOne.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/index.d.ts +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/index.js +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/CC-BY-ND-4.0.txt +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/FONTS.md +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/OFL-1.1.txt +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/package.json +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Bore.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/CourierPrime.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/DMSans.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Glacier.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/HelicoCentrica.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Piazzolla.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/SOURCE.txt +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Synco.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Tektur.woff2 +0 -0
- /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/YatraOne.woff2 +0 -0
- /package/kits/physics-3d/drawings/{crate.pxart → crate.sprite} +0 -0
- /package/kits/physics-3d/drawings/{door-edge.pxart → door-edge.sprite} +0 -0
- /package/kits/physics-3d/drawings/{door.pxart → door.sprite} +0 -0
- /package/kits/physics-3d/drawings/{face.pxart → face.sprite} +0 -0
- /package/kits/physics-3d/drawings/{floor.pxart → floor.sprite} +0 -0
- /package/kits/physics-3d/drawings/{platform.pxart → platform.sprite} +0 -0
- /package/kits/physics-3d/drawings/{statue.pxart → statue.sprite} +0 -0
- /package/kits/physics-3d/drawings/{wall.pxart → wall.sprite} +0 -0
- /package/kits/{tabletop → turn-based}/.prettierrc +0 -0
|
@@ -26,7 +26,8 @@
|
|
|
26
26
|
"blueprints/**",
|
|
27
27
|
"behaviors/**",
|
|
28
28
|
"assets/**",
|
|
29
|
-
"theme.style"
|
|
29
|
+
"theme.style",
|
|
30
|
+
"fonts.generated.js"
|
|
30
31
|
],
|
|
31
32
|
"fileTypes": [
|
|
32
33
|
{
|
|
@@ -37,14 +38,6 @@
|
|
|
37
38
|
"editor": "kit",
|
|
38
39
|
"data": true
|
|
39
40
|
},
|
|
40
|
-
{
|
|
41
|
-
"ext": ".pxart",
|
|
42
|
-
"label": "Pixel Art",
|
|
43
|
-
"new": "New drawing",
|
|
44
|
-
"icon": "layer-group",
|
|
45
|
-
"editor": "kit",
|
|
46
|
-
"data": true
|
|
47
|
-
},
|
|
48
41
|
{
|
|
49
42
|
"ext": ".pxmodel",
|
|
50
43
|
"label": "Model",
|
|
@@ -65,12 +58,16 @@
|
|
|
65
58
|
"imports": {
|
|
66
59
|
"castle.physics-2d": {
|
|
67
60
|
"deckId": "ckRZGFW4iPrx",
|
|
68
|
-
"version": "2026-08-
|
|
61
|
+
"version": "2026-08-31T02:59:16.016Z"
|
|
62
|
+
},
|
|
63
|
+
"castle.base": {
|
|
64
|
+
"deckId": "yRcmH4_aYllE",
|
|
65
|
+
"version": "2026-09-01T01:02:34.326Z"
|
|
69
66
|
}
|
|
70
67
|
},
|
|
71
68
|
"main": "main.jsx",
|
|
72
69
|
"autoUpdateWhenImported": true,
|
|
73
70
|
"deckId": "JH0SclbPVP0y",
|
|
74
71
|
"cardId": "eXfAaUdbSU5A",
|
|
75
|
-
"publishedVersion": "2026-
|
|
72
|
+
"publishedVersion": "2026-09-02T03:33:52.370Z"
|
|
76
73
|
}
|
|
@@ -77,7 +77,7 @@ Geometry -- same vocabulary as the `Shape` behavior, plus an editable mesh:
|
|
|
77
77
|
|
|
78
78
|
Material -- same fields as `Shape`, per part:
|
|
79
79
|
|
|
80
|
-
- `color`, `art` (a `.
|
|
80
|
+
- `color`, `art` (a `.sprite`/image path, or on boxes a per-face object
|
|
81
81
|
`{ all, sides, front, back, left, right, top, bottom }`, any ref may pick a
|
|
82
82
|
frame with `path#<n>`), `artRepeat: [u, v]`, `tint`, `roughness`,
|
|
83
83
|
`metalness`, `clearcoat`, `emissive`, `emissiveIntensity`.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Mesh + material construction from a Shape component. Materials are
|
|
2
2
|
// MeshPhysicalMaterial throughout (the vase look): PBR with a small set of
|
|
3
|
-
// knobs, albedo optionally baked from a `.
|
|
3
|
+
// knobs, albedo optionally baked from a `.sprite` (or uploaded image) with
|
|
4
4
|
// nearest filtering so pixels stay crisp.
|
|
5
5
|
//
|
|
6
6
|
// `Shape.art` is either one ref for the whole shape, or (boxes only) a per-face
|
|
@@ -35,7 +35,7 @@ function albedoTexture(art, frame) {
|
|
|
35
35
|
return texture;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
// 'drawings/door.
|
|
38
|
+
// 'drawings/door.sprite#1' -> { path, frame }
|
|
39
39
|
function parseArtRef(ref) {
|
|
40
40
|
const hash = ref.lastIndexOf('#');
|
|
41
41
|
if (hash === -1) return { path: ref, frame: 0 };
|
|
@@ -65,7 +65,7 @@ function artRefsOf(shape) {
|
|
|
65
65
|
return Object.values(shape.art).filter((ref) => typeof ref === 'string');
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
// Stable id per parsed art OBJECT. A repainted
|
|
68
|
+
// Stable id per parsed art OBJECT. A repainted sprite re-parses into a fresh
|
|
69
69
|
// object, so identity (not just readiness) has to be part of a signature --
|
|
70
70
|
// otherwise an edited texture never reaches an already-built mesh.
|
|
71
71
|
let artIdCounter = 0;
|
|
@@ -28,7 +28,7 @@ export const initialFiles3D = {
|
|
|
28
28
|
),
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
-
const DATA_EXTS = ['.scene', '.pxart', '.drawing', '.pxmodel'];
|
|
31
|
+
const DATA_EXTS = ['.scene', '.sprite', '.pxart', '.drawing', '.pxmodel'];
|
|
32
32
|
const CODE_EXTS = ['.js', '.jsx', '.ts', '.tsx', '.css'];
|
|
33
33
|
|
|
34
34
|
// Extensions the DECK declared that this kit has never heard of, learned from
|
|
@@ -151,7 +151,9 @@ export function useStableSprites(files) {
|
|
|
151
151
|
if (isImageFile(path)) sprites[path] = imageArt(path, url);
|
|
152
152
|
}
|
|
153
153
|
for (const [path, text] of Object.entries(files)) {
|
|
154
|
-
|
|
154
|
+
// `.pxart` is the old extension for the same format; decks saved before the
|
|
155
|
+
// rename still carry it.
|
|
156
|
+
if (!path.endsWith('.sprite') && !path.endsWith('.pxart')) continue;
|
|
155
157
|
let cached = cacheRef.current.get(path);
|
|
156
158
|
if (!cached || cached.text !== text) {
|
|
157
159
|
const art = parseFull(text);
|
|
@@ -104,7 +104,7 @@ function addGround(scene) {
|
|
|
104
104
|
return { ground, grid };
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
// A scene can dress the ground: `"ground": { "art": "drawings/floor.
|
|
107
|
+
// A scene can dress the ground: `"ground": { "art": "drawings/floor.sprite",
|
|
108
108
|
// "tile": 2, "color": "#..." }` at the scene file's top level. `tile` is the
|
|
109
109
|
// world size of one texture repeat (meters). The line grid only shows on the
|
|
110
110
|
// untextured ground -- over art it reads as noise.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Generated by the Style editor. Do not edit -- picking a font rewrites it.
|
|
2
|
+
//
|
|
3
|
+
// `theme.style` records that this deck's font is DMSans; this module is what
|
|
4
|
+
// puts DMSans's bytes in the published bundle. It has to name the face in a
|
|
5
|
+
// static import: reaching it through the name in `theme.style` instead would
|
|
6
|
+
// inline all nine Castle faces into the deck, since no bundler can tell which
|
|
7
|
+
// one a runtime lookup reads.
|
|
8
|
+
export { installDMSans as installDeckFont } from 'castle-web-fonts';
|
|
9
|
+
export const deckFont = 'DMSans';
|
package/kits/physics-3d/main.jsx
CHANGED
|
@@ -15,7 +15,7 @@ if (!root) throw new Error('Missing root element');
|
|
|
15
15
|
// Route on URL params, same shape as the physics-2d kit's entry:
|
|
16
16
|
// (no edit / ?edit=0) -> play the deck (playtest panel)
|
|
17
17
|
// ?file=<path>[&editor=<id>] -> a single rich editor for that file
|
|
18
|
-
// `.scene` opens the kit's own 3d scene editor; `.
|
|
18
|
+
// `.scene` opens the kit's own 3d scene editor; `.sprite` (and media viewers)
|
|
19
19
|
// reuse the imported physics-2d kit's editors.
|
|
20
20
|
const params = new URLSearchParams(window.location.search);
|
|
21
21
|
function pick() {
|
|
@@ -12,10 +12,13 @@
|
|
|
12
12
|
"@codemirror/state": "^6.6.0",
|
|
13
13
|
"@codemirror/view": "^6.41.1",
|
|
14
14
|
"@dimforge/rapier3d-compat": "^0.19.2",
|
|
15
|
+
"@dnd-kit/core": "^6.3.1",
|
|
16
|
+
"@dnd-kit/modifiers": "^9.0.0",
|
|
17
|
+
"@dnd-kit/sortable": "^10.0.0",
|
|
18
|
+
"@dnd-kit/utilities": "^3.2.2",
|
|
15
19
|
"@fortawesome/free-solid-svg-icons": "^5.15.4",
|
|
16
20
|
"@lezer/highlight": "^1.2.3",
|
|
17
21
|
"castle-web-fonts": "^1.0.0",
|
|
18
|
-
"castle-web-sdk": "^0.4.13",
|
|
19
22
|
"codemirror": "^6.0.2",
|
|
20
23
|
"matter-js": "^0.20.0",
|
|
21
24
|
"react": "^19.2.4",
|
|
@@ -23,6 +26,7 @@
|
|
|
23
26
|
"three": "^0.180.0"
|
|
24
27
|
},
|
|
25
28
|
"devDependencies": {
|
|
29
|
+
"castle-web-sdk": "file:../../sdk",
|
|
26
30
|
"eslint": "^9.0.0",
|
|
27
31
|
"eslint-plugin-react-hooks": "^5.0.0",
|
|
28
32
|
"jscpd": "^4.0.5",
|
|
@@ -31,7 +35,8 @@
|
|
|
31
35
|
},
|
|
32
36
|
"../../sdk": {
|
|
33
37
|
"name": "castle-web-sdk",
|
|
34
|
-
"version": "0.4.
|
|
38
|
+
"version": "0.4.23",
|
|
39
|
+
"dev": true,
|
|
35
40
|
"devDependencies": {
|
|
36
41
|
"eslint": "^9.0.0",
|
|
37
42
|
"jscpd": "^4.0.5",
|
|
@@ -202,6 +207,74 @@
|
|
|
202
207
|
"integrity": "sha512-mMVdSj1PRTT108s9Swbu2GQOmHbn8kbJANRV5xfczL3s0T4vkgZAuoMRgvBzQcHanpKusbC0ZJj6z3mC3aj3vg==",
|
|
203
208
|
"license": "Apache-2.0"
|
|
204
209
|
},
|
|
210
|
+
"node_modules/@dnd-kit/accessibility": {
|
|
211
|
+
"version": "3.1.1",
|
|
212
|
+
"resolved": "https://registry.npmjs.org/@dnd-kit/accessibility/-/accessibility-3.1.1.tgz",
|
|
213
|
+
"integrity": "sha512-2P+YgaXF+gRsIihwwY1gCsQSYnu9Zyj2py8kY5fFvUM1qm2WA2u639R6YNVfU4GWr+ZM5mqEsfHZZLoRONbemw==",
|
|
214
|
+
"license": "MIT",
|
|
215
|
+
"dependencies": {
|
|
216
|
+
"tslib": "^2.0.0"
|
|
217
|
+
},
|
|
218
|
+
"peerDependencies": {
|
|
219
|
+
"react": ">=16.8.0"
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
"node_modules/@dnd-kit/core": {
|
|
223
|
+
"version": "6.3.1",
|
|
224
|
+
"resolved": "https://registry.npmjs.org/@dnd-kit/core/-/core-6.3.1.tgz",
|
|
225
|
+
"integrity": "sha512-xkGBRQQab4RLwgXxoqETICr6S5JlogafbhNsidmrkVv2YRs5MLwpjoF2qpiGjQt8S9AoxtIV603s0GIUpY5eYQ==",
|
|
226
|
+
"license": "MIT",
|
|
227
|
+
"peer": true,
|
|
228
|
+
"dependencies": {
|
|
229
|
+
"@dnd-kit/accessibility": "^3.1.1",
|
|
230
|
+
"@dnd-kit/utilities": "^3.2.2",
|
|
231
|
+
"tslib": "^2.0.0"
|
|
232
|
+
},
|
|
233
|
+
"peerDependencies": {
|
|
234
|
+
"react": ">=16.8.0",
|
|
235
|
+
"react-dom": ">=16.8.0"
|
|
236
|
+
}
|
|
237
|
+
},
|
|
238
|
+
"node_modules/@dnd-kit/modifiers": {
|
|
239
|
+
"version": "9.0.0",
|
|
240
|
+
"resolved": "https://registry.npmjs.org/@dnd-kit/modifiers/-/modifiers-9.0.0.tgz",
|
|
241
|
+
"integrity": "sha512-ybiLc66qRGuZoC20wdSSG6pDXFikui/dCNGthxv4Ndy8ylErY0N3KVxY2bgo7AWwIbxDmXDg3ylAFmnrjcbVvw==",
|
|
242
|
+
"license": "MIT",
|
|
243
|
+
"dependencies": {
|
|
244
|
+
"@dnd-kit/utilities": "^3.2.2",
|
|
245
|
+
"tslib": "^2.0.0"
|
|
246
|
+
},
|
|
247
|
+
"peerDependencies": {
|
|
248
|
+
"@dnd-kit/core": "^6.3.0",
|
|
249
|
+
"react": ">=16.8.0"
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
"node_modules/@dnd-kit/sortable": {
|
|
253
|
+
"version": "10.0.0",
|
|
254
|
+
"resolved": "https://registry.npmjs.org/@dnd-kit/sortable/-/sortable-10.0.0.tgz",
|
|
255
|
+
"integrity": "sha512-+xqhmIIzvAYMGfBYYnbKuNicfSsk4RksY2XdmJhT+HAC01nix6fHCztU68jooFiMUB01Ky3F0FyOvhG/BZrWkg==",
|
|
256
|
+
"license": "MIT",
|
|
257
|
+
"dependencies": {
|
|
258
|
+
"@dnd-kit/utilities": "^3.2.2",
|
|
259
|
+
"tslib": "^2.0.0"
|
|
260
|
+
},
|
|
261
|
+
"peerDependencies": {
|
|
262
|
+
"@dnd-kit/core": "^6.3.0",
|
|
263
|
+
"react": ">=16.8.0"
|
|
264
|
+
}
|
|
265
|
+
},
|
|
266
|
+
"node_modules/@dnd-kit/utilities": {
|
|
267
|
+
"version": "3.2.2",
|
|
268
|
+
"resolved": "https://registry.npmjs.org/@dnd-kit/utilities/-/utilities-3.2.2.tgz",
|
|
269
|
+
"integrity": "sha512-+MKAJEOfaBe5SmV6t34p80MMKhjvUz0vRrvVJbPT0WElzaOJ/1xs+D+KDv+tD/NE5ujfrChEcshd4fLn0wpiqg==",
|
|
270
|
+
"license": "MIT",
|
|
271
|
+
"dependencies": {
|
|
272
|
+
"tslib": "^2.0.0"
|
|
273
|
+
},
|
|
274
|
+
"peerDependencies": {
|
|
275
|
+
"react": ">=16.8.0"
|
|
276
|
+
}
|
|
277
|
+
},
|
|
205
278
|
"node_modules/@eslint-community/eslint-utils": {
|
|
206
279
|
"version": "4.10.1",
|
|
207
280
|
"resolved": "https://registry.npmjs.org/@eslint-community/eslint-utils/-/eslint-utils-4.10.1.tgz",
|
|
@@ -605,6 +678,7 @@
|
|
|
605
678
|
"integrity": "sha512-lGq+9yr1/GuAWaVYIHRjvvySG5/4VfKIvC8EWxStPdcDh/Ka7FG3twP6v4d5BkravUilhIAsG4Qj83t02LWUPQ==",
|
|
606
679
|
"dev": true,
|
|
607
680
|
"license": "MIT",
|
|
681
|
+
"peer": true,
|
|
608
682
|
"bin": {
|
|
609
683
|
"acorn": "bin/acorn"
|
|
610
684
|
},
|
|
@@ -1065,6 +1139,7 @@
|
|
|
1065
1139
|
"integrity": "sha512-DgZS62aPLXKlnxILS/AYCoRvHaZeXceIzlXPkkGGzJWSow1aEk0lbTlxUSlyjC8jcaKxAdOnTDz+o1JFSBsyjw==",
|
|
1066
1140
|
"dev": true,
|
|
1067
1141
|
"license": "MIT",
|
|
1142
|
+
"peer": true,
|
|
1068
1143
|
"dependencies": {
|
|
1069
1144
|
"@eslint-community/eslint-utils": "^4.8.0",
|
|
1070
1145
|
"@eslint-community/regexpp": "^4.12.1",
|
|
@@ -2374,6 +2449,7 @@
|
|
|
2374
2449
|
"resolved": "https://registry.npmjs.org/react/-/react-19.2.8.tgz",
|
|
2375
2450
|
"integrity": "sha512-PWaYA1L/q9u2u7xYQi+Y3L3Yfnie7XyLeaJICV1MGD6LprsBxcAqGjYyr0eY3p+QdsA+x/Irkt4Qif8D63+Sbw==",
|
|
2376
2451
|
"license": "MIT",
|
|
2452
|
+
"peer": true,
|
|
2377
2453
|
"engines": {
|
|
2378
2454
|
"node": ">=0.10.0"
|
|
2379
2455
|
}
|
|
@@ -2383,6 +2459,7 @@
|
|
|
2383
2459
|
"resolved": "https://registry.npmjs.org/react-dom/-/react-dom-19.2.8.tgz",
|
|
2384
2460
|
"integrity": "sha512-rVprimfGBG3DR+Tq0IQG2DT5PxKth1WIGDmj5yPmlzr4YBe7uyE+Du4oVqTDXZSHGGGXRtTJEGSSePyQCMBglQ==",
|
|
2385
2461
|
"license": "MIT",
|
|
2462
|
+
"peer": true,
|
|
2386
2463
|
"dependencies": {
|
|
2387
2464
|
"scheduler": "^0.27.0"
|
|
2388
2465
|
},
|
|
@@ -2619,6 +2696,12 @@
|
|
|
2619
2696
|
"dev": true,
|
|
2620
2697
|
"license": "MIT"
|
|
2621
2698
|
},
|
|
2699
|
+
"node_modules/tslib": {
|
|
2700
|
+
"version": "2.8.1",
|
|
2701
|
+
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz",
|
|
2702
|
+
"integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==",
|
|
2703
|
+
"license": "0BSD"
|
|
2704
|
+
},
|
|
2622
2705
|
"node_modules/type-check": {
|
|
2623
2706
|
"version": "0.4.0",
|
|
2624
2707
|
"resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz",
|
|
@@ -25,10 +25,13 @@
|
|
|
25
25
|
"@codemirror/state": "^6.6.0",
|
|
26
26
|
"@codemirror/view": "^6.41.1",
|
|
27
27
|
"@dimforge/rapier3d-compat": "^0.19.2",
|
|
28
|
+
"@dnd-kit/core": "^6.3.1",
|
|
29
|
+
"@dnd-kit/modifiers": "^9.0.0",
|
|
30
|
+
"@dnd-kit/sortable": "^10.0.0",
|
|
31
|
+
"@dnd-kit/utilities": "^3.2.2",
|
|
28
32
|
"@fortawesome/free-solid-svg-icons": "^5.15.4",
|
|
29
33
|
"@lezer/highlight": "^1.2.3",
|
|
30
34
|
"castle-web-fonts": "^1.0.0",
|
|
31
|
-
"castle-web-sdk": "^0.4.13",
|
|
32
35
|
"codemirror": "^6.0.2",
|
|
33
36
|
"matter-js": "^0.20.0",
|
|
34
37
|
"react": "^19.2.4",
|
|
@@ -39,6 +42,7 @@
|
|
|
39
42
|
"eslint": "^9.0.0",
|
|
40
43
|
"eslint-plugin-react-hooks": "^5.0.0",
|
|
41
44
|
"jscpd": "^4.0.5",
|
|
42
|
-
"prettier": "^3.8.3"
|
|
45
|
+
"prettier": "^3.8.3",
|
|
46
|
+
"castle-web-sdk": "file:../../sdk"
|
|
43
47
|
}
|
|
44
48
|
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# real-time
|
|
2
|
+
|
|
3
|
+
The multiplayer core that has no dimensions: the wire kinds, the clock, the pose
|
|
4
|
+
tracks, the smoothing, the ownership rule, the transport, and the session
|
|
5
|
+
process. It draws nothing, holds no scene, and never reads inside a pose.
|
|
6
|
+
|
|
7
|
+
Two kits sit on it -- `castle.multiplayer-2d` (matter) and
|
|
8
|
+
`castle.multiplayer-3d` (rapier). Each holds only what knows about its own
|
|
9
|
+
dimensions. A deck imports one of those for the physics and the actors, and
|
|
10
|
+
imports these modules from here directly.
|
|
11
|
+
|
|
12
|
+
## Layout
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
code/client/ the wire format, and the browser half of it
|
|
16
|
+
code/server/ what runs in the session process
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
`client/messages.js` is the exception to the split: it is the wire format, so
|
|
20
|
+
`server/session.js` reads it too.
|
|
21
|
+
|
|
22
|
+
## What a kit supplies
|
|
23
|
+
|
|
24
|
+
A pose is an opaque array of numbers here. Every operation that has to look
|
|
25
|
+
inside one is an argument:
|
|
26
|
+
|
|
27
|
+
| here | the caller passes |
|
|
28
|
+
| ---------------------------------------------- | ------------------------------------------------------- |
|
|
29
|
+
| `pushSample(track, simMs, pose, same)` | `same(a, b)` -- whether a pose repeats the last |
|
|
30
|
+
| `sampleAt(track, simMs, blend)` | `blend(a, b, f)` -- mix two poses |
|
|
31
|
+
| `smooth(store, id, target, dt, poses)` | `poses.far(a, b)` and `poses.advance(shown, target, f)` |
|
|
32
|
+
| `toClaim` / `toRelease` | `reach.radius` and `reach.distanceBetween(a, b)` |
|
|
33
|
+
| `poseChanged(before, after, count, epsilonAt)` | `epsilonAt(index)`, since a pose can mix units |
|
|
34
|
+
| `makeSession(parts)` | `world`, `players`, `ownership` and `poses` |
|
|
35
|
+
|
|
36
|
+
Each kit gathers those into one `code/client/poses.js`, so a call site passes
|
|
37
|
+
`Poses.blend` or `Poses` itself and there is one file to read to learn what a
|
|
38
|
+
pose is over there.
|
|
39
|
+
|
|
40
|
+
`connection.js` takes an already-packed pose in `sendMove`, so the transport
|
|
41
|
+
never learns the format.
|
|
42
|
+
|
|
43
|
+
`makeSession` reads two things off `poses`: `PLAYER_GEOMETRY`, how many leading
|
|
44
|
+
values of a player pose are geometry before the `clamped` flag, and `epsilonAt`,
|
|
45
|
+
which a kit whose pose values are all one unit leaves undefined.
|
|
46
|
+
|
|
47
|
+
## Rules for editing this kit
|
|
48
|
+
|
|
49
|
+
**Root-anchor anything that reaches out.** Once imported these files live at
|
|
50
|
+
`imports/<alias>/code/...`, so a relative path that escapes `code/` finds this
|
|
51
|
+
kit and not the deck. `gameHooks.js` globs `/code/server/game.js` for exactly
|
|
52
|
+
that reason, and the glob still names the deck when the module is re-exported
|
|
53
|
+
from another kit.
|
|
54
|
+
|
|
55
|
+
**Nothing here may import a physics engine, three.js, or an engine kit.** The
|
|
56
|
+
test is that this kit bundles with no dependencies but the SDK.
|
|
57
|
+
|
|
58
|
+
**A change here reaches both kits and every deck built on them.** The pose
|
|
59
|
+
codecs are the seam: add an argument rather than a branch on how many numbers a
|
|
60
|
+
pose has.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"title": "real-time",
|
|
3
|
+
"editor": {
|
|
4
|
+
"hiddenPaths": [],
|
|
5
|
+
"visiblePaths": [
|
|
6
|
+
"code/**"
|
|
7
|
+
]
|
|
8
|
+
},
|
|
9
|
+
"imports": {
|
|
10
|
+
"castle.base": {
|
|
11
|
+
"deckId": "yRcmH4_aYllE",
|
|
12
|
+
"version": "2026-09-01T01:02:34.326Z"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"autoUpdateWhenImported": true,
|
|
16
|
+
"deckId": "xFVr-afOLuUQ",
|
|
17
|
+
"cardId": "w6G45sy3_P_m",
|
|
18
|
+
"publishedVersion": "2026-09-02T03:33:53.318Z"
|
|
19
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Server-clock estimation for client-side interpolation. Arrivals update the
|
|
2
|
+
// clock offset and the jitter delay; render calls convert local page time to
|
|
3
|
+
// simulation time.
|
|
4
|
+
//
|
|
5
|
+
// The dimensional kit's client system owns one clock: it calls `noteArrival`
|
|
6
|
+
// for each drained state message and `renderTime` once per frame to pick the
|
|
7
|
+
// simulation moment it draws.
|
|
8
|
+
|
|
9
|
+
// Five seconds supplies many arrivals for the percentile while remaining
|
|
10
|
+
// responsive to clock drift. The delay bounds cap latency after a long stall.
|
|
11
|
+
const WINDOW_MS = 5000;
|
|
12
|
+
const MIN_DELAY_MS = 55;
|
|
13
|
+
const MAX_DELAY_MS = 260;
|
|
14
|
+
|
|
15
|
+
// One broadcast interval at a 30 Hz tick rate. Drawing that far back means there
|
|
16
|
+
// is normally a later sample to interpolate toward.
|
|
17
|
+
const INTERVAL_MS = 34;
|
|
18
|
+
|
|
19
|
+
// Create an uninitialized clock estimate for one connection.
|
|
20
|
+
export function makeClock() {
|
|
21
|
+
return { samples: [], offset: null, delay: MIN_DELAY_MS };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Record one state arrival and update the offset and render delay. `serverMs` is
|
|
25
|
+
// the simulation time the message describes; `localMs` is the page clock that
|
|
26
|
+
// `connection.js` captured when the message arrived.
|
|
27
|
+
export function noteArrival(clock, serverMs, localMs) {
|
|
28
|
+
// A message with no simulation timestamp cannot contribute to the estimate.
|
|
29
|
+
if (!Number.isFinite(serverMs)) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// One sample is the gap between the two clocks: the true offset, plus however
|
|
34
|
+
// long this packet spent in transit.
|
|
35
|
+
clock.samples.push({ offset: localMs - serverMs, localMs });
|
|
36
|
+
|
|
37
|
+
// Keep a rolling window so the estimate follows clock drift.
|
|
38
|
+
while (clock.samples.length > 1 && clock.samples[0].localMs < localMs - WINDOW_MS) {
|
|
39
|
+
clock.samples.shift();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Transit time only ever adds to the measured gap, so the smallest gap seen is
|
|
43
|
+
// the closest estimate of the offset available.
|
|
44
|
+
const offsets = clock.samples.map((sample) => sample.offset).sort((a, b) => a - b);
|
|
45
|
+
clock.offset = offsets[0];
|
|
46
|
+
|
|
47
|
+
// Add the 90th-percentile delivery spread to one broadcast interval. A single
|
|
48
|
+
// stalled message then does not raise the delay for the whole window.
|
|
49
|
+
const spread = offsets[Math.floor(offsets.length * 0.9)] - offsets[0];
|
|
50
|
+
clock.delay = clamp(INTERVAL_MS + spread, MIN_DELAY_MS, MAX_DELAY_MS);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Report whether `renderTime` has enough data to place network samples. The
|
|
54
|
+
// first timestamped broadcast establishes the offset.
|
|
55
|
+
export function ready(clock) {
|
|
56
|
+
return clock.offset !== null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Convert a page-clock reading to the delayed simulation time used for drawing.
|
|
60
|
+
// Call this only after `ready` reports that an offset has been established.
|
|
61
|
+
// The result is what `interpolate.sampleAt` takes as its `simMs`.
|
|
62
|
+
export function renderTime(clock, localMs) {
|
|
63
|
+
return localMs - clock.offset - clock.delay;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function clamp(value, min, max) {
|
|
67
|
+
return Math.max(min, Math.min(max, value));
|
|
68
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// Client transport over the Castle multiplayer SDK. This module joins the
|
|
2
|
+
// public session, queues arriving messages, tracks the authenticated roster, and
|
|
3
|
+
// rate-limits movement reports.
|
|
4
|
+
|
|
5
|
+
import { Multiplayer } from 'castle-web-sdk';
|
|
6
|
+
import * as Messages from './messages.js';
|
|
7
|
+
|
|
8
|
+
const SEND_MS = 1000 / Messages.MOVE_HZ;
|
|
9
|
+
|
|
10
|
+
// The private state the returned methods share:
|
|
11
|
+
//
|
|
12
|
+
// connection the SDK connection
|
|
13
|
+
// inbox arrived messages, each with `localMs` added
|
|
14
|
+
// roster playerId -> username, as the platform last reported it
|
|
15
|
+
// you the SDK's entry for the local player, null until a roster has it
|
|
16
|
+
// sentAt `performance.now()` of the last move, so sends stay at MOVE_HZ
|
|
17
|
+
// claims ids asked for since the last send, held until it goes
|
|
18
|
+
// releases ids handed back since the last send, held until it goes
|
|
19
|
+
function makeNet(connection) {
|
|
20
|
+
return {
|
|
21
|
+
connection,
|
|
22
|
+
inbox: [],
|
|
23
|
+
roster: new Map(),
|
|
24
|
+
you: null,
|
|
25
|
+
sentAt: 0,
|
|
26
|
+
claims: new Set(),
|
|
27
|
+
releases: new Set(),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Join the deck's public session and return the interface its client system
|
|
32
|
+
// uses. The SDK assigns the session and supplies authenticated roster names, so
|
|
33
|
+
// a deck message carries no sender-chosen username.
|
|
34
|
+
export async function connectSession({ onLog = () => {} } = {}) {
|
|
35
|
+
const connection = await Multiplayer.join({ mode: 'public' });
|
|
36
|
+
const net = makeNet(connection);
|
|
37
|
+
|
|
38
|
+
// Messages are queued with this page's clock at arrival. The client system
|
|
39
|
+
// drains the queue on its own frame and pairs `localMs` with the message's `t`
|
|
40
|
+
// in `clock.js`.
|
|
41
|
+
connection.onMessage((data) => net.inbox.push({ ...data, localMs: performance.now() }));
|
|
42
|
+
connection.onRoster(({ players, you }) => {
|
|
43
|
+
// The roster arrives after the connection, so this is the first point at
|
|
44
|
+
// which the local player's own name can be read.
|
|
45
|
+
if (!net.you && you) {
|
|
46
|
+
onLog(`you are ${you.username}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Replace the map from the complete SDK roster so departures disappear.
|
|
50
|
+
net.roster = new Map(players.map((player) => [player.playerId, player.username]));
|
|
51
|
+
net.you = you;
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Forward SDK connection transitions to the caller. The deck controls how
|
|
55
|
+
// connection state appears in its status UI or logs.
|
|
56
|
+
connection.onStateChange((state, meta) => onLog(`${state}${meta ? ` (${meta.type})` : ''}`));
|
|
57
|
+
return {
|
|
58
|
+
// The player id this connection was assigned.
|
|
59
|
+
selfId: () => connection.playerId,
|
|
60
|
+
|
|
61
|
+
// A stable fallback until the SDK roster includes this player.
|
|
62
|
+
username: (playerId) => net.roster.get(playerId) ?? 'player',
|
|
63
|
+
|
|
64
|
+
// Every other player's roster entry. The local player is simulated by this client,
|
|
65
|
+
// so the deck draws only these from network samples.
|
|
66
|
+
others: () => new Map([...net.roster].filter(([id]) => id !== connection.playerId)),
|
|
67
|
+
|
|
68
|
+
// A copy of the whole roster, for anything that lists everyone.
|
|
69
|
+
roster: () => new Map(net.roster),
|
|
70
|
+
|
|
71
|
+
// Every queued message, and a fresh queue. A frame that skips the drain
|
|
72
|
+
// leaves those messages for the next one.
|
|
73
|
+
drain: () => {
|
|
74
|
+
const messages = net.inbox;
|
|
75
|
+
net.inbox = [];
|
|
76
|
+
return messages;
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
// Report this client's packed pose, the poses of its owned objects, and its
|
|
80
|
+
// ownership requests. The pose codec packs each pose before this call, so
|
|
81
|
+
// the transport handles every dimensional format in the same way.
|
|
82
|
+
// The client system calls this every frame; most calls only accumulate the
|
|
83
|
+
// ownership requests, which are held until the next rate-limited send.
|
|
84
|
+
sendMove: (pose, owned, claims, releases) => sendMove(net, pose, owned, claims, releases),
|
|
85
|
+
|
|
86
|
+
// Send deck-specific data. The server routes these to `code/server/game.js`.
|
|
87
|
+
send: (data) => net.connection.send(data),
|
|
88
|
+
|
|
89
|
+
// Connection and presence data for a deck status line.
|
|
90
|
+
status: () => ({
|
|
91
|
+
state: connection.state,
|
|
92
|
+
solo: connection.isSolo,
|
|
93
|
+
others: Math.max(0, net.roster.size - 1),
|
|
94
|
+
|
|
95
|
+
// The SDK derives `you` by finding the local id in its player list. Use
|
|
96
|
+
// the stored roster while that derived entry is still null.
|
|
97
|
+
you: net.you?.username ?? net.roster.get(connection.playerId) ?? '',
|
|
98
|
+
}),
|
|
99
|
+
|
|
100
|
+
// Leave the session when the engine runtime is disposed.
|
|
101
|
+
close: () => connection.leave(),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Collect ownership requests from every frame and send at `MOVE_HZ`.
|
|
106
|
+
function sendMove(net, pose, owned, claims, releases) {
|
|
107
|
+
// Gathered on every frame, including the ones that send nothing, so a claim
|
|
108
|
+
// made in between is still in the next message. The sets also collapse
|
|
109
|
+
// repeated requests for the same object over that interval.
|
|
110
|
+
for (const id of claims) {
|
|
111
|
+
net.claims.add(id);
|
|
112
|
+
}
|
|
113
|
+
for (const id of releases) {
|
|
114
|
+
net.releases.add(id);
|
|
115
|
+
}
|
|
116
|
+
const now = performance.now();
|
|
117
|
+
if (now - net.sentAt < SEND_MS) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
net.sentAt = now;
|
|
121
|
+
|
|
122
|
+
// One movement packet includes all requests accumulated since the last send.
|
|
123
|
+
net.connection.send({
|
|
124
|
+
k: Messages.MOVE,
|
|
125
|
+
p: pose,
|
|
126
|
+
c: [...net.claims],
|
|
127
|
+
r: [...net.releases],
|
|
128
|
+
o: owned,
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// Clear only after a packet; rate-limited returns above retain the requests.
|
|
132
|
+
net.claims.clear();
|
|
133
|
+
net.releases.clear();
|
|
134
|
+
}
|