@zaisma/physics 0.0.0-stage → 2.4.1
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/LICENSE.txt +93 -0
- package/README.md +146 -2
- package/characters.js +189 -0
- package/data.js +85 -0
- package/declare.js +495 -0
- package/engine.js +48 -0
- package/index.d.ts +743 -0
- package/index.js +27 -0
- package/level-characters.js +139 -0
- package/package.json +57 -4
- package/piece.json +136 -0
- package/piece.json.js +137 -0
- package/rapier/LICENSE.txt +201 -0
- package/rapier/rapier.d.ts +903 -0
- package/rapier/rapier.js +12674 -0
- package/rapier/rapier.wasm +0 -0
- package/server.d.ts +34 -0
- package/server.js +17 -0
- package/simulation.js +1136 -0
- package/sync.js +1035 -0
- package/world.d.ts +4 -0
- package/world.js +5 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,743 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zaisma/physics: physics that works as it's added, alone and online, for every kind of game. One world of bodies
|
|
3
|
+
* (moving, kinematic or fixed), ground for a big outdoor map laid only where things are, joints, wheeled vehicles,
|
|
4
|
+
* seats, collision layers and queries, and characters whose movement is the piece's walk or the game's own. The
|
|
5
|
+
* engine under it is Rapier, shipped with the piece.
|
|
6
|
+
*
|
|
7
|
+
* Online it's a world on the online core (@zaisma/net's worldKind), and the game writes nothing for the sync:
|
|
8
|
+
* - Whoever is in charge of the room runs the world: the game server when the game has one, else the host's game.
|
|
9
|
+
* - A player's own moves answer at once: their game steps the same world a little ahead and settles to the room's
|
|
10
|
+
* word, easing what was off instead of jumping. Every place steps on exactly the numbers the wire carries, so a
|
|
11
|
+
* player's game and the game server don't drift apart between words.
|
|
12
|
+
* - Other players are drawn where they are, and what one of them pushes, rides or holds is drawn with them.
|
|
13
|
+
* - A body at rest costs nothing: it's said once where it stopped, and a room where nothing moves sends nothing.
|
|
14
|
+
*
|
|
15
|
+
* What belongs to one kind of game is a character kind on top: the piece's own walk (walkingCharacters), a game's
|
|
16
|
+
* own (defineCharacterKind: a shooter's thrusters, a farm's speed by surface), or a piece's, like
|
|
17
|
+
* @zaisma/physics-party's hands that grab, carry and throw.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* // world.js: the world, in a file of its own. With a game server, the same file goes in zaisma-server/ too.
|
|
21
|
+
* import { definePhysicsWorld } from "./pieces/@zaisma/physics/world.js";
|
|
22
|
+
* export const yard = definePhysicsWorld({
|
|
23
|
+
* bodies: [
|
|
24
|
+
* { id: "valley", motionKind: "fixed", shapes: [{ shapeKind: "heightfield", dataPath: "levels/valley-heights.bin", rowCount: 1501, columnCount: 1501, cellSizeM: 1 }] },
|
|
25
|
+
* { id: "cart:1", bodyKindName: "cart", positionM: { x: 700, y: 71, z: 700 } },
|
|
26
|
+
* ],
|
|
27
|
+
* bodyKinds: { cart: { massKg: 200, shapes: [{ shapeKind: "box", sizeM: { x: 1.4, y: 0.6, z: 2.2 } }], wheels: [...], seats: [{ positionM: { x: 0, y: 0.4, z: 0 } }] } },
|
|
28
|
+
* startPositionsM: [{ x: 700, y: 72, z: 705 }],
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* // Online: on the relay it needs no server code.
|
|
33
|
+
* import { createNet } from "./pieces/@zaisma/net/index.js";
|
|
34
|
+
* import { loadPhysics, loadPhysicsData, physicsKind } from "./pieces/@zaisma/physics/index.js";
|
|
35
|
+
*
|
|
36
|
+
* await loadPhysics();
|
|
37
|
+
* await loadPhysicsData(yard); // the valley's heights, from beside the page
|
|
38
|
+
* const room = await Zaisma.joinRoom();
|
|
39
|
+
* const net = createNet(room); // with a game server: createNet(room, { authorityKind: "server" })
|
|
40
|
+
* const physics = net.use(physicsKind(yard));
|
|
41
|
+
* function frame() {
|
|
42
|
+
* physics.setInput({ moveX: stick.x, moveY: stick.y, viewYawRad: lookYawRad, isRunning: buttons.isHeld("run"), jump: buttons.wasPressed("jump") });
|
|
43
|
+
* net.update();
|
|
44
|
+
* for (const characterId of physics.characterIds()) drawCharacter(characterId, physics.drawnCharacter(characterId));
|
|
45
|
+
* }
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
import type { Kind } from "../net/index.js";
|
|
49
|
+
import type { Hooks, Settings } from "../piece/index.js";
|
|
50
|
+
import type * as Engine from "./rapier/rapier.js";
|
|
51
|
+
|
|
52
|
+
export { PieceError } from "../piece/index.js";
|
|
53
|
+
|
|
54
|
+
// ---- The engine ----
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Loads and starts the engine (Rapier, as WebAssembly), once: call it and wait before the first createPhysics or
|
|
58
|
+
* net.use(physicsKind(...)). In a browser it fetches rapier/rapier.wasm from beside the piece's files, about 0.6 MB
|
|
59
|
+
* as Zaisma serves it. Give wasm to bring it another way. Later calls answer at once. Throws PieceError
|
|
60
|
+
* "engine_unreachable", "unknown_argument".
|
|
61
|
+
*/
|
|
62
|
+
export declare function loadPhysics(loadOptions?: { wasm?: BufferSource | WebAssembly.Module | Response | Promise<Response> }): Promise<void>;
|
|
63
|
+
|
|
64
|
+
/** The engine itself, started, typed (rapier/rapier.d.ts): for what the piece doesn't do for a game. Throws PieceError "engine_not_loaded" before loadPhysics. */
|
|
65
|
+
export declare function physicsEngine(): typeof Engine;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* In a player's game, before the world opens: fetches every data file the world's shapes read (a heightfield's
|
|
69
|
+
* heights, a big mesh), from beside the page or with readFile(path) answering its bytes. A game server reads them
|
|
70
|
+
* itself, from its own folder, as the world opens. Throws PieceError "bad_data_file", "data_file_unreachable".
|
|
71
|
+
*/
|
|
72
|
+
export declare function loadPhysicsData(world: PhysicsWorld, loadOptions?: { readFile?(path: string): Promise<ArrayBuffer | ArrayBufferView> }): Promise<void>;
|
|
73
|
+
|
|
74
|
+
// ---- A world ----
|
|
75
|
+
|
|
76
|
+
/** A point or a direction in the world: x, y and z in metres, +Y up. */
|
|
77
|
+
export interface Point {
|
|
78
|
+
x: number;
|
|
79
|
+
y: number;
|
|
80
|
+
z: number;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** A rotation as the engine and three.js hold one: a quaternion's x, y, z and w. */
|
|
84
|
+
export interface Rotation {
|
|
85
|
+
x: number;
|
|
86
|
+
y: number;
|
|
87
|
+
z: number;
|
|
88
|
+
w: number;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** A turn as a level is written: degrees about each axis, x first, then y, then z, as three.js turns an object. */
|
|
92
|
+
export interface TurnDeg {
|
|
93
|
+
x: number;
|
|
94
|
+
y: number;
|
|
95
|
+
z: number;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* One shape of a body. Each shapeKind takes its own fields and refuses the others:
|
|
100
|
+
* - "box": sizeM, how wide (x), tall (y) and deep (z) it is.
|
|
101
|
+
* - "ball": radiusM.
|
|
102
|
+
* - "capsule": radiusM and heightM, its whole height, round ends included, standing along Y.
|
|
103
|
+
* - "cylinder": radiusM and heightM, standing along Y.
|
|
104
|
+
* - "hull": pointsM, 4 to 255 points as numbers in a row (x, y, z of each): the simple solid shape round them.
|
|
105
|
+
* - "mesh": a level's ground and walls as they are, only on a fixed body: pointsM and triangleIndices (up to 50,000
|
|
106
|
+
* triangles), or dataPath for a file of them (up to 2,000,000; loadPhysicsData says how it's written).
|
|
107
|
+
* - "heightfield": a big outdoor map's ground, only on a fixed body: rowCount by columnCount heights (2 to 4,096
|
|
108
|
+
* each) cellSizeM apart, the first at the shape's place, a row running along +x and the next row cellSizeM along
|
|
109
|
+
* +z; heightsM, or dataPath for a file of them (32-bit floats, little endian, row after row). It's laid in tiles
|
|
110
|
+
* only near what moves (world.groundReachM), so a valley kilometres across costs what its busy corners cost.
|
|
111
|
+
*/
|
|
112
|
+
export interface ShapeDeclaration {
|
|
113
|
+
shapeKind: "box" | "ball" | "capsule" | "cylinder" | "hull" | "mesh" | "heightfield";
|
|
114
|
+
sizeM?: Point;
|
|
115
|
+
radiusM?: number;
|
|
116
|
+
heightM?: number;
|
|
117
|
+
/** A hull's or a mesh's points, x, y, z of each in a row: a list, or for a mesh a Float32Array the game already holds (taken as it is, up to 2,000,000 triangles with a Uint32Array of indices). */
|
|
118
|
+
pointsM?: number[] | Float32Array;
|
|
119
|
+
triangleIndices?: number[] | Uint32Array;
|
|
120
|
+
/** A mesh's triangles in groups the world switches on and off while playing (a door, a bridge): one number a triangle, 0 for always there, 1 to 31 for a group (setTriangleGroup). */
|
|
121
|
+
triangleGroupNumbers?: number[] | Uint8Array;
|
|
122
|
+
dataPath?: string;
|
|
123
|
+
heightsM?: number[] | Float32Array;
|
|
124
|
+
rowCount?: number;
|
|
125
|
+
columnCount?: number;
|
|
126
|
+
cellSizeM?: number;
|
|
127
|
+
/** Where the shape sits in its body; default its middle. */
|
|
128
|
+
positionM?: Point;
|
|
129
|
+
/** How the shape is turned in its body; default not at all. */
|
|
130
|
+
rotationDeg?: TurnDeg;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** A wheel of a vehicle: where it hangs from the body, its size, its springs, and whether it steers and drives. */
|
|
134
|
+
export interface WheelDeclaration {
|
|
135
|
+
positionM: Point;
|
|
136
|
+
radiusM: number;
|
|
137
|
+
/** The spring's length at rest, default 0.3 m; and how far it travels, default 0.25 m. */
|
|
138
|
+
suspensionRestM?: number;
|
|
139
|
+
suspensionTravelM?: number;
|
|
140
|
+
/** Default false. */
|
|
141
|
+
isSteered?: boolean;
|
|
142
|
+
/** Default true. */
|
|
143
|
+
isDriven?: boolean;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** How a vehicle drives: its engine, brakes, steering and springs. */
|
|
147
|
+
export interface VehicleDeclaration {
|
|
148
|
+
/** All the driven wheels together, default 4000 N. */
|
|
149
|
+
maximumEngineForceNewtons?: number;
|
|
150
|
+
/** Each wheel, default 60. */
|
|
151
|
+
maximumBrakeForceNewtons?: number;
|
|
152
|
+
/** Default 32. */
|
|
153
|
+
maximumSteerDeg?: number;
|
|
154
|
+
suspensionStiffnessNumber?: number;
|
|
155
|
+
suspensionDampingNumber?: number;
|
|
156
|
+
/** How hard the tyres grip before they slip, default 10.5. */
|
|
157
|
+
frictionSlipNumber?: number;
|
|
158
|
+
/** How hard they grip sideways, default 1. */
|
|
159
|
+
sideFrictionNumber?: number;
|
|
160
|
+
/** The most each wheel's spring pushes, newtons; default the engine's 6,000, or two and a half times the vehicle's weight on each wheel when that's more (a heavy truck). */
|
|
161
|
+
maximumSuspensionForceNewtons?: number;
|
|
162
|
+
/** The collision layers its wheels stand on; default every layer the vehicle touches but characters'. Never a character: ["world"] drives over loose things too. */
|
|
163
|
+
wheelCollisionLayerNames?: string[];
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** What a body is made of: its shapes, its weight, its surface, its layer, and its wheels and seats when it has them. */
|
|
167
|
+
export interface BodyKindDeclaration {
|
|
168
|
+
/** How heavy it is, 0.01 to 100000 kg; default worked out from its size (250 kg a cubic metre). */
|
|
169
|
+
massKg?: number;
|
|
170
|
+
/** How much of a hit's speed comes back, 0 to 100; default the world's (bouncePct). */
|
|
171
|
+
bouncePct?: number;
|
|
172
|
+
/** How much its surface holds on (its friction), 0 to 300; default the world's (gripPct). */
|
|
173
|
+
gripPct?: number;
|
|
174
|
+
/** How quickly its speed and its spin die away by themselves, 0 (never) to 100; default the world's (0.05 and 0.2): loose food settles sooner than a cart rolls on. */
|
|
175
|
+
linearDampingNumber?: number;
|
|
176
|
+
angularDampingNumber?: number;
|
|
177
|
+
/** Whether a character kind's hands may take hold of it; default true. */
|
|
178
|
+
canBeGrabbed?: boolean;
|
|
179
|
+
/** One of the world's collisionLayers; default "body". */
|
|
180
|
+
collisionLayerName?: string;
|
|
181
|
+
/** Wheels make it a vehicle, driven by whoever sits in its first seat. */
|
|
182
|
+
wheels?: readonly WheelDeclaration[];
|
|
183
|
+
vehicle?: VehicleDeclaration;
|
|
184
|
+
/** Where characters sit, up to 8: the first is the driver's. */
|
|
185
|
+
seats?: readonly { positionM: Point; facingYawDeg?: number }[];
|
|
186
|
+
shapes: readonly ShapeDeclaration[] | ShapeDeclaration[];
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** A body: a thing in the world, made of 1 to 16 shapes of its own, or one of the world's bodyKinds. */
|
|
190
|
+
export interface BodyDeclaration extends Partial<BodyKindDeclaration> {
|
|
191
|
+
/** 1 to 64 letters, digits, "_", ".", ":" or "-": "sofa", "crate:3". */
|
|
192
|
+
id: string;
|
|
193
|
+
/** One of the world's bodyKinds, by name: the body is made as the kind says, with nothing of its own but its place. */
|
|
194
|
+
bodyKindName?: string;
|
|
195
|
+
/**
|
|
196
|
+
* "moving" (the default): physics moves it. "kinematic": it moves by its own speed and spin (a lift, a platform, a
|
|
197
|
+
* door the game swings), pushing what's in its way and pushed by nothing. "fixed": the world that never moves.
|
|
198
|
+
*/
|
|
199
|
+
motionKind?: "moving" | "kinematic" | "fixed";
|
|
200
|
+
/** Where its middle starts; default the middle of the world. */
|
|
201
|
+
positionM?: Point;
|
|
202
|
+
/** How it starts turned; default not at all. */
|
|
203
|
+
rotationDeg?: TurnDeg;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** A body added while playing: one of the world's bodyKinds, somewhere. */
|
|
207
|
+
export interface AddedBody {
|
|
208
|
+
id: string;
|
|
209
|
+
bodyKindName: string;
|
|
210
|
+
positionM?: Point;
|
|
211
|
+
rotationDeg?: TurnDeg;
|
|
212
|
+
/** How it moves from the start; default "moving". A building piece placed on a plot is "fixed" at once. */
|
|
213
|
+
motionKind?: "moving" | "kinematic" | "fixed";
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* A joint: two bodies (or a body and a character, or a body and the world) held together. "fixed" holds them as
|
|
218
|
+
* one; "ball" at a point, turning any way; "hinge" turning about axis (a door; minimumNumber and maximumNumber limit
|
|
219
|
+
* it, in radians); "slider" sliding along axis (limits in metres); "spring" pulling toward restLengthM apart
|
|
220
|
+
* (stiffnessNumber, dampingNumber); "rope" never further than restLengthM apart.
|
|
221
|
+
*/
|
|
222
|
+
export interface JointDeclaration {
|
|
223
|
+
id: string;
|
|
224
|
+
jointKind: "fixed" | "ball" | "hinge" | "slider" | "spring" | "rope";
|
|
225
|
+
bodyId: string;
|
|
226
|
+
/** The other body's or character's id; left out, the world itself, otherAnchorM then a place in the world. */
|
|
227
|
+
otherBodyId?: string;
|
|
228
|
+
/** Where it holds each, in each one's own space. */
|
|
229
|
+
anchorM?: Point;
|
|
230
|
+
otherAnchorM?: Point;
|
|
231
|
+
axis?: Point;
|
|
232
|
+
minimumNumber?: number;
|
|
233
|
+
maximumNumber?: number;
|
|
234
|
+
restLengthM?: number;
|
|
235
|
+
stiffnessNumber?: number;
|
|
236
|
+
dampingNumber?: number;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export interface WorldDeclaration {
|
|
240
|
+
/** The world's bodies, up to 1000. A body added while playing (addBody) counts too. */
|
|
241
|
+
bodies?: readonly BodyDeclaration[];
|
|
242
|
+
/** Kinds of body by name, up to 250: bodies that are alike, and what can be added while playing. */
|
|
243
|
+
bodyKinds?: Record<string, BodyKindDeclaration>;
|
|
244
|
+
/** Joints there from the start, up to 1000 with those added while playing. */
|
|
245
|
+
joints?: JointDeclaration[];
|
|
246
|
+
/**
|
|
247
|
+
* Layers by name, up to 15, with the layers each touches (both ways: a layer touches another when either says so).
|
|
248
|
+
* "world" (fixed bodies'), "body" (moving bodies') and "character" (characters') are where things are unless they
|
|
249
|
+
* say otherwise. Default: those three, all touching all. "riding" is the piece's own: what sits in a seat is on it,
|
|
250
|
+
* touching nothing.
|
|
251
|
+
*/
|
|
252
|
+
collisionLayers?: Record<string, { touches?: string[] }>;
|
|
253
|
+
/**
|
|
254
|
+
* How big every character is (heightM default 1.5, radiusM default 0.28), the highest step it walks up
|
|
255
|
+
* (stepHeightM, default 0.35) and the steepest slope it climbs (maximumSlopeDeg, default 50).
|
|
256
|
+
*/
|
|
257
|
+
character?: { heightM?: number; radiusM?: number; stepHeightM?: number; maximumSlopeDeg?: number };
|
|
258
|
+
/** "on_join" (the default): every player gets a character with their id as they join. "by_game": the game adds them (addCharacter). */
|
|
259
|
+
playerCharacterMode?: "on_join" | "by_game";
|
|
260
|
+
/** Where characters start, their feet on the ground: each new character takes the next, round again. */
|
|
261
|
+
startPositionsM?: Point[];
|
|
262
|
+
/** The height below which anything that falls is put back where it started; default -30. */
|
|
263
|
+
fallOutHeightM?: number;
|
|
264
|
+
/** How many engine steps each of the room's fixed steps takes, 1 to 8; default 2 (60 a second in a 30-a-second room: what cars and quick things want). */
|
|
265
|
+
substepCount?: number;
|
|
266
|
+
/** How far round what moves a heightfield is laid, 2 to 1000 m; default 24. */
|
|
267
|
+
groundReachM?: number;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** A world, checked: what createPhysics and physicsKind take. */
|
|
271
|
+
export interface PhysicsWorld {
|
|
272
|
+
readonly isPhysicsWorld: true;
|
|
273
|
+
readonly bodies: readonly CheckedBody[];
|
|
274
|
+
readonly bodyKinds: readonly { name: string; declared: BodyKindDeclaration }[];
|
|
275
|
+
readonly joints: readonly JointDeclaration[];
|
|
276
|
+
readonly collisionLayers: readonly { name: string; touchedNames: string[] }[];
|
|
277
|
+
readonly character: { heightM: number; radiusM: number; stepHeightM: number; maximumSlopeDeg: number };
|
|
278
|
+
readonly playerCharacterMode: "on_join" | "by_game";
|
|
279
|
+
readonly startPositionsM: readonly Point[];
|
|
280
|
+
readonly fallOutHeightM: number;
|
|
281
|
+
readonly substepCount: number;
|
|
282
|
+
readonly groundReachM: number;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** A body's declaration, checked: every field there, its turns as rotations. */
|
|
286
|
+
export interface CheckedBody {
|
|
287
|
+
id: string;
|
|
288
|
+
/** 0 for a body with shapes of its own; else the place of its kind among the world's bodyKinds, from 1. */
|
|
289
|
+
bodyKindNumber: number;
|
|
290
|
+
motionKind: "moving" | "kinematic" | "fixed";
|
|
291
|
+
positionM: Point;
|
|
292
|
+
rotation: Rotation;
|
|
293
|
+
massKg: number | null;
|
|
294
|
+
bouncePct: number | null;
|
|
295
|
+
gripPct: number | null;
|
|
296
|
+
canBeGrabbed: boolean;
|
|
297
|
+
collisionLayerName: string;
|
|
298
|
+
shapes: readonly unknown[];
|
|
299
|
+
wheels: readonly Required<WheelDeclaration>[];
|
|
300
|
+
vehicle: Required<VehicleDeclaration> | null;
|
|
301
|
+
seats: readonly { positionM: Point; facingYawRad: number }[];
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** Checks a world: what createPhysics and physicsKind take. Throws PieceError "bad_world", "bad_body", "bad_joint" naming the field at fault. */
|
|
305
|
+
export declare function definePhysicsWorld(world?: WorldDeclaration): PhysicsWorld;
|
|
306
|
+
|
|
307
|
+
// ---- Characters ----
|
|
308
|
+
|
|
309
|
+
/** A character as a character kind's step sees it. Change facingYawRad, isGrounded, airTickCount, velocity (a kinematic one's) and own in place. */
|
|
310
|
+
export interface SteppedCharacter {
|
|
311
|
+
readonly id: string;
|
|
312
|
+
/** A kinematic character's speed, metres a second: the engine keeps none for it. A dynamic one's is rigidBody.linvel(). */
|
|
313
|
+
velocity: Point;
|
|
314
|
+
facingYawRad: number;
|
|
315
|
+
isGrounded: boolean;
|
|
316
|
+
airTickCount: number;
|
|
317
|
+
/** The kind's own fields, by name. */
|
|
318
|
+
own: Record<string, any>;
|
|
319
|
+
readonly rigidBody: Engine.RigidBody;
|
|
320
|
+
readonly collider: Engine.Collider;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** A query's hit: whose it was (a body's or a character's id, the other null), where, the surface's normal there, and how far along. */
|
|
324
|
+
export interface QueryHit {
|
|
325
|
+
bodyId: string | null;
|
|
326
|
+
characterId: string | null;
|
|
327
|
+
pointM: Point;
|
|
328
|
+
normal: Point;
|
|
329
|
+
distanceM: number;
|
|
330
|
+
}
|
|
331
|
+
export interface RayQuery {
|
|
332
|
+
fromM: Point;
|
|
333
|
+
directionM: Point;
|
|
334
|
+
maximumDistanceM: number;
|
|
335
|
+
/** Only these layers; default all. */
|
|
336
|
+
collisionLayerNames?: string[];
|
|
337
|
+
/** A body's or character's id to look past: the one asking. */
|
|
338
|
+
excludedId?: string;
|
|
339
|
+
}
|
|
340
|
+
export interface BallCastQuery extends RayQuery {
|
|
341
|
+
radiusM: number;
|
|
342
|
+
/** false to pass out of what the ball starts touching (a camera pressed against a wall moving away from it); default true, a hit at 0 m. */
|
|
343
|
+
isStoppedAtPenetration?: boolean;
|
|
344
|
+
}
|
|
345
|
+
export interface CapsuleCastQuery extends BallCastQuery {
|
|
346
|
+
/** Its whole height, round ends included: at least twice its radius. */
|
|
347
|
+
heightM: number;
|
|
348
|
+
/** How it's turned; default standing up. */
|
|
349
|
+
rotation?: Rotation;
|
|
350
|
+
}
|
|
351
|
+
export interface CapsuleOverlapQuery {
|
|
352
|
+
centerM: Point;
|
|
353
|
+
radiusM: number;
|
|
354
|
+
heightM: number;
|
|
355
|
+
rotation?: Rotation;
|
|
356
|
+
/** How near counts as touching, 0 to 1 m; default 0 (only what overlaps). */
|
|
357
|
+
marginM?: number;
|
|
358
|
+
collisionLayerNames?: string[];
|
|
359
|
+
excludedId?: string;
|
|
360
|
+
}
|
|
361
|
+
/** Where a capsule touches the world: whose, the point, the surface's normal there (out of it, toward the capsule), and how deep the capsule is in it. */
|
|
362
|
+
export interface CapsuleContact {
|
|
363
|
+
bodyId: string | null;
|
|
364
|
+
characterId: string | null;
|
|
365
|
+
pointM: Point;
|
|
366
|
+
normal: Point;
|
|
367
|
+
depthM: number;
|
|
368
|
+
}
|
|
369
|
+
export interface BallOverlapQuery {
|
|
370
|
+
centerM: Point;
|
|
371
|
+
radiusM: number;
|
|
372
|
+
collisionLayerNames?: string[];
|
|
373
|
+
excludedId?: string;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** The world's numbers a step moves by: the piece's settings, turned by a character kind's own. */
|
|
377
|
+
export interface PhysicsTuning {
|
|
378
|
+
gravityMPerSecPerSec: number;
|
|
379
|
+
bouncePct: number;
|
|
380
|
+
gripPct: number;
|
|
381
|
+
linearDampingNumber: number;
|
|
382
|
+
angularDampingNumber: number;
|
|
383
|
+
characterMassKg: number;
|
|
384
|
+
walkSpeedMPerSec: number;
|
|
385
|
+
jumpSpeedMPerSec: number;
|
|
386
|
+
[tuned: string]: number;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** What a character kind's step reaches: the world's queries, the move, and whose step it is. */
|
|
390
|
+
export interface CharacterStepContext {
|
|
391
|
+
tickIndex: number;
|
|
392
|
+
tickDurationSec: number;
|
|
393
|
+
isReplay: boolean;
|
|
394
|
+
/** Whether this place is the word on the world (the game server, or the host's game on the relay). */
|
|
395
|
+
isInCharge: boolean;
|
|
396
|
+
tuning: PhysicsTuning;
|
|
397
|
+
/** Says something happened in this step, as @zaisma/net's emit: told once, where the step is first run. */
|
|
398
|
+
emit(eventName: string, payload?: unknown, emitOptions?: { entityId?: string }): void;
|
|
399
|
+
/**
|
|
400
|
+
* Moves a kinematic character by as much of movedM as the world lets it: sliding along walls, up steps and slopes
|
|
401
|
+
* it can climb, snapped down to the ground under it, pushing what's in its way. Answers how far it went and
|
|
402
|
+
* whether it stands.
|
|
403
|
+
*/
|
|
404
|
+
moveAndSlide(character: SteppedCharacter, movedM: Point): { movedM: Point; isGrounded: boolean };
|
|
405
|
+
castRay(query: RayQuery, into?: QueryHit): QueryHit | null;
|
|
406
|
+
castBall(query: BallCastQuery, into?: QueryHit): QueryHit | null;
|
|
407
|
+
castCapsule(query: CapsuleCastQuery, into?: QueryHit): QueryHit | null;
|
|
408
|
+
overlapBall(query: BallOverlapQuery): { bodyId: string | null; characterId: string | null }[];
|
|
409
|
+
overlapCapsule(query: CapsuleOverlapQuery): CapsuleContact[];
|
|
410
|
+
/** A body as the world has it now, read only (pulling a cart: how fast it lets him go), or null. */
|
|
411
|
+
bodyState(bodyId: string): (Omit<BodyState, "riderCharacterIds"> & { massKg: number | null }) | null;
|
|
412
|
+
/** Another character as the world has it now, read only, or null. */
|
|
413
|
+
characterState(characterId: string): { positionM: Point; velocityMPerSec: Point; facingYawRad: number; isGrounded: boolean; rideBodyId: string | null; collisionLayerName: string; own: Record<string, any> } | null;
|
|
414
|
+
/** Ties or lets go a joint from the step (a press that takes a cart's handles), so a replay runs it exactly. Answers false for a joint that isn't here. */
|
|
415
|
+
setJoint(jointId: string, changes: { isEnabled: boolean }): boolean;
|
|
416
|
+
/** Puts the character on one of the world's collision layers from its step (through his own cart while he holds it). */
|
|
417
|
+
setLayer(character: SteppedCharacter, collisionLayerName: string): void;
|
|
418
|
+
/** The engine, for what the piece doesn't do. */
|
|
419
|
+
engine: typeof Engine;
|
|
420
|
+
rapierWorld: Engine.World;
|
|
421
|
+
/** The piece's own memory of the world, for a piece built on physics: its bodies and characters by index, and the kind's startKindState answer. */
|
|
422
|
+
bodies: readonly unknown[];
|
|
423
|
+
characters: readonly (SteppedCharacter | null)[];
|
|
424
|
+
bodyIndexById: ReadonlyMap<string, number>;
|
|
425
|
+
characterIndexById: ReadonlyMap<string, number>;
|
|
426
|
+
kindState: any;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** A field as @zaisma/net declares one (index.d.ts there, "Shapes"). */
|
|
430
|
+
export type FieldDeclaration = import("../net/index.js").FieldDeclaration;
|
|
431
|
+
export type FieldGroupDeclaration = import("../net/index.js").FieldGroupDeclaration;
|
|
432
|
+
|
|
433
|
+
export interface CharacterKindDeclaration {
|
|
434
|
+
/** Lowercase words joined by underscores, the same in the game and on its game server. */
|
|
435
|
+
name: string;
|
|
436
|
+
/** "kinematic" (the default): the kind moves it with moveAndSlide. "dynamic": physics moves it, and the kind pushes it with forces. */
|
|
437
|
+
bodyKind?: "kinematic" | "dynamic";
|
|
438
|
+
/**
|
|
439
|
+
* What the kind keeps of each character beyond the piece's own (fuel, stamina, cooldowns): shared on the wire. A
|
|
440
|
+
* field with isShared: false is each game's own, never sent nor compared: set by each step afresh and read by
|
|
441
|
+
* drawnCharacter and characterState (sprinting, stopped at deep water). It isn't put back on a replay.
|
|
442
|
+
*/
|
|
443
|
+
stateFields?: ((FieldDeclaration & { isShared?: boolean }) | FieldGroupDeclaration)[];
|
|
444
|
+
/** What moves it. */
|
|
445
|
+
inputFields: FieldDeclaration[];
|
|
446
|
+
/** One fixed step of one character, the same in every player's game and on the game server. */
|
|
447
|
+
step(character: SteppedCharacter, input: Record<string, any>, stepContext: CharacterStepContext): void;
|
|
448
|
+
/** How a driver's input drives what it sits in: each -1 to 1. Default the stick's (moveY, moveX, isBraking). The vehicle as stepContext.bodyState reads it, with its id. */
|
|
449
|
+
vehicleControls?(input: Record<string, any>, vehicle: NonNullable<ReturnType<CharacterStepContext["bodyState"]>> & { bodyId: string }, stepContext: CharacterStepContext): { engineNumber: number; steerNumber: number; brakeNumber: number };
|
|
450
|
+
/** Step before the world's first engine step instead of just after it (a dynamic kind that pushes with forces). */
|
|
451
|
+
isSteppedBeforeWorld?: boolean;
|
|
452
|
+
/** One fixed step of a character sitting in a seat, to keep its own fields going (stamina back on a drive); it goes where its seat goes. Left out, riders aren't stepped. */
|
|
453
|
+
stepRiding?(character: SteppedCharacter, input: Record<string, any>, stepContext: CharacterStepContext): void;
|
|
454
|
+
/** false for characters that never shove what they walk into (a parked handcart); default true. */
|
|
455
|
+
canPushBodies?: boolean;
|
|
456
|
+
|
|
457
|
+
// ---- For a piece built on physics (like @zaisma/physics-party): hooks into the world's step ----
|
|
458
|
+
/** The kind's own memory for one world, made as it opens: stepContext.kindState. */
|
|
459
|
+
startKindState?(stepContext: CharacterStepContext): any;
|
|
460
|
+
/** Before and after each fixed step of the world, once (not per character): step bodies of its own, hold what's held. */
|
|
461
|
+
beforeWorldStep?(stepContext: CharacterStepContext): void;
|
|
462
|
+
afterWorldStep?(stepContext: CharacterStepContext): void;
|
|
463
|
+
/** A body left the world (removed, or put back after falling out): let go of it. */
|
|
464
|
+
onBodyRemoved?(bodyIndex: number, stepContext: CharacterStepContext): void;
|
|
465
|
+
onCharacterAdded?(character: SteppedCharacter, stepContext: CharacterStepContext): void;
|
|
466
|
+
onCharacterRemoved?(character: SteppedCharacter, stepContext: CharacterStepContext): void;
|
|
467
|
+
/** A dynamic character may rest (false keeps it awake: it holds something). */
|
|
468
|
+
canRest?(character: SteppedCharacter, stepContext: CharacterStepContext): boolean;
|
|
469
|
+
/** A kinematic character standing still also rests only when this answers true. */
|
|
470
|
+
isResting?(character: SteppedCharacter, stepContext: CharacterStepContext): boolean;
|
|
471
|
+
/** The world's numbers turned by the kind's own settings. */
|
|
472
|
+
tuning?(base: PhysicsTuning, rules: Record<string, number | string>): PhysicsTuning;
|
|
473
|
+
onTuning?(tuning: PhysicsTuning, stepContext: CharacterStepContext): void;
|
|
474
|
+
/** Whose time a body is drawn in (the character holding it); null to keep it in this game's own, undefined to let the piece decide. */
|
|
475
|
+
drawnWith?(bodyIndex: number, ownCharacterId: string, stepContext: CharacterStepContext): string | null | undefined;
|
|
476
|
+
/** A character as it's drawn, with the kind's own fields made into what a game reads. */
|
|
477
|
+
drawnCharacter?(view: CharacterView, drawnFields: Record<string, any>, drawnContext: { things: unknown; simulation: unknown; world: PhysicsWorld }): CharacterView;
|
|
478
|
+
/** The kind's own settings and their statement (a piece's piece.json), and which of them every side steps by. */
|
|
479
|
+
settings?: Settings<any>;
|
|
480
|
+
settingsStatement?: unknown;
|
|
481
|
+
syncedSettingNames?: string[];
|
|
482
|
+
/** Calls of the kind's own on what net.use and onServer answer. */
|
|
483
|
+
calls?(calls: Physics, callsContext: unknown): any;
|
|
484
|
+
serverCalls?(calls: ServerPhysics, callsContext: unknown): any;
|
|
485
|
+
/** How each character's own fields start, are read and written, when they aren't plain fields. */
|
|
486
|
+
startOwn?(stepContext: CharacterStepContext): Record<string, any>;
|
|
487
|
+
readOwn?(character: SteppedCharacter, stepContext: CharacterStepContext): Record<string, any>;
|
|
488
|
+
writeOwn?(character: SteppedCharacter, fields: Record<string, any>, stepContext: CharacterStepContext): void;
|
|
489
|
+
}
|
|
490
|
+
export interface CharacterKind extends CharacterKindDeclaration {
|
|
491
|
+
readonly isCharacterKind: true;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* A character kind of the game's own: its movement rules, its fields, its input. Declare it in a file both sides
|
|
496
|
+
* import (from world.js, at the same place in a game and its game server). Throws PieceError "bad_character_kind".
|
|
497
|
+
*/
|
|
498
|
+
export declare function defineCharacterKind(declaration: CharacterKindDeclaration): CharacterKind;
|
|
499
|
+
/** The piece's own character kind: walks and runs where the stick points along the view, jumps, steps up, slides along walls, drives what it sits in. */
|
|
500
|
+
export declare function walkingCharacters(): CharacterKind;
|
|
501
|
+
/** One step of the piece's walk, for a kind of the game's own to call for its walking part. */
|
|
502
|
+
export declare function walkStep(character: SteppedCharacter, input: Record<string, any>, stepContext: CharacterStepContext): void;
|
|
503
|
+
/** Where the player pushes, in the world, from a stick and where they look. */
|
|
504
|
+
export declare function wishOf(input: { moveX?: number; moveY?: number; viewYawRad?: number }): { x: number; z: number; lengthNumber: number };
|
|
505
|
+
/** Drives a vehicle from its driver's stick: moveY forward and back, moveX steering, isBraking. */
|
|
506
|
+
export declare function stickVehicleControls(input: Record<string, any>): { engineNumber: number; steerNumber: number; brakeNumber: number };
|
|
507
|
+
|
|
508
|
+
// ---- Settings and hooks ----
|
|
509
|
+
|
|
510
|
+
export interface PhysicsSettingValues {
|
|
511
|
+
/** How fast things fall, metres a second each second; default 9.81. */
|
|
512
|
+
gravityMPerSecPerSec: number;
|
|
513
|
+
/** How much of a hit's speed comes back, 0 to 100; default 10. */
|
|
514
|
+
bouncePct: number;
|
|
515
|
+
/** How much surfaces hold on, 0 to 300; default 70. */
|
|
516
|
+
gripPct: number;
|
|
517
|
+
/** How fast walking characters walk, 0 to 40; default 4.5 (running is 1.8 times this). */
|
|
518
|
+
walkSpeedMPerSec: number;
|
|
519
|
+
/** How high walking characters jump, 0 to 20; default 1. */
|
|
520
|
+
jumpHeightM: number;
|
|
521
|
+
/** How heavy a character is, 1 to 1000; default 70. */
|
|
522
|
+
characterMassKg: number;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
export interface PhysicsHookHandlers {
|
|
526
|
+
/** Where the world is run: something fell out of the world and goes back. Answer where instead. */
|
|
527
|
+
beforePutBack: (positionM: Point, fallen: { id: string; isCharacter: boolean; roomState: any }) => Point | void;
|
|
528
|
+
/** In every game: a moving body hit something hard enough to hear. */
|
|
529
|
+
bodyHit: (hit: { bodyId: string | null; otherBodyId: string | null; otherCharacterId: string | null; speedMPerSec: number }) => void;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// ---- What a game calls ----
|
|
533
|
+
|
|
534
|
+
/** A body as the world has it now. */
|
|
535
|
+
export interface BodyState {
|
|
536
|
+
positionM: Point;
|
|
537
|
+
rotation: Rotation;
|
|
538
|
+
velocityMPerSec: Point;
|
|
539
|
+
spinRadPerSec: Point;
|
|
540
|
+
motionKind: "moving" | "kinematic" | "fixed";
|
|
541
|
+
collisionLayerName: string;
|
|
542
|
+
/** How heavy it is now; null for a fixed body. */
|
|
543
|
+
massKg: number | null;
|
|
544
|
+
isResting: boolean;
|
|
545
|
+
riderCharacterIds: string[];
|
|
546
|
+
/** A character kind's own (with @zaisma/physics-party: holderCharacterIds on a body; heldBodyId, handPositionM, loadPct on a character). */
|
|
547
|
+
[kindOwn: string]: any;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/** A character as the world has it, or as it's drawn: its feet, facing, speed, footing, what it rides, and its kind's own fields. */
|
|
551
|
+
export interface CharacterView {
|
|
552
|
+
characterId: string;
|
|
553
|
+
positionM: Point;
|
|
554
|
+
facingYawRad: number;
|
|
555
|
+
velocityMPerSec: Point;
|
|
556
|
+
speedMPerSec: number;
|
|
557
|
+
riseSpeedMPerSec: number;
|
|
558
|
+
isOnGround: boolean;
|
|
559
|
+
rideBodyId: string | null;
|
|
560
|
+
seatIndex: number | null;
|
|
561
|
+
collisionLayerName: string;
|
|
562
|
+
own: Record<string, any>;
|
|
563
|
+
/** A character kind's own (with @zaisma/physics-party: holderCharacterIds on a body; heldBodyId, handPositionM, loadPct on a character). */
|
|
564
|
+
[kindOwn: string]: any;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/** What changes the world, the same alone, in a player's game that runs it, and on a game server. */
|
|
568
|
+
export interface PhysicsChanges {
|
|
569
|
+
bodyIds(): string[];
|
|
570
|
+
hasBody(bodyId: string): boolean;
|
|
571
|
+
bodyState(bodyId: string): BodyState | null;
|
|
572
|
+
/** Adds a body of one of the world's bodyKinds. Answers false when the id is taken. */
|
|
573
|
+
addBody(body: AddedBody): boolean;
|
|
574
|
+
removeBody(bodyId: string): boolean;
|
|
575
|
+
/** Sets a body at once: its place and turn (a teleport), its speed or spin, how it moves (a parked cart made fixed), its layer. */
|
|
576
|
+
setBody(bodyId: string, changes: { positionM?: Point; rotationDeg?: TurnDeg; velocityMPerSec?: Point; spinRadPerSec?: Point; motionKind?: "moving" | "kinematic" | "fixed"; collisionLayerName?: string; /** How heavy it is now, 0.01 to 100000 kg, to the hundredth: what rides in a cart's bed weighs on it. */ massKg?: number }): boolean;
|
|
577
|
+
/** A sudden change of a body's speed, as a hit gives. */
|
|
578
|
+
pushBody(bodyId: string, pushMPerSec: Point): boolean;
|
|
579
|
+
characterIds(): string[];
|
|
580
|
+
hasCharacter(characterId: string): boolean;
|
|
581
|
+
characterState(characterId: string): CharacterView | null;
|
|
582
|
+
addCharacter(characterId: string, addOptions?: { positionM?: Point }): boolean;
|
|
583
|
+
removeCharacter(characterId: string): boolean;
|
|
584
|
+
/** Sets a character at once: its feet's place (a teleport), speed, facing, layer, or its kind's own fields. */
|
|
585
|
+
setCharacter(characterId: string, changes: { positionM?: Point; velocityMPerSec?: Point; facingYawRad?: number; collisionLayerName?: string; own?: Record<string, any> }): boolean;
|
|
586
|
+
/** Seats a character in a body's seat (the first drives one with wheels); with bodyId null, lets it out where it is. Answers false for a seat that's taken. */
|
|
587
|
+
seatCharacter(characterId: string, bodyId: string | null, seatIndex?: number): boolean;
|
|
588
|
+
jointIds(): string[];
|
|
589
|
+
/** Adds a joint while playing. */
|
|
590
|
+
addJoint(joint: JointDeclaration): boolean;
|
|
591
|
+
removeJoint(jointId: string): boolean;
|
|
592
|
+
/** Lets a joint go (isEnabled false), or holds again. */
|
|
593
|
+
setJoint(jointId: string, changes: { isEnabled: boolean }): boolean;
|
|
594
|
+
isJointEnabled(jointId: string): boolean;
|
|
595
|
+
/** The first thing a ray meets, or null. With into (an object the game keeps), written there instead of a new one: for a game casting thousands a second. */
|
|
596
|
+
castRay(query: RayQuery, into?: QueryHit): QueryHit | null;
|
|
597
|
+
castBall(query: BallCastQuery, into?: QueryHit): QueryHit | null;
|
|
598
|
+
/** The first thing a capsule (standing up, or turned) meets moving along a direction, or null. */
|
|
599
|
+
castCapsule(query: CapsuleCastQuery, into?: QueryHit): QueryHit | null;
|
|
600
|
+
overlapBall(query: BallOverlapQuery): { bodyId: string | null; characterId: string | null }[];
|
|
601
|
+
/** Where a capsule touches the world, with each contact's normal and depth: what a game's own movement pushes out of walls by. */
|
|
602
|
+
overlapCapsule(query: CapsuleOverlapQuery): CapsuleContact[];
|
|
603
|
+
/** Switches one of the world's triangle groups on or off for everyone (a door that opens, a bridge that falls), 1 to 31. */
|
|
604
|
+
setTriangleGroup(groupNumber: number, isEnabled: boolean): boolean;
|
|
605
|
+
isTriangleGroupEnabled(groupNumber: number): boolean;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/** What a game calls on a world in play, alone or online. */
|
|
609
|
+
export interface Physics extends PhysicsChanges {
|
|
610
|
+
readonly settings: Settings<PhysicsSettingValues>;
|
|
611
|
+
readonly hooks: Hooks<PhysicsHookHandlers>;
|
|
612
|
+
/** The character kind's own settings and hooks (a piece built on physics), or null. */
|
|
613
|
+
readonly characterSettings: Settings<any> | null;
|
|
614
|
+
readonly characterHooks: Hooks<any> | null;
|
|
615
|
+
readonly world: PhysicsWorld;
|
|
616
|
+
/** Whether this game runs the world: alone, or as the room's host on the relay. Where it doesn't, the changes answer false (on the relay) or throw "not_in_charge" (with a game server: change it there). */
|
|
617
|
+
readonly isInCharge: boolean;
|
|
618
|
+
/** Whether the world is here yet: at once alone or in charge, else once the room has said how it is. */
|
|
619
|
+
readonly isReady: boolean;
|
|
620
|
+
readonly ownCharacterId: string;
|
|
621
|
+
bodyDeclaration(bodyId: string): CheckedBody | null;
|
|
622
|
+
/**
|
|
623
|
+
* Where to draw a body this frame, a vehicle's steering, and each of its wheels as this game's world has it: how far
|
|
624
|
+
* its spring reaches (where the wheel hangs below its place), how far round it's turned, its steering, and whether it
|
|
625
|
+
* touches anything. A body without wheels has none.
|
|
626
|
+
*/
|
|
627
|
+
drawnBody(bodyId: string): { positionM: Point; rotation: Rotation; steerNumber: number; wheels: { suspensionM: number; spinRad: number; steerRad: number; isOnGround: boolean }[] } | null;
|
|
628
|
+
/** Puts a three.js object where a body is drawn this frame. */
|
|
629
|
+
applyTo(object3d: { position: { set(x: number, y: number, z: number): void }; quaternion: { set(...parts: number[]): unknown } }, bodyId: string): boolean;
|
|
630
|
+
/** This player's input from now on, or with { characterId } a bot's this game moves: the character kind's input fields. */
|
|
631
|
+
setInput(input: Record<string, any>, setOptions?: { characterId?: string }): boolean;
|
|
632
|
+
/** Where and how to draw a character this frame: what @zaisma/body's playMovement takes, and the kind's own fields. */
|
|
633
|
+
drawnCharacter(characterId: string): CharacterView | null;
|
|
634
|
+
/** Lays the ground near a place for the next step, though nothing moves there: under a camera looking past, before a query far off. */
|
|
635
|
+
keepGroundNear(pointM: Point): void;
|
|
636
|
+
on(eventName: "bodyAdded" | "bodyRemoved" | "characterAdded" | "characterRemoved", listener: (id: string) => void): () => void;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
/** A world played alone: Physics, and update() every frame. */
|
|
640
|
+
export interface AlonePhysics extends Physics {
|
|
641
|
+
/** Runs the fixed steps that are due on the clock given (the page's, left out). */
|
|
642
|
+
update(nowMs?: number): void;
|
|
643
|
+
/** Hears what a character kind's step said happened (emit). */
|
|
644
|
+
onEmitted(listener: (emitted: { entityId: string | null; eventName: string; payload: unknown; tickIndex: number }) => void): () => void;
|
|
645
|
+
/** 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. */
|
|
646
|
+
dispose(): void;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/** Physics' calls on a game server: physics.onServer(state, room). */
|
|
650
|
+
export interface ServerPhysics extends PhysicsChanges {
|
|
651
|
+
/** This room's settings: changing them changes this room alone. */
|
|
652
|
+
readonly settings: Settings<PhysicsSettingValues>;
|
|
653
|
+
/** This room's character kind's settings, or null. */
|
|
654
|
+
readonly characterSettings: Settings<any> | null;
|
|
655
|
+
/** A bot's input from now on. Answers false for a player's character. */
|
|
656
|
+
setInput(characterId: string, input: Record<string, any>): boolean;
|
|
657
|
+
/** What steps said happened since this was last asked (emit). */
|
|
658
|
+
takeEmitted(): unknown[];
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
export interface PhysicsKindOptions {
|
|
662
|
+
settings?: Partial<PhysicsSettingValues>;
|
|
663
|
+
hooks?: Partial<PhysicsHookHandlers>;
|
|
664
|
+
/** How the world's characters move: the piece's walk (the default), a game's own, or a piece's (@zaisma/physics-party). */
|
|
665
|
+
characters?: CharacterKind | CharacterKindDeclaration;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
export interface PhysicsKind extends Kind<Physics, ServerPhysics> {
|
|
669
|
+
readonly settings: Settings<PhysicsSettingValues>;
|
|
670
|
+
readonly hooks: Hooks<PhysicsHookHandlers>;
|
|
671
|
+
readonly world: PhysicsWorld;
|
|
672
|
+
readonly characterKind: CharacterKind & { settings?: Settings<any> };
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/** Shared physics for a world: a kind of sync for createNet(room).use and defineNetServer's kinds. */
|
|
676
|
+
export declare function physicsKind(world: PhysicsWorld | WorldDeclaration, kindOptions?: PhysicsKindOptions): PhysicsKind;
|
|
677
|
+
|
|
678
|
+
export interface CreatePhysicsOptions extends PhysicsKindOptions {
|
|
679
|
+
/** Fixed steps a second, 1 to 60; default 30. */
|
|
680
|
+
ticksPerSec?: number;
|
|
681
|
+
/** This player's own character's id; default "player". */
|
|
682
|
+
playerId?: string;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
/** A world played alone, with no room. Throws PieceError "engine_not_loaded" before loadPhysics() has finished. */
|
|
686
|
+
export declare function createPhysics(world: PhysicsWorld | WorldDeclaration, createOptions?: CreatePhysicsOptions): AlonePhysics;
|
|
687
|
+
|
|
688
|
+
// ---- Characters over a level ----
|
|
689
|
+
|
|
690
|
+
export interface LevelCharactersOptions {
|
|
691
|
+
/** The kind's name in the room; default "characters". */
|
|
692
|
+
name?: string;
|
|
693
|
+
/** How they move: the piece's walk (the default), or a game's own kind (its step, fields and input). */
|
|
694
|
+
characters?: CharacterKind | CharacterKindDeclaration;
|
|
695
|
+
/** The piece's settings (walkSpeedMPerSec, gravity...): the same in the game and on its game server, since a level has no room of its own to say them. */
|
|
696
|
+
settings?: Partial<PhysicsSettingValues>;
|
|
697
|
+
playerCharacterMode?: "on_join" | "by_game";
|
|
698
|
+
/** What steps may emit (a shot), as @zaisma/net's. */
|
|
699
|
+
eventNames?: string[];
|
|
700
|
+
/** What the whole room shares and every step reads (stepContext.room), set by the game server (setRoom). */
|
|
701
|
+
roomFields?: FieldDeclaration[];
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
/** What a game calls on characters over a level: @zaisma/net's characters, and the level's queries and drawing. */
|
|
705
|
+
export interface LevelCharacters extends import("../net/index.js").Characters {
|
|
706
|
+
readonly world: PhysicsWorld;
|
|
707
|
+
readonly settings: Settings<PhysicsSettingValues>;
|
|
708
|
+
/** Where and how to draw a character this frame: its feet, facing, speed, footing and its kind's own fields. */
|
|
709
|
+
drawnCharacter(characterId: string): CharacterView | null;
|
|
710
|
+
characterState(characterId: string): CharacterView | null;
|
|
711
|
+
castRay(query: RayQuery, into?: QueryHit): QueryHit | null;
|
|
712
|
+
castBall(query: BallCastQuery, into?: QueryHit): QueryHit | null;
|
|
713
|
+
castCapsule(query: CapsuleCastQuery, into?: QueryHit): QueryHit | null;
|
|
714
|
+
overlapBall(query: BallOverlapQuery): { bodyId: string | null; characterId: string | null }[];
|
|
715
|
+
overlapCapsule(query: CapsuleOverlapQuery): CapsuleContact[];
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
/** The same on a game server: kind.onServer(state, room). */
|
|
719
|
+
export interface ServerLevelCharacters extends import("../net/index.js").ServerCharacters {
|
|
720
|
+
readonly world: PhysicsWorld;
|
|
721
|
+
characterState(characterId: string): CharacterView | null;
|
|
722
|
+
castRay(query: RayQuery, into?: QueryHit): QueryHit | null;
|
|
723
|
+
castBall(query: BallCastQuery, into?: QueryHit): QueryHit | null;
|
|
724
|
+
castCapsule(query: CapsuleCastQuery, into?: QueryHit): QueryHit | null;
|
|
725
|
+
overlapBall(query: BallOverlapQuery): { bodyId: string | null; characterId: string | null }[];
|
|
726
|
+
overlapCapsule(query: CapsuleOverlapQuery): CapsuleContact[];
|
|
727
|
+
/** Switches one of the level's triangle groups in this room (a door, a bridge), 1 to 31: every player's game from the next step. */
|
|
728
|
+
setTriangleGroup(groupNumber: number, isEnabled: boolean): boolean;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
export interface LevelCharactersKind extends Kind<LevelCharacters, ServerLevelCharacters> {
|
|
732
|
+
readonly world: PhysicsWorld;
|
|
733
|
+
readonly settings: Settings<PhysicsSettingValues>;
|
|
734
|
+
readonly characterKind: CharacterKind;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
/**
|
|
738
|
+
* Characters that each move on their own over a world's level (its fixed and kinematic bodies): a shooter's soldiers.
|
|
739
|
+
* The online core shares them as characters, not a world: each player's game predicts only its own, everyone else is
|
|
740
|
+
* drawn a moment behind, and a hit is judged by what the shooter saw (stateAt, viewTickNumber). Nothing in the level
|
|
741
|
+
* moves, characters walk through each other, and one copy of the level serves every room a game server runs.
|
|
742
|
+
*/
|
|
743
|
+
export declare function levelCharactersKind(world: PhysicsWorld | WorldDeclaration, options?: LevelCharactersOptions): LevelCharactersKind;
|