mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
The old PUNKBUSTER check compared client-reported position to server position and snapped the player back when latency made them diverge, which felt like getting teleported under any real network conditions. Replaces that with proper client-side prediction + reconciliation: client tags each input with a sequence number and keeps an input buffer; server tracks the last processed sequence and reports its authoritative position via a per-user inputAck alongside each worldUpdate. The client only corrects when the actual disagreement exceeds what the unacked input time can explain — so steady-state movement runs purely on local physics, and only genuine unexpected events (collisions, being hit) trigger a smooth blend toward the server state. Includes adaptive threshold scaling so high-latency sessions don't false-positive corrections during normal running.
98 lines
No EOL
2.7 KiB
JavaScript
98 lines
No EOL
2.7 KiB
JavaScript
define([
|
|
"Game/Client/Player",
|
|
"Game/Config/Settings",
|
|
"Lib/Utilities/NotificationCenter",
|
|
"Lib/Utilities/Assert",
|
|
"Game/Client/Control/PlayerController",
|
|
"Game/Client/InputBuffer",
|
|
],
|
|
|
|
function (Parent, Settings, nc, Assert, PlayerController, InputBuffer) {
|
|
|
|
"use strict";
|
|
|
|
function Me(id, physicsEngine, user) {
|
|
Parent.call(this, id, physicsEngine, user);
|
|
|
|
// View uses this to calculate center position
|
|
this.lookAtXY = {
|
|
x: Settings.VIEWPORT_LOOK_AHEAD,
|
|
y: 0
|
|
};
|
|
|
|
this.inputBuffer = new InputBuffer();
|
|
|
|
this.arrowMesh = null;
|
|
this.createAndAddArrow();
|
|
this.playerController = new PlayerController(this);
|
|
}
|
|
|
|
Me.prototype = Object.create(Parent.prototype);
|
|
|
|
Me.prototype.lookAt = function(x, y) {
|
|
this.lookAtXY = {
|
|
x: x,
|
|
y: y
|
|
};
|
|
|
|
Parent.prototype.lookAt.call(this, x, y);
|
|
};
|
|
|
|
Me.prototype.getLookAt = function() {
|
|
return {
|
|
x: this.lookAtXY.x,
|
|
y: this.lookAtXY.y
|
|
};
|
|
};
|
|
|
|
Me.prototype.applyReconciliation = function(x, y, vx, vy) {
|
|
var currentPos = this.doll.body.GetPosition();
|
|
var diffX = x - currentPos.x;
|
|
var diffY = y - currentPos.y;
|
|
var distance = Math.sqrt(diffX * diffX + diffY * diffY);
|
|
|
|
if (distance > Settings.RECONCILIATION_SNAP_THRESHOLD) {
|
|
// Large error — snap immediately (server-side teleport, respawn, etc.)
|
|
this.doll.body.SetPosition({x: x, y: y});
|
|
} else {
|
|
// Small error — blend toward reconciled position
|
|
var factor = Settings.RECONCILIATION_BLEND_FACTOR;
|
|
this.doll.body.SetPosition({
|
|
x: currentPos.x + diffX * factor,
|
|
y: currentPos.y + diffY * factor
|
|
});
|
|
}
|
|
|
|
this.doll.body.SetLinearVelocity({x: vx, y: vy});
|
|
};
|
|
|
|
Me.prototype.createAndAddArrow = function() {
|
|
var self = this;
|
|
|
|
var position = this.getPosition();
|
|
|
|
var options = {
|
|
x: position.x * Settings.RATIO,
|
|
y: position.y * Settings.RATIO,
|
|
};
|
|
|
|
var callback = function(arrowMesh) {
|
|
self.arrowMesh = arrowMesh;
|
|
};
|
|
nc.trigger(nc.ns.client.view.playerArrow.createAndAdd, callback, options);
|
|
};
|
|
|
|
Me.prototype.render = function() {
|
|
|
|
Parent.prototype.render.call(this);
|
|
|
|
var position = this.getPosition();
|
|
var options = {
|
|
x: position.x * Settings.RATIO,
|
|
y: position.y * Settings.RATIO,
|
|
};
|
|
nc.trigger(nc.ns.client.view.playerArrow.update, this.arrowMesh, options);
|
|
};
|
|
|
|
return Me;
|
|
}); |