Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Got "sliding" working when moving against a wall with two directions …

…held
  • Loading branch information...
commit 8e424be6fd4bfa1e3a49677aa32504c6458e752b 1 parent 2665a7c
James Gregory jagregory authored
4 public/css/bomber.css
View
@@ -88,4 +88,8 @@ body {
.intersecting {
background: rgba(0, 0, 255, 0.5);
+}
+
+.candidate {
+ background: rgba(255, 0, 0, 0.5);
}
2  public/js/game/client.js
View
@@ -36,7 +36,6 @@ KaboomClient.prototype = {
var key = $.hotkeys.specialKeys[event.which] || String.fromCharCode(event.which).toLowerCase();
var playerActuallyChanged = window.player.go(key);
if (playerActuallyChanged) this.notifyPlayerChanged();
- console.log("KEY_DOWN" + window.player.toString());
return (this.sendKeyToBrowser(key));
},
@@ -49,7 +48,6 @@ KaboomClient.prototype = {
var key = $.hotkeys.specialKeys[event.which] || String.fromCharCode(event.which).toLowerCase();
window.player.stop(key);
this.notifyPlayerChanged();
- console.log("KEY_UP" + window.player.toString());
return (this.sendKeyToBrowser(key));
},
notifyPlayerChanged: function() {
85 public/js/game/kaboom.game.js
View
@@ -87,40 +87,38 @@ KaboomGame.prototype = {
update: function() {
var game = this;
- /* For each player, assume they have moved DISTANCE in their own velocity */
+ var tryMove = function(pos, delta) {
+ var newPos = pos.translate(delta);
+ var chosen = newPos;
+
+ 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) {
+ tile.isIntersecting = true;
+ chosen = pos;
+ return;
+ }
+ }
+ });
+
+ return chosen;
+ };
+
this.players.forEach(function(p, idx) {
if (!p) return;
if (p.velocity.dx == 0 && p.velocity.dy == 0) return;
- var playerRect = p
- .getBounds(game)
- .translate({
- x: game.DISTANCE * p.velocity.dx,
- y: game.DISTANCE * p.velocity.dy
- });
-
- // We need to handle horizontal and vertical collision detection separately
- // so that Marvin can run ALONG walls but not run THROUGH them
- var verticalHitTestRect = playerRect.contract(4);
- verticalHitTestRect.left += 8;
- verticalHitTestRect.width -= 16;
-
- var horizontalHitTestRect = playerRect.contract(4);
- horizontalHitTestRect.top += 8;
- horizontalHitTestRect.height -= 16;
-
- var canMoveVertically = true;
- var canMoveHorizontally = true;
-
- game.level.forEachIntersectingTile(verticalHitTestRect, game, function(tile) {
- if (tile.solid) canMoveVertically = false;
- });
-
- game.level.forEachIntersectingTile(horizontalHitTestRect, game, function(tile) {
- if (tile.solid) canMoveHorizontally = false;
- });
- if (canMoveHorizontally) p.position.x = playerRect.x;
- if (canMoveVertically) p.position.y = playerRect.y;
+ var bounds = p.getBounds(game);
+
+ bounds = tryMove(bounds, { x: game.DISTANCE * p.velocity.dx, y: 0 });
+ bounds = tryMove(bounds, { x: 0, y: game.DISTANCE * p.velocity.dy });
+
+ p.position = bounds.topLeft;
});
}
};
@@ -162,19 +160,14 @@ function KaboomLevel(initialTileMap) {
});
this.spawns = that.spawns;
}
-
- this.forEachIntersectingTile = function(rect, game, callback) {
- $(this.rows).each(function(ri, row) {
- $(row).each(function(ti, tile) {
- tile.isIntersecting = false;
- var bounds = tile.getBounds(game);
- if (bounds.intersects(rect)) {
- tile.isIntersecting = true;
- callback(tile);
- }
- });
- });
- };
+
+ this.forEachTile = function(callback) {
+ for (var ri = 0; ri < this.rows.length; ri++) {
+ for (var ti = 0; ti < this.rows[ri].length; ti++) {
+ callback(this.rows[ri][ti]);
+ }
+ }
+ },
this.parseLevel = function(tileMap) {
var r, c;
@@ -275,7 +268,7 @@ function Spawn(num, x, y) {
this.number = num;
this.position = new Position(x, y);
this.player = null;
-}
+};
function Rectangle(x, y, width, height) {
this.x = this.left = x;
@@ -292,8 +285,8 @@ function Rectangle(x, y, width, height) {
Rectangle.prototype = {
pointIntersects: function(point) {
- return point.x > this.left && point.x < this.right &&
- point.y > this.top && point.y < this.bottom;
+ return point.x >= this.left && point.x <= this.right &&
+ point.y >= this.top && point.y <= this.bottom;
},
intersects: function(that) {
6 public/js/game/kaboom.renderer.js
View
@@ -34,6 +34,12 @@ Tile.prototype.showBoundingBox = function(layer, game) {
} else {
boundingBox.removeClass('intersecting');
}
+
+ if (this.candidate) {
+ boundingBox.addClass('candidate');
+ } else {
+ boundingBox.removeClass('candidate');
+ }
};
function KaboomRenderer(opts) {
Please sign in to comment.
Something went wrong with that request. Please try again.