Private
Public Access
1
0

fix Handy FPS Problems
All checks were successful
Dynamic Branch Deploy / build-and-deploy (push) Successful in 1m51s

This commit is contained in:
Sebastian Unterschütz
2025-11-25 18:50:05 +01:00
parent 5175f52652
commit 553f4c2944
3 changed files with 39 additions and 7 deletions

View File

@@ -6,6 +6,9 @@ const JUMP_POWER = -12;
const GROUND_Y = 350;
const GAME_SPEED = 5;
const CHUNK_SIZE = 60;
const TARGET_FPS = 60;
const MS_PER_TICK = 1000 / TARGET_FPS;
// RNG Klasse
class PseudoRNG {

View File

@@ -44,14 +44,39 @@ function gameOver(reason) {
drawGame();
}
function gameLoop() {
if (!isLoaded) return;
if (isGameRunning && !isGameOver) {
updateGameLogic(); currentTick++; score++;
const scoreEl = document.getElementById('score'); if (scoreEl) scoreEl.innerText = Math.floor(score / 10);
if (currentTick - lastSentTick >= CHUNK_SIZE) sendChunk();
function gameLoop(timestamp) {
requestAnimationFrame(gameLoop);
if (!isLoaded || !isGameRunning || isGameOver) {
lastTime = timestamp;
return;
}
drawGame(); requestAnimationFrame(gameLoop);
if (!lastTime) lastTime = timestamp;
const deltaTime = timestamp - lastTime;
lastTime = timestamp;
if (deltaTime > 1000) {
accumulator = 0;
return;
}
accumulator += deltaTime;
while (accumulator >= MS_PER_TICK) {
updateGameLogic();
currentTick++;
score++;
if (currentTick - lastSentTick >= CHUNK_SIZE) sendChunk();
accumulator -= MS_PER_TICK;
}
const scoreEl = document.getElementById('score');
if (scoreEl) scoreEl.innerText = Math.floor(score / 10);
drawGame();
}
async function initGame() {

View File

@@ -12,6 +12,10 @@ let lastSentTick = 0;
let inputLog = [];
let isCrouching = false;
// Tick Time
let lastTime = 0;
let accumulator = 0;
// Grafiken
let sprites = {};
let playerSprite = new Image();