@zaisma/physics 2.4.1 → 2.6.0
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/README.md +64 -4
- package/batch.js +206 -0
- package/characters.js +31 -11
- package/declare.js +248 -24
- package/index.d.ts +111 -19
- package/level-characters.js +48 -13
- package/package.json +3 -1
- package/piece.json +6 -2
- package/piece.json.js +6 -2
- package/rapier/CHANGES.md +18 -0
- package/rapier/rapier.js +4620 -2762
- package/rapier/rapier.wasm +0 -0
- package/simulation.js +1020 -277
- package/sync.js +179 -66
package/sync.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
// calls (physicsCalls), the same alone and online.
|
|
10
10
|
import statement from "./piece.json.js";
|
|
11
11
|
import { camelName, openPiece, openSettings } from "../piece/index.js";
|
|
12
|
-
import { checkBody, checkId, checkJoint, refuse, rotationFromDeg, worldOf, MAXIMUM_BODY_COUNT, MAXIMUM_CHARACTER_COUNT, MAXIMUM_JOINT_COUNT, MAXIMUM_TRIANGLE_GROUP_NUMBER, MOTION_KINDS, WORLD_REACH_M } from "./declare.js";
|
|
12
|
+
import { checkBody, checkId, checkJoint, refuse, rotationFromDeg, triangleGroupBitNumberOf, triangleGroupMaskIndexOf, worldOf, MAXIMUM_BODY_COUNT, MAXIMUM_CHARACTER_COUNT, MAXIMUM_CHARACTER_SIZE_COUNT, MAXIMUM_FIXED_BODY_COUNT, MAXIMUM_JOINT_COUNT, MAXIMUM_TRIANGLE_GROUP_MASK_NUMBER, MAXIMUM_TRIANGLE_GROUP_NUMBER, MOTION_KINDS, WORLD_REACH_M } from "./declare.js";
|
|
13
13
|
import { physicsEngine } from "./engine.js";
|
|
14
14
|
import { defineCharacterKind, walkingCharacters } from "./characters.js";
|
|
15
15
|
import { createSimulation, turned } from "./simulation.js";
|
|
@@ -18,16 +18,21 @@ import { createSimulation, turned } from "./simulation.js";
|
|
|
18
18
|
const RULES_ID = "physics:rules";
|
|
19
19
|
/** The one thing that says which of the world's triangle groups are off (a door, a bridge), in a world whose meshes have them. */
|
|
20
20
|
const TRIANGLE_GROUPS_ID = "physics:triangle_groups";
|
|
21
|
-
|
|
21
|
+
/** Which triangle groups are off: three whole numbers with a bit for each group (triangleGroupMaskIndexOf, triangleGroupBitNumberOf). */
|
|
22
|
+
const TRIANGLE_GROUP_MASK_NAMES = ["offMaskNumber", "offSecondMaskNumber", "offThirdMaskNumber"];
|
|
23
|
+
const TRIANGLE_GROUP_FIELDS = TRIANGLE_GROUP_MASK_NAMES.map((name) => ({ name, valueKind: "integer", minimumNumber: 0, maximumNumber: MAXIMUM_TRIANGLE_GROUP_MASK_NUMBER }));
|
|
24
|
+
const NO_TRIANGLE_GROUPS_OFF = Object.freeze(Object.fromEntries(TRIANGLE_GROUP_MASK_NAMES.map((name) => [name, 0])));
|
|
22
25
|
const hasTriangleGroups = (world) => world.bodies.some((body) => body.shapes.some((shape) => shape.triangleGroupNumbers));
|
|
23
26
|
/** The piece's settings every side steps by, in the order they're sent. */
|
|
24
27
|
const BASE_SYNCED_SETTING_NAMES = ["gravityMPerSecPerSec", "bouncePct", "gripPct", "walkSpeedMPerSec", "jumpHeightM", "characterMassKg"];
|
|
25
28
|
const LISTENED_EVENT_NAMES = ["bodyAdded", "bodyRemoved", "characterAdded", "characterRemoved"];
|
|
26
29
|
const SET_BODY_KEYS = ["positionM", "rotationDeg", "velocityMPerSec", "spinRadPerSec", "motionKind", "collisionLayerName", "massKg"];
|
|
27
|
-
const SET_CHARACTER_KEYS = ["positionM", "velocityMPerSec", "facingYawRad", "collisionLayerName", "own"];
|
|
30
|
+
const SET_CHARACTER_KEYS = ["positionM", "velocityMPerSec", "facingYawRad", "collisionLayerName", "characterSizeName", "own"];
|
|
31
|
+
const ADD_CHARACTER_KEYS = ["positionM", "characterSizeName"];
|
|
28
32
|
// How far a game may be off the room's word, field by field, before it goes back and runs again: two copies of the
|
|
29
33
|
// engine drift a hair apart after going back, and nobody sees three centimetres. Whole numbers and yes-or-no fields
|
|
30
34
|
// are never off at all.
|
|
35
|
+
// A place is on millimetres: a timetable's places are too (declare.js), so what a body carries keeps up with it exactly.
|
|
31
36
|
const place = (name) => ({ name, valueKind: "number", minimumNumber: -WORLD_REACH_M, maximumNumber: WORLD_REACH_M, stepSizeNumber: 0.001, toleranceNumber: 0.03, snapAboveNumber: 3 });
|
|
32
37
|
const speed = (name) => ({ name, valueKind: "number", minimumNumber: -250, maximumNumber: 250, stepSizeNumber: 0.02, toleranceNumber: 0.6 });
|
|
33
38
|
const BODY_FIELDS = [
|
|
@@ -40,7 +45,7 @@ const BODY_FIELDS = [
|
|
|
40
45
|
speed("velocityZ"),
|
|
41
46
|
...["spinX", "spinY", "spinZ"].map((name) => ({ name, valueKind: "number", minimumNumber: -120, maximumNumber: 120, stepSizeNumber: 0.05, toleranceNumber: 1.5 })),
|
|
42
47
|
// Which body it is in every side's simulation, so a joint and a seat can name it; and which of the world's kinds it's made as (0: declared with shapes of its own).
|
|
43
|
-
{ name: "bodyNumber", valueKind: "integer", minimumNumber: 0, maximumNumber: MAXIMUM_BODY_COUNT },
|
|
48
|
+
{ name: "bodyNumber", valueKind: "integer", minimumNumber: 0, maximumNumber: MAXIMUM_BODY_COUNT + MAXIMUM_FIXED_BODY_COUNT },
|
|
44
49
|
{ name: "bodyKindNumber", valueKind: "integer", minimumNumber: 0, maximumNumber: 255 },
|
|
45
50
|
// Which of the world's collision layers it's on, and how it moves now (moving, kinematic or fixed): a parked cart made fixed.
|
|
46
51
|
{ name: "layerNumber", valueKind: "integer", minimumNumber: 0, maximumNumber: 15, defaultValue: 1 },
|
|
@@ -61,11 +66,21 @@ const CHARACTER_BASE_FIELDS = [
|
|
|
61
66
|
{ name: "isGrounded", valueKind: "boolean" },
|
|
62
67
|
{ name: "airTickCount", valueKind: "integer", minimumNumber: 0, maximumNumber: 250 },
|
|
63
68
|
// The body it rides in (its number and one more, 0 for none), and which of its seats.
|
|
64
|
-
{ name: "rideBodyNumber", valueKind: "integer", minimumNumber: 0, maximumNumber: MAXIMUM_BODY_COUNT + 1 },
|
|
69
|
+
{ name: "rideBodyNumber", valueKind: "integer", minimumNumber: 0, maximumNumber: MAXIMUM_BODY_COUNT + MAXIMUM_FIXED_BODY_COUNT + 1 },
|
|
65
70
|
{ name: "seatIndex", valueKind: "integer", minimumNumber: 0, maximumNumber: 7 },
|
|
66
71
|
{ name: "layerNumber", valueKind: "integer", minimumNumber: 0, maximumNumber: 15, defaultValue: 2 },
|
|
72
|
+
// Which size it is: 0 for the world's own character's, then the world's characterSizes in order.
|
|
73
|
+
{ name: "sizeNumber", valueKind: "integer", minimumNumber: 0, maximumNumber: MAXIMUM_CHARACTER_SIZE_COUNT },
|
|
67
74
|
];
|
|
68
75
|
const JOINT_FIELDS = [{ name: "isEnabled", valueKind: "boolean", defaultValue: true }];
|
|
76
|
+
// The same grain as the engine takes it, so a step leaves every awake body on the wire's steps (and reading it back
|
|
77
|
+
// from the wire changes nothing): each number field's range, step and count of steps, counted as the core's shape
|
|
78
|
+
// counts them, and how many steps each part of the rotation has.
|
|
79
|
+
const grainOf = (fieldName) => {
|
|
80
|
+
const field = BODY_FIELDS.find((each) => each.name === fieldName);
|
|
81
|
+
return { minimumNumber: field.minimumNumber, maximumNumber: field.maximumNumber, stepSizeNumber: field.stepSizeNumber, maximumStepCount: Math.ceil((field.maximumNumber - field.minimumNumber) / field.stepSizeNumber - 1e-9) };
|
|
82
|
+
};
|
|
83
|
+
const BODY_GRAIN = { place: grainOf("x"), speed: grainOf("velocityX"), spin: grainOf("spinX"), rotationMaximumStepCount: 2 ** BODY_FIELDS.find((each) => each.name === "rotation").componentBitCount - 1 };
|
|
69
84
|
|
|
70
85
|
/** A rules field for a setting, as its statement states it. */
|
|
71
86
|
const ruleFieldOf = (stated) => (stated.valueKind === "choice" ? { name: stated.name, valueKind: "choice", choices: stated.choices, defaultValue: stated.defaultValue } : { name: stated.name, valueKind: "number", minimumNumber: stated.minimumNumber, maximumNumber: stated.maximumNumber, stepSizeNumber: 0.001, defaultValue: stated.defaultValue });
|
|
@@ -88,7 +103,7 @@ function baseTuningOf(rules) {
|
|
|
88
103
|
}
|
|
89
104
|
|
|
90
105
|
function pointOrRefuse(given, what, fix) {
|
|
91
|
-
if (!given || !
|
|
106
|
+
if (!given || !Number.isFinite(given.x) || !Number.isFinite(given.y) || !Number.isFinite(given.z)) refuse("bad_argument", `${what} isn't { x, y, z }.`, what, fix);
|
|
92
107
|
return given;
|
|
93
108
|
}
|
|
94
109
|
|
|
@@ -130,7 +145,7 @@ function openWorld({ plan, settings, kindSettings, ask, tell, tickDurationSec, i
|
|
|
130
145
|
isStateOnSteps: true,
|
|
131
146
|
};
|
|
132
147
|
const ownRules = () => Object.fromEntries(ruleNames.map((name) => [name, BASE_SYNCED_SETTING_NAMES.includes(name) ? settings.get(name) : kindSettings.get(name)]));
|
|
133
|
-
const simulation = createSimulation(physicsEngine(), world, { tickDurationSec, characterKind: kind, tuning: tuningOf({ ...defaultRules, ...ownRules() }), readDataFile, tracksTouches });
|
|
148
|
+
const simulation = createSimulation(physicsEngine(), world, { tickDurationSec, characterKind: kind, tuning: tuningOf({ ...defaultRules, ...ownRules() }), readDataFile, tracksTouches, bodyGrain: BODY_GRAIN });
|
|
134
149
|
/** Where each character started, to put it back when it falls out of the world. */
|
|
135
150
|
const startOf = new Map();
|
|
136
151
|
/** The kind of thing each id is: "body", "character", "joint" or "rules". */
|
|
@@ -184,8 +199,7 @@ function openWorld({ plan, settings, kindSettings, ask, tell, tickDurationSec, i
|
|
|
184
199
|
},
|
|
185
200
|
/** The next body number nothing has. */
|
|
186
201
|
freeBodyNumber() {
|
|
187
|
-
|
|
188
|
-
return freeIndex === -1 ? simulation.bodies.length : freeIndex;
|
|
202
|
+
return simulation.freeBodyIndex();
|
|
189
203
|
},
|
|
190
204
|
add(entity, state) {
|
|
191
205
|
kindOf.set(entity.id, entity.entityKindName);
|
|
@@ -204,7 +218,7 @@ function openWorld({ plan, settings, kindSettings, ask, tell, tickDurationSec, i
|
|
|
204
218
|
return;
|
|
205
219
|
}
|
|
206
220
|
if (entity.entityKindName === "triangle_groups") {
|
|
207
|
-
simulation.setOffTriangleGroups(state.offMaskNumber);
|
|
221
|
+
simulation.setOffTriangleGroups(state.offMaskNumber, state.offSecondMaskNumber ?? 0, state.offThirdMaskNumber ?? 0);
|
|
208
222
|
return;
|
|
209
223
|
}
|
|
210
224
|
if (entity.entityKindName === "joint") {
|
|
@@ -246,9 +260,10 @@ function openWorld({ plan, settings, kindSettings, ask, tell, tickDurationSec, i
|
|
|
246
260
|
}
|
|
247
261
|
if (entityKindName === "character") {
|
|
248
262
|
const characterIndex = simulation.characterIndexById.get(entityId);
|
|
249
|
-
|
|
263
|
+
// The character's own object, filled again: the core reads it at once and keeps nothing.
|
|
264
|
+
return characterIndex === undefined ? null : simulation.readSteppedCharacter(characterIndex);
|
|
250
265
|
}
|
|
251
|
-
if (entityKindName === "triangle_groups") return { offMaskNumber: simulation.offTriangleGroupMaskNumber() };
|
|
266
|
+
if (entityKindName === "triangle_groups") return { offMaskNumber: simulation.offTriangleGroupMaskNumber(0), offSecondMaskNumber: simulation.offTriangleGroupMaskNumber(1), offThirdMaskNumber: simulation.offTriangleGroupMaskNumber(2) };
|
|
252
267
|
if (entityKindName === "joint") {
|
|
253
268
|
const isEnabled = simulation.joints.has(entityId) ? simulation.isJointEnabled(entityId) : pendingJoints.has(entityId) ? pendingJoints.get(entityId).isEnabled : null;
|
|
254
269
|
if (isEnabled !== null) readJointEnabled.set(entityId, isEnabled);
|
|
@@ -256,7 +271,13 @@ function openWorld({ plan, settings, kindSettings, ask, tell, tickDurationSec, i
|
|
|
256
271
|
}
|
|
257
272
|
const bodyIndex = simulation.bodyIndexById.get(entityId);
|
|
258
273
|
if (bodyIndex === undefined) return null;
|
|
259
|
-
|
|
274
|
+
// The body's own object, filled again: the core reads it at once and keeps nothing.
|
|
275
|
+
return simulation.readSteppedBody(bodyIndex);
|
|
276
|
+
},
|
|
277
|
+
/** Whether the last step left a body on the wire's steps itself, so what read answers needs no rounding and no writing back. */
|
|
278
|
+
isOnSteps(entityId) {
|
|
279
|
+
const bodyIndex = kindOf.get(entityId) === "body" ? simulation.bodyIndexById.get(entityId) : undefined;
|
|
280
|
+
return bodyIndex !== undefined && simulation.isBodyOnSteps(bodyIndex);
|
|
260
281
|
},
|
|
261
282
|
write(entityId, state, writeInfo) {
|
|
262
283
|
const entityKindName = kindOf.get(entityId);
|
|
@@ -266,7 +287,7 @@ function openWorld({ plan, settings, kindSettings, ask, tell, tickDurationSec, i
|
|
|
266
287
|
const characterIndex = simulation.characterIndexById.get(entityId);
|
|
267
288
|
if (characterIndex !== undefined) simulation.writeCharacter(characterIndex, state, writeInfo.isResting);
|
|
268
289
|
} else if (entityKindName === "triangle_groups") {
|
|
269
|
-
simulation.setOffTriangleGroups(state.offMaskNumber);
|
|
290
|
+
simulation.setOffTriangleGroups(state.offMaskNumber, state.offSecondMaskNumber ?? 0, state.offThirdMaskNumber ?? 0);
|
|
270
291
|
} else if (entityKindName === "joint") {
|
|
271
292
|
if (simulation.joints.has(entityId)) simulation.setJointEnabled(entityId, state.isEnabled);
|
|
272
293
|
else if (pendingJoints.has(entityId)) pendingJoints.get(entityId).isEnabled = state.isEnabled;
|
|
@@ -314,10 +335,10 @@ function openWorld({ plan, settings, kindSettings, ask, tell, tickDurationSec, i
|
|
|
314
335
|
const bodyIndex = simulation.bodyIndexById.get(entityId);
|
|
315
336
|
return bodyIndex !== undefined && simulation.isBodyResting(bodyIndex);
|
|
316
337
|
},
|
|
317
|
-
step({ tickIndex, isReplay, inputOf, isInCharge: isInChargeNow, emit }) {
|
|
338
|
+
step({ tickIndex, timetableTickIndex = tickIndex, isReplay, inputOf, isInCharge: isInChargeNow, emit }) {
|
|
318
339
|
isInCharge = isInChargeNow;
|
|
319
340
|
decideRules();
|
|
320
|
-
simulation.step({ tickIndex, isReplay, isInCharge, inputOf, emit: emit ?? (() => {}), roomState: opened.roomState, ask, tell });
|
|
341
|
+
simulation.step({ tickIndex, timetableTickIndex, isReplay, isInCharge, inputOf, emit: emit ?? (() => {}), roomState: opened.roomState, ask, tell });
|
|
321
342
|
const hits = simulation.takeHits();
|
|
322
343
|
// A step run again already happened: no sound, no hook, twice.
|
|
323
344
|
if (!isReplay) {
|
|
@@ -334,9 +355,7 @@ function openWorld({ plan, settings, kindSettings, ask, tell, tickDurationSec, i
|
|
|
334
355
|
const backM = putBack(startOf.get(character.id) ?? world.startPositionsM[0], character.id, true);
|
|
335
356
|
simulation.writeCharacter(character.index, { ...fields, x: backM.x, y: backM.y + world.character.heightM / 2, z: backM.z, velocityX: 0, velocityY: 0, velocityZ: 0, isGrounded: false, airTickCount: 0, rideBodyNumber: 0, seatIndex: 0, ...startFieldsOf(kind, simulation.stepTools) });
|
|
336
357
|
}
|
|
337
|
-
simulation.
|
|
338
|
-
if (simulation.readBody(bodyIndex).y < world.fallOutHeightM) fallen.push(bodyIndex);
|
|
339
|
-
});
|
|
358
|
+
simulation.addAwakeBodiesBelow(world.fallOutHeightM, fallen);
|
|
340
359
|
for (const bodyIndex of fallen.splice(0)) {
|
|
341
360
|
const declaration = simulation.bodies[bodyIndex].declaration;
|
|
342
361
|
const backM = putBack(declaration.positionM, declaration.id, false);
|
|
@@ -380,7 +399,7 @@ function worldFields(plan) {
|
|
|
380
399
|
{ id: RULES_ID, entityKindName: "rules", state: { ...plan.defaultRules } },
|
|
381
400
|
...world.bodies.flatMap((body, bodyIndex) => (body.motionKind !== "fixed" ? [{ id: body.id, entityKindName: "body", state: declaredBodyFields(body, bodyIndex, body.bodyKindNumber, world) }] : [])),
|
|
382
401
|
...world.joints.map((joint) => ({ id: joint.id, entityKindName: "joint", state: { isEnabled: true } })),
|
|
383
|
-
...(hasTriangleGroups(world) ? [{ id: TRIANGLE_GROUPS_ID, entityKindName: "triangle_groups", state: {
|
|
402
|
+
...(hasTriangleGroups(world) ? [{ id: TRIANGLE_GROUPS_ID, entityKindName: "triangle_groups", state: { ...NO_TRIANGLE_GROUPS_OFF } }] : []),
|
|
384
403
|
],
|
|
385
404
|
};
|
|
386
405
|
}
|
|
@@ -422,12 +441,19 @@ function checkAddedBody(world, declaration) {
|
|
|
422
441
|
if (declaration === null || typeof declaration !== "object" || declaration.bodyKindName === undefined) {
|
|
423
442
|
refuse("bad_body", "A body added while playing names one of the world's bodyKinds.", "body.bodyKindName", `Declare what it's made of in the world's bodyKinds, and add it as { id, bodyKindName, positionM }${world.bodyKinds.length ? `: this world has ${world.bodyKinds.map((bodyKind) => bodyKind.name).join(", ")}` : ""}. Every player's game then knows how to make it.`);
|
|
424
443
|
}
|
|
444
|
+
if (declaration.timetable !== undefined) refuse("bad_body", "A body added while playing has a timetable.", "body.timetable", "Declare a body on a timetable in the world's bodies, in the game and on its game server: every side works out where it is from the step alone.");
|
|
425
445
|
// How it moves from the start: a building piece placed fixed on a plot, never moving first.
|
|
426
446
|
const { motionKind, ...rest } = declaration;
|
|
427
447
|
if (motionKind !== undefined && !MOTION_KINDS.includes(motionKind)) refuse("bad_body", `body.motionKind is ${JSON.stringify(motionKind)}.`, "body.motionKind", `Use: ${MOTION_KINDS.join(", ")}; or leave it out for a body that moves.`);
|
|
428
448
|
return { ...checkBody(rest, "body", world.bodyKinds), startMotionKind: motionKind ?? "moving" };
|
|
429
449
|
}
|
|
430
450
|
|
|
451
|
+
/** A size's number on the wire: 0 for the world's own character's (null), then the world's characterSizes in order. */
|
|
452
|
+
const sizeNumberOf = (world, characterSizeName) => {
|
|
453
|
+
const sizeNumber = characterSizeName === null ? 0 : world.characterSizes.findIndex((size) => size.name === characterSizeName) + 1;
|
|
454
|
+
if (sizeNumber === 0 && characterSizeName !== null) refuse("bad_argument", `characterSizeName is ${JSON.stringify(characterSizeName)}.`, "characterSizeName", `Use one of the world's characterSizes (${world.characterSizes.map((size) => size.name).join(", ") || "it names none"}), or null for the world's own character.`);
|
|
455
|
+
return sizeNumber;
|
|
456
|
+
};
|
|
431
457
|
const layerNumberOf = (world, layerName, what) => {
|
|
432
458
|
const layerNumber = world.collisionLayers.findIndex((layer) => layer.name === layerName);
|
|
433
459
|
if (layerNumber < 0 || layerName === "riding") refuse("bad_argument", `${what} is ${JSON.stringify(layerName)}.`, what, `Use one of the world's collisionLayers: ${world.collisionLayers.filter((layer) => layer.name !== "riding").map((layer) => layer.name).join(", ")}.`);
|
|
@@ -475,7 +501,7 @@ function bodyStateOf(simulation, bodyId) {
|
|
|
475
501
|
};
|
|
476
502
|
}
|
|
477
503
|
|
|
478
|
-
const isFixedBody = (world, bodyId) => world.
|
|
504
|
+
const isFixedBody = (world, bodyId) => world.bodyById.get(bodyId)?.motionKind === "fixed";
|
|
479
505
|
const unknownBody = (bodyId) => refuse("unknown_body", `There's no body ${JSON.stringify(bodyId)} in this world.`, "bodyId", "Give the id of a body in bodyIds(): one the world declares, or one added with addBody.");
|
|
480
506
|
const unknownCharacter = (characterId) => refuse("unknown_character", `There's no character ${JSON.stringify(characterId)} here.`, "characterId", "Give the id of a character in characterIds(): a player's has the player's id.");
|
|
481
507
|
|
|
@@ -500,6 +526,7 @@ function characterView(world, simulation, fields, characterId) {
|
|
|
500
526
|
rideBodyId: rideIndex >= 0 ? (simulation?.bodies[rideIndex]?.declaration.id ?? null) : null,
|
|
501
527
|
seatIndex: rideIndex >= 0 ? fields.seatIndex : null,
|
|
502
528
|
collisionLayerName: world.collisionLayers[fields.layerNumber]?.name ?? "character",
|
|
529
|
+
characterSizeName: world.characterSizes[(fields.sizeNumber ?? 0) - 1]?.name ?? null,
|
|
503
530
|
};
|
|
504
531
|
}
|
|
505
532
|
|
|
@@ -524,27 +551,46 @@ const ownOf = (plan, fields, simulation = null, characterId = null) => {
|
|
|
524
551
|
return Object.fromEntries(plan.kind.stateFields.map((field) => [field.name, field.isShared === false ? (localOwn?.[field.name] ?? null) : fields[field.name]]));
|
|
525
552
|
};
|
|
526
553
|
|
|
527
|
-
/**
|
|
528
|
-
|
|
554
|
+
/** What each query takes, beside collisionLayerNames and excludedId (and, for a shape, isStoppedAtPenetration, rotation and marginM). */
|
|
555
|
+
const QUERY_KEYS = {
|
|
556
|
+
castRay: ["fromM", "directionM", "maximumDistanceM"],
|
|
557
|
+
castBall: ["fromM", "radiusM", "directionM", "maximumDistanceM"],
|
|
558
|
+
castCapsule: ["fromM", "radiusM", "heightM", "directionM", "maximumDistanceM"],
|
|
559
|
+
overlapCapsule: ["centerM", "radiusM", "heightM"],
|
|
560
|
+
overlapBall: ["centerM", "radiusM"],
|
|
561
|
+
};
|
|
562
|
+
const SHAPE_QUERY_KEYS = ["isStoppedAtPenetration", "rotation", "marginM"];
|
|
563
|
+
const OVERLAP_QUERY_KEYS = [...SHAPE_QUERY_KEYS, "isEachTriangle", "isOneSided"];
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* A query's request, checked: the points and lengths a ray, a ball's sweep or an overlap takes. The request itself
|
|
567
|
+
* goes on to the search, and checking it makes nothing: a game asks thousands a second.
|
|
568
|
+
*/
|
|
569
|
+
function checkQuery(given, what, isShape = false) {
|
|
570
|
+
const keys = QUERY_KEYS[what];
|
|
529
571
|
if (given === null || typeof given !== "object") refuse("bad_argument", `${what} takes an object.`, what, `Give { ${keys.join(", ")} }.`);
|
|
530
|
-
for (const key
|
|
531
|
-
for (
|
|
532
|
-
|
|
533
|
-
|
|
572
|
+
for (const key in given) if (!keys.includes(key) && key !== "collisionLayerNames" && key !== "excludedId" && !(isShape && (what === "overlapCapsule" ? OVERLAP_QUERY_KEYS : SHAPE_QUERY_KEYS).includes(key))) refuse("unknown_argument", `${what} has no "${key}".`, key, `It takes: ${keys.join(", ")}, collisionLayerNames, excludedId.`);
|
|
573
|
+
for (let keyIndex = 0; keyIndex < keys.length; keyIndex += 1) {
|
|
574
|
+
const key = keys[keyIndex];
|
|
575
|
+
const givenValue = given[key];
|
|
576
|
+
if (typeof givenValue === "object" && key.endsWith("M")) pointOrRefuse(givenValue, key, "Give three numbers: x, y and z, in metres.");
|
|
577
|
+
else if (!Number.isFinite(givenValue) || givenValue < 0) refuse("bad_argument", `${key} is ${JSON.stringify(givenValue)}.`, key, "Give a length in metres from 0.");
|
|
534
578
|
}
|
|
535
579
|
return given;
|
|
536
580
|
}
|
|
537
581
|
|
|
538
|
-
/**
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
const { isStoppedAtPenetration, rotation, marginM, ...rest } = request ?? {};
|
|
582
|
+
/** A shape's query, checked: its points and lengths, and the isStoppedAtPenetration, rotation and marginM a shape's cast and overlap take beside them. */
|
|
583
|
+
function shapeQueryOf(request, what) {
|
|
584
|
+
const { isStoppedAtPenetration, rotation, marginM } = checkQuery(request, what, true);
|
|
542
585
|
if (isStoppedAtPenetration !== undefined && typeof isStoppedAtPenetration !== "boolean") refuse("bad_argument", "isStoppedAtPenetration isn't true or false.", "isStoppedAtPenetration", "Give false to pass out of what the shape starts touching (a camera pressed to a wall), or leave it out.");
|
|
543
|
-
if (rotation !== undefined && !(rotation &&
|
|
586
|
+
if (rotation !== undefined && !(rotation && Number.isFinite(rotation.x) && Number.isFinite(rotation.y) && Number.isFinite(rotation.z) && Number.isFinite(rotation.w))) refuse("bad_argument", "rotation isn't { x, y, z, w }.", "rotation", "Give the turn as the engine and three.js hold it (a quaternion), or leave it out for standing up.");
|
|
544
587
|
if (marginM !== undefined && !(Number.isFinite(marginM) && marginM >= 0 && marginM <= 1)) refuse("bad_argument", `marginM is ${JSON.stringify(marginM)}.`, "marginM", "Give how near counts as touching, 0 to 1 m, or leave it out.");
|
|
545
|
-
|
|
588
|
+
if (request.isEachTriangle !== undefined && typeof request.isEachTriangle !== "boolean") refuse("bad_argument", "isEachTriangle isn't true or false.", "isEachTriangle", "Give true for every triangle of a mesh the capsule touches, or leave it out for the deepest one of each mesh.");
|
|
589
|
+
if (request.isOneSided !== undefined && typeof request.isOneSided !== "boolean") refuse("bad_argument", "isOneSided isn't true or false.", "isOneSided", "Give true to leave out triangles that face away from the capsule, or leave it out.");
|
|
590
|
+
return request;
|
|
546
591
|
}
|
|
547
592
|
|
|
593
|
+
/** The queries a game asks of a world: the same alone, in a player's game and on a game server. */
|
|
548
594
|
function queryCalls(simulationNow) {
|
|
549
595
|
const checkInto = (into) => {
|
|
550
596
|
if (into !== undefined && into !== null && typeof into !== "object") refuse("bad_argument", "into isn't an object.", "into", "Give an object of your own to write the hit into (kept and reused), or leave it out for a new one.");
|
|
@@ -552,15 +598,21 @@ function queryCalls(simulationNow) {
|
|
|
552
598
|
};
|
|
553
599
|
return {
|
|
554
600
|
/** The first thing a ray meets: { bodyId, characterId, pointM, normal, distanceM }, or null. With into, written there instead of a new object. */
|
|
555
|
-
castRay: (request, into) => simulationNow()?.castRay(checkQuery(request,
|
|
601
|
+
castRay: (request, into) => simulationNow()?.castRay(checkQuery(request, "castRay"), checkInto(into)) ?? null,
|
|
556
602
|
/** The first thing a ball meets moving along a direction, or null. */
|
|
557
|
-
castBall: (request, into) => simulationNow()?.castBall(shapeQueryOf(request,
|
|
603
|
+
castBall: (request, into) => simulationNow()?.castBall(shapeQueryOf(request, "castBall"), checkInto(into)) ?? null,
|
|
558
604
|
/** The first thing a capsule (standing up, or turned by rotation) meets moving along a direction, or null. */
|
|
559
|
-
castCapsule: (request, into) => simulationNow()?.castCapsule(shapeQueryOf(request,
|
|
605
|
+
castCapsule: (request, into) => simulationNow()?.castCapsule(shapeQueryOf(request, "castCapsule"), checkInto(into)) ?? null,
|
|
560
606
|
/** Where a capsule touches the world: [{ bodyId, characterId, pointM, normal, depthM }], to push out of walls by. */
|
|
561
|
-
overlapCapsule
|
|
607
|
+
overlapCapsule(request, into) {
|
|
608
|
+
if (into !== undefined && into !== null && !Array.isArray(into)) refuse("bad_argument", "into isn't a list.", "into", "Give a list of your own to write the contacts into (kept and given again), or leave it out for a new one.");
|
|
609
|
+
const simulation = simulationNow();
|
|
610
|
+
if (simulation) return simulation.overlapCapsule(shapeQueryOf(request, "overlapCapsule"), into ?? null);
|
|
611
|
+
if (into) into.length = 0;
|
|
612
|
+
return into ?? [];
|
|
613
|
+
},
|
|
562
614
|
/** Everything a ball overlaps: [{ bodyId, characterId }]. */
|
|
563
|
-
overlapBall: (request) => simulationNow()?.overlapBall(checkQuery(request,
|
|
615
|
+
overlapBall: (request) => simulationNow()?.overlapBall(checkQuery(request, "overlapBall")) ?? [],
|
|
564
616
|
};
|
|
565
617
|
}
|
|
566
618
|
|
|
@@ -569,7 +621,7 @@ function queryCalls(simulationNow) {
|
|
|
569
621
|
* online core's World gives it (ids, has, entityKindName, state, drawn, add, remove, set, setInput, on, simulation),
|
|
570
622
|
* and the rest says who this game is in it.
|
|
571
623
|
*/
|
|
572
|
-
function physicsCalls({ plan, settings, hooks, things, ownCharacterId, isInCharge, isReady, hasGameServer }) {
|
|
624
|
+
function physicsCalls({ plan, settings, hooks, things, ownCharacterId, isInCharge, isReady, hasGameServer, tickNumber }) {
|
|
573
625
|
const { world, kind } = plan;
|
|
574
626
|
const listeners = Object.fromEntries(LISTENED_EVENT_NAMES.map((eventName) => [eventName, new Set()]));
|
|
575
627
|
/** Each thing's kind, kept so a removal can still say what was removed. */
|
|
@@ -599,10 +651,14 @@ function physicsCalls({ plan, settings, hooks, things, ownCharacterId, isInCharg
|
|
|
599
651
|
return null;
|
|
600
652
|
};
|
|
601
653
|
/** Where a body is drawn: a moving one from the world in play, a fixed one where it's declared. */
|
|
654
|
+
const timetabledPlace = { x: 0, y: 0, z: 0, rotationX: 0, rotationY: 0, rotationZ: 0, rotationW: 1 };
|
|
602
655
|
const drawnOf = (bodyId) => {
|
|
603
656
|
const drawn = isBody(bodyId) ? things.drawn(bodyId) : null;
|
|
657
|
+
// A body on a timetable is drawn where this frame's moment has it, between two steps.
|
|
658
|
+
const simulation = drawn ? simulationNow() : null;
|
|
659
|
+
if (simulation?.timetablePlace(simulation.bodyIndexById.get(bodyId), tickNumber(), timetabledPlace)) return { ...drawn, x: timetabledPlace.x, y: timetabledPlace.y, z: timetabledPlace.z, rotation: { x: timetabledPlace.rotationX, y: timetabledPlace.rotationY, z: timetabledPlace.rotationZ, w: timetabledPlace.rotationW } };
|
|
604
660
|
if (drawn) return drawn;
|
|
605
|
-
const declaration = world
|
|
661
|
+
const declaration = isFixedBody(world, bodyId) ? world.bodyById.get(bodyId) : null;
|
|
606
662
|
return declaration ? { x: declaration.positionM.x, y: declaration.positionM.y, z: declaration.positionM.z, rotation: declaration.rotation } : null;
|
|
607
663
|
};
|
|
608
664
|
const shared = sharedCalls({ plan, things, simulationNow, inChargeOf: inCharge });
|
|
@@ -630,7 +686,7 @@ function physicsCalls({ plan, settings, hooks, things, ownCharacterId, isInCharg
|
|
|
630
686
|
bodyDeclaration(bodyId) {
|
|
631
687
|
const simulation = simulationNow();
|
|
632
688
|
if (simulation) return simulation.bodies[simulation.bodyIndexById.get(bodyId)]?.declaration ?? null;
|
|
633
|
-
return world.
|
|
689
|
+
return world.bodyById.get(bodyId) ?? null;
|
|
634
690
|
},
|
|
635
691
|
/** Where to draw a body this frame: { positionM, rotation, steerNumber }. null for a body that isn't here. */
|
|
636
692
|
drawnBody(bodyId) {
|
|
@@ -687,7 +743,7 @@ function sharedCalls({ plan, things, simulationNow, inChargeOf }) {
|
|
|
687
743
|
// Every thing in play, declared ones included: an id freed by a removal may be used again, even in the same step.
|
|
688
744
|
const ids = () => new Set(things.ids());
|
|
689
745
|
return {
|
|
690
|
-
bodyIds: () => [...world.
|
|
746
|
+
bodyIds: () => [...world.fixedBodyIds, ...things.ids("body")],
|
|
691
747
|
hasBody: (bodyId) => isBody(bodyId) || isFixedBody(world, bodyId),
|
|
692
748
|
/** A body as this world has it now. */
|
|
693
749
|
bodyState: (bodyId) => bodyStateOf(simulationNow(), bodyId),
|
|
@@ -704,8 +760,9 @@ function sharedCalls({ plan, things, simulationNow, inChargeOf }) {
|
|
|
704
760
|
setBody(bodyId, changes) {
|
|
705
761
|
const running = inChargeOf("bodies are set");
|
|
706
762
|
const fields = bodyChanges(world, changes);
|
|
707
|
-
if (!isBody(bodyId)) return world.
|
|
763
|
+
if (!isBody(bodyId)) return world.bodyById.has(bodyId) ? false : unknownBody(bodyId);
|
|
708
764
|
const isMoved = ["x", "y", "z", "rotation"].some((name) => name in fields);
|
|
765
|
+
if (world.bodyById.get(bodyId)?.timetable && Object.keys(fields).some((name) => name !== "layerNumber")) refuse("bad_argument", `setBody was given more than a collision layer for "${bodyId}", which is on a timetable.`, "changes", "Its timetable says where it is at every step: give collisionLayerName alone, or declare it without a timetable to move it yourself.");
|
|
709
766
|
return Boolean(running && running.set(bodyId, fields, { isTeleport: isMoved }));
|
|
710
767
|
},
|
|
711
768
|
pushBody(bodyId, pushMPerSec) {
|
|
@@ -727,8 +784,11 @@ function sharedCalls({ plan, things, simulationNow, inChargeOf }) {
|
|
|
727
784
|
const running = inChargeOf("characters are added");
|
|
728
785
|
if (!running || things.has(characterId)) return false;
|
|
729
786
|
if (things.ids("character").length >= MAXIMUM_CHARACTER_COUNT) refuse("too_many_characters", `A world holds at most ${MAXIMUM_CHARACTER_COUNT} characters.`, "characterId", "Remove characters nobody needs.");
|
|
787
|
+
for (const key of Object.keys(addOptions ?? {})) if (!ADD_CHARACTER_KEYS.includes(key)) refuse("unknown_argument", `addCharacter has no "${key}".`, key, `It takes: ${ADD_CHARACTER_KEYS.join(", ")}.`);
|
|
730
788
|
const feetM = addOptions?.positionM === undefined ? null : pointOrRefuse(addOptions.positionM, "positionM", "Give where its feet stand, in metres.");
|
|
731
|
-
|
|
789
|
+
const sizeNumber = addOptions?.characterSizeName === undefined ? 0 : sizeNumberOf(world, addOptions.characterSizeName);
|
|
790
|
+
const state = { ...(feetM ? { x: feetM.x, y: feetM.y + world.character.heightM / 2, z: feetM.z } : {}), ...(sizeNumber ? { sizeNumber } : {}) };
|
|
791
|
+
return Boolean(running.add(characterId, { entityKindName: "character", ...(Object.keys(state).length ? { state } : {}) }));
|
|
732
792
|
},
|
|
733
793
|
removeCharacter: (characterId) => Boolean(inChargeOf("characters are removed") && isCharacter(characterId) && things.remove(characterId)),
|
|
734
794
|
/** Sets a character at once: its feet's place (a teleport), its speed, facing, collision layer, or its kind's own fields. */
|
|
@@ -744,6 +804,7 @@ function sharedCalls({ plan, things, simulationNow, inChargeOf }) {
|
|
|
744
804
|
if (changes?.velocityMPerSec !== undefined) Object.assign(fields, { velocityX: pointOrRefuse(changes.velocityMPerSec, "velocityMPerSec", "Give three numbers: x, y and z.").x, velocityY: changes.velocityMPerSec.y, velocityZ: changes.velocityMPerSec.z });
|
|
745
805
|
if (changes?.facingYawRad !== undefined) fields.facingYawRad = changes.facingYawRad;
|
|
746
806
|
if (changes?.collisionLayerName !== undefined) fields.layerNumber = layerNumberOf(world, changes.collisionLayerName, "collisionLayerName");
|
|
807
|
+
if (changes?.characterSizeName !== undefined) fields.sizeNumber = sizeNumberOf(world, changes.characterSizeName);
|
|
747
808
|
if (changes?.own !== undefined) {
|
|
748
809
|
if (changes.own === null || typeof changes.own !== "object") refuse("bad_argument", "own isn't an object of the character kind's fields.", "own", `Give some of: ${kind.stateFields.map((field) => field.name).join(", ") || "(this kind has none)"}.`);
|
|
749
810
|
for (const [name, ownValue] of Object.entries(changes.own)) {
|
|
@@ -787,18 +848,19 @@ function sharedCalls({ plan, things, simulationNow, inChargeOf }) {
|
|
|
787
848
|
return Boolean(running && running.set(jointId, { isEnabled: changes.isEnabled }, { isTeleport: false }));
|
|
788
849
|
},
|
|
789
850
|
isJointEnabled: (jointId) => (isJoint(jointId) ? Boolean(things.state(jointId)?.isEnabled) : false),
|
|
790
|
-
/** Switches one of the world's triangle groups on or off for everyone (a door that opens, a bridge that falls), 1 to
|
|
851
|
+
/** Switches one of the world's triangle groups on or off for everyone (a door that opens, a bridge that falls), 1 to 63. */
|
|
791
852
|
setTriangleGroup(groupNumber, isEnabled) {
|
|
792
853
|
const running = inChargeOf("triangle groups are switched");
|
|
793
854
|
if (!Number.isInteger(groupNumber) || groupNumber < 1 || groupNumber > MAXIMUM_TRIANGLE_GROUP_NUMBER) refuse("bad_argument", `groupNumber is ${JSON.stringify(groupNumber)}.`, "groupNumber", `Give a group from 1 to ${MAXIMUM_TRIANGLE_GROUP_NUMBER}, as the mesh's triangleGroupNumbers number it.`);
|
|
794
855
|
if (typeof isEnabled !== "boolean") refuse("bad_argument", "setTriangleGroup takes the group and true or false.", "isEnabled", "Give true to have its triangles there, false to take them away.");
|
|
795
856
|
if (!hasTriangleGroups(world)) refuse("no_triangle_groups", "This world's meshes have no triangleGroupNumbers.", "groupNumber", "Give a mesh triangleGroupNumbers in the world's declaration, in the game and on its game server.");
|
|
796
|
-
const
|
|
797
|
-
const
|
|
798
|
-
|
|
857
|
+
const maskName = TRIANGLE_GROUP_MASK_NAMES[triangleGroupMaskIndexOf(groupNumber)];
|
|
858
|
+
const maskNumber = things.state(TRIANGLE_GROUPS_ID)?.[maskName] ?? 0;
|
|
859
|
+
const bitNumber = triangleGroupBitNumberOf(groupNumber);
|
|
860
|
+
return Boolean(running && running.set(TRIANGLE_GROUPS_ID, { [maskName]: isEnabled ? maskNumber & ~bitNumber : maskNumber | bitNumber }, { isTeleport: false }));
|
|
799
861
|
},
|
|
800
862
|
/** Whether a triangle group is there now. */
|
|
801
|
-
isTriangleGroupEnabled: (groupNumber) => !((things.state(TRIANGLE_GROUPS_ID)?.
|
|
863
|
+
isTriangleGroupEnabled: (groupNumber) => !((things.state(TRIANGLE_GROUPS_ID)?.[TRIANGLE_GROUP_MASK_NAMES[triangleGroupMaskIndexOf(groupNumber)]] ?? 0) & triangleGroupBitNumberOf(groupNumber)),
|
|
802
864
|
};
|
|
803
865
|
}
|
|
804
866
|
|
|
@@ -854,6 +916,7 @@ export const physicsKindFrom = (worldKind) => (givenWorld, given = {}) => {
|
|
|
854
916
|
isInCharge,
|
|
855
917
|
isReady: () => isInCharge() || (world.playerCharacterMode === "on_join" && !kindContext.isSpectator ? things.drawn(kindContext.myPlayerId) !== null : kindContext.tickIndex() !== null),
|
|
856
918
|
hasGameServer: kindContext.authorityKind === "server",
|
|
919
|
+
tickNumber: () => (kindContext.tickIndex() ?? 0) + kindContext.fractionNumber(),
|
|
857
920
|
});
|
|
858
921
|
},
|
|
859
922
|
// What physics.onServer(state, room) answers on a game server.
|
|
@@ -897,6 +960,27 @@ function physicsServerCalls({ plan, settings, things, roomState }) {
|
|
|
897
960
|
return kind.serverCalls ? kind.serverCalls(calls, { things, simulation, opened, roomState }) : calls;
|
|
898
961
|
}
|
|
899
962
|
|
|
963
|
+
/** A body's place, rotation, speed and spin from what a step left, with the fields a step doesn't move from where they were last put on steps. */
|
|
964
|
+
function copyMotion(into, read, rest) {
|
|
965
|
+
into.x = read.x;
|
|
966
|
+
into.y = read.y;
|
|
967
|
+
into.z = read.z;
|
|
968
|
+
into.rotation.x = read.rotation.x;
|
|
969
|
+
into.rotation.y = read.rotation.y;
|
|
970
|
+
into.rotation.z = read.rotation.z;
|
|
971
|
+
into.rotation.w = read.rotation.w;
|
|
972
|
+
into.velocityX = read.velocityX;
|
|
973
|
+
into.velocityY = read.velocityY;
|
|
974
|
+
into.velocityZ = read.velocityZ;
|
|
975
|
+
into.spinX = read.spinX;
|
|
976
|
+
into.spinY = read.spinY;
|
|
977
|
+
into.spinZ = read.spinZ;
|
|
978
|
+
into.layerNumber = rest.layerNumber;
|
|
979
|
+
into.motionNumber = rest.motionNumber;
|
|
980
|
+
into.steerNumber = rest.steerNumber;
|
|
981
|
+
into.massKg = rest.massKg;
|
|
982
|
+
}
|
|
983
|
+
|
|
900
984
|
/**
|
|
901
985
|
* A world played alone: the same calls over the same simulation, stepped here with no room. update(nowMs) runs the
|
|
902
986
|
* fixed steps that are due on the clock it's given.
|
|
@@ -919,6 +1003,8 @@ export function alonePhysics(givenWorld, { settings: givenSettings, hooks: given
|
|
|
919
1003
|
let lastTickIndex = null;
|
|
920
1004
|
let lastTickAtMs = 0;
|
|
921
1005
|
let nowMs = 0;
|
|
1006
|
+
/** Steps run so far: the clock of the world's timetables, which starts with the world and never skips. */
|
|
1007
|
+
let ranTickCount = 0;
|
|
922
1008
|
/** Puts a thing's fields on their steps, as online: the same numbers alone as in a room. */
|
|
923
1009
|
const onSteps = (entityKindName, state) => shapes[entityKindName].onSteps(state);
|
|
924
1010
|
|
|
@@ -981,7 +1067,47 @@ export function alonePhysics(givenWorld, { settings: givenSettings, hooks: given
|
|
|
981
1067
|
for (const entity of fields.entities) things.add(entity.id, { entityKindName: entity.entityKindName, state: entity.state });
|
|
982
1068
|
if (world.playerCharacterMode === "on_join") things.add(playerId, { entityKindName: "character" });
|
|
983
1069
|
|
|
984
|
-
const
|
|
1070
|
+
const steppedWriteInfo = { isResting: false, isTeleport: false };
|
|
1071
|
+
/** After a step: a thing's fields as the step left them, on the wire's steps as online, beside the step before for drawing between the two. */
|
|
1072
|
+
function takeStepped(entity, id) {
|
|
1073
|
+
if (entity.entityKindName === "rules") return;
|
|
1074
|
+
const isResting = opened.isResting(id);
|
|
1075
|
+
// A thing at rest is as it was: read once where it stopped, then nothing to read and nothing to blend.
|
|
1076
|
+
if (isResting && entity.isSettled) return;
|
|
1077
|
+
const read = opened.read(id);
|
|
1078
|
+
if (!read) return;
|
|
1079
|
+
let onStepsRead;
|
|
1080
|
+
if (entity.entityKindName === "body" && entity.onStepsRest && opened.isOnSteps(id) && read.layerNumber === entity.readLayerNumber && read.motionNumber === entity.readMotionNumber && read.steerNumber === entity.readSteerNumber && read.massKg === entity.readMassKg) {
|
|
1081
|
+
// The step left it on the wire's steps already: nothing to round and nothing to write back, so a
|
|
1082
|
+
// thousand bodies falling leave nothing for the garbage collector. Its fields go into one of two
|
|
1083
|
+
// objects of its own, turn about (nothing outside keeps them).
|
|
1084
|
+
if (!entity.ownFirst) {
|
|
1085
|
+
entity.ownFirst = { ...entity.onStepsRest, rotation: { x: 0, y: 0, z: 0, w: 1 } };
|
|
1086
|
+
entity.ownSecond = { ...entity.onStepsRest, rotation: { x: 0, y: 0, z: 0, w: 1 } };
|
|
1087
|
+
}
|
|
1088
|
+
onStepsRead = entity.later === entity.ownFirst ? entity.ownSecond : entity.ownFirst;
|
|
1089
|
+
copyMotion(onStepsRead, read, entity.onStepsRest);
|
|
1090
|
+
} else {
|
|
1091
|
+
onStepsRead = onSteps(entity.entityKindName, read);
|
|
1092
|
+
// As online, every place steps on the wire's numbers.
|
|
1093
|
+
steppedWriteInfo.isResting = isResting;
|
|
1094
|
+
opened.write(id, onStepsRead, steppedWriteInfo);
|
|
1095
|
+
if (entity.entityKindName === "body") {
|
|
1096
|
+
entity.readLayerNumber = read.layerNumber;
|
|
1097
|
+
entity.readMotionNumber = read.motionNumber;
|
|
1098
|
+
entity.readSteerNumber = read.steerNumber;
|
|
1099
|
+
entity.readMassKg = read.massKg;
|
|
1100
|
+
entity.onStepsRest = onStepsRead;
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
entity.earlier = entity.isSettled ? onStepsRead : entity.later;
|
|
1104
|
+
entity.later = onStepsRead;
|
|
1105
|
+
entity.isSettled = isResting;
|
|
1106
|
+
if (isResting) entity.earlier = entity.later;
|
|
1107
|
+
}
|
|
1108
|
+
// The moment a frame draws: the last step run, and how far the clock is into the next.
|
|
1109
|
+
const tickNumber = () => Math.max(ranTickCount - 1, 0) + Math.min(Math.max((nowMs - lastTickAtMs) / (tickDurationSec * 1000), 0), 1);
|
|
1110
|
+
const calls = physicsCalls({ plan, settings, hooks, things, ownCharacterId: playerId, isInCharge: () => true, isReady: () => true, hasGameServer: false, tickNumber });
|
|
985
1111
|
return Object.assign(calls, {
|
|
986
1112
|
/** Frees the engine's memory this world holds: a menu played alone, before the game joins a room. Nothing may be called on it after. */
|
|
987
1113
|
dispose() {
|
|
@@ -1007,23 +1133,10 @@ export function alonePhysics(givenWorld, { settings: givenSettings, hooks: given
|
|
|
1007
1133
|
const emit = (eventName, payload = null, emitOptions = {}) => {
|
|
1008
1134
|
for (const listener of [...listeners.emitted]) listener({ kindName: "physics", entityId: emitOptions.entityId ?? null, eventName, payload, tickIndex, playerId: emitOptions.entityId === playerId ? playerId : null, viewTickNumber: null, isReplay: false });
|
|
1009
1135
|
};
|
|
1010
|
-
opened.step({ tickIndex, tickDurationSec, isReplay: false, isInCharge: true, inputOf: (id) => inputs.get(id) ?? null, emit });
|
|
1136
|
+
opened.step({ tickIndex, timetableTickIndex: ranTickCount, tickDurationSec, isReplay: false, isInCharge: true, inputOf: (id) => inputs.get(id) ?? null, emit });
|
|
1137
|
+
ranTickCount += 1;
|
|
1011
1138
|
for (const held of inputs.values()) for (const name of inputShape.pressNames) held[name] = false;
|
|
1012
|
-
|
|
1013
|
-
if (entity.entityKindName === "rules") continue;
|
|
1014
|
-
const isResting = opened.isResting(id);
|
|
1015
|
-
// A thing at rest is as it was: read once where it stopped, then nothing to read and nothing to blend.
|
|
1016
|
-
if (isResting && entity.isSettled) continue;
|
|
1017
|
-
const read = opened.read(id);
|
|
1018
|
-
if (!read) continue;
|
|
1019
|
-
const onStepsRead = onSteps(entity.entityKindName, read);
|
|
1020
|
-
// As online, every place steps on the wire's numbers.
|
|
1021
|
-
opened.write(id, onStepsRead, { isResting, isTeleport: false });
|
|
1022
|
-
entity.earlier = entity.isSettled ? onStepsRead : entity.later;
|
|
1023
|
-
entity.later = onStepsRead;
|
|
1024
|
-
entity.isSettled = isResting;
|
|
1025
|
-
if (isResting) entity.earlier = entity.later;
|
|
1026
|
-
}
|
|
1139
|
+
entities.forEach(takeStepped);
|
|
1027
1140
|
// The step's own time, not the frame's: a frame just before a step is due and the one after it then blend evenly.
|
|
1028
1141
|
lastTickAtMs = tickIndex * tickDurationSec * 1000;
|
|
1029
1142
|
}
|