2013-10-11 03:42:11 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2014-02-05 05:54:25 +00:00
|
|
|
* @copyright 2014 Photon Storm Ltd.
|
2013-10-11 03:42:11 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-09-16 18:44:04 +00:00
|
|
|
* A Tile is a representation of a single tile within the Tilemap.
|
2013-10-11 03:42:11 +00:00
|
|
|
*
|
|
|
|
* @class Phaser.Tile
|
|
|
|
* @constructor
|
2014-01-14 22:34:41 +00:00
|
|
|
* @param {object} layer - The layer in the Tilemap data that this tile belongs to.
|
2013-11-27 16:33:49 +00:00
|
|
|
* @param {number} index - The index of this tile type in the core map data.
|
|
|
|
* @param {number} x - The x coordinate of this tile.
|
|
|
|
* @param {number} y - The y coordinate of this tile.
|
|
|
|
* @param {number} width - Width of the tile.
|
|
|
|
* @param {number} height - Height of the tile.
|
2013-10-11 03:42:11 +00:00
|
|
|
*/
|
2014-01-14 22:34:41 +00:00
|
|
|
Phaser.Tile = function (layer, index, x, y, width, height) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {object} layer - The layer in the Tilemap data that this tile belongs to.
|
|
|
|
*/
|
|
|
|
this.layer = layer;
|
2013-10-11 03:42:11 +00:00
|
|
|
|
|
|
|
/**
|
2014-04-28 01:42:38 +00:00
|
|
|
* @property {number} index - The index of this tile within the map data corresponding to the tileset, or -1 if this represents a blank/null tile.
|
2013-10-11 03:42:11 +00:00
|
|
|
*/
|
|
|
|
this.index = index;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-12-05 18:12:16 +00:00
|
|
|
/**
|
|
|
|
* @property {number} x - The x map coordinate of this tile.
|
|
|
|
*/
|
|
|
|
this.x = x;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-12-05 18:12:16 +00:00
|
|
|
/**
|
|
|
|
* @property {number} y - The y map coordinate of this tile.
|
|
|
|
*/
|
|
|
|
this.y = y;
|
2015-02-08 20:03:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} rotation - The rotation angle of this tile.
|
|
|
|
*/
|
|
|
|
this.rotation = 0;
|
2013-12-05 18:12:16 +00:00
|
|
|
|
2015-02-08 20:03:19 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} flipped - Whether this tile is flipped (mirrored) or not.
|
|
|
|
*/
|
|
|
|
this.flipped = false;
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* @property {number} x - The x map coordinate of this tile.
|
|
|
|
*/
|
|
|
|
this.worldX = x * width;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* @property {number} y - The y map coordinate of this tile.
|
|
|
|
*/
|
|
|
|
this.worldY = y * height;
|
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
/**
|
|
|
|
* @property {number} width - The width of the tile in pixels.
|
|
|
|
*/
|
|
|
|
this.width = width;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
/**
|
|
|
|
* @property {number} height - The height of the tile in pixels.
|
|
|
|
*/
|
|
|
|
this.height = height;
|
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* @property {number} width - The width of the tile in pixels.
|
|
|
|
*/
|
|
|
|
this.centerX = Math.abs(width / 2);
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-03-06 06:27:16 +00:00
|
|
|
/**
|
|
|
|
* @property {number} height - The height of the tile in pixels.
|
|
|
|
*/
|
|
|
|
this.centerY = Math.abs(height / 2);
|
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
/**
|
2013-12-05 18:12:16 +00:00
|
|
|
* @property {number} alpha - The alpha value at which this tile is drawn to the canvas.
|
2013-10-11 03:42:11 +00:00
|
|
|
*/
|
2013-12-05 18:12:16 +00:00
|
|
|
this.alpha = 1;
|
2013-10-11 03:42:11 +00:00
|
|
|
|
|
|
|
/**
|
2013-12-18 04:40:10 +00:00
|
|
|
* @property {object} properties - Tile specific properties.
|
2013-10-11 03:42:11 +00:00
|
|
|
*/
|
2013-12-18 04:40:10 +00:00
|
|
|
this.properties = {};
|
2013-12-05 18:12:16 +00:00
|
|
|
|
2014-01-28 17:13:07 +00:00
|
|
|
/**
|
2014-01-29 17:10:13 +00:00
|
|
|
* @property {boolean} scanned - Has this tile been walked / turned into a poly?
|
2014-01-28 17:13:07 +00:00
|
|
|
*/
|
2014-01-29 17:10:13 +00:00
|
|
|
this.scanned = false;
|
2014-01-28 17:13:07 +00:00
|
|
|
|
2013-12-18 04:40:10 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} faceTop - Is the top of this tile an interesting edge?
|
|
|
|
*/
|
2013-12-05 18:12:16 +00:00
|
|
|
this.faceTop = false;
|
2013-12-18 04:40:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} faceBottom - Is the bottom of this tile an interesting edge?
|
|
|
|
*/
|
2013-12-05 18:12:16 +00:00
|
|
|
this.faceBottom = false;
|
2013-12-18 04:40:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} faceLeft - Is the left of this tile an interesting edge?
|
|
|
|
*/
|
2013-12-05 18:12:16 +00:00
|
|
|
this.faceLeft = false;
|
2013-12-18 04:40:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} faceRight - Is the right of this tile an interesting edge?
|
|
|
|
*/
|
2013-12-05 18:12:16 +00:00
|
|
|
this.faceRight = false;
|
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} collideLeft - Indicating collide with any object on the left.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.collideLeft = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} collideRight - Indicating collide with any object on the right.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.collideRight = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} collideUp - Indicating collide with any object on the top.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.collideUp = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} collideDown - Indicating collide with any object on the bottom.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.collideDown = false;
|
|
|
|
|
|
|
|
/**
|
2014-02-10 01:49:58 +00:00
|
|
|
* @property {function} collisionCallback - Tile collision callback.
|
2013-10-15 03:28:16 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2014-02-10 01:49:58 +00:00
|
|
|
this.collisionCallback = null;
|
2013-10-15 03:28:16 +00:00
|
|
|
|
|
|
|
/**
|
2014-02-10 01:49:58 +00:00
|
|
|
* @property {object} collisionCallbackContext - The context in which the collision callback will be called.
|
2013-10-15 03:28:16 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2014-02-10 01:49:58 +00:00
|
|
|
this.collisionCallbackContext = this;
|
2013-10-15 03:28:16 +00:00
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Tile.prototype = {
|
|
|
|
|
2014-03-14 02:33:58 +00:00
|
|
|
/**
|
|
|
|
* Check if the given x and y world coordinates are within this Tile.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tile#containsPoint
|
|
|
|
* @param {number} x - The x coordinate to test.
|
|
|
|
* @param {number} y - The y coordinate to test.
|
|
|
|
* @return {boolean} True if the coordinates are within this Tile, otherwise false.
|
|
|
|
*/
|
|
|
|
containsPoint: function (x, y) {
|
|
|
|
|
|
|
|
return !(x < this.worldX || y < this.worldY || x > this.right || y > this.bottom);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-03-13 21:14:10 +00:00
|
|
|
/**
|
|
|
|
* Check for intersection with this tile.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tile#intersects
|
|
|
|
* @param {number} x - The x axis in pixels.
|
|
|
|
* @param {number} y - The y axis in pixels.
|
|
|
|
* @param {number} right - The right point.
|
|
|
|
* @param {number} bottom - The bottom point.
|
|
|
|
*/
|
2014-03-06 06:27:16 +00:00
|
|
|
intersects: function (x, y, right, bottom) {
|
|
|
|
|
|
|
|
if (right <= this.worldX)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bottom <= this.worldY)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x >= this.worldX + this.width)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y >= this.worldY + this.height)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-15 03:28:16 +00:00
|
|
|
/**
|
2014-01-14 22:34:41 +00:00
|
|
|
* Set a callback to be called when this tile is hit by an object.
|
|
|
|
* The callback must true true for collision processing to take place.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2013-11-27 16:33:49 +00:00
|
|
|
* @method Phaser.Tile#setCollisionCallback
|
2014-01-14 22:34:41 +00:00
|
|
|
* @param {function} callback - Callback function.
|
2014-02-10 01:49:58 +00:00
|
|
|
* @param {object} context - Callback will be called within this context.
|
2013-10-15 03:28:16 +00:00
|
|
|
*/
|
|
|
|
setCollisionCallback: function (callback, context) {
|
|
|
|
|
|
|
|
this.collisionCallback = callback;
|
2014-02-10 01:49:58 +00:00
|
|
|
this.collisionCallbackContext = context;
|
2013-10-15 03:28:16 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
/**
|
|
|
|
* Clean up memory.
|
2014-03-13 21:14:10 +00:00
|
|
|
*
|
2013-11-27 16:33:49 +00:00
|
|
|
* @method Phaser.Tile#destroy
|
2013-10-11 03:42:11 +00:00
|
|
|
*/
|
|
|
|
destroy: function () {
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-12-18 04:40:10 +00:00
|
|
|
this.collisionCallback = null;
|
|
|
|
this.collisionCallbackContext = null;
|
|
|
|
this.properties = null;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-08-29 17:03:32 +00:00
|
|
|
* Sets the collision flags for each side of this tile and updates the interesting faces list.
|
2014-03-13 21:14:10 +00:00
|
|
|
*
|
2013-11-27 16:33:49 +00:00
|
|
|
* @method Phaser.Tile#setCollision
|
|
|
|
* @param {boolean} left - Indicating collide with any object on the left.
|
|
|
|
* @param {boolean} right - Indicating collide with any object on the right.
|
|
|
|
* @param {boolean} up - Indicating collide with any object on the top.
|
|
|
|
* @param {boolean} down - Indicating collide with any object on the bottom.
|
2013-10-11 03:42:11 +00:00
|
|
|
*/
|
2013-10-14 18:37:44 +00:00
|
|
|
setCollision: function (left, right, up, down) {
|
2013-10-11 03:42:11 +00:00
|
|
|
|
|
|
|
this.collideLeft = left;
|
|
|
|
this.collideRight = right;
|
|
|
|
this.collideUp = up;
|
|
|
|
this.collideDown = down;
|
|
|
|
|
2014-08-29 17:03:32 +00:00
|
|
|
this.faceLeft = left;
|
|
|
|
this.faceRight = right;
|
|
|
|
this.faceTop = up;
|
|
|
|
this.faceBottom = down;
|
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset collision status flags.
|
2014-03-13 21:14:10 +00:00
|
|
|
*
|
2013-11-27 16:33:49 +00:00
|
|
|
* @method Phaser.Tile#resetCollision
|
2013-10-11 03:42:11 +00:00
|
|
|
*/
|
|
|
|
resetCollision: function () {
|
|
|
|
|
|
|
|
this.collideLeft = false;
|
|
|
|
this.collideRight = false;
|
|
|
|
this.collideUp = false;
|
|
|
|
this.collideDown = false;
|
2014-03-13 21:14:10 +00:00
|
|
|
|
2014-03-04 03:08:35 +00:00
|
|
|
this.faceTop = false;
|
|
|
|
this.faceBottom = false;
|
|
|
|
this.faceLeft = false;
|
|
|
|
this.faceRight = false;
|
2013-10-11 03:42:11 +00:00
|
|
|
|
2014-01-14 00:31:58 +00:00
|
|
|
},
|
|
|
|
|
2014-03-13 22:49:08 +00:00
|
|
|
/**
|
|
|
|
* Is this tile interesting?
|
|
|
|
*
|
|
|
|
* @method Phaser.Tile#isInteresting
|
|
|
|
* @param {boolean} collides - If true will check any collides value.
|
|
|
|
* @param {boolean} faces - If true will check any face value.
|
|
|
|
* @return {boolean} True if the Tile is interesting, otherwise false.
|
|
|
|
*/
|
|
|
|
isInteresting: function (collides, faces) {
|
|
|
|
|
|
|
|
if (collides && faces)
|
|
|
|
{
|
2014-03-14 04:21:56 +00:00
|
|
|
// Does this tile have any collide flags OR interesting face?
|
2014-03-16 00:53:50 +00:00
|
|
|
return (this.collideLeft || this.collideRight || this.collideUp || this.collideDown || this.faceTop || this.faceBottom || this.faceLeft || this.faceRight || this.collisionCallback);
|
2014-03-13 22:49:08 +00:00
|
|
|
}
|
|
|
|
else if (collides)
|
|
|
|
{
|
|
|
|
// Does this tile collide?
|
|
|
|
return (this.collideLeft || this.collideRight || this.collideUp || this.collideDown);
|
|
|
|
}
|
|
|
|
else if (faces)
|
|
|
|
{
|
|
|
|
// Does this tile have an interesting face?
|
|
|
|
return (this.faceTop || this.faceBottom || this.faceLeft || this.faceRight);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-01-14 00:31:58 +00:00
|
|
|
/**
|
|
|
|
* Copies the tile data and properties from the given tile to this tile.
|
2014-03-13 21:14:10 +00:00
|
|
|
*
|
2014-01-14 00:31:58 +00:00
|
|
|
* @method Phaser.Tile#copy
|
|
|
|
* @param {Phaser.Tile} tile - The tile to copy from.
|
|
|
|
*/
|
|
|
|
copy: function (tile) {
|
|
|
|
|
|
|
|
this.index = tile.index;
|
|
|
|
this.alpha = tile.alpha;
|
|
|
|
this.properties = tile.properties;
|
2014-03-13 21:14:10 +00:00
|
|
|
|
2014-01-14 00:31:58 +00:00
|
|
|
this.collideUp = tile.collideUp;
|
|
|
|
this.collideDown = tile.collideDown;
|
|
|
|
this.collideLeft = tile.collideLeft;
|
|
|
|
this.collideRight = tile.collideRight;
|
2014-03-13 21:14:10 +00:00
|
|
|
|
2014-01-14 00:31:58 +00:00
|
|
|
this.collisionCallback = tile.collisionCallback;
|
|
|
|
this.collisionCallbackContext = tile.collisionCallbackContext;
|
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
Phaser.Tile.prototype.constructor = Phaser.Tile;
|
|
|
|
|
2014-03-13 21:14:10 +00:00
|
|
|
/**
|
2014-04-14 12:52:53 +00:00
|
|
|
* @name Phaser.Tile#collides
|
|
|
|
* @property {boolean} collides - True if this tile can collide on any of its faces.
|
2014-03-13 21:14:10 +00:00
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Tile.prototype, "collides", {
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-03-13 21:14:10 +00:00
|
|
|
get: function () {
|
|
|
|
return (this.collideLeft || this.collideRight || this.collideUp || this.collideDown);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-01-14 22:34:41 +00:00
|
|
|
/**
|
|
|
|
* @name Phaser.Tile#canCollide
|
2014-04-14 12:52:53 +00:00
|
|
|
* @property {boolean} canCollide - True if this tile can collide on any of its faces or has a collision callback set.
|
2014-01-14 22:34:41 +00:00
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Tile.prototype, "canCollide", {
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-01-14 22:34:41 +00:00
|
|
|
get: function () {
|
2014-03-16 00:53:50 +00:00
|
|
|
return (this.collideLeft || this.collideRight || this.collideUp || this.collideDown || this.collisionCallback);
|
2014-01-14 22:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
2014-01-28 06:52:56 +00:00
|
|
|
* @name Phaser.Tile#left
|
2014-03-13 21:14:10 +00:00
|
|
|
* @property {number} left - The x value in pixels.
|
2013-11-27 16:33:49 +00:00
|
|
|
* @readonly
|
|
|
|
*/
|
2014-01-28 06:52:56 +00:00
|
|
|
Object.defineProperty(Phaser.Tile.prototype, "left", {
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
get: function () {
|
2014-03-13 21:14:10 +00:00
|
|
|
return this.worldX;
|
2013-10-11 03:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* @name Phaser.Tile#right
|
|
|
|
* @property {number} right - The sum of the x and width properties.
|
|
|
|
* @readonly
|
|
|
|
*/
|
2013-10-11 03:42:11 +00:00
|
|
|
Object.defineProperty(Phaser.Tile.prototype, "right", {
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
get: function () {
|
2014-03-13 21:14:10 +00:00
|
|
|
return this.worldX + this.width;
|
2013-10-11 03:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2014-01-28 06:52:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Tile#top
|
|
|
|
* @property {number} top - The y value.
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Tile.prototype, "top", {
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-01-28 06:52:56 +00:00
|
|
|
get: function () {
|
2014-03-13 21:14:10 +00:00
|
|
|
return this.worldY;
|
2014-01-28 06:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Tile#bottom
|
|
|
|
* @property {number} bottom - The sum of the y and height properties.
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Tile.prototype, "bottom", {
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-01-28 06:52:56 +00:00
|
|
|
get: function () {
|
2014-03-13 21:14:10 +00:00
|
|
|
return this.worldY + this.height;
|
2014-01-28 06:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|