Permalink
Browse files

the player can't walk through walls

  • Loading branch information...
1 parent 14dfbc6 commit 7a2352b3bc9f0f42f6af783e1f4c98c2c8a84fb0 Jon Distad committed Oct 22, 2009
Showing with 41 additions and 8 deletions.
  1. +23 −5 player.js
  2. +18 −3 world.js
View
@@ -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');
}
};
View
@@ -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.