Small clean up

This commit is contained in:
Andras Schmelczer 2026-06-11 20:32:56 +01:00
parent 3b807a968c
commit 3640242506
2 changed files with 31 additions and 16 deletions

View file

@ -1,14 +1,18 @@
class InputHandler { class InputHandler {
constructor(idOfWatchedArea) { constructor(canvasHandler) {
const elem = document.getElementById(idOfWatchedArea); const elem = canvasHandler.canvas;
const handleMouse = ({ offsetX, offsetY }) => const handleMouse = ({ clientX, clientY }) =>
this.setPointerPosition([offsetX, offsetY]); this.setPointerPosition(canvasHandler.clientToCanvas(clientX, clientY));
const handleTouch = ({ touches }) =>
this.setPointerPosition([touches[0].clientX, touches[0].clientY]); const handleTouch = (event) => {
event.preventDefault();
const { clientX, clientY } = event.touches[0];
this.setPointerPosition(canvasHandler.clientToCanvas(clientX, clientY));
};
elem.addEventListener("mousemove", handleMouse); elem.addEventListener("mousemove", handleMouse);
elem.addEventListener("touchmove", handleTouch); elem.addEventListener("touchmove", handleTouch, { passive: false });
this.pointerPosition = [0, 0]; this.pointerPosition = [0, 0];
} }

View file

@ -1,7 +1,7 @@
const enemySpawnFrequency = 4.5; const enemySpawnFrequency = 4.5;
const canvasHandler = new CanvasHandler("canvas"); const canvasHandler = new CanvasHandler("canvas");
const inputHandler = new InputHandler("canvas"); const inputHandler = new InputHandler(canvasHandler);
let game; let game;
function newGame() { function newGame() {
@ -31,7 +31,10 @@ class Circle {
canvasHandler.drawCircle(this.position, this.size, this.color); canvasHandler.drawCircle(this.position, this.size, this.color);
} }
isTouching(other) { isTouching(other) {
return magnitude(subtract(this.position, other.position)) < 2 * this.size; return (
magnitude(subtract(this.position, other.position)) <
this.size + other.size
);
} }
} }
@ -47,7 +50,7 @@ class Game {
this.circleHolder = [this.player]; this.circleHolder = [this.player];
this.spawnInterval = 1000 / enemySpawnFrequency; this.spawnInterval = 1000 / enemySpawnFrequency;
this.previousSpawn = this.previousTimestamp = 0; this.previousSpawn = this.previousTime = 0;
this.score = 0; this.score = 0;
this.menuBox = document.getElementById("menuBox").style; this.menuBox = document.getElementById("menuBox").style;
@ -55,14 +58,15 @@ class Game {
"none"; "none";
this.isActive = true; this.isActive = true;
window.requestAnimationFrame(this.nextFrame.bind(this)); this.boundNextFrame = this.nextFrame.bind(this);
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)");
this.spawnEnemyIfNeeded(timestamp); this.spawnEnemyIfNeeded(timestamp);
this.moveCircles(timestamp); this.moveCircles(timestamp);
window.requestAnimationFrame(this.nextFrame.bind(this)); window.requestAnimationFrame(this.boundNextFrame);
} }
} }
spawnEnemyIfNeeded(timestamp) { spawnEnemyIfNeeded(timestamp) {
@ -86,20 +90,27 @@ class Game {
} }
moveCircles(timestamp) { moveCircles(timestamp) {
const deltaTime = this.getDeltaTime(timestamp); const deltaTime = this.getDeltaTime(timestamp);
const collided = new Set();
for (let i = 0; i < this.circleHolder.length; i++) { for (let i = 0; i < this.circleHolder.length; i++) {
this.circleHolder[i].move(deltaTime); this.circleHolder[i].move(deltaTime);
this.circleHolder[i].draw(canvasHandler); this.circleHolder[i].draw(canvasHandler);
for (let j = i + 1; j < this.circleHolder.length; j++) { for (let j = i + 1; j < this.circleHolder.length; j++) {
if (this.circleHolder[i].isTouching(this.circleHolder[j])) { if (this.circleHolder[i].isTouching(this.circleHolder[j])) {
if (this.circleHolder[i] == this.player) this.showEndScreen(); if (this.circleHolder[i] === this.player) {
else { this.showEndScreen();
this.circleHolder.splice(i, 1); return;
this.circleHolder.splice(j - 1, 1);
} }
collided.add(i).add(j);
} }
} }
} }
if (collided.size > 0) {
this.circleHolder = this.circleHolder.filter(
(_, index) => !collided.has(index)
);
}
} }
getDeltaTime(timestamp) { getDeltaTime(timestamp) {
const tempPreviousTime = this.previousTime; const tempPreviousTime = this.previousTime;