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
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
// This module builds and advances the server's Matter world from the deck scene.
|
|
2
|
+
// It creates engine-compatible bodies, runs fixed physics steps and server-side
|
|
3
|
+
// corrections, then packs object poses for network snapshots.
|
|
4
|
+
|
|
5
|
+
import Matter from 'matter-js';
|
|
6
|
+
import {
|
|
7
|
+
getColliderRect,
|
|
8
|
+
getColliderShapes,
|
|
9
|
+
pointsBounds,
|
|
10
|
+
} from '@imports/castle.physics-2d/engine/collider.js';
|
|
11
|
+
import { packPose } from '../client/poses.js';
|
|
12
|
+
import { MAX_STEPS_PER_TICK, STEP } from '@imports/castle.real-time/code/server/limits.js';
|
|
13
|
+
import { readScene } from './sceneBodies.js';
|
|
14
|
+
import * as Limits from './limits.js';
|
|
15
|
+
|
|
16
|
+
const DEG_TO_RAD = Math.PI / 180;
|
|
17
|
+
|
|
18
|
+
// Matter's force scale. The engine's world gravity is in these units, and
|
|
19
|
+
// the server reads the same number out of the scene file.
|
|
20
|
+
const GRAVITY_SCALE = 0.001;
|
|
21
|
+
|
|
22
|
+
// The simulation every hook is handed as `sim`:
|
|
23
|
+
//
|
|
24
|
+
// engine the matter engine, whose world holds every body
|
|
25
|
+
// objects actor id -> { body, offset, home, claimable, owner, reportedAt,
|
|
26
|
+
// claimedAt }
|
|
27
|
+
// players playerId -> { body, offset, at, facing, clamped, movedAt }
|
|
28
|
+
// spawn the top-left corner the authored character was placed at
|
|
29
|
+
// template that authored actor, for the shape a player body is built from
|
|
30
|
+
// gravity world gravity, from the scene's `physics` block
|
|
31
|
+
// acc seconds not yet turned into whole physics steps
|
|
32
|
+
// timeMs milliseconds of simulation, not of wall clock. It advances by
|
|
33
|
+
// whole physics steps, so a client can space samples by it exactly.
|
|
34
|
+
function emptySim(gravity) {
|
|
35
|
+
const engine = Matter.Engine.create();
|
|
36
|
+
|
|
37
|
+
// applyGravity handles each body's gravityScale, so built-in world gravity is
|
|
38
|
+
// disabled to match the browser engine.
|
|
39
|
+
engine.gravity.scale = 0;
|
|
40
|
+
return {
|
|
41
|
+
engine,
|
|
42
|
+
objects: new Map(),
|
|
43
|
+
players: new Map(),
|
|
44
|
+
spawn: { x: 250, y: 350 },
|
|
45
|
+
template: null,
|
|
46
|
+
gravity,
|
|
47
|
+
acc: 0,
|
|
48
|
+
timeMs: 0,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Build the simulation passed to every server game hook. The async signature
|
|
53
|
+
// matches makeSession's simulation factory contract.
|
|
54
|
+
export async function createSimulation() {
|
|
55
|
+
const scene = readScene();
|
|
56
|
+
const sim = emptySim(scene.gravity);
|
|
57
|
+
buildScene(sim, scene.actors);
|
|
58
|
+
return sim;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Create bodies and indexes from the reduced scene representation.
|
|
62
|
+
function buildScene(sim, actors) {
|
|
63
|
+
// Body construction mirrors engine/physics/matterBridge.js so browser and
|
|
64
|
+
// server simulations start from the same scene representation.
|
|
65
|
+
for (const actor of actors) {
|
|
66
|
+
const layout = actor.components.Layout;
|
|
67
|
+
const collider = actor.components.Collider;
|
|
68
|
+
if (!layout || !collider) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// The authored Player provides a spawn point and body template. addPlayer
|
|
73
|
+
// later creates one body from this template for each connection.
|
|
74
|
+
if (actor.components.Player) {
|
|
75
|
+
sim.spawn = { x: layout.x, y: layout.y };
|
|
76
|
+
sim.template = actor;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
const entry = addBody(sim, actor);
|
|
80
|
+
|
|
81
|
+
// Bodies with changing poses require network snapshots. Static walls remain
|
|
82
|
+
// in Matter and are omitted from object pose messages.
|
|
83
|
+
const type = bodyTypeOf(actor);
|
|
84
|
+
if (type === 'dynamic' || type === 'kinematic') {
|
|
85
|
+
sim.objects.set(actor.id, {
|
|
86
|
+
...entry,
|
|
87
|
+
home: { x: layout.x, y: layout.y, angle: (layout.rotation ?? 0) * DEG_TO_RAD },
|
|
88
|
+
|
|
89
|
+
// A dynamic body can be handed to a client to simulate. A kinematic one
|
|
90
|
+
// is driven by the server and stays server-owned.
|
|
91
|
+
claimable: type === 'dynamic',
|
|
92
|
+
owner: null,
|
|
93
|
+
reportedAt: 0,
|
|
94
|
+
claimedAt: 0,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Build one body from engine component data and add it to the world. Returns the
|
|
101
|
+
// body and the offset from the Layout box's top-left corner to the body's
|
|
102
|
+
// centre, which is what the wire pose is measured from.
|
|
103
|
+
export function addBody(sim, actor) {
|
|
104
|
+
const layout = actor.components.Layout;
|
|
105
|
+
|
|
106
|
+
// The engine collider helpers resolve rectangles, circles, and polygons from
|
|
107
|
+
// the same component data used by the browser physics bridge.
|
|
108
|
+
const rect = getColliderRect(actor);
|
|
109
|
+
const parts = (getColliderShapes(actor) ?? []).map(shapeToPart).filter(Boolean);
|
|
110
|
+
const centre = { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 };
|
|
111
|
+
const body = bodyFromParts(parts, rect, centre);
|
|
112
|
+
Matter.Body.setPosition(body, centre);
|
|
113
|
+
Matter.Body.setAngle(body, (layout.rotation ?? 0) * DEG_TO_RAD);
|
|
114
|
+
|
|
115
|
+
// actorId lets collision-oriented deck hooks identify the source scene actor.
|
|
116
|
+
body.plugin.actorId = actor.id;
|
|
117
|
+
Matter.Body.setStatic(body, bodyTypeOf(actor) !== 'dynamic');
|
|
118
|
+
patchBody(body, actor);
|
|
119
|
+
Matter.Composite.add(sim.engine.world, body);
|
|
120
|
+
return { body, offset: { dx: centre.x - layout.x, dy: centre.y - layout.y } };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// One part is the body; several become a compound. A collider that produced no
|
|
124
|
+
// part at all falls back to its bounding box, so the actor still collides.
|
|
125
|
+
function bodyFromParts(parts, rect, centre) {
|
|
126
|
+
if (parts.length === 0) {
|
|
127
|
+
return Matter.Bodies.rectangle(
|
|
128
|
+
centre.x,
|
|
129
|
+
centre.y,
|
|
130
|
+
Math.max(1, rect.width),
|
|
131
|
+
Math.max(1, rect.height),
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Matter requires a parent body when a collider contains multiple parts.
|
|
136
|
+
return parts.length === 1 ? parts[0] : Matter.Body.create({ parts });
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// One matter part for a resolved, world-space collider shape. A triangle or
|
|
140
|
+
// convex polygon goes through `fromVertices`; anything matter cannot build falls
|
|
141
|
+
// back to its bounding box so it still collides.
|
|
142
|
+
function shapeToPart(shape) {
|
|
143
|
+
if (shape.type === 'circle') {
|
|
144
|
+
return Matter.Bodies.circle(shape.cx, shape.cy, shape.radius > 0 ? shape.radius : 0.5);
|
|
145
|
+
}
|
|
146
|
+
if ((shape.type === 'triangle' || shape.type === 'polygon') && (shape.points?.length ?? 0) >= 3) {
|
|
147
|
+
return polygonPart(shape.points);
|
|
148
|
+
}
|
|
149
|
+
const options = shape.angle ? { angle: shape.angle * DEG_TO_RAD } : undefined;
|
|
150
|
+
return Matter.Bodies.rectangle(
|
|
151
|
+
shape.cx,
|
|
152
|
+
shape.cy,
|
|
153
|
+
Math.max(1, shape.width),
|
|
154
|
+
Math.max(1, shape.height),
|
|
155
|
+
options,
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// One matter part from a polygon's world-space points.
|
|
160
|
+
function polygonPart(points) {
|
|
161
|
+
// fromVertices expects a centre plus world-space vertices.
|
|
162
|
+
let cx = 0;
|
|
163
|
+
let cy = 0;
|
|
164
|
+
for (const point of points) {
|
|
165
|
+
cx += point.x;
|
|
166
|
+
cy += point.y;
|
|
167
|
+
}
|
|
168
|
+
const part = Matter.Bodies.fromVertices(cx / points.length, cy / points.length, [points]);
|
|
169
|
+
if (part) {
|
|
170
|
+
return part;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Concave or degenerate input may fail Matter's polygon construction; its
|
|
174
|
+
// world-space bounds preserve a usable collision area.
|
|
175
|
+
const [x0, y0, x1, y1] = pointsBounds(points);
|
|
176
|
+
return Matter.Bodies.rectangle(
|
|
177
|
+
(x0 + x1) / 2,
|
|
178
|
+
(y0 + y1) / 2,
|
|
179
|
+
Math.max(1, x1 - x0),
|
|
180
|
+
Math.max(1, y1 - y0),
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// The material and motion properties, matching the engine's `patchBody`.
|
|
185
|
+
function patchBody(body, actor) {
|
|
186
|
+
const collider = actor.components.Collider;
|
|
187
|
+
const rigid = actor.components.RigidBody;
|
|
188
|
+
body.restitution = collider.bounciness ?? 0;
|
|
189
|
+
body.friction = collider.friction ?? 0.1;
|
|
190
|
+
body.frictionStatic = collider.frictionStatic ?? 0.5;
|
|
191
|
+
body.isSensor = Boolean(collider.isTrigger);
|
|
192
|
+
body.frictionAir = rigid?.drag ?? 0.01;
|
|
193
|
+
body.plugin.gravityScale = rigid?.gravityScale ?? 1;
|
|
194
|
+
body.plugin.angularDrag = rigid?.angularDrag ?? 0;
|
|
195
|
+
|
|
196
|
+
// Density sets mass, and only a dynamic body may have it: on a static one it
|
|
197
|
+
// would replace the infinite mass that makes the body an immovable obstacle.
|
|
198
|
+
// It recomputes inertia, so it runs before the frozen-rotation override.
|
|
199
|
+
if (bodyTypeOf(actor) === 'dynamic') {
|
|
200
|
+
Matter.Body.setDensity(body, collider.density > 0 ? collider.density : 0.001);
|
|
201
|
+
}
|
|
202
|
+
if (rigid?.freezeRotation) {
|
|
203
|
+
Matter.Body.setInertia(body, Infinity);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// An actor with no RigidBody is a static obstacle, which is what a lone Collider
|
|
208
|
+
// means in the engine too.
|
|
209
|
+
export function bodyTypeOf(actor) {
|
|
210
|
+
// Collider-only actors use the engine's default static body type.
|
|
211
|
+
return actor.components.RigidBody?.bodyType ?? 'static';
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
//
|
|
215
|
+
// Stepping
|
|
216
|
+
//
|
|
217
|
+
|
|
218
|
+
// Advance as many whole fixed steps as this tick's elapsed time allows, up to
|
|
219
|
+
// the catch-up limit. `timeMs` advances in exact step units, which is what
|
|
220
|
+
// client interpolation spaces its samples by.
|
|
221
|
+
export function step(sim, elapsedSeconds) {
|
|
222
|
+
// acc carries a fractional step into the next host tick.
|
|
223
|
+
sim.acc += elapsedSeconds;
|
|
224
|
+
let steps = 0;
|
|
225
|
+
while (sim.acc >= STEP && steps < MAX_STEPS_PER_TICK) {
|
|
226
|
+
// Each accepted client position is written into its player body just before
|
|
227
|
+
// the step reads contacts. The velocity that comes with it is what lets a
|
|
228
|
+
// walking character shove a crate.
|
|
229
|
+
for (const player of sim.players.values()) {
|
|
230
|
+
const to = { x: player.at.x + player.offset.dx, y: player.at.y + player.offset.dy };
|
|
231
|
+
Matter.Body.setPosition(player.body, to, true);
|
|
232
|
+
}
|
|
233
|
+
applyGravity(sim);
|
|
234
|
+
applyAngularDrag(sim);
|
|
235
|
+
Matter.Engine.update(sim.engine, STEP * 1000);
|
|
236
|
+
keepObjectsSane(sim);
|
|
237
|
+
sim.timeMs += STEP * 1000;
|
|
238
|
+
sim.acc -= STEP;
|
|
239
|
+
steps += 1;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Past the catch-up limit, skip whole physics steps but advance simulation
|
|
243
|
+
// time across them. Keeping old time after a host stall would make client
|
|
244
|
+
// interpolation render delayed state until the clock caught up.
|
|
245
|
+
if (steps === MAX_STEPS_PER_TICK) {
|
|
246
|
+
// Floating-point division can report 56 steps as 55.99999; the epsilon keeps
|
|
247
|
+
// the whole skipped-step count.
|
|
248
|
+
const skipped = Math.floor(sim.acc / STEP + 1e-9);
|
|
249
|
+
sim.timeMs += skipped * STEP * 1000;
|
|
250
|
+
sim.acc -= skipped * STEP;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Apply world gravity per dynamic body so each RigidBody gravityScale is honored.
|
|
255
|
+
// The starter room configures zero gravity, which exits before body iteration.
|
|
256
|
+
function applyGravity(sim) {
|
|
257
|
+
if (sim.gravity.x === 0 && sim.gravity.y === 0) {
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
for (const body of Matter.Composite.allBodies(sim.engine.world)) {
|
|
261
|
+
if (body.isStatic) {
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
const scale = (body.plugin.gravityScale ?? 1) * GRAVITY_SCALE;
|
|
265
|
+
body.force.x += body.mass * sim.gravity.x * scale;
|
|
266
|
+
body.force.y += body.mass * sim.gravity.y * scale;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Matter applies frictionAir to linear and angular motion. angularDrag adds
|
|
271
|
+
// rotational damping for bodies configured to stop turning sooner than sliding.
|
|
272
|
+
function applyAngularDrag(sim) {
|
|
273
|
+
for (const body of Matter.Composite.allBodies(sim.engine.world)) {
|
|
274
|
+
const drag = body.plugin.angularDrag ?? 0;
|
|
275
|
+
if (drag > 0 && !body.isStatic) {
|
|
276
|
+
body.angularVelocity *= Math.max(0, 1 - drag * STEP);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Apply bounds and speed corrections after each step to server-simulated objects.
|
|
282
|
+
// Client-owned and authored-static bodies are excluded from these corrections.
|
|
283
|
+
function keepObjectsSane(sim) {
|
|
284
|
+
for (const entry of sim.objects.values()) {
|
|
285
|
+
if (entry.owner || entry.body.isStatic) {
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Reset an out-of-room body to its authored pose with zero velocity.
|
|
290
|
+
if (!Limits.inRoom(entry.body.position)) {
|
|
291
|
+
Matter.Body.setPosition(entry.body, {
|
|
292
|
+
x: entry.home.x + entry.offset.dx,
|
|
293
|
+
y: entry.home.y + entry.offset.dy,
|
|
294
|
+
});
|
|
295
|
+
Matter.Body.setAngle(entry.body, entry.home.angle);
|
|
296
|
+
Matter.Body.setVelocity(entry.body, { x: 0, y: 0 });
|
|
297
|
+
Matter.Body.setAngularVelocity(entry.body, 0);
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Cap velocity imparted by a server-side character body. limits.js explains
|
|
302
|
+
// why the maximum is close to character walking speed.
|
|
303
|
+
const speed = Math.hypot(entry.body.velocity.x, entry.body.velocity.y);
|
|
304
|
+
if (speed > Limits.MAX_PUSHED_SPEED) {
|
|
305
|
+
const scale = Limits.MAX_PUSHED_SPEED / speed;
|
|
306
|
+
Matter.Body.setVelocity(entry.body, {
|
|
307
|
+
x: entry.body.velocity.x * scale,
|
|
308
|
+
y: entry.body.velocity.y * scale,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
//
|
|
315
|
+
// Packing poses for snapshots
|
|
316
|
+
//
|
|
317
|
+
|
|
318
|
+
// Every indexed object's current pose, as the Layout box's top-left corner and
|
|
319
|
+
// the body's angle. `server/index.js` drops the unchanged ones before sending.
|
|
320
|
+
export function objectPoses(sim) {
|
|
321
|
+
const out = [];
|
|
322
|
+
for (const [id, entry] of sim.objects) {
|
|
323
|
+
// The spread maps Matter centre coordinates through layoutOf before packing.
|
|
324
|
+
out.push([id, ...packPose(...layoutOf(entry))]);
|
|
325
|
+
}
|
|
326
|
+
return out;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// A body's pose in the terms the client writes onto a Layout box. The offset is
|
|
330
|
+
// not rotated with the body, matching how the engine writes its own bodies back.
|
|
331
|
+
export function layoutOf(entry) {
|
|
332
|
+
return [
|
|
333
|
+
entry.body.position.x - entry.offset.dx,
|
|
334
|
+
entry.body.position.y - entry.offset.dy,
|
|
335
|
+
entry.body.angle,
|
|
336
|
+
];
|
|
337
|
+
}
|