mirror of
https://github.com/thilo-behnke/wasm-pong.git
synced 2026-07-10 18:20:00 +00:00
enhancement/only-throttle-fps-in-multiplayer
This commit is contained in:
@@ -65,7 +65,7 @@
|
||||
{:else if session.state === SessionState.RUNNING}
|
||||
<CopyToClipboard text={watchLink}></CopyToClipboard>
|
||||
{#if session.type === SessionType.HOST}
|
||||
<TickWrapper gameFieldStore={gameField} inputs={$sessionInputs} let:tick={tick} let:inputs={inputs} let:handleError={handleError}>
|
||||
<TickWrapper gameFieldStore={gameField} inputs={$sessionInputs} throttle={true} let:tick={tick} let:inputs={inputs} let:handleError={handleError}>
|
||||
<slot inputs={inputs} tick={tick} events={$networkSessionStateEvents}></slot>
|
||||
</TickWrapper>
|
||||
{:else}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
export let gameFieldStore: GameFieldStore;
|
||||
export let inputs;
|
||||
export let killLoopOnError = true;
|
||||
export let throttle = false;
|
||||
|
||||
const targetFps = 60;
|
||||
const frameThreshold = 1_000 / targetFps;
|
||||
@@ -22,6 +23,14 @@
|
||||
(function loop() {
|
||||
frame = requestAnimationFrame(loop);
|
||||
const now = Date.now();
|
||||
if (!throttle) {
|
||||
const dt = now - lastTime;
|
||||
lastTime = now;
|
||||
elapsed += dt;
|
||||
fn(elapsed / 1_000, dt / 1_000);
|
||||
return;
|
||||
}
|
||||
|
||||
const dtMs = now - lastTime;
|
||||
if (dtMs < frameThreshold) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user