From fd1e0447a439139117150d1c79836a6d56fdc9e8 Mon Sep 17 00:00:00 2001 From: Marcel Date: Wed, 10 Apr 2019 10:29:41 +0200 Subject: [PATCH] Improved hit box and collisions --- ball.js | 17 ++++++++++++++--- game.js | 15 ++++++++------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/ball.js b/ball.js index 0d14a0d..1de7cbf 100644 --- a/ball.js +++ b/ball.js @@ -17,7 +17,7 @@ class Ball { this.cx += this.vx / stepCount; this.cy -= this.vy / stepCount; - if (this.cx - this.game.ballRadius <= 0 || this.cx + this.game.ballRadius >= this.game.canvas.width) { + if (this.cx - this.game.ballRadius <= game.startAtX || this.cx + this.game.ballRadius >= this.game.canvas.width - game.startAtX) { this.vx *= -1; } @@ -77,12 +77,23 @@ class Ball { const collLeft = (this.cx + this.game.ballRadius) - rect.x; const collRight = rect.x + this.game.rectSize - (this.cx - this.game.ballRadius); - if ((collTop <= collLeft && collTop <= collRight) || (collBottom <= collLeft && collBottom <= collRight)) { + if ((collTop <= collBottom && collTop <= collLeft && collTop <= collRight) || (collBottom <= collTop && collBottom <= collLeft && collBottom <= collRight)) { this.vy *= -1; - } else { + } + if((collLeft <= collRight && collLeft <= collTop && collLeft <= collBottom) || (collRight <= collLeft && collRight <= collTop && collRight <= collBottom)) { this.vx *= -1; } + if (distX > 0) + this.cx += game.ballRadius - distX; + else if (distX < 0) + this.cx -= game.ballRadius + distX; + + if (distY > 0) + this.cy += game.ballRadius - distY; + else if(distY < 0) + this.cy -= game.ballRadius + distY; + rect.hit(this); } } diff --git a/game.js b/game.js index b7b951e..7146286 100644 --- a/game.js +++ b/game.js @@ -16,13 +16,14 @@ class Game { this.colCount = 7; this.rowCount = 7; - this.rectSize = this.width / this.colCount; + this.rectSize = Math.floor(this.width / this.colCount); + this.startAtX = (this.width - this.colCount * this.rectSize) / 2; this.originX = this.width / 2; this.originY = (this.rowCount + 2) * this.rectSize; this.newOriginX = null; - this.ballRadius = 12; + this.ballRadius = 10; this.ballVelocity = 15; this.powerUpRadius = 15; @@ -74,7 +75,7 @@ class Game { } removeEventListeners() { - + // this.canvas.removeEventListener() } draw() { @@ -198,25 +199,25 @@ class Game { } addNewObjects() { - const spawnBall = Math.random() < .5, + const spawnBall = this.roundNumber > 1, ballPos = spawnBall ? Math.round(Math.random() * (this.colCount - 1)) : null, spawnCoin = Math.random() < .33, coinPos = spawnCoin ? Math.round(Math.random() * (this.colCount - 1)) : null; for (let i = 0; i < this.colCount; i++) { if (spawnBall && ballPos === i) { - this.powerUps.push(new PowerUpBall(this, (i + .5) * this.rectSize, .5 * this.rectSize)); + this.powerUps.push(new PowerUpBall(this, this.startAtX + (i + .5) * this.rectSize, .5 * this.rectSize)); continue; } if (spawnCoin && coinPos === i) { - this.powerUps.push(new PowerUpCoin(this, (i + .5) * this.rectSize, .5 * this.rectSize)); + this.powerUps.push(new PowerUpCoin(this, this.startAtX + (i + .5) * this.rectSize, .5 * this.rectSize)); continue; } if (Math.random() < .6) { const number = Math.random() < .6 ? this.roundNumber : 2 * this.roundNumber; - this.rects.push(new Rect(this, i * this.rectSize, 0, number)); + this.rects.push(new Rect(this, this.startAtX + i * this.rectSize, 0, number)); } } }