Format
All checks were successful
Check & deploy / deploy (push) Successful in 7s

This commit is contained in:
Andras Schmelczer 2026-06-11 20:38:44 +01:00
parent 2cb12f93e8
commit c34177b216

View file

@ -1,4 +1,5 @@
const enemySpawnFrequency = 4.5; const enemySpawnFrequencyPerSecond = 4.5;
const maxDeltaTimeInMs = 50;
const canvasHandler = new CanvasHandler("canvas"); const canvasHandler = new CanvasHandler("canvas");
const inputHandler = new InputHandler(canvasHandler); const inputHandler = new InputHandler(canvasHandler);
@ -27,9 +28,11 @@ class Circle {
this.position = add(this.position, limit(deltaPosition, maxDistance)); this.position = add(this.position, limit(deltaPosition, maxDistance));
} }
} }
draw(canvasHandler) { draw(canvasHandler) {
canvasHandler.drawCircle(this.position, this.size, this.color); canvasHandler.drawCircle(this.position, this.size, this.color);
} }
isTouching(other) { isTouching(other) {
return ( return (
magnitude(subtract(this.position, other.position)) < magnitude(subtract(this.position, other.position)) <
@ -49,7 +52,7 @@ class Game {
); );
this.circleHolder = [this.player]; this.circleHolder = [this.player];
this.spawnInterval = 1000 / enemySpawnFrequency; this.spawnInterval = 1000 / enemySpawnFrequencyPerSecond;
this.previousSpawn = this.previousTime = 0; this.previousSpawn = this.previousTime = 0;
this.score = 0; this.score = 0;
@ -61,6 +64,7 @@ class Game {
this.boundNextFrame = this.nextFrame.bind(this); this.boundNextFrame = this.nextFrame.bind(this);
window.requestAnimationFrame(this.boundNextFrame); window.requestAnimationFrame(this.boundNextFrame);
} }
nextFrame(timestamp) { nextFrame(timestamp) {
if (this.isActive) { if (this.isActive) {
canvasHandler.fillCanvas("rgba(165, 64, 45, 0.1)"); canvasHandler.fillCanvas("rgba(165, 64, 45, 0.1)");
@ -69,6 +73,7 @@ class Game {
window.requestAnimationFrame(this.boundNextFrame); window.requestAnimationFrame(this.boundNextFrame);
} }
} }
spawnEnemyIfNeeded(timestamp) { spawnEnemyIfNeeded(timestamp) {
if (timestamp - this.previousSpawn > this.spawnInterval) { if (timestamp - this.previousSpawn > this.spawnInterval) {
this.circleHolder.push( this.circleHolder.push(
@ -88,6 +93,7 @@ class Game {
this.score++; this.score++;
} }
} }
moveCircles(timestamp) { moveCircles(timestamp) {
const deltaTime = this.getDeltaTime(timestamp); const deltaTime = this.getDeltaTime(timestamp);
const collided = new Set(); const collided = new Set();
@ -112,14 +118,17 @@ class Game {
); );
} }
} }
getDeltaTime(timestamp) { getDeltaTime(timestamp) {
const tempPreviousTime = this.previousTime; const tempPreviousTime = this.previousTime;
this.previousTime = timestamp; this.previousTime = timestamp;
return timestamp - tempPreviousTime; return Math.min(timestamp - tempPreviousTime, maxDeltaTimeInMs);
} }
showEndScreen() { showEndScreen() {
document.getElementById("endText").style.display = "block"; document.getElementById("endText").style.display = "block";
document.getElementById("endScore").innerHTML = `Your score: ${this.score}`; document.getElementById("endScore").innerHTML = `Your score: ${this.score}`;
document.getElementById("startButton").textContent = "Play again";
this.menuBox.display = "flex"; this.menuBox.display = "flex";
this.isActive = false; this.isActive = false;
} }