Skip to content

Commit

Permalink
feat(collision-detection): move to web worker
Browse files Browse the repository at this point in the history
  • Loading branch information
Niklas Kiefer committed Mar 22, 2019
1 parent 8ac52da commit 592aac7
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 59 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dist/
node_modules/
lib/jquery/
lib/threadify
coverage/
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"no-trailing-spaces": ["error", { "skipBlankLines": true }],
"padded-blocks": ["error", "always"],
"no-unused-vars": 0,
"new-cap": 0,
"no-console": "error",
"no-only-tests/no-only-tests": 2
},
Expand Down
57 changes: 0 additions & 57 deletions lib/canvas/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,63 +220,6 @@ class Canvas {

}

/**
* checks collision between obstacles and bird object
*/
collisionDetection () {

let birdShape = this.bird.shape;
let obstacles = this.obstacles;
let birdWidth = birdShape.x + birdShape.width;

const _hitAction = () => {

if (typeof this.onClear === 'function') {

this.onClear();

}

};

obstacles.forEach(obstacle => {

let obstacleShape = obstacle.shape;
let obstacleHeight = obstacleShape.y + obstacleShape.height;
let obstacleWidth = obstacleShape.x + obstacleShape.width;

if (
(birdShape.x >= obstacleShape.x && birdWidth <= obstacleWidth) ||
(birdWidth >= obstacleShape.x && birdWidth <= obstacleWidth) ||
(birdShape.x === obstacleWidth) ||
(birdShape.x === obstacleShape.x) ||
(birdWidth === obstacleWidth)
) {

if (obstacleShape.y === 0) { // check upper obstacle

if (birdShape.y <= obstacleHeight) {

_hitAction();

}

} else { // check lower obstacle

if (birdShape.y + birdShape.height >= obstacleShape.y) {

_hitAction();

}

}

}

});

}

/**
* completely deletes canvas and all its shapes
*/
Expand Down
49 changes: 49 additions & 0 deletions lib/collision-detection/collision-detection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

/**
* @param {Bird} options.bird
* @param {Array<Obstacle>} options.obstacle
*/
function collisionDetection ({ bird, obstacles }) {

let birdShape = bird.shape;
let birdWidth = birdShape.x + birdShape.width;

obstacles.forEach(obstacle => {

let obstacleShape = obstacle.shape;
let obstacleHeight = obstacleShape.y + obstacleShape.height;
let obstacleWidth = obstacleShape.x + obstacleShape.width;

if (
(birdShape.x >= obstacleShape.x && birdWidth <= obstacleWidth) ||
(birdWidth >= obstacleShape.x && birdWidth <= obstacleWidth) ||
(birdShape.x === obstacleWidth) ||
(birdShape.x === obstacleShape.x) ||
(birdWidth === obstacleWidth)
) {

if (obstacleShape.y === 0) { // check upper obstacle

if (birdShape.y <= obstacleHeight) {

return true;

}

} else { // check lower obstacle

if (birdShape.y + birdShape.height >= obstacleShape.y) {

return true;

}

}

}

return false;

});

}
21 changes: 19 additions & 2 deletions lib/main/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global Canvas, Footer, KeyboardHandler */
/* global Canvas, Footer, KeyboardHandler, collisionDetection, threadify */
let mrflap, canvas, bird;

function appendFooter () {
Expand Down Expand Up @@ -93,7 +93,24 @@ function spawnObstacles () {
setInterval(() => {

canvas.moveObstacles();
canvas.collisionDetection();

// execute collision detection inside web worker
const collisionDetectionWrapped = threadify(collisionDetection);

const job = collisionDetectionWrapped({
bird: canvas.bird,
obstacles: canvas.obstacles
});

job.done = (isCollided) => {

if (isCollided) {

canvas.onClear();

}

};

}, 1000 / 30);

Expand Down
1 change: 1 addition & 0 deletions lib/threadify/threadify.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 592aac7

Please sign in to comment.