Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions lib/explosion.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Explosion {
this.width += 35
}
const fb = new FireBlock(this.x, this.y, this.game)
this.collisions.movingObject = fb
fb.draw('primarySprite')
this.checkTopRect()
this.checkLeftRect()
Expand All @@ -64,8 +65,6 @@ class Explosion {
checkForPlayers(x, y, width, height) {
const playerOne = this.game.firstPlayer
const playerTwo = this.game.secondPlayer
console.log(playerOne.x, playerOne.y, playerOne.width, playerOne.height, playerOne.alive)
console.log(x, y, width, height)
if (((x <= playerOne.x) && (playerOne.x <= (x + width))) && ((y <= playerOne.y) && (playerOne.y <= (y + height)))) {
playerOne.alive = false
}
Expand All @@ -76,13 +75,12 @@ class Explosion {

checkTopRect() {
if (this.y > 0) {
const collidingBlocks = this.game.blocks.filter(this.collisions.blockAbove.bind(this))
const breakableBlocks = this.game.breakableBlocks.filter(this.collisions.blockAbove.bind(this))
const breakableBlocks = this.game.breakableBlocks.filter(this.collisions.blockAbove.bind(this.collisions))
/* removing blocks altogether instead of clearing the rectangle,
* existing blocks are automatically rerendered on loop.
* Game removes blocks in batches */
this.game.removeBreakableBlocks(breakableBlocks)
if (collidingBlocks.length === 0) {
if (this.collisions.checkForCollision()['fire']['up']()) {
const fBlock = new FireBlock(this.x, this.y - this.speed, this.game)
this.checkForPlayers(fBlock.x, fBlock.y, fBlock.width, fBlock.height)
fBlock.draw('topSprite')
Expand All @@ -94,10 +92,9 @@ class Explosion {

checkLeftRect() {
if (this.x > 0) {
const collidingBlocks = this.game.blocks.filter(this.collisions.blockLeft.bind(this))
const breakableBlocks = this.game.breakableBlocks.filter(this.collisions.blockLeft.bind(this))
const breakableBlocks = this.game.breakableBlocks.filter(this.collisions.blockLeft.bind(this.collisions))
this.game.removeBreakableBlocks(breakableBlocks)
if (collidingBlocks.length === 0) {
if (this.collisions.checkForCollision()['fire']['left']()) {
const fBlock = new FireBlock(this.x - this.speed, this.y, this.game)
this.checkForPlayers(fBlock.x, fBlock.y, fBlock.width, fBlock.height)
fBlock.draw('leftSprite')
Expand All @@ -109,10 +106,9 @@ class Explosion {

checkRightRect() {
if (this.x < this.game.canvas.width - this.width) {
const collidingBlocks = this.game.blocks.filter(this.collisions.blockRight.bind(this))
const breakableBlocks = this.game.breakableBlocks.filter(this.collisions.blockRight.bind(this))
const breakableBlocks = this.game.breakableBlocks.filter(this.collisions.blockRight.bind(this.collisions))
this.game.removeBreakableBlocks(breakableBlocks)
if (collidingBlocks.length === 0) {
if (this.collisions.checkForCollision()['fire']['right']()) {
const fBlock = new FireBlock(this.x + this.speed, this.y, this.game)
this.checkForPlayers(fBlock.x, fBlock.y, fBlock.width, fBlock.height)
fBlock.draw('rightSprite')
Expand All @@ -124,10 +120,9 @@ class Explosion {

checkBottomRect() {
if (this.y < this.game.canvas.height - this.height) {
const collidingBlocks = this.game.blocks.filter(this.collisions.blockBelow.bind(this))
const breakableBlocks = this.game.breakableBlocks.filter(this.collisions.blockBelow.bind(this))
const breakableBlocks = this.game.breakableBlocks.filter(this.collisions.blockBelow.bind(this.collisions))
this.game.removeBreakableBlocks(breakableBlocks)
if (collidingBlocks.length === 0) {
if (this.collisions.checkForCollision()['fire']['down']()) {
const fBlock = new FireBlock(this.x, this.y + this.speed, this.game)
this.checkForPlayers(fBlock.x, fBlock.y, fBlock.width, fBlock.height)
fBlock.draw('bottomSprite')
Expand Down
1 change: 1 addition & 0 deletions lib/fireBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Block = require('./block')
class FireBlock extends Block {
constructor(x, y, game, image) {
super(x, y, game)
this.speed = this.height
this.image = './../images/bomberman-miscellaneous.png'
this.primarySprite = { x: 324, y: 85, width: 17, height: 17 }
this.rightSprite = { x: 273, y: 120, width: 16, height: 16 }
Expand Down
149 changes: 117 additions & 32 deletions lib/helpers/collisionEngine.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,155 @@
class CollisionEngine {
constructor(movingObject) {
this.movingObject = movingObject
}

checkForCollision() {
return {
player: {
right: this.playerCollisionsRight.bind(this),
left: this.playerCollisionsLeft.bind(this),
up: this.playerCollisionsUp.bind(this),
down: this.playerCollisionsDown.bind(this)
},
fire: {
right: this.fireCollisionsRight.bind(this),
left: this.fireCollisionsLeft.bind(this),
up: this.fireCollisionsUp.bind(this),
down: this.fireCollisionsDown.bind(this)
},
bomb: {
deploy: this.bombDeploymentCollision.bind(this)
}
}
}

bombDeploymentCollision() {
this.movingObject.speed = 25
const blocksBelow = this.movingObject.game.blocks.filter(this.downCollide.bind(this))
const breakablesBelow = this.movingObject.game.breakableBlocks.filter(this.downCollide.bind(this))
this.movingObject.speed = 3
return (blocksBelow.length + breakablesBelow.length) === 0
}

fireCollisionsRight() {
const collidingBlocks = this.movingObject.game.blocks.filter(this.blockRight.bind(this))
return collidingBlocks.length === 0
}

fireCollisionsLeft() {
const collidingBlocks = this.movingObject.game.blocks.filter(this.blockLeft.bind(this))
return collidingBlocks.length === 0
}

fireCollisionsUp() {
const collidingBlocks = this.movingObject.game.blocks.filter(this.blockAbove.bind(this))
return collidingBlocks.length === 0
}

fireCollisionsDown() {
const collidingBlocks = this.movingObject.game.blocks.filter(this.blockBelow.bind(this))
return collidingBlocks.length === 0
}

playerCollisionsRight() {
const collidingBlocks = this.movingObject.game.blocks.filter(this.rightCollide.bind(this))
const collidingBombs = this.movingObject.game.bombs.filter(this.bombRight.bind(this))
const collidingBreakables = this.movingObject.game.breakableBlocks.filter(this.rightCollide.bind(this))
const possibleCollisions = collidingBlocks.length + collidingBombs.length + collidingBreakables.length
return possibleCollisions === 0
}

playerCollisionsLeft() {
const collidingBlocks = this.movingObject.game.blocks.filter(this.leftCollide.bind(this))
const collidingBombs = this.movingObject.game.bombs.filter(this.bombLeft.bind(this))
const collidingBreakables = this.movingObject.game.breakableBlocks.filter(this.leftCollide.bind(this))
const possibleCollisions = collidingBlocks.length + collidingBombs.length + collidingBreakables.length
return possibleCollisions === 0
}

playerCollisionsUp() {
const collidingBlocks = this.movingObject.game.blocks.filter(this.upCollide.bind(this))
const collidingBombs = this.movingObject.game.bombs.filter(this.bombAbove.bind(this))
const collidingBreakables = this.movingObject.game.breakableBlocks.filter(this.upCollide.bind(this))
const possibleCollisions = collidingBlocks.length + collidingBombs.length + collidingBreakables.length
return possibleCollisions === 0
}

playerCollisionsDown() {
const collidingBlocks = this.movingObject.game.blocks.filter(this.downCollide.bind(this))
const collidingBombs = this.movingObject.game.bombs.filter(this.bombBelow.bind(this))
const collidingBreakables = this.movingObject.game.breakableBlocks.filter(this.downCollide.bind(this))
const possibleCollisions = collidingBlocks.length + collidingBombs.length + collidingBreakables.length
return possibleCollisions === 0
}


rightCollide(block) {
return (block.x - this.speed <= (this.x + this.width) && (this.x + this.width) <= block.x) &&
((block.y < this.y && this.y < block.y + block.height) ||
(block.y < this.y + this.height && this.y + this.height < block.y + block.height))
return (block.x - this.movingObject.speed <= (this.movingObject.x + this.movingObject.width) && (this.movingObject.x + this.movingObject.width) <= block.x) &&
((block.y < this.movingObject.y && this.movingObject.y < block.y + block.height) ||
(block.y < this.movingObject.y + this.movingObject.height && this.movingObject.y + this.movingObject.height < block.y + block.height))
}

leftCollide(block) {
return (this.x <= (block.x + block.width + this.speed) && this.x >= (block.x + block.width)) &&
((block.y < this.y && this.y < block.y + block.height) ||
(block.y < this.y + this.height && this.y + this.height < block.y + block.height))
return (this.movingObject.x <= (block.x + block.width + this.movingObject.speed) && this.movingObject.x >= (block.x + block.width)) &&
((block.y < this.movingObject.y && this.movingObject.y < block.y + block.height) ||
(block.y < this.movingObject.y + this.movingObject.height && this.movingObject.y + this.movingObject.height < block.y + block.height))
}

upCollide(block) {
return (this.y <= (block.y + block.height + this.speed) && this.y >= (block.y + block.height)) &&
((block.x < this.x && this.x < block.x + block.width) ||
(block.x < this.x + this.width && this.x + this.width < block.x + block.width))
return (this.movingObject.y <= (block.y + block.height + this.movingObject.speed) && this.movingObject.y >= (block.y + block.height)) &&
((block.x < this.movingObject.x && this.movingObject.x < block.x + block.width) ||
(block.x < this.movingObject.x + this.movingObject.width && this.movingObject.x + this.movingObject.width < block.x + block.width))
}

downCollide(block) {
return ((this.y + this.height) <= block.y && (this.y + this.height) >= (block.y - this.speed)) &&
((block.x < this.x && this.x < block.x + block.width) ||
(block.x < this.x + this.width && this.x + this.width < block.x + block.width))
return ((this.movingObject.y + this.movingObject.height) <= block.y && (this.movingObject.y + this.movingObject.height) >= (block.y - this.movingObject.speed)) &&
((block.x < this.movingObject.x && this.movingObject.x < block.x + block.width) ||
(block.x < this.movingObject.x + this.movingObject.width && this.movingObject.x + this.movingObject.width < block.x + block.width))
}

blockAbove(block) {
return (this.y === block.y + block.height) &&
(this.x === block.x)
return (this.movingObject.y === block.y + block.height) &&
(this.movingObject.x === block.x)
}

blockBelow(block) {
return (this.y + this.height === block.y) &&
(this.x === block.x)
return (this.movingObject.y + this.movingObject.height === block.y) &&
(this.movingObject.x === block.x)
}

blockLeft(block) {
return (this.x === block.x + block.width) &&
(this.y === block.y)
return (this.movingObject.x === block.x + block.width) &&
(this.movingObject.y === block.y)
}

blockRight(block) {
return (this.x + this.width === block.x) &&
(this.y === block.y)
return (this.movingObject.x + this.movingObject.width === block.x) &&
(this.movingObject.y === block.y)
}

bombAbove(bomb) {
return ((bomb.y + bomb.height + this.speed) > this.y) &&
((bomb.y + bomb.height + this.speed) <= (this.y + this.height)) &&
(((bomb.x + bomb.width) > this.x) && (bomb.x < (this.x + this.width)))
return ((bomb.y + bomb.height + this.movingObject.speed) > this.movingObject.y) &&
((bomb.y + bomb.height + this.movingObject.speed) <= (this.movingObject.y + this.movingObject.height)) &&
(((bomb.x + bomb.width) > this.movingObject.x) && (bomb.x < (this.movingObject.x + this.movingObject.width)))
}

bombBelow(bomb) {
return ((this.y + this.height + this.speed) > bomb.y) &&
((this.y + this.height + this.speed) <= (bomb.y + bomb.height)) &&
(((bomb.x + bomb.width) > this.x) && (bomb.x < (this.x + this.width)))
return ((this.movingObject.y + this.movingObject.height + this.movingObject.speed) > bomb.y) &&
((this.movingObject.y + this.movingObject.height + this.movingObject.speed) <= (bomb.y + bomb.height)) &&
(((bomb.x + bomb.width) > this.movingObject.x) && (bomb.x < (this.movingObject.x + this.movingObject.width)))
}

bombRight(bomb) {
return ((this.x + this.width + this.speed) >= bomb.x) &&
((this.x + this.width + this.speed) <= (bomb.x + bomb.width)) &&
((bomb.y + bomb.height > this.y) && (bomb.y < (this.y + this.height)))
return ((this.movingObject.x + this.movingObject.width + this.movingObject.speed) >= bomb.x) &&
((this.movingObject.x + this.movingObject.width + this.movingObject.speed) <= (bomb.x + bomb.width)) &&
((bomb.y + bomb.height > this.movingObject.y) && (bomb.y < (this.movingObject.y + this.movingObject.height)))
}

bombLeft(bomb) {
return ((bomb.x + bomb.width + this.speed) >= this.x) &&
((bomb.x + bomb.width + this.speed) <= (this.x + this.width)) &&
((bomb.y + bomb.height > this.y) && (bomb.y < (this.y + this.height)))
return ((bomb.x + bomb.width + this.movingObject.speed) >= this.movingObject.x) &&
((bomb.x + bomb.width + this.movingObject.speed) <= (this.movingObject.x + this.movingObject.width)) &&
((bomb.y + bomb.height > this.movingObject.y) && (bomb.y < (this.movingObject.y + this.movingObject.height)))
}
}

Expand Down
33 changes: 6 additions & 27 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Player {
this.width = 35
this.speed = 3
this.game = game
this.collisions = new CollisionEngine()
this.collisions = new CollisionEngine(this)
}

get centerX() {
Expand Down Expand Up @@ -62,12 +62,7 @@ class Player {
}

verifyCleanBombDeployment(bomb) {
this.speed = bomb.height
const blocksBelow = this.game.blocks.filter(this.collisions.downCollide.bind(this))
const breakablesBelow = this.game.breakableBlocks.filter(this.collisions.downCollide.bind(this))
this.speed = 3
const possibleCollisions = blocksBelow.length + breakablesBelow.length
if (possibleCollisions === 0) {
if (this.collisions.checkForCollision()['bomb']['deploy']()) {
bomb.draw()
}
else {
Expand All @@ -84,11 +79,7 @@ class Player {

moveRight() {
if (this.x < this.game.canvas.width - this.width) {
const collidingBlocks = this.game.blocks.filter(this.collisions.rightCollide.bind(this))
const collidingBombs = this.game.bombs.filter(this.collisions.bombRight.bind(this))
const collidingBreakables = this.game.breakableBlocks.filter(this.collisions.rightCollide.bind(this))
const possibleCollisions = collidingBlocks.length + collidingBombs.length + collidingBreakables.length
if (possibleCollisions === 0) {
if (this.collisions.checkForCollision()['player']['right']()) {
this.clear()
this.x += this.speed
this.draw('rightSprite')
Expand All @@ -99,11 +90,7 @@ class Player {

moveLeft() {
if (this.x > 0) {
const collidingBlocks = this.game.blocks.filter(this.collisions.leftCollide.bind(this))
const collidingBombs = this.game.bombs.filter(this.collisions.bombLeft.bind(this))
const collidingBreakables = this.game.breakableBlocks.filter(this.collisions.leftCollide.bind(this))
const possibleCollisions = collidingBlocks.length + collidingBombs.length + collidingBreakables.length
if (possibleCollisions === 0) {
if (this.collisions.checkForCollision()['player']['left']()) {
this.clear()
this.x -= this.speed
this.draw('leftSprite')
Expand All @@ -114,11 +101,7 @@ class Player {

moveUp() {
if (this.y > 0) {
const collidingBlocks = this.game.blocks.filter(this.collisions.upCollide.bind(this))
const collidingBombs = this.game.bombs.filter(this.collisions.bombAbove.bind(this))
const collidingBreakables = this.game.breakableBlocks.filter(this.collisions.upCollide.bind(this))
const possibleCollisions = collidingBlocks.length + collidingBombs.length + collidingBreakables.length
if (possibleCollisions === 0) {
if (this.collisions.checkForCollision()['player']['up']()) {
this.clear()
this.y -= this.speed
this.draw('upSprite')
Expand All @@ -129,11 +112,7 @@ class Player {

moveDown() {
if (this.y < this.game.canvas.height - this.height) {
const collidingBlocks = this.game.blocks.filter(this.collisions.downCollide.bind(this))
const collidingBombs = this.game.bombs.filter(this.collisions.bombBelow.bind(this))
const collidingBreakables = this.game.breakableBlocks.filter(this.collisions.downCollide.bind(this))
const possibleCollisions = collidingBlocks.length + collidingBombs.length + collidingBreakables.length
if (possibleCollisions === 0) {
if (this.collisions.checkForCollision()['player']['down']()) {
this.clear()
this.y += this.speed
this.draw('downSprite')
Expand Down