All checks were successful
Dynamic Branch Deploy / build-and-deploy (push) Successful in 1m51s
95 lines
3.0 KiB
JavaScript
95 lines
3.0 KiB
JavaScript
async function loadAssets() {
|
|
playerSprite.src = "assets/player.png";
|
|
if (gameConfig.backgrounds && gameConfig.backgrounds.length > 0) {
|
|
const bgName = gameConfig.backgrounds[0];
|
|
if (!bgName.startsWith("#")) bgSprite.src = "assets/" + bgName;
|
|
}
|
|
const promises = gameConfig.obstacles.map(def => {
|
|
return new Promise((resolve) => {
|
|
if (!def.image) { resolve(); return; }
|
|
const img = new Image(); img.src = "assets/" + def.image;
|
|
img.onload = () => { sprites[def.id] = img; resolve(); };
|
|
img.onerror = () => { resolve(); };
|
|
});
|
|
});
|
|
if (bgSprite.src) {
|
|
promises.push(new Promise(r => { bgSprite.onload = r; bgSprite.onerror = r; }));
|
|
}
|
|
await Promise.all(promises);
|
|
}
|
|
|
|
window.startGameClick = async function() {
|
|
if (!isLoaded) return;
|
|
startScreen.style.display = 'none';
|
|
document.body.classList.add('game-active'); // Handy Rotate Check
|
|
try {
|
|
const sRes = await fetch('/api/start', {method:'POST'});
|
|
const sData = await sRes.json();
|
|
sessionID = sData.sessionId;
|
|
rng = new PseudoRNG(sData.seed);
|
|
isGameRunning = true;
|
|
resize();
|
|
} catch(e) { location.reload(); }
|
|
};
|
|
|
|
function gameOver(reason) {
|
|
if (isGameOver) return;
|
|
isGameOver = true;
|
|
const finalScoreVal = Math.floor(score / 10);
|
|
const currentHighscore = localStorage.getItem('escape_highscore') || 0;
|
|
if (finalScoreVal > currentHighscore) localStorage.setItem('escape_highscore', finalScoreVal);
|
|
gameOverScreen.style.display = 'flex';
|
|
document.getElementById('finalScore').innerText = finalScoreVal;
|
|
loadLeaderboard();
|
|
drawGame();
|
|
}
|
|
|
|
function gameLoop(timestamp) {
|
|
requestAnimationFrame(gameLoop);
|
|
|
|
if (!isLoaded || !isGameRunning || isGameOver) {
|
|
lastTime = timestamp;
|
|
return;
|
|
}
|
|
|
|
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() {
|
|
try {
|
|
const cRes = await fetch('/api/config'); gameConfig = await cRes.json();
|
|
await loadAssets();
|
|
await loadStartScreenLeaderboard();
|
|
isLoaded = true;
|
|
if(loadingText) loadingText.style.display = 'none'; if(startBtn) startBtn.style.display = 'inline-block';
|
|
const savedHighscore = localStorage.getItem('escape_highscore') || 0;
|
|
const hsEl = document.getElementById('localHighscore'); if(hsEl) hsEl.innerText = savedHighscore;
|
|
requestAnimationFrame(gameLoop);
|
|
} catch(e) { if(loadingText) loadingText.innerText = "Fehler!"; }
|
|
}
|
|
|
|
initGame(); |