enhancement/only-throttle-fps-in-multiplayer

This commit is contained in:
Thilo Behnke
2022-07-09 19:57:24 +02:00
parent 5f1d74b1d4
commit bb74832995
2 changed files with 10 additions and 1 deletions

View File

@@ -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}

View File

@@ -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;