Skip to content

Commit

Permalink
Merge pull request #30 from palfrey/improved-collision-detection
Browse files Browse the repository at this point in the history
Improved collision detection
  • Loading branch information
jagregory committed Jun 4, 2011
2 parents fe367a5 + 4d698a1 commit a952777
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
4 changes: 2 additions & 2 deletions data/level.txt
@@ -1,7 +1,7 @@
*****************
*1 4*
* *
*1 * 4*
* *
** *
* *
* *
* *
Expand Down
30 changes: 22 additions & 8 deletions public/js/game/kaboom.game.js
Expand Up @@ -90,19 +90,33 @@ KaboomGame.prototype = {
{
if (p != null)
{
/* hacky "looks right for the image we've got values */
var width = 43;
var height = 38;

var newPos = new Position(
p.position.x + game.DISTANCE * p.velocity.dx,
p.position.y + game.DISTANCE * p.velocity.dy);
var tilePos = game.pixelsToTiles(newPos);
var tile = game.level.rows[tilePos.y][tilePos.x];

function goodPos(position)
{
var tilePos = game.pixelsToTiles(position);
var tile = game.level.rows[tilePos.y][tilePos.x];

try {
if (!tile.solid)
return true;
}
catch (ex) {
console.log(tilePos);
return false;
}
}

try {
if (!tile.solid)
var rightPos = new Position(newPos.x + width, newPos.y);
var downPos = new Position(newPos.x, newPos.y + height);
if (goodPos(newPos) && goodPos(rightPos) && goodPos(downPos))
p.position = newPos;
}
catch (ex) {
console.log(tilePos);
}
}
});
}
Expand Down

0 comments on commit a952777

Please sign in to comment.