2013-10-11 04:42:11 +01:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2013 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-12-18 04:40:10 +00:00
|
|
|
* Create a new `Tile` object.
|
2013-10-11 04:42:11 +01:00
|
|
|
*
|
|
|
|
* @class Phaser.Tile
|
2013-12-18 04:40:10 +00:00
|
|
|
* @classdesc A Tile is a representation of a single tile within the Tilemap.
|
2013-10-11 04:42:11 +01:00
|
|
|
* @constructor
|
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 04:42:11 +01:00
|
|
|
*/
|
2013-12-05 18:12:16 +00:00
|
|
|
Phaser.Tile = function (index, x, y, width, height) {
|
2013-10-11 04:42:11 +01:00
|
|
|
|
|
|
|
/**
|
2013-12-18 04:40:10 +00:00
|
|
|
* @property {number} index - The index of this tile within the map.
|
2013-10-11 04:42:11 +01:00
|
|
|
*/
|
|
|
|
this.index = index;
|
|
|
|
|
2013-12-05 18:12:16 +00:00
|
|
|
/**
|
|
|
|
* @property {number} x - The x map coordinate of this tile.
|
|
|
|
*/
|
|
|
|
this.x = x;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} y - The y map coordinate of this tile.
|
|
|
|
*/
|
|
|
|
this.y = y;
|
|
|
|
|
2013-10-11 04:42:11 +01:00
|
|
|
/**
|
|
|
|
* @property {number} width - The width of the tile in pixels.
|
|
|
|
*/
|
|
|
|
this.width = width;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} height - The height of the tile in pixels.
|
|
|
|
*/
|
|
|
|
this.height = height;
|
|
|
|
|
|
|
|
/**
|
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 04:42:11 +01:00
|
|
|
*/
|
2013-12-05 18:12:16 +00:00
|
|
|
this.alpha = 1;
|
2013-10-11 04:42:11 +01:00
|
|
|
|
|
|
|
/**
|
2013-12-18 04:40:10 +00:00
|
|
|
* @property {object} properties - Tile specific properties.
|
2013-10-11 04:42:11 +01:00
|
|
|
*/
|
2013-12-18 04:40:10 +00:00
|
|
|
this.properties = {};
|
2013-12-05 18:12:16 +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-12-18 04:40:10 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} collides - Does this tile collide at all?
|
|
|
|
*/
|
2013-12-05 18:12:16 +00:00
|
|
|
this.collides = false;
|
|
|
|
|
2013-10-11 04:42:11 +01:00
|
|
|
/**
|
|
|
|
* @property {boolean} collideNone - Indicating this Tile doesn't collide at all.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.collideNone = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} separateX - Enable separation at x-axis.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-12-05 18:12:16 +00:00
|
|
|
// this.separateX = true;
|
2013-10-11 04:42:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} separateY - Enable separation at y-axis.
|
|
|
|
* @default
|
|
|
|
*/
|
2013-12-05 18:12:16 +00:00
|
|
|
// this.separateY = true;
|
2013-10-11 04:42:11 +01:00
|
|
|
|
2013-10-15 04:28:16 +01:00
|
|
|
/**
|
|
|
|
* @property {boolean} collisionCallback - Tilemap collision callback.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.collisionCallback = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} collisionCallback - Tilemap collision callback.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.collisionCallbackContext = this;
|
|
|
|
|
2013-10-11 04:42:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Tile.prototype = {
|
|
|
|
|
2013-10-15 04:28:16 +01:00
|
|
|
/**
|
|
|
|
* Set callback to be called when this tilemap collides.
|
|
|
|
*
|
2013-11-27 16:33:49 +00:00
|
|
|
* @method Phaser.Tile#setCollisionCallback
|
2013-10-15 04:28:16 +01:00
|
|
|
* @param {Function} callback - Callback function.
|
|
|
|
* @param {object} context - Callback will be called with this context.
|
|
|
|
*/
|
|
|
|
setCollisionCallback: function (callback, context) {
|
|
|
|
|
|
|
|
this.collisionCallbackContext = context;
|
|
|
|
this.collisionCallback = callback;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-11 04:42:11 +01:00
|
|
|
/**
|
|
|
|
* Clean up memory.
|
2013-11-27 16:33:49 +00:00
|
|
|
* @method Phaser.Tile#destroy
|
2013-10-11 04:42:11 +01:00
|
|
|
*/
|
|
|
|
destroy: function () {
|
2013-10-11 06:30:28 +01:00
|
|
|
|
2013-12-18 04:40:10 +00:00
|
|
|
this.collisionCallback = null;
|
|
|
|
this.collisionCallbackContext = null;
|
|
|
|
this.properties = null;
|
2013-10-11 06:30:28 +01:00
|
|
|
|
2013-10-11 04:42:11 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-27 16:33:49 +00:00
|
|
|
* Set collision settings on this tile.
|
|
|
|
* @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 04:42:11 +01:00
|
|
|
*/
|
2013-10-14 19:37:44 +01:00
|
|
|
setCollision: function (left, right, up, down) {
|
2013-10-11 04:42:11 +01:00
|
|
|
|
|
|
|
this.collideLeft = left;
|
|
|
|
this.collideRight = right;
|
|
|
|
this.collideUp = up;
|
|
|
|
this.collideDown = down;
|
|
|
|
|
|
|
|
if (left || right || up || down)
|
|
|
|
{
|
|
|
|
this.collideNone = false;
|
|
|
|
}
|
2013-10-14 19:37:44 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
this.collideNone = true;
|
|
|
|
}
|
2013-10-11 04:42:11 +01:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset collision status flags.
|
2013-11-27 16:33:49 +00:00
|
|
|
* @method Phaser.Tile#resetCollision
|
2013-10-11 04:42:11 +01:00
|
|
|
*/
|
|
|
|
resetCollision: function () {
|
|
|
|
|
|
|
|
this.collideNone = true;
|
|
|
|
this.collideLeft = false;
|
|
|
|
this.collideRight = false;
|
|
|
|
this.collideUp = false;
|
|
|
|
this.collideDown = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* @name Phaser.Tile#bottom
|
|
|
|
* @property {number} bottom - The sum of the y and height properties.
|
|
|
|
* @readonly
|
|
|
|
*/
|
2013-10-11 04:42:11 +01:00
|
|
|
Object.defineProperty(Phaser.Tile.prototype, "bottom", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
return this.y + this.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
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 04:42:11 +01:00
|
|
|
Object.defineProperty(Phaser.Tile.prototype, "right", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
return this.x + this.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|