Permalink
Browse files

First stab at implementing separate H/V collision detection so Marvin…

… can "wallbang"...
  • Loading branch information...
1 parent 4a1a5bd commit 471e09d0e11da7d06893c4d085adeb499b8321b0 @dylanbeattie committed Jun 5, 2011
View
Oops, something went wrong.
Deleted file not rendered
Deleted file not rendered
Deleted file not rendered
@@ -37,6 +37,7 @@ 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,6 +50,7 @@ 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() {
@@ -12,7 +12,7 @@ if (typeof require == "function") {
};
var KaboomGame = function (levelMap) {
- this.level = new Level(levelMap);
+ this.level = new KaboomLevel(levelMap);
this.players = [];
this.DISTANCE = 5;
this.TILE_SIZE = 48;
@@ -23,7 +23,7 @@ KaboomGame.prototype = {
copyStateFrom : function(gameState) {
this.DISTANCE = gameState.DISTANCE;
this.TILE_SIZE = gameState.TILE_SIZE;
- this.level = this.level || new Level();
+ this.level = this.level || new KaboomLevel();
this.level.copyStateFrom(gameState.level);
for (var i = 0; i < gameState.players.length; i++) {
var sourcePlayer = gameState.players[i];
@@ -99,18 +99,28 @@ KaboomGame.prototype = {
y: game.DISTANCE * p.velocity.dy
});
- var hitTestRect = playerRect.contract(4);
- var canMove = true;
+ // 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.left += 8;
+ horizontalHitTestRect.width -= 16;
+
+ var canMoveVertically = true;
+ var canMoveHorizontally = true;
- game.level.forEachIntersectingTile(hitTestRect, game, function(tile) {
- if (tile.solid) {
- canMove = false;
- }
+ game.level.forEachIntersectingTile(verticalHitTestRect, game, function(tile) {
+ if (tile.solid) canMoveVertically = false;
});
-
- if (canMove) {
- p.position = playerRect.topLeft;
- }
+
+ 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;
});
}
};
@@ -125,7 +135,7 @@ TileType = {
Blank: 2
};
-function Level(initialTileMap) {
+function KaboomLevel(initialTileMap) {
/*this.rows = 13;
this.cols = 17;*/
@@ -246,7 +256,7 @@ function Tile (solid, tileType, row, col) {
this.tileType = tileType;
this.row = row;
this.col = col;
-};
+}
Tile.prototype = {
getBounds: function(game) {
@@ -289,7 +299,7 @@ Rectangle.prototype = {
translate: function(point) {
return new Rectangle(this.x + point.x, this.y + point.y, this.width, this.height);
},
-
+
contract: function(amount) {
return new Rectangle(this.x + amount, this.y + amount, this.width - (amount * 2), this.height - (amount * 2));
},
@@ -38,9 +38,9 @@ Tile.prototype.showBoundingBox = function(layer, game) {
function KaboomRenderer(opts) {
var game = opts.game;
- this.itemImages = ["images/solid-block.png",
- "images/destroyable-block.png",
- "images/blank.png" ];
+ this.itemImages = ["images/" + game.TILE_SIZE + "/solid-block.png",
+ "images/" + game.TILE_SIZE + "/destroyable-block.png",
+ "images/" + game.TILE_SIZE + "/blank.png" ];
this.initialise = function() {
var level = game.level;
@@ -91,9 +91,7 @@ KaboomSocket.prototype = {
this.client.playerChangedDirection(msg.playerState);
break;
}
- ;
}
- ;
},
join : function() {
@@ -32,7 +32,7 @@
'*4 ----------- 2*\n' +
'*****************';
- var level = new Level();
+ var level = new KaboomLevel();
level.parseLevel(levelMap);
game = new KaboomGame(level);

0 comments on commit 471e09d

Please sign in to comment.