Decouple gameplay input from rendering and wait for world synchronization

This commit is contained in:
dingo
2026-09-09 06:48:04 -04:00
parent c52bdea026
commit 8f999c837e
5 changed files with 71 additions and 15 deletions
+12 -5
View File
@@ -91,16 +91,17 @@ function action(op: string) {
lastProgress = undefined; lastProgressAt = Date.now();
}
function connect() {
requestedClose = false; sequence = 0;
requestedClose = false; sequence = 0; snapshot = undefined;
const ending = $<HTMLDialogElement>('ending'); ending.close();
ws = new WebSocket(`${location.protocol === 'https:' ? 'wss:' : 'ws:'}//${location.host}${worldPrefix}/ws`);
ws.onopen = () => { $('connection-status').textContent = 'Connected'; $('connection-dot').parentElement!.classList.remove('offline'); };
ws.onopen = () => { $('connection-status').textContent = 'Synchronizing world'; };
ws.onmessage = event => {
const value = JSON.parse(event.data) as Snapshot;
if (value.type !== 'snapshot' || value.version !== 1) { ws?.close(); notice('Client and server versions do not match.'); return; }
if(pendingAction?.op==='gather' && snapshot && value.self.gatherAttempt!==snapshot.self.gatherAttempt){pendingAction=undefined;walkTo=undefined;gatherSent=0;}
if(gatherSent && value.self.ack>=gatherSent)gatherSent=0;
snapshot = value; view.update(value); updateHUD(value);
$('connection-status').textContent = 'Connected'; $('connection-dot').parentElement!.classList.remove('offline');
const state = value.self.entity.state;
if (state === 'dead') showEnding('A life, remembered.', 'This character is permanently dead. Their belongings remain in this world.', false);
else if (state === 'eligible') showEnding('A passage forward.', 'The Harbinger has fallen. Your character and belongings can continue to a new world.', true);
@@ -194,10 +195,16 @@ async function boot() {
else if(key==='inventory')$('inventory-toggle').click();
else if(key==='heal'||key==='return'||key==='jump')send(key);
});
view.start(dt => {
view.start();
// Send bounded intents on elapsed wall time, independently of GPU frame rate.
// A delayed timer performs one update; it never replays a backlog of inputs.
let lastInputAt = performance.now();
window.setInterval(() => {
const now = performance.now(), dt = (now - lastInputAt) / 1000;
lastInputAt = now;
inputElapsed += dt;
if (!snapshot || ws?.readyState !== WebSocket.OPEN || $<HTMLDialogElement>('ending').open) return;
if(hasOpenMenu()||isEditing(document.activeElement)){
if(document.hidden||hasOpenMenu()||isEditing(document.activeElement)){
input.clear();walkTo=undefined;pendingAction=undefined;view.moving=false;
if(inputElapsed>=0.05){send('move');inputElapsed=0;}return;
}
@@ -242,7 +249,7 @@ async function boot() {
}
view.moving = !!(x || z);
if (inputElapsed >= 0.05) { send('move', '', x, z); inputElapsed = 0; }
});
}, 50);
const [loadedConfig, chunks] = await Promise.all([api<Config>('/api/config'), api<Chunk[]>('/api/preview'), view.load()]);
config = loadedConfig;
$<HTMLInputElement>('zoom').value = String(view.distance);
+1 -4
View File
@@ -29,7 +29,6 @@ export class WorldView {
private matrix = new T.Matrix4();
private dummy = new T.Object3D();
private ray = new T.Raycaster();
private onFrame: (dt: number) => void = () => {};
constructor(readonly canvas: HTMLCanvasElement) {
this.renderer = new T.WebGLRenderer({ canvas, antialias: true, powerPreference: 'high-performance' });
this.renderer.setPixelRatio(Math.min(devicePixelRatio, 1.5));
@@ -210,11 +209,9 @@ export class WorldView {
}
return { point: this.ray.intersectObjects([...this.chunks.values()].map(c => c.mesh), false)[0]?.point };
}
start(onFrame: (dt: number) => void) {
this.onFrame = onFrame;
start() {
this.renderer.setAnimationLoop(() => {
const dt = Math.min(this.clock.getDelta(), 0.05);
this.onFrame(dt);
// Presentation interpolation follows server positions; it never changes authoritative movement.
this.position.lerp(this.aim, 1 - Math.exp(-14 * dt));
this.focus.lerp(this.position, 1 - Math.exp(-8 * dt));