Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP feat(collision-detection): move to web worker #53

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
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
2 changes: 2 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ gulp.task('default', gulp.series(['clean', 'html', 'css', 'js', 'vendor']));

gulp.task('test', gulp.series(['default'], function (done) {

process.env.NODE_ENV = 'test';

return new KarmaServ({
configFile: `${configuration.paths.test}/config/karma.unit.js`,
singleRun: true
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
87 changes: 87 additions & 0 deletions lib/collision-detection/collisionDetection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@

/* global threadify */
class CollisionDetection {

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

const thread = this;

let birdWidth = bird.x + bird.width;

obstacles.forEach(obstacle => {

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

if (
(bird.x >= obstacle.x && birdWidth <= obstacleWidth) ||
(birdWidth >= obstacle.x && birdWidth <= obstacleWidth) ||
(bird.x === obstacleWidth) ||
(bird.x === obstacle.x) ||
(birdWidth === obstacleWidth)
) {

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

if (bird.y <= obstacleHeight) {

thread.return(true);

}

} else { // check lower obstacle

if (bird.y + bird.height >= obstacle.y) {

thread.return(true);

}

}

}

});

thread.return(false);

}

static wrapped (canvas) {

// cleanup objects for later use
const bird = {
x: canvas.bird.shape.x,
y: canvas.bird.shape.y,
width: canvas.bird.shape.width,
height: canvas.bird.shape.height
};

const obstacles = canvas.obstacles.map(o => {

return {
x: o.shape.x,
y: o.shape.y,
width: o.shape.width,
height: o.shape.height
};

});

const wrappedFn = threadify(this.collisionDetection);

return {
collisionDetection: wrappedFn,
collideCheckObject: {
bird,
obstacles
}
};

}

}
33 changes: 31 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 */
let mrflap, canvas, bird;

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

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

if (_isTestMode()) {

return;

}

// execute collision detection inside web worker
const {
collisionDetection,
collideCheckObject
} = CollisionDetection.wrapped(canvas);

const job = collisionDetection(collideCheckObject);

job.done = function (isCollided) {

if (isCollided) {

canvas.onClear();

}

};

}, 1000 / 30);

Expand Down Expand Up @@ -153,6 +176,12 @@ function _reset () {

}

function _isTestMode () {

return $('#test-mode').length > 0;

}

document.addEventListener('DOMContentLoaded', event => {

_init();
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.

71 changes: 0 additions & 71 deletions test/canvas/canvasSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,75 +176,4 @@ describe('Canvas', function () {

});

describe('#collisionDetection', function () {

let canvas, bird, hitSpy;

beforeEach(function () {

hitSpy = sinon.spy();

canvas = new Canvas({
mrflapDiv: mrflapDiv,
onClear: hitSpy
});

bird = canvas.drawBird();

});

it('should hit lower obstacle', function () {

// given
const {
obstacleBottom
} = canvas.drawObstacle({
heightBottom: 50
});

// when
for (let i = 0; i < 110; i++) {

canvas.moveObstacles();

}

canvas.collisionDetection();

// then
expect(hitSpy).to.have.been.called;

});

it('should hit upper obstacle', function () {

// given
const {
obstacleTop
} = canvas.drawObstacle({
heightBottom: 50
});

bird.moveUp({
speed: 100
});

const originalX = obstacleTop.x;

// when
for (let i = 0; i < 110; i++) {

canvas.moveObstacles();

}

canvas.collisionDetection();

// then
expect(hitSpy).to.have.been.called;

});

});

});
Loading