2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2015-02-25 03:36:23 +00:00
|
|
|
* @copyright 2015 Photon Storm Ltd.
|
2013-11-27 16:33:49 +00:00
|
|
|
* @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.
|
2014-10-25 00:08:20 +00:00
|
|
|
*
|
|
|
|
* Tilesets are normally created automatically when Tiled data is loaded.
|
2013-11-27 16:33:49 +00:00
|
|
|
*
|
|
|
|
* @class Phaser.Tileset
|
|
|
|
* @constructor
|
2013-12-18 16:56:14 +00:00
|
|
|
* @param {string} name - The name of the tileset in the map data.
|
2014-10-25 00:08:20 +00:00
|
|
|
* @param {integer} firstgid - The first tile index this tileset contains.
|
|
|
|
* @param {integer} [width=32] - Width of each tile (in pixels).
|
|
|
|
* @param {integer} [height=32] - Height of each tile (in pixels).
|
|
|
|
* @param {integer} [margin=0] - The margin around all tiles in the sheet (in pixels).
|
|
|
|
* @param {integer} [spacing=0] - The spacing between each tile in the sheet (in pixels).
|
|
|
|
* @param {object} [properties={}] - Custom Tileset properties.
|
2013-11-27 16:33:49 +00:00
|
|
|
*/
|
2013-12-18 16:56:14 +00:00
|
|
|
Phaser.Tileset = function (name, firstgid, width, height, margin, spacing, properties) {
|
2013-10-11 03:42:11 +00:00
|
|
|
|
2015-07-22 09:37:15 +00:00
|
|
|
if (width === undefined || width <= 0) { width = 32; }
|
|
|
|
if (height === undefined || height <= 0) { height = 32; }
|
|
|
|
if (margin === undefined) { margin = 0; }
|
|
|
|
if (spacing === undefined) { spacing = 0; }
|
2014-03-23 23:57:46 +00:00
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
/**
|
2014-10-25 00:08:20 +00:00
|
|
|
* The name of the Tileset.
|
2014-10-29 05:21:47 +00:00
|
|
|
* @property {string} name
|
2013-10-11 03:42:11 +00:00
|
|
|
*/
|
2013-12-18 16:56:14 +00:00
|
|
|
this.name = name;
|
2013-10-11 03:42:11 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
2014-10-25 00:08:20 +00:00
|
|
|
* The Tiled firstgid value.
|
|
|
|
* This is the starting index of the first tile index this Tileset contains.
|
2014-10-29 05:21:47 +00:00
|
|
|
* @property {integer} firstgid
|
2013-11-27 16:33:49 +00:00
|
|
|
*/
|
2014-10-25 00:08:20 +00:00
|
|
|
this.firstgid = firstgid | 0;
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-12-18 00:44:04 +00:00
|
|
|
/**
|
2014-10-25 00:08:20 +00:00
|
|
|
* The width of each tile (in pixels).
|
2014-10-29 05:21:47 +00:00
|
|
|
* @property {integer} tileWidth
|
|
|
|
* @readonly
|
2013-12-18 00:44:04 +00:00
|
|
|
*/
|
2014-10-25 00:08:20 +00:00
|
|
|
this.tileWidth = width | 0;
|
2013-12-18 00:44:04 +00:00
|
|
|
|
|
|
|
/**
|
2014-10-25 00:08:20 +00:00
|
|
|
* The height of each tile (in pixels).
|
2014-10-29 05:21:47 +00:00
|
|
|
* @property {integer} tileHeight
|
|
|
|
* @readonly
|
2013-12-18 00:44:04 +00:00
|
|
|
*/
|
2014-10-25 00:08:20 +00:00
|
|
|
this.tileHeight = height | 0;
|
2013-12-18 00:44:04 +00:00
|
|
|
|
2013-12-17 05:07:00 +00:00
|
|
|
/**
|
2014-10-25 00:08:20 +00:00
|
|
|
* The margin around the tiles in the sheet (in pixels).
|
2014-10-29 05:21:47 +00:00
|
|
|
* Use `setSpacing` to change.
|
|
|
|
* @property {integer} tileMarge
|
|
|
|
* @readonly
|
2013-12-17 05:07:00 +00:00
|
|
|
*/
|
2014-10-29 05:21:47 +00:00
|
|
|
// Modified internally
|
2014-10-25 00:08:20 +00:00
|
|
|
this.tileMargin = margin | 0;
|
2013-12-17 05:07:00 +00:00
|
|
|
|
|
|
|
/**
|
2014-10-25 00:08:20 +00:00
|
|
|
* The spacing between each tile in the sheet (in pixels).
|
2014-10-29 05:21:47 +00:00
|
|
|
* Use `setSpacing` to change.
|
|
|
|
* @property {integer} tileSpacing
|
2014-11-27 03:42:42 +00:00
|
|
|
* @readonly
|
2013-12-17 05:07:00 +00:00
|
|
|
*/
|
2014-10-25 00:08:20 +00:00
|
|
|
this.tileSpacing = spacing | 0;
|
2013-12-17 05:07:00 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
2014-10-25 00:08:20 +00:00
|
|
|
* Tileset-specific properties that are typically defined in the Tiled editor.
|
2014-10-29 05:21:47 +00:00
|
|
|
* @property {object} properties
|
2013-11-27 16:33:49 +00:00
|
|
|
*/
|
2014-10-25 00:08:20 +00:00
|
|
|
this.properties = properties || {};
|
2013-11-27 16:33:49 +00:00
|
|
|
|
|
|
|
/**
|
2014-11-27 03:42:42 +00:00
|
|
|
* The cached image that contains the individual tiles. Use {@link Phaser.Tileset.setImage setImage} to set.
|
2014-10-29 05:21:47 +00:00
|
|
|
* @property {?object} image
|
|
|
|
* @readonly
|
2013-12-18 16:56:14 +00:00
|
|
|
*/
|
2014-10-25 00:08:20 +00:00
|
|
|
// Modified internally
|
2013-12-18 16:56:14 +00:00
|
|
|
this.image = null;
|
|
|
|
|
|
|
|
/**
|
2014-11-27 03:42:42 +00:00
|
|
|
* The number of tile rows in the the tileset.
|
2014-10-29 05:21:47 +00:00
|
|
|
* @property {integer}
|
2014-10-25 00:08:20 +00:00
|
|
|
* @readonly
|
2013-12-18 16:56:14 +00:00
|
|
|
*/
|
2014-10-25 00:08:20 +00:00
|
|
|
// Modified internally
|
2013-12-18 16:56:14 +00:00
|
|
|
this.rows = 0;
|
|
|
|
|
|
|
|
/**
|
2014-11-27 03:42:42 +00:00
|
|
|
* The number of tile columns in the tileset.
|
2014-10-29 05:21:47 +00:00
|
|
|
* @property {integer} columns
|
2014-10-25 00:08:20 +00:00
|
|
|
* @readonly
|
2013-12-18 16:56:14 +00:00
|
|
|
*/
|
2014-10-25 00:08:20 +00:00
|
|
|
// Modified internally
|
2013-12-18 16:56:14 +00:00
|
|
|
this.columns = 0;
|
|
|
|
|
|
|
|
/**
|
2014-11-27 03:42:42 +00:00
|
|
|
* The total number of tiles in the tileset.
|
2014-10-29 05:21:47 +00:00
|
|
|
* @property {integer} total
|
2014-10-25 00:08:20 +00:00
|
|
|
* @readonly
|
2013-11-27 16:33:49 +00:00
|
|
|
*/
|
2014-10-25 00:08:20 +00:00
|
|
|
// Modified internally
|
2013-12-18 16:56:14 +00:00
|
|
|
this.total = 0;
|
2013-10-11 03:42:11 +00:00
|
|
|
|
2014-03-03 05:19:46 +00:00
|
|
|
/**
|
2014-10-25 00:08:20 +00:00
|
|
|
* The look-up table to specific tile image offsets.
|
|
|
|
* The coordinates are interlaced such that it is [x0, y0, x1, y1 .. xN, yN] and the tile with the index of firstgid is found at indices 0/1.
|
2014-10-29 05:21:47 +00:00
|
|
|
* @property {integer[]} drawCoords
|
2014-03-03 05:19:46 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.drawCoords = [];
|
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
};
|
2013-10-11 03:42:11 +00:00
|
|
|
|
|
|
|
Phaser.Tileset.prototype = {
|
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
2014-03-03 05:19:46 +00:00
|
|
|
* Draws a tile from this Tileset at the given coordinates on the context.
|
2013-11-27 16:33:49 +00:00
|
|
|
*
|
2014-03-03 05:19:46 +00:00
|
|
|
* @method Phaser.Tileset#draw
|
2014-10-30 09:00:20 +00:00
|
|
|
* @public
|
2014-10-21 21:43:42 +00:00
|
|
|
* @param {CanvasRenderingContext2D} context - The context to draw the tile onto.
|
2014-03-03 05:19:46 +00:00
|
|
|
* @param {number} x - The x coordinate to draw to.
|
|
|
|
* @param {number} y - The y coordinate to draw to.
|
2014-10-25 00:08:20 +00:00
|
|
|
* @param {integer} index - The index of the tile within the set to draw.
|
2014-03-03 05:19:46 +00:00
|
|
|
*/
|
|
|
|
draw: function (context, x, y, index) {
|
|
|
|
|
2014-10-25 00:08:20 +00:00
|
|
|
// Correct the tile index for the set and bias for interlacing
|
|
|
|
var coordIndex = (index - this.firstgid) << 1;
|
|
|
|
|
|
|
|
if (coordIndex >= 0 && (coordIndex + 1) < this.drawCoords.length)
|
2014-03-03 05:19:46 +00:00
|
|
|
{
|
2014-10-25 00:08:20 +00:00
|
|
|
context.drawImage(
|
|
|
|
this.image,
|
|
|
|
this.drawCoords[coordIndex],
|
|
|
|
this.drawCoords[coordIndex + 1],
|
|
|
|
this.tileWidth,
|
|
|
|
this.tileHeight,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
this.tileWidth,
|
|
|
|
this.tileHeight
|
|
|
|
);
|
2014-03-03 05:19:46 +00:00
|
|
|
}
|
|
|
|
|
2014-10-25 00:08:20 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if and only if this tileset contains the given tile index.
|
|
|
|
*
|
2014-10-30 09:00:20 +00:00
|
|
|
* @method Phaser.Tileset#containsTileIndex
|
2014-10-25 00:08:20 +00:00
|
|
|
* @public
|
|
|
|
* @return {boolean} True if this tileset contains the given index.
|
|
|
|
*/
|
2015-03-05 14:02:04 +00:00
|
|
|
containsTileIndex: function (tileIndex) {
|
2014-10-25 00:08:20 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
tileIndex >= this.firstgid &&
|
|
|
|
tileIndex < (this.firstgid + this.total)
|
2014-03-03 05:19:46 +00:00
|
|
|
);
|
2013-10-11 03:42:11 +00:00
|
|
|
|
2013-12-17 05:07:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-10-25 00:08:20 +00:00
|
|
|
* Set the image associated with this Tileset and update the tile data.
|
2013-12-17 05:07:00 +00:00
|
|
|
*
|
2014-10-30 09:00:20 +00:00
|
|
|
* @method Phaser.Tileset#setImage
|
2014-10-25 00:08:20 +00:00
|
|
|
* @public
|
|
|
|
* @param {Image} image - The image that contains the tiles.
|
2014-03-03 05:19:46 +00:00
|
|
|
*/
|
|
|
|
setImage: function (image) {
|
2013-12-17 05:07:00 +00:00
|
|
|
|
2014-03-03 05:19:46 +00:00
|
|
|
this.image = image;
|
2014-11-27 03:42:42 +00:00
|
|
|
this.updateTileData(image.width, image.height);
|
2014-10-25 00:08:20 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets tile spacing and margins.
|
|
|
|
*
|
2014-10-30 09:00:20 +00:00
|
|
|
* @method Phaser.Tileset#setSpacing
|
2014-10-25 00:08:20 +00:00
|
|
|
* @public
|
2015-07-07 01:18:53 +00:00
|
|
|
* @param {integer} [margin=0] - The margin around the tiles in the sheet (in pixels).
|
|
|
|
* @param {integer} [spacing=0] - The spacing between the tiles in the sheet (in pixels).
|
2014-10-25 00:08:20 +00:00
|
|
|
*/
|
|
|
|
setSpacing: function (margin, spacing) {
|
|
|
|
|
|
|
|
this.tileMargin = margin | 0;
|
|
|
|
this.tileSpacing = spacing | 0;
|
|
|
|
|
2014-11-27 03:42:42 +00:00
|
|
|
if (this.image)
|
|
|
|
{
|
|
|
|
this.updateTileData(this.image.width, this.image.height);
|
|
|
|
}
|
2014-10-25 00:08:20 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates tile coordinates and tileset data.
|
2014-10-30 09:00:20 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Tileset#updateTileData
|
|
|
|
* @private
|
2014-11-27 03:42:42 +00:00
|
|
|
* @param {integer} imageWidth - The (expected) width of the image to slice.
|
|
|
|
* @param {integer} imageHeight - The (expected) height of the image to slice.
|
2014-10-25 00:08:20 +00:00
|
|
|
*/
|
2014-11-27 03:42:42 +00:00
|
|
|
updateTileData: function (imageWidth, imageHeight) {
|
|
|
|
|
|
|
|
// May be fractional values
|
2015-02-26 04:34:41 +00:00
|
|
|
var rowCount = (imageHeight - this.tileMargin * 2 + this.tileSpacing) / (this.tileHeight + this.tileSpacing);
|
|
|
|
var colCount = (imageWidth - this.tileMargin * 2 + this.tileSpacing) / (this.tileWidth + this.tileSpacing);
|
2014-11-27 03:42:42 +00:00
|
|
|
|
|
|
|
if (rowCount % 1 !== 0 || colCount % 1 !== 0)
|
|
|
|
{
|
|
|
|
console.warn("Phaser.Tileset - image tile area is not an even multiple of tile size");
|
|
|
|
}
|
|
|
|
|
|
|
|
// In Tiled a tileset image that is not an even multiple of the tile dimensions
|
|
|
|
// is truncated - hence the floor when calculating the rows/columns.
|
|
|
|
rowCount = Math.floor(rowCount);
|
|
|
|
colCount = Math.floor(colCount);
|
|
|
|
|
|
|
|
if ((this.rows && this.rows !== rowCount) || (this.columns && this.columns !== colCount))
|
|
|
|
{
|
|
|
|
console.warn("Phaser.Tileset - actual and expected number of tile rows and columns differ");
|
|
|
|
}
|
2013-12-17 05:07:00 +00:00
|
|
|
|
2014-11-27 03:42:42 +00:00
|
|
|
this.rows = rowCount;
|
|
|
|
this.columns = colCount;
|
|
|
|
this.total = rowCount * colCount;
|
2013-12-17 05:07:00 +00:00
|
|
|
|
2014-03-03 05:19:46 +00:00
|
|
|
this.drawCoords.length = 0;
|
|
|
|
|
|
|
|
var tx = this.tileMargin;
|
|
|
|
var ty = this.tileMargin;
|
2013-12-17 05:07:00 +00:00
|
|
|
|
2014-03-03 05:19:46 +00:00
|
|
|
for (var y = 0; y < this.rows; y++)
|
|
|
|
{
|
|
|
|
for (var x = 0; x < this.columns; x++)
|
|
|
|
{
|
2014-10-25 00:08:20 +00:00
|
|
|
this.drawCoords.push(tx);
|
|
|
|
this.drawCoords.push(ty);
|
2014-03-03 05:19:46 +00:00
|
|
|
tx += this.tileWidth + this.tileSpacing;
|
|
|
|
}
|
|
|
|
|
|
|
|
tx = this.tileMargin;
|
|
|
|
ty += this.tileHeight + this.tileSpacing;
|
|
|
|
}
|
2013-10-11 03:42:11 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-10-11 03:42:11 +00:00
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Tileset.prototype.constructor = Phaser.Tileset;
|