Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updated collision detection so it doesn't scan all tiles, instead it …

…only scans the tiles adjacent to the player (9 max, depending on how the player intersects)
  • Loading branch information...
commit ccdc741b1192fa2679778a100cc57a34713c3937 1 parent 8e424be
@jagregory jagregory authored
Showing with 24 additions and 14 deletions.
  1. +24 −14 public/js/game/kaboom.game.js
View
38 public/js/game/kaboom.game.js
@@ -16,6 +16,7 @@ var KaboomGame = function (levelMap) {
this.players = [];
this.DISTANCE = 5;
this.TILE_SIZE = 48;
+ this.COLLISION_RANGE = 1;
};
KaboomGame.prototype = {
@@ -84,29 +85,38 @@ KaboomGame.prototype = {
return new Position(tileX, tileY);
},
-
update: function() {
var game = this;
var tryMove = function(pos, delta) {
var newPos = pos.translate(delta);
- var chosen = newPos;
+ var gridPos = {
+ topLeft: game.pixelsToTiles(newPos.topLeft),
+ bottomRight: game.pixelsToTiles(newPos.bottomRight)
+ };
+ var collisionRange = {
+ xStart: Math.max(gridPos.topLeft.x - game.COLLISION_RANGE, 0),
+ xEnd: Math.min(gridPos.bottomRight.x + game.COLLISION_RANGE, 16), // TODO: un-hardcode this
+ yStart: Math.max(gridPos.topLeft.y - game.COLLISION_RANGE, 0),
+ yEnd: Math.min(gridPos.bottomRight.y + game.COLLISION_RANGE, 12) // TODO: un-hardcode this
+ };
- game.level.forEachTile(function(tile) {
- var bounds = tile.getBounds(game);
-
- tile.candidate = false;
- tile.isIntersecting = false;
-
- if (bounds.intersects(newPos.contract(4))) {
- if (tile.solid) {
+ for (var x = collisionRange.xStart; x <= collisionRange.xEnd; x++) {
+ for (var y = collisionRange.yStart; y <= collisionRange.yEnd; y++) {
+ var tile = game.level.rows[y][x];
+ var bounds = tile.getBounds(game);
+
+ tile.isIntersecting = false;
+
+ if (bounds.intersects(newPos.contract(4))) {
tile.isIntersecting = true;
- chosen = pos;
- return;
+ if (tile.solid) {
+ return pos;
+ }
}
}
- });
+ }
- return chosen;
+ return newPos;
};
this.players.forEach(function(p, idx) {
Please sign in to comment.
Something went wrong with that request. Please try again.