-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(collision-detection): move to web worker
- Loading branch information
Niklas Kiefer
committed
Mar 22, 2019
1 parent
8ac52da
commit f7f2e9a
Showing
8 changed files
with
211 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
dist/ | ||
node_modules/ | ||
lib/jquery/ | ||
lib/threadify | ||
coverage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* global it, describe, expect, beforeEach, __html__, Canvas, sinon, CollisionDetection */ | ||
/* eslint-disable no-unused-expressions */ | ||
describe('CollisionDetection', function () { | ||
|
||
let mrflapDiv; | ||
|
||
const _initPlayground = function () { | ||
|
||
$('body').append('<div class="mrflap-playground"></div>'); | ||
|
||
}; | ||
|
||
beforeEach(function () { | ||
|
||
document.body.innerHTML = __html__['test.html']; | ||
_initPlayground(); | ||
mrflapDiv = $('.mrflap-playground'); | ||
|
||
}); | ||
describe.only('#collisionDetection', function () { | ||
|
||
let canvas, bird; | ||
|
||
beforeEach(function () { | ||
|
||
canvas = new Canvas({ | ||
mrflapDiv: mrflapDiv | ||
}); | ||
|
||
bird = canvas.drawBird(); | ||
|
||
}); | ||
|
||
it('should hit lower obstacle', function () { | ||
|
||
// given | ||
canvas.drawObstacle({ | ||
heightBottom: 50 | ||
}); | ||
|
||
// when | ||
for (let i = 0; i < 110; i++) { | ||
|
||
canvas.moveObstacles(); | ||
|
||
} | ||
|
||
// when | ||
const { | ||
collisionDetection, | ||
collideCheckObject | ||
} = CollisionDetection.wrapped(canvas); | ||
|
||
const job = collisionDetection(collideCheckObject); | ||
|
||
job.done = (isCollided) => { | ||
|
||
// then | ||
expect(isCollided).to.be.true; | ||
|
||
}; | ||
|
||
}); | ||
|
||
it('should hit upper obstacle', function () { | ||
|
||
// given | ||
canvas.drawObstacle({ | ||
heightBottom: 50 | ||
}); | ||
|
||
bird.moveUp({ | ||
speed: 100 | ||
}); | ||
|
||
// when | ||
for (let i = 0; i < 110; i++) { | ||
|
||
canvas.moveObstacles(); | ||
|
||
} | ||
|
||
// when | ||
const { | ||
collisionDetection, | ||
collideCheckObject | ||
} = CollisionDetection.wrapped(canvas); | ||
|
||
const job = collisionDetection(collideCheckObject); | ||
|
||
job.done = (isCollided) => { | ||
|
||
// then | ||
expect(isCollided).to.be.true; | ||
|
||
}; | ||
|
||
}); | ||
|
||
}); | ||
|
||
}); |