2013-11-27 16:33:49 +00: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-10-11 03:42:11 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* A Tile set is a combination of an image containing the tiles and collision data per tile.
|
|
|
|
*
|
|
|
|
* @class Phaser.Tileset
|
|
|
|
* @constructor
|
|
|
|
* @param {Image} image - The Image object from the Cache.
|
|
|
|
* @param {string} key - The key of the tileset in the cache.
|
2013-12-17 05:07:00 +00:00
|
|
|
* @param {number} total - The total number of tiles in the tilesheet.
|
2013-11-27 16:33:49 +00:00
|
|
|
* @param {number} tileWidth - The width of the tile in pixels.
|
|
|
|
* @param {number} tileHeight - The height of the tile in pixels.
|
2013-12-17 05:07:00 +00:00
|
|
|
* @param {number} [firstgid=0] - The first index number (as specified by Tiled, otherwise set to zero)
|
|
|
|
* @param {number} [tileMargin=0] - The margin around the tiles in the sheet.
|
|
|
|
* @param {number} [tileSpacing=0] - The spacing between the tiles in the sheet.
|
2013-11-27 16:33:49 +00:00
|
|
|
*/
|
2013-12-17 05:07:00 +00:00
|
|
|
Phaser.Tileset = function (image, key, total, tileWidth, tileHeight, firstgid, tileMargin, tileSpacing) {
|
2013-10-16 20:25:51 +00:00
|
|
|
|
2013-12-17 05:07:00 +00:00
|
|
|
if (typeof firstgid === "undefined") { firstgid = 0; }
|
2013-10-16 20:25:51 +00:00
|
|
|
if (typeof tileMargin === "undefined") { tileMargin = 0; }
|
|
|
|
if (typeof tileSpacing === "undefined") { tileSpacing = 0; }
|
2013-10-11 03:42:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {string} key - The cache ID.
|
|
|
|
*/
|
|
|
|
this.key = key;
|
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* @property {object} image - The image used for rendering.
|
|
|
|
*/
|
2013-10-11 05:30:28 +00:00
|
|
|
this.image = image;
|
|
|
|
|
2013-12-17 05:07:00 +00:00
|
|
|
/**
|
|
|
|
* @property {number} total - The total number of tiles in the tilesheet.
|
|
|
|
*/
|
|
|
|
this.total = total;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} firstgid - The total number of tiles in the tilesheet.
|
|
|
|
*/
|
|
|
|
this.firstgid = firstgid;
|
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* @property {number} tileWidth - The width of a tile in pixels.
|
|
|
|
*/
|
2013-10-11 05:30:28 +00:00
|
|
|
this.tileWidth = tileWidth;
|
2013-11-27 16:33:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} tileHeight - The height of a tile in pixels.
|
|
|
|
*/
|
2013-10-11 03:42:11 +00:00
|
|
|
this.tileHeight = tileHeight;
|
2013-11-27 16:33:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} tileMargin - The margin around the tiles in the sheet.
|
|
|
|
*/
|
2013-12-17 05:07:00 +00:00
|
|
|
this.tileMargin = tileMargin;
|
2013-11-27 16:33:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} tileSpacing - The margin around the tiles in the sheet.
|
|
|
|
*/
|
2013-12-17 05:07:00 +00:00
|
|
|
this.tileSpacing = tileSpacing;
|
2013-10-11 03:42:11 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
2013-12-17 05:07:00 +00:00
|
|
|
* @property {array} tiles - An array of the tile data.
|
2013-11-27 16:33:49 +00:00
|
|
|
*/
|
2013-10-11 05:30:28 +00:00
|
|
|
this.tiles = [];
|
2013-10-11 03:42:11 +00:00
|
|
|
|
2013-12-17 05:07:00 +00:00
|
|
|
this.build();
|
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Phaser.Tileset.prototype = {
|
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
2013-12-17 05:07:00 +00:00
|
|
|
* Builds the tile data
|
2013-11-27 16:33:49 +00:00
|
|
|
*
|
2013-12-17 05:07:00 +00:00
|
|
|
* @method Phaser.Tileset#build
|
2013-11-27 16:33:49 +00:00
|
|
|
*/
|
2013-12-17 05:07:00 +00:00
|
|
|
build: function () {
|
|
|
|
|
|
|
|
var x = this.tileMargin;
|
|
|
|
var y = this.tileMargin;
|
2013-10-11 03:42:11 +00:00
|
|
|
|
2013-12-17 05:07:00 +00:00
|
|
|
for (var i = this.firstgid; i < this.firstgid + this.total; i++)
|
|
|
|
{
|
|
|
|
// Can add extra properties here as needed
|
|
|
|
this.tiles[i] = [x, y];
|
2013-10-11 03:42:11 +00:00
|
|
|
|
2013-12-17 05:07:00 +00:00
|
|
|
x += this.tileWidth + this.tileSpacing;
|
|
|
|
|
|
|
|
if (x === this.image.width)
|
|
|
|
{
|
|
|
|
x = this.tileMargin;
|
|
|
|
y += this.tileHeight + this.tileSpacing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// console.table(this.tiles);
|
2013-10-11 03:42:11 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* Gets a Tile from this set.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tileset#getTile
|
|
|
|
* @param {number} index - The index of the tile within the set.
|
2013-12-17 05:07:00 +00:00
|
|
|
* @return {object} The tile object.
|
2013-11-27 16:33:49 +00:00
|
|
|
*/
|
2013-10-11 03:42:11 +00:00
|
|
|
getTile: function (index) {
|
|
|
|
|
2013-12-17 05:07:00 +00:00
|
|
|
return this.tiles[index];
|
2013-10-11 03:42:11 +00:00
|
|
|
|
2013-12-17 05:07:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a Tile from this set.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tileset#getTileX
|
|
|
|
* @param {number} index - The index of the tile within the set.
|
|
|
|
* @return {object} The tile object.
|
|
|
|
*/
|
|
|
|
getTileX: function (index) {
|
|
|
|
|
|
|
|
return this.tiles[index][0];
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a Tile from this set.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tileset#getTileY
|
|
|
|
* @param {number} index - The index of the tile within the set.
|
|
|
|
* @return {object} The tile object.
|
|
|
|
*/
|
|
|
|
getTileY: function (index) {
|
|
|
|
|
|
|
|
return this.tiles[index][1];
|
2013-10-11 03:42:11 +00:00
|
|
|
|
2013-10-11 05:30:28 +00:00
|
|
|
},
|
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* Sets tile spacing and margins.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tileset#setSpacing
|
|
|
|
* @param {number} [tileMargin] - The margin around the tiles in the sheet.
|
|
|
|
* @param {number} [tileSpacing] - The spacing between the tiles in the sheet.
|
|
|
|
*/
|
2013-10-16 20:25:51 +00:00
|
|
|
setSpacing: function (margin, spacing) {
|
|
|
|
|
|
|
|
this.tileMargin = margin;
|
|
|
|
this.tileSpacing = spacing;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* Checks if the tile at the given index can collide.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tileset#canCollide
|
|
|
|
* @param {number} index - The index of the tile within the set.
|
|
|
|
* @return {boolean} True or false depending on the tile collision or null if no tile was found at the given index.
|
2013-10-15 03:28:16 +00:00
|
|
|
canCollide: function (index) {
|
|
|
|
|
|
|
|
if (this.tiles[index])
|
|
|
|
{
|
|
|
|
return this.tiles[index].collideNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
},
|
2013-12-17 05:07:00 +00:00
|
|
|
*/
|
2013-10-15 03:28:16 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* Checks if the tile at the given index exists.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tileset#checkTileIndex
|
|
|
|
* @param {number} index - The index of the tile within the set.
|
|
|
|
* @return {boolean} True if a tile exists at the given index otherwise false.
|
|
|
|
*/
|
2013-10-11 05:30:28 +00:00
|
|
|
checkTileIndex: function (index) {
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
return (this.tiles[index]);
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-10-15 03:28:16 +00:00
|
|
|
},
|
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* Sets collision values on a range of tiles in the set.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tileset#setCollisionRange
|
|
|
|
* @param {number} start - The index to start setting the collision data on.
|
|
|
|
* @param {number} stop - The index to stop setting the collision data on.
|
|
|
|
* @param {boolean} left - Should the tile collide on the left?
|
|
|
|
* @param {boolean} right - Should the tile collide on the right?
|
|
|
|
* @param {boolean} up - Should the tile collide on the top?
|
|
|
|
* @param {boolean} down - Should the tile collide on the bottom?
|
2013-10-15 03:28:16 +00:00
|
|
|
setCollisionRange: function (start, stop, left, right, up, down) {
|
|
|
|
|
|
|
|
if (this.tiles[start] && this.tiles[stop] && start < stop)
|
|
|
|
{
|
|
|
|
for (var i = start; i <= stop; i++)
|
|
|
|
{
|
|
|
|
this.tiles[i].setCollision(left, right, up, down);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
2013-12-17 05:07:00 +00:00
|
|
|
*/
|
2013-10-15 03:28:16 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* Sets collision values on a tile in the set.
|
|
|
|
*
|
|
|
|
* @method Phaser.Tileset#setCollision
|
|
|
|
* @param {number} index - The index of the tile within the set.
|
|
|
|
* @param {boolean} left - Should the tile collide on the left?
|
|
|
|
* @param {boolean} right - Should the tile collide on the right?
|
|
|
|
* @param {boolean} up - Should the tile collide on the top?
|
|
|
|
* @param {boolean} down - Should the tile collide on the bottom?
|
2013-10-15 03:28:16 +00:00
|
|
|
setCollision: function (index, left, right, up, down) {
|
|
|
|
|
|
|
|
if (this.tiles[index])
|
|
|
|
{
|
|
|
|
this.tiles[index].setCollision(left, right, up, down);
|
|
|
|
}
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-12-17 05:07:00 +00:00
|
|
|
*/
|
2013-10-11 03:42:11 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Tileset#total
|
|
|
|
* @property {number} total - The total number of tiles in this Tileset.
|
|
|
|
* @readonly
|
|
|
|
*/
|
2013-12-17 05:07:00 +00:00
|
|
|
Object.defineProperty(Phaser.Tileset.prototype, "XXXtotal", {
|
2013-10-11 03:42:11 +00:00
|
|
|
|
|
|
|
get: function () {
|
2013-10-11 05:30:28 +00:00
|
|
|
return this.tiles.length;
|
2013-10-11 03:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|