big refactor
Some checks failed
Dynamic Branch Deploy / build-and-deploy (push) Failing after 48s
Some checks failed
Dynamic Branch Deploy / build-and-deploy (push) Failing after 48s
This commit is contained in:
70
static/js/main.js
Normal file
70
static/js/main.js
Normal file
@@ -0,0 +1,70 @@
|
||||
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() {
|
||||
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();
|
||||
}
|
||||
drawGame(); requestAnimationFrame(gameLoop);
|
||||
}
|
||||
|
||||
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();
|
||||
Reference in New Issue
Block a user