2013-11-27 16:33:49 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2014-02-05 05:54:25 +00:00
|
|
|
* @copyright 2014 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.
|
2013-12-18 00:44:04 +00:00
|
|
|
* You should not normally instantiate this class directly.
|
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-03-03 05:19:46 +00:00
|
|
|
* @param {number} firstgid - The Tiled firstgid value. In non-Tiled data this should be considered the starting index value of the first tile in this set.
|
2014-03-23 23:57:46 +00:00
|
|
|
* @param {number} [width=32] - Width of each tile in pixels.
|
|
|
|
* @param {number} [height=32] - Height of each tile in pixels.
|
|
|
|
* @param {number} [margin=0] - The amount of margin around the tilesheet.
|
|
|
|
* @param {number} [spacing=0] - The amount of spacing between each tile in the sheet.
|
|
|
|
* @param {object} [properties] - 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
|
|
|
|
2014-03-23 23:57:46 +00:00
|
|
|
if (typeof width === 'undefined' || width <= 0) { width = 32; }
|
|
|
|
if (typeof height === 'undefined' || height <= 0) { height = 32; }
|
|
|
|
if (typeof margin === 'undefined') { margin = 0; }
|
|
|
|
if (typeof spacing === 'undefined') { spacing = 0; }
|
|
|
|
|
2013-10-11 03:42:11 +00:00
|
|
|
/**
|
2013-12-18 16:56:14 +00:00
|
|
|
* @property {string} name - The name of the Tileset.
|
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-03-03 05:19:46 +00:00
|
|
|
* @property {number} firstgid - The Tiled firstgid value. In non-Tiled data this should be considered the starting index value of the first tile in this set.
|
2013-11-27 16:33:49 +00:00
|
|
|
*/
|
2013-12-18 16:56:14 +00:00
|
|
|
this.firstgid = firstgid;
|
2013-10-11 05:30:28 +00:00
|
|
|
|
2013-12-18 00:44:04 +00:00
|
|
|
/**
|
2013-12-18 16:56:14 +00:00
|
|
|
* @property {number} tileWidth - The width of a tile in pixels.
|
2013-12-18 00:44:04 +00:00
|
|
|
*/
|
2013-12-18 16:56:14 +00:00
|
|
|
this.tileWidth = width;
|
2013-12-18 00:44:04 +00:00
|
|
|
|
|
|
|
/**
|
2013-12-18 16:56:14 +00:00
|
|
|
* @property {number} tileHeight - The height of a tile in pixels.
|
2013-12-18 00:44:04 +00:00
|
|
|
*/
|
2013-12-18 16:56:14 +00:00
|
|
|
this.tileHeight = height;
|
2013-12-18 00:44:04 +00:00
|
|
|
|
2013-12-17 05:07:00 +00:00
|
|
|
/**
|
2014-03-03 05:19:46 +00:00
|
|
|
* @property {number} tileMargin - The margin around the tiles in the tileset.
|
2013-12-17 05:07:00 +00:00
|
|
|
*/
|
2013-12-18 16:56:14 +00:00
|
|
|
this.tileMargin = margin;
|
2013-12-17 05:07:00 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-03 05:19:46 +00:00
|
|
|
* @property {number} tileSpacing - The spacing in pixels between each tile in the tileset.
|
2013-12-17 05:07:00 +00:00
|
|
|
*/
|
2013-12-18 16:56:14 +00:00
|
|
|
this.tileSpacing = spacing;
|
2013-12-17 05:07:00 +00:00
|
|
|
|
2013-11-27 16:33:49 +00:00
|
|
|
/**
|
2013-12-18 16:56:14 +00:00
|
|
|
* @property {object} properties - Tileset specific properties (typically defined in the Tiled editor).
|
2013-11-27 16:33:49 +00:00
|
|
|
*/
|
2013-12-18 16:56:14 +00:00
|
|
|
this.properties = properties;
|
2013-11-27 16:33:49 +00:00
|
|
|
|
|
|
|
/**
|
2014-01-15 13:20:51 +00:00
|
|
|
* @property {object} image - The image used for rendering. This is a reference to the image stored in Phaser.Cache.
|
2013-12-18 16:56:14 +00:00
|
|
|
*/
|
|
|
|
this.image = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} rows - The number of rows in the tile sheet.
|
|
|
|
*/
|
|
|
|
this.rows = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} columns - The number of columns in the tile sheet.
|
|
|
|
*/
|
|
|
|
this.columns = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} total - The total number of tiles in the tilesheet.
|
2013-11-27 16:33:49 +00:00
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* @property {array} draw - The tile drawImage look-up table
|
|
|
|
* @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-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.
|
|
|
|
* @param {number} index - The index of the tile within the set to draw.
|
|
|
|
*/
|
|
|
|
draw: function (context, x, y, index) {
|
|
|
|
|
|
|
|
if (!this.image || !this.drawCoords[index])
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
context.drawImage(
|
|
|
|
this.image,
|
|
|
|
this.drawCoords[index][0],
|
|
|
|
this.drawCoords[index][1],
|
|
|
|
this.tileWidth,
|
|
|
|
this.tileHeight,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
this.tileWidth,
|
|
|
|
this.tileHeight
|
|
|
|
);
|
2013-10-11 03:42:11 +00:00
|
|
|
|
2013-12-17 05:07:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-03-03 05:19:46 +00:00
|
|
|
* Adds a reference from this Tileset to an Image stored in the Phaser.Cache.
|
2013-12-17 05:07:00 +00:00
|
|
|
*
|
2014-03-03 05:19:46 +00:00
|
|
|
* @method Phaser.Tileset#setImage
|
|
|
|
* @param {Image} image - The image this tileset will use to draw with.
|
|
|
|
*/
|
|
|
|
setImage: function (image) {
|
2013-12-17 05:07:00 +00:00
|
|
|
|
2014-03-03 05:19:46 +00:00
|
|
|
this.image = image;
|
2013-12-17 05:07:00 +00:00
|
|
|
|
2014-03-03 05:19:46 +00:00
|
|
|
this.rows = Math.round((image.height - this.tileMargin) / (this.tileHeight + this.tileSpacing));
|
|
|
|
this.columns = Math.round((image.width - this.tileMargin) / (this.tileWidth + this.tileSpacing));
|
|
|
|
this.total = this.rows * this.columns;
|
2013-12-17 05:07:00 +00:00
|
|
|
|
2014-03-03 05:19:46 +00:00
|
|
|
// Create the index look-up
|
|
|
|
this.drawCoords.length = 0;
|
|
|
|
|
|
|
|
var tx = this.tileMargin;
|
|
|
|
var ty = this.tileMargin;
|
|
|
|
var i = this.firstgid;
|
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++)
|
|
|
|
{
|
|
|
|
this.drawCoords[i] = [ tx, ty ];
|
|
|
|
tx += this.tileWidth + this.tileSpacing;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
tx = this.tileMargin;
|
|
|
|
ty += this.tileHeight + this.tileSpacing;
|
|
|
|
}
|
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;
|
|
|
|
|
2014-03-03 05:19:46 +00:00
|
|
|
this.setImage(this.image);
|
2013-10-11 05:30:28 +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;
|