Commit 9dddef01 authored by Terence Martin's avatar Terence Martin

Give Player entity a reference point

This adds a reference point property to the player entity, which it
will use to update its position on screen.

This is done in combination with its map posiition and this reference
position so that we can easily shift the player around and still know
what position it's in.
parent 74392e29
......@@ -1366,6 +1366,8 @@ var nurdz;
// the entity is based on the size of the sprites, so we let the
// callback handle that.
this._sheet = new game.SpriteSheet(stage, "sprites_5_12.png", 5, 12, true, this.setDimensions);
// The default reference point is the upper left corner of the screen.
this._referencePoint = new game.Point(0, 0);
// Set up animations. There are multiple idle and rotate animations,
// and a set for the player and human.
//
......@@ -1412,6 +1414,42 @@ var nurdz;
enumerable: true,
configurable: true
});
Object.defineProperty(Player.prototype, "referencePoint", {
/**
* Get the reference position that sets us as far left as we can be and
* still be in bounds of the maze. This is used to calculate our
* position when it changes.
*
* @returns {Point} the current reference position.
*/
get: function () { return this._referencePoint; },
/**
* Change the reference position that sets us as far left as we can be
* and still be in bounds of the maze.
*
* When this is set to a new value, our position is automatically
* recalculated based on this point and our current mapPosition.
*
* @param {Point} newPoint the new reference point
*/
set: function (newPoint) {
this._referencePoint.setTo(newPoint);
this.updateScreenPosition();
},
enumerable: true,
configurable: true
});
/**
* Update our screen position based on our currently set reference point
* and our map position.
*
* Only the X value of the map position is used to calculate, since we
* never actually enter the maze.
*/
Player.prototype.updateScreenPosition = function () {
this._position.setToXY(this._referencePoint.x +
(this.mapPosition.x * this._width), this._referencePoint.y);
};
/**
* Get an indication as to whether the player is capable of pushing at
* the moment.
......
......@@ -55,6 +55,13 @@ module nurdz.game
*/
private _playerDirection : PlayerDirection;
/**
* This point is the reference for the location that would put us
* visually into the maze at position (0, -1), the position right above
* the top left corner.
*/
private _referencePoint : Point;
/**
* Get the type of the current player; this controls what the player
* looks like.
......@@ -73,6 +80,31 @@ module nurdz.game
get playerDirection () : PlayerDirection
{ return this._playerDirection; }
/**
* Get the reference position that sets us as far left as we can be and
* still be in bounds of the maze. This is used to calculate our
* position when it changes.
*
* @returns {Point} the current reference position.
*/
get referencePoint () : Point
{ return this._referencePoint; }
/**
* Change the reference position that sets us as far left as we can be
* and still be in bounds of the maze.
*
* When this is set to a new value, our position is automatically
* recalculated based on this point and our current mapPosition.
*
* @param {Point} newPoint the new reference point
*/
set referencePoint (newPoint : Point)
{
this._referencePoint.setTo (newPoint);
this.updateScreenPosition ();
}
/**
* Construct a new maze cell that will render on the stage provided and
* which has the entity name provided.
......@@ -96,6 +128,9 @@ module nurdz.game
// callback handle that.
this._sheet = new SpriteSheet (stage, "sprites_5_12.png", 5, 12, true, this.setDimensions);
// The default reference point is the upper left corner of the screen.
this._referencePoint = new Point (0, 0);
// Set up animations. There are multiple idle and rotate animations,
// and a set for the player and human.
//
......@@ -140,6 +175,20 @@ module nurdz.game
this.makeRectangle (sheet.width, sheet.height);
}
/**
* Update our screen position based on our currently set reference point
* and our map position.
*
* Only the X value of the map position is used to calculate, since we
* never actually enter the maze.
*/
private updateScreenPosition () : void
{
this._position.setToXY (this._referencePoint.x +
(this.mapPosition.x * this._width),
this._referencePoint.y);
}
/**
* Get an indication as to whether the player is capable of pushing at
* the moment.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment