Skip to content

Commit

Permalink
the player can't walk through walls
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Distad committed Oct 22, 2009
1 parent 14dfbc6 commit 7a2352b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
28 changes: 23 additions & 5 deletions player.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,49 @@
function Player() {
this._x = 30;
this._y = 30;
this._radius = 15;
this._moveDistance = 5;
this._moveTime = 200;
this._avatar = Game.world.grantAvatar(this._x, this._y);
this._avatar = Game.world.grantAvatar(this._x, this._y, this._radius);

this.left = function() {
return this._x - this._radius;
}
this.right = function() {
return this._x + this._radius;
}
this.top = function() {
return this._y - this._radius;
}
this.bottom = function() {
return this._y + this._radius;
}

this.signal = function(event) {
$(this._avatar.node).trigger('player:' + event, this);
}

this.moveLeft = function() {
this._x -= this._moveDistance;
this._avatar.animate({ cx: this._x }, this._moveTime);
this._avatar.node.trigger('player:moveleft')
this.signal('move');
}

this.moveRight = function() {
this._x += this._moveDistance;
this._avatar.animate({ cx: this._x }, this._moveTime);
this._avatar.node.trigger('player:moveright')
this.signal('move');
}

this.moveUp = function() {
this._y -= this._moveDistance;
this._avatar.animate({ cy: this._y }, this._moveTime);
this._avatar.node.trigger('player:moveup')
this.signal('move');
}

this.moveDown = function() {
this._y += this._moveDistance;
this._avatar.animate({ cy: this._y }, this._moveTime);
this._avatar.node.trigger('player:movedown')
this.signal('move');
}
};
21 changes: 18 additions & 3 deletions world.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,22 @@ function World() {
this._height = 400;
this._tile = Raphael('game_window', this._width, this._height);

this.grantAvatar = function(x, y) {
return this._tile.circle(x, y, 15);
this.grantAvatar = function(x, y, radius) {
return this._tile.circle(x, y, radius);
}
};

this.monitor = function(event, callback) {
$(this._tile.canvas).bind(event, callback);
}

this.monitor('player:move', function(event, player) {
if (player.left() < 0)
player.moveRight();
if (player.right() > this._width)
player.moveLeft();
if (player.top() < 0)
player.moveDown();
if (player.bottom() > this._height)
player.moveUp();
})
}

0 comments on commit 7a2352b

Please sign in to comment.