Skip to content

1681-engine / index / PredictedMover

Class: PredictedMover ​

Defined in: src/net/PredictedMover.ts:15

Client-side prediction for the local player's body. Every input step is applied at once (the player sees their own move with no lag) and kept until the server acknowledges it. When a snapshot says where the server has the body as of step ack, the body is put there and every step after ack is replayed on top.

Movement is deterministic (stepMover), so with the same steps, params and grid the replay lands exactly where the prediction was and nothing moves. When it does not (the server refused a step, a collision only it knew of), the difference is correction: draw the body at drawX/drawY, which ease that jump out over a few frames instead of snapping.

Constructors ​

Constructor ​

ts
new PredictedMover(
   x, 
   y, 
   params, 
   grid
): PredictedMover;

Defined in: src/net/PredictedMover.ts:27

Parameters ​

ParameterType
xnumber
ynumber
paramsMovementParams
gridCollisionGrid

Returns ​

PredictedMover

Properties ​

correction ​

ts
correction: number = 0;

Defined in: src/net/PredictedMover.ts:23

How big the last correction was (0 when the prediction was right).


mover ​

ts
readonly mover: MoverState;

Defined in: src/net/PredictedMover.ts:16


smoothing ​

ts
smoothing: number = 0.8;

Defined in: src/net/PredictedMover.ts:25

Share of a visual correction left after each 60th of a second (default 0.8).

Accessors ​

drawX ​

Get Signature ​

ts
get drawX(): number;

Defined in: src/net/PredictedMover.ts:80

Where to draw the body: the predicted place plus the correction still being eased out.

Returns ​

number


drawY ​

Get Signature ​

ts
get drawY(): number;

Defined in: src/net/PredictedMover.ts:84

Returns ​

number


unacknowledged ​

Get Signature ​

ts
get unacknowledged(): number;

Defined in: src/net/PredictedMover.ts:45

Steps sent but not yet acknowledged.

Returns ​

number

Methods ​

reconcile() ​

ts
reconcile(ack, server): void;

Defined in: src/net/PredictedMover.ts:50

The server's word: the body as of step ack. Rewinds there and replays the rest.

Parameters ​

ParameterType
acknumber
serverMoverWire

Returns ​

void


setGrid() ​

ts
setGrid(grid): void;

Defined in: src/net/PredictedMover.ts:93

Parameters ​

ParameterType
gridCollisionGrid

Returns ​

void


setParams() ​

ts
setParams(params): void;

Defined in: src/net/PredictedMover.ts:89

New movement rules (a buff, a slow): applied to future steps and to replays.

Parameters ​

ParameterType
paramsMovementParams

Returns ​

void


smooth() ​

ts
smooth(dt): void;

Defined in: src/net/PredictedMover.ts:71

Eases the visual correction out; call once a frame with its length in seconds.

Parameters ​

ParameterType
dtnumber

Returns ​

void


step() ​

ts
step(step): number;

Defined in: src/net/PredictedMover.ts:37

Applies one input step now and keeps it for replay; returns its sequence number (send it with the step).

Parameters ​

ParameterType
stepInputStep

Returns ​

number

Released under the MIT License.