castle-web-cli 0.4.157 → 0.4.158
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-BuSYLy-C.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 +79 -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
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
// This engine system connects a browser runtime to its shared session. It
|
|
2
|
+
// receives timestamped state, renders other players' actors and objects, applies server
|
|
3
|
+
// corrections, and reports poses and ownership requests from local simulation.
|
|
4
|
+
//
|
|
5
|
+
// The imported real-time modules (clock, interpolation, smoothing, ownership,
|
|
6
|
+
// transport) never read inside a pose. This system passes the 2D codec `Poses`
|
|
7
|
+
// wherever those modules take a pose callback or a reach definition.
|
|
8
|
+
|
|
9
|
+
import Matter from 'matter-js';
|
|
10
|
+
import * as Avatars from '../client/avatars.js';
|
|
11
|
+
import * as Clock from '@imports/castle.real-time/code/client/clock.js';
|
|
12
|
+
import * as Interpolate from '@imports/castle.real-time/code/client/interpolate.js';
|
|
13
|
+
import * as Ownership from '@imports/castle.real-time/code/client/ownership.js';
|
|
14
|
+
import * as Smoothing from '@imports/castle.real-time/code/client/smooth.js';
|
|
15
|
+
import * as Poses from '../client/poses.js';
|
|
16
|
+
import { connectSession } from '@imports/castle.real-time/code/client/connection.js';
|
|
17
|
+
import { STATE, WORLD } from '@imports/castle.real-time/code/client/messages.js';
|
|
18
|
+
import { game } from '@imports/castle.real-time/code/client/gameHooks.js';
|
|
19
|
+
|
|
20
|
+
// How much of a refused move is taken back per frame, and the distance past
|
|
21
|
+
// which the correction is taken at once. Easing a correction of half the room
|
|
22
|
+
// would draw motion through positions the server already rejected.
|
|
23
|
+
const CLAMP_EASE = 0.25;
|
|
24
|
+
const CLAMP_SNAP = 120;
|
|
25
|
+
|
|
26
|
+
// A deck may provide its own multiplayer system. The engine registry permits
|
|
27
|
+
// duplicate systems, which would create duplicate session connections, so these
|
|
28
|
+
// lazy globs detect deck-owned files without evaluating them. Vite excludes the
|
|
29
|
+
// current module from its own glob when this kit is served directly.
|
|
30
|
+
const deckSystem = {
|
|
31
|
+
...import.meta.glob('/systems/multiplayer.js'),
|
|
32
|
+
...import.meta.glob('/code/systems/multiplayer.js'),
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// Install one multiplayer system into an engine runtime. The engine discovers
|
|
36
|
+
// `code/systems/**/*.js` and calls installSystem once for each runtime. Return
|
|
37
|
+
// null when the deck supplies its own system so that only one connection opens.
|
|
38
|
+
export function installSystem(runtime) {
|
|
39
|
+
if (Object.keys(deckSystem).length > 0) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
const system = new MultiplayerSystem();
|
|
43
|
+
|
|
44
|
+
// registerSystem adds lifecycle callbacks such as afterBehaviors and dispose.
|
|
45
|
+
runtime.registerSystem(system);
|
|
46
|
+
return system;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
class MultiplayerSystem {
|
|
50
|
+
constructor() {
|
|
51
|
+
// The transport is created on the first stepped frame.
|
|
52
|
+
this.net = null;
|
|
53
|
+
|
|
54
|
+
// `starting` keeps a second join attempt out while the first is pending.
|
|
55
|
+
this.starting = false;
|
|
56
|
+
|
|
57
|
+
// Pose histories, indexed separately for players and scene objects.
|
|
58
|
+
this.players = new Map();
|
|
59
|
+
this.objects = new Map();
|
|
60
|
+
|
|
61
|
+
// The clock maps page time to a delayed server simulation time.
|
|
62
|
+
this.clock = Clock.makeClock();
|
|
63
|
+
|
|
64
|
+
// Which objects this client is simulating.
|
|
65
|
+
this.own = Ownership.makeOwnership();
|
|
66
|
+
|
|
67
|
+
// The pose actually drawn for each network-drawn id.
|
|
68
|
+
this.shown = Smoothing.makeSmoothing();
|
|
69
|
+
|
|
70
|
+
// Which actor stands in for which other player.
|
|
71
|
+
this.avatars = Avatars.makeAvatars();
|
|
72
|
+
|
|
73
|
+
// The last position the server refused a move to, or null.
|
|
74
|
+
this.clampedTo = null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// The network frame, after behaviors and after local physics. Connect,
|
|
78
|
+
// receive, draw, correct, report, in that order.
|
|
79
|
+
afterBehaviors(scene, dt) {
|
|
80
|
+
// Systems run in registration order. The imported physics system registers
|
|
81
|
+
// first, so Matter has stepped and copied body poses into Layout by this call.
|
|
82
|
+
// Connecting on the first stepped frame keeps the editor's own runtimes out
|
|
83
|
+
// of the session: it builds them and never steps them.
|
|
84
|
+
if (!this.net) {
|
|
85
|
+
return void this.start();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Player is a behavior component on the actor controlled by this browser.
|
|
89
|
+
const local = scene.actorWith('Player');
|
|
90
|
+
|
|
91
|
+
// Every queued message is drained before this frame picks its render time.
|
|
92
|
+
this.receive(scene);
|
|
93
|
+
|
|
94
|
+
// Drawing runs a fixed delay behind the server clock, so interpolation
|
|
95
|
+
// normally has a sample on each side of the moment being drawn. The local
|
|
96
|
+
// engine goes on stepping before the first timestamp arrives.
|
|
97
|
+
const at = Clock.ready(this.clock) ? Clock.renderTime(this.clock, performance.now()) : null;
|
|
98
|
+
if (at !== null) {
|
|
99
|
+
this.applyObjectPoses(scene, at, dt);
|
|
100
|
+
this.applyPlayerPoses(scene, at, dt);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Apply a refused-move correction before collecting this frame's report.
|
|
104
|
+
this.acceptClamp(scene, local);
|
|
105
|
+
|
|
106
|
+
game.beforeReport?.(scene, local, this);
|
|
107
|
+
if (game.report) {
|
|
108
|
+
game.report(scene, local, this);
|
|
109
|
+
} else {
|
|
110
|
+
this.report(scene, local);
|
|
111
|
+
}
|
|
112
|
+
game.afterFrame?.(scene, dt, this);
|
|
113
|
+
this.publish(scene, local);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Join the public session once. Scene resets retain this transport.
|
|
117
|
+
async start() {
|
|
118
|
+
if (this.starting) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
this.starting = true;
|
|
122
|
+
try {
|
|
123
|
+
this.net = await connectSession({ onLog: (text) => console.log(`[multiplayer] ${text}`) });
|
|
124
|
+
} catch (err) {
|
|
125
|
+
console.log(`[multiplayer] could not join: ${err?.message ?? err}`);
|
|
126
|
+
this.starting = false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Drain the queue. A state message updates the clock, then ownership, then the
|
|
131
|
+
// pose histories, in that order; anything else goes to the deck.
|
|
132
|
+
receive(scene) {
|
|
133
|
+
const selfId = this.net.selfId();
|
|
134
|
+
for (const msg of this.net.drain()) {
|
|
135
|
+
if (msg.k !== STATE && msg.k !== WORLD) {
|
|
136
|
+
game.message?.(scene, msg, this);
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Pair server simulation time with page arrival time before filing samples.
|
|
141
|
+
Clock.noteArrival(this.clock, msg.t, msg.localMs);
|
|
142
|
+
|
|
143
|
+
// Apply ownership before poses so a newly granted object stops consuming
|
|
144
|
+
// network poses on the first frame of local simulation.
|
|
145
|
+
Ownership.applyHandovers(this.own, msg.own, selfId);
|
|
146
|
+
for (const [id, x, y, facing, clamped] of msg.ps ?? []) {
|
|
147
|
+
if (id !== selfId) {
|
|
148
|
+
sample(this.players, msg, id, [x, y, facing]);
|
|
149
|
+
} else if (clamped) {
|
|
150
|
+
// Store this client's returned pose only when the server marks the
|
|
151
|
+
// report clamped. Accepted movement remains client-simulated.
|
|
152
|
+
this.clampedTo = { x, y };
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
for (const [id, ...pose] of msg.os ?? []) {
|
|
156
|
+
sample(this.objects, msg, id, pose);
|
|
157
|
+
}
|
|
158
|
+
game.state?.(scene, msg, this);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
//
|
|
163
|
+
// Applying network state
|
|
164
|
+
//
|
|
165
|
+
|
|
166
|
+
// Place every crate the server is simulating, at one delayed simulation time.
|
|
167
|
+
applyObjectPoses(scene, at, dt) {
|
|
168
|
+
for (const [id, track] of this.objects) {
|
|
169
|
+
// Local physics controls owned objects. Applying an older server sample
|
|
170
|
+
// would replace the current local pose during a push.
|
|
171
|
+
if (this.own.mine.has(id)) {
|
|
172
|
+
Smoothing.forget(this.shown, id);
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const actor = scene.getActor(id);
|
|
176
|
+
const sampled = actor?.components.Layout && Interpolate.sampleAt(track, at, Poses.blend);
|
|
177
|
+
if (!sampled) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// The sampled target is smoothed to absorb the small steps uneven delivery
|
|
182
|
+
// leaves behind.
|
|
183
|
+
const pose = Smoothing.smooth(this.shown, id, sampled, dt, Poses);
|
|
184
|
+
|
|
185
|
+
// Layout drives rendering and editor-visible component state.
|
|
186
|
+
Poses.applyPose(actor.components.Layout, pose);
|
|
187
|
+
|
|
188
|
+
// Write both engine representations. Matter copies a dynamic body's
|
|
189
|
+
// pose back over Layout every frame, so writing only Layout lasts one
|
|
190
|
+
// frame.
|
|
191
|
+
writeBody(scene, actor, pose[2]);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Apply each other player's pose, spawning its actor when needed.
|
|
196
|
+
applyPlayerPoses(scene, at, dt) {
|
|
197
|
+
const others = this.net.others();
|
|
198
|
+
for (const [playerId, username] of others) {
|
|
199
|
+
const track = this.players.get(playerId);
|
|
200
|
+
const sampled = track && Interpolate.sampleAt(track, at, Poses.blend);
|
|
201
|
+
if (!sampled) {
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
const pose = Smoothing.smooth(this.shown, playerId, sampled, dt, Poses);
|
|
205
|
+
|
|
206
|
+
// A deck hook may select a different blueprint, label, or hide the avatar.
|
|
207
|
+
const look = game.avatar ? game.avatar(playerId, username, this) : { username };
|
|
208
|
+
if (!look) {
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
Avatars.showPlayer(this.avatars, scene, playerId, look.username ?? null, pose, {
|
|
212
|
+
blueprint: look.blueprint,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// The session roster is authoritative for removing actors and pose tracks.
|
|
217
|
+
Avatars.dropMissing(this.avatars, scene, others);
|
|
218
|
+
for (const playerId of [...this.players.keys()]) {
|
|
219
|
+
if (!others.has(playerId)) {
|
|
220
|
+
this.dropPlayer(playerId);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
dropPlayer(playerId) {
|
|
226
|
+
// Remove both interpolation and smoothing history for the departed player.
|
|
227
|
+
this.players.delete(playerId);
|
|
228
|
+
Smoothing.forget(this.shown, playerId);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
//
|
|
232
|
+
// The local player
|
|
233
|
+
//
|
|
234
|
+
|
|
235
|
+
// Correct the local actor after the server clamps a reported move. Accepted
|
|
236
|
+
// snapshots do not move the locally simulated character.
|
|
237
|
+
acceptClamp(scene, local) {
|
|
238
|
+
if (!this.clampedTo) {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
const to = this.clampedTo;
|
|
242
|
+
this.clampedTo = null;
|
|
243
|
+
const layout = local?.components.Layout;
|
|
244
|
+
if (!layout) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
const off = { x: to.x - layout.x, y: to.y - layout.y };
|
|
248
|
+
|
|
249
|
+
// Large corrections snap; short corrections converge over successive frames.
|
|
250
|
+
const ease = Math.hypot(off.x, off.y) > CLAMP_SNAP ? 1 : CLAMP_EASE;
|
|
251
|
+
layout.x += off.x * ease;
|
|
252
|
+
layout.y += off.y * ease;
|
|
253
|
+
|
|
254
|
+
// The body is where the next physics step reads the character from, so the
|
|
255
|
+
// correction has to reach it as well as Layout.
|
|
256
|
+
const body = scene.physics.bodyFor(local);
|
|
257
|
+
if (body) {
|
|
258
|
+
Matter.Body.setPosition(body, centreOf(scene, local));
|
|
259
|
+
Matter.Body.setVelocity(body, { x: 0, y: 0 });
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Report the local character's post-physics pose, the poses of the objects
|
|
264
|
+
// this client owns, and its ownership requests. The Player behavior and local
|
|
265
|
+
// physics step have both already run this frame.
|
|
266
|
+
report(scene, local) {
|
|
267
|
+
if (!local) {
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
const now = performance.now();
|
|
271
|
+
const layout = local.components.Layout;
|
|
272
|
+
const facing = local.components.Box?.facing ?? 0;
|
|
273
|
+
const objects = game.claimable?.(scene, this) ?? this.claimable(scene);
|
|
274
|
+
const me = boxCentre(layout);
|
|
275
|
+
const claims = Ownership.toClaim(
|
|
276
|
+
this.own,
|
|
277
|
+
objects,
|
|
278
|
+
me,
|
|
279
|
+
this.otherPlaces(scene),
|
|
280
|
+
now,
|
|
281
|
+
Poses.REACH,
|
|
282
|
+
);
|
|
283
|
+
const releases = Ownership.toRelease(this.own, objects, me, Poses.REACH);
|
|
284
|
+
|
|
285
|
+
// Only granted objects are reported, because the server ignores a pose for
|
|
286
|
+
// any other id.
|
|
287
|
+
const owned = [];
|
|
288
|
+
for (const [id] of objects) {
|
|
289
|
+
const actor = this.own.mine.has(id) && scene.getActor(id);
|
|
290
|
+
if (actor) {
|
|
291
|
+
owned.push([id, ...Poses.poseOf(actor.components.Layout)]);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// `sendMove` only goes out at MOVE_HZ; the claims are held until it does.
|
|
296
|
+
this.net.sendMove(Poses.packMove(layout, facing), owned, claims, releases);
|
|
297
|
+
|
|
298
|
+
// Use the same shortened account label for local and other players' Box components.
|
|
299
|
+
if (local.components.Box) {
|
|
300
|
+
local.components.Box.label = Avatars.shortName(this.net.status().you);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// `[id, centre]` for every scene object the server has sent a pose for. The
|
|
305
|
+
// default report uses the same set for claims, releases and owned poses.
|
|
306
|
+
claimable(scene) {
|
|
307
|
+
const out = [];
|
|
308
|
+
for (const id of this.objects.keys()) {
|
|
309
|
+
const layout = scene.getActor(id)?.components.Layout;
|
|
310
|
+
if (layout) {
|
|
311
|
+
// Ownership reach tests use Layout-box centres on the client.
|
|
312
|
+
out.push([id, boxCentre(layout)]);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return out;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// Where the other characters are. `applyPlayerPoses` has already placed them for
|
|
319
|
+
// this frame, so the actors carry the drawn positions. Claims use these to
|
|
320
|
+
// require the local player to be the closest one.
|
|
321
|
+
otherPlaces(scene) {
|
|
322
|
+
const out = [];
|
|
323
|
+
for (const actorId of this.avatars.actors.values()) {
|
|
324
|
+
const layout = scene.getActor(actorId)?.components.Layout;
|
|
325
|
+
if (layout) {
|
|
326
|
+
out.push(boxCentre(layout));
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return out;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// Publish the client-visible room state for the two-client integration test.
|
|
333
|
+
// This debug object uses the same Layout values that rendering consumes.
|
|
334
|
+
publish(scene, local) {
|
|
335
|
+
const objects = {};
|
|
336
|
+
for (const id of this.objects.keys()) {
|
|
337
|
+
const layout = scene.getActor(id)?.components.Layout;
|
|
338
|
+
if (layout) {
|
|
339
|
+
objects[id] = [layout.x, layout.y, layout.rotation ?? 0];
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
const players = {};
|
|
343
|
+
for (const [playerId, actorId] of this.avatars.actors) {
|
|
344
|
+
const layout = scene.getActor(actorId)?.components.Layout;
|
|
345
|
+
if (layout) {
|
|
346
|
+
players[playerId] = [layout.x, layout.y];
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
const status = this.net.status();
|
|
350
|
+
globalThis.__multiplayer = {
|
|
351
|
+
selfId: this.net.selfId(),
|
|
352
|
+
state: status.state,
|
|
353
|
+
others: status.others,
|
|
354
|
+
delay: Math.round(this.clock.delay),
|
|
355
|
+
ready: Clock.ready(this.clock),
|
|
356
|
+
mine: [...this.own.mine],
|
|
357
|
+
me: local ? [local.components.Layout.x, local.components.Layout.y] : null,
|
|
358
|
+
players,
|
|
359
|
+
objects,
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// Clear scene-owned actors and histories after a scene load while retaining
|
|
364
|
+
// the session connection.
|
|
365
|
+
reset(scene) {
|
|
366
|
+
game.reset?.(scene, this);
|
|
367
|
+
Avatars.clearAvatars(this.avatars);
|
|
368
|
+
this.players.clear();
|
|
369
|
+
this.objects.clear();
|
|
370
|
+
this.own = Ownership.makeOwnership();
|
|
371
|
+
this.shown = Smoothing.makeSmoothing();
|
|
372
|
+
this.clampedTo = null;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
dispose(scene) {
|
|
376
|
+
// Runtime disposal also closes transport resources retained across resets.
|
|
377
|
+
game.dispose?.(scene, this);
|
|
378
|
+
this.reset(scene);
|
|
379
|
+
this.net?.close();
|
|
380
|
+
this.net = null;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// File one pose under the message's simulation timestamp.
|
|
385
|
+
function sample(tracks, msg, id, pose) {
|
|
386
|
+
// A WORLD message is a complete snapshot. Replace any track containing
|
|
387
|
+
// samples from an earlier scene or connection before adding its pose.
|
|
388
|
+
if (msg.k === WORLD) {
|
|
389
|
+
tracks.delete(id);
|
|
390
|
+
}
|
|
391
|
+
let track = tracks.get(id);
|
|
392
|
+
if (!track) {
|
|
393
|
+
tracks.set(id, (track = Interpolate.makeTrack()));
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// File under `msg.t` so sample intervals use server simulation time and do not
|
|
397
|
+
// include network delivery jitter recorded by the arrival clock.
|
|
398
|
+
Interpolate.pushSample(track, msg.t, pose, Poses.same);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// Put an actor's matter body where its Layout now says, at rest. The next
|
|
402
|
+
// network sample supplies the position after this one.
|
|
403
|
+
function writeBody(scene, actor, angle) {
|
|
404
|
+
const body = scene.physics.bodyFor(actor);
|
|
405
|
+
if (!body) {
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
Matter.Body.setPosition(body, centreOf(scene, actor));
|
|
409
|
+
Matter.Body.setAngle(body, angle);
|
|
410
|
+
Matter.Body.setVelocity(body, { x: 0, y: 0 });
|
|
411
|
+
Matter.Body.setAngularVelocity(body, 0);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// The point matter holds a body at: the centre of the actor's collider box, in
|
|
415
|
+
// world space, which is what the engine and the server both measure from.
|
|
416
|
+
function centreOf(scene, actor) {
|
|
417
|
+
const rect = scene.colliderRect(actor);
|
|
418
|
+
return { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 };
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
function boxCentre(layout) {
|
|
422
|
+
// Layout x and y identify the top-left corner used by the wire pose.
|
|
423
|
+
return { x: layout.x + layout.width / 2, y: layout.y + layout.height / 2 };
|
|
424
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// This config defines lint scope and syntax rules for the kit's code modules.
|
|
2
|
+
// The first block excludes external or generated trees; the second configures
|
|
3
|
+
// JavaScript parsing and required braces under code/.
|
|
4
|
+
export default [
|
|
5
|
+
// Keep imported, installed, generated, and runtime files outside lint checks.
|
|
6
|
+
{
|
|
7
|
+
ignores: ['node_modules/**', 'dist/**', '.castle/**', 'imports/**'],
|
|
8
|
+
},
|
|
9
|
+
// Prettier does not add braces, so eslint --fix applies the curly rule.
|
|
10
|
+
// Requiring braces keeps network validation branches explicit during edits.
|
|
11
|
+
{
|
|
12
|
+
files: ['code/**/*.{js,jsx}'],
|
|
13
|
+
languageOptions: {
|
|
14
|
+
ecmaVersion: 'latest',
|
|
15
|
+
sourceType: 'module',
|
|
16
|
+
},
|
|
17
|
+
rules: {
|
|
18
|
+
curly: ['error', 'all'],
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
];
|
|
@@ -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';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<!-- Browser shell for playing the multiplayer-2d kit directly. The head sets
|
|
3
|
+
card viewport and initial colors; the body provides the engine mount and
|
|
4
|
+
loads the kit entry module. -->
|
|
5
|
+
<html>
|
|
6
|
+
<head>
|
|
7
|
+
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
|
|
8
|
+
<meta name="color-scheme" content="dark" />
|
|
9
|
+
<title>multiplayer-2d</title>
|
|
10
|
+
<!-- Declare the dark scheme before the JS-imported CSS loads, so the frame
|
|
11
|
+
never paints light first (Safari won't repaint a composited iframe on a
|
|
12
|
+
color-scheme change). -->
|
|
13
|
+
<style>
|
|
14
|
+
html {
|
|
15
|
+
color-scheme: dark;
|
|
16
|
+
background: #0a0a0a;
|
|
17
|
+
}
|
|
18
|
+
</style>
|
|
19
|
+
</head>
|
|
20
|
+
<body>
|
|
21
|
+
<div id="root"></div>
|
|
22
|
+
<!-- The kit's own entry, so playing this kit plays its starter game. -->
|
|
23
|
+
<script type="module" src="./main.jsx"></script>
|
|
24
|
+
</body>
|
|
25
|
+
</html>
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// This is the browser entry point for decks using multiplayer-2d. It delegates
|
|
2
|
+
// runtime and editor startup to physics-2d; the imported multiplayer files add
|
|
3
|
+
// netcode, the client system, and the starter scene through kit discovery.
|
|
4
|
+
|
|
5
|
+
// A deck may load `/imports/castle.multiplayer-2d/main.jsx` as its single kit
|
|
6
|
+
// entry point. castle.json publishes this same path as the kit's `main` module.
|
|
7
|
+
//
|
|
8
|
+
// physics-2d starts the engine and editors, then discovers this kit's behavior
|
|
9
|
+
// classes and `code/systems/multiplayer.js` through its normal module globs.
|
|
10
|
+
import '@imports/castle.physics-2d/main.jsx';
|