phaser/src/tilemaps/ImageCollection.js

167 lines
5 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2019-01-15 16:20:22 +00:00
* @copyright 2019 Photon Storm Ltd.
2019-05-10 15:15:04 +00:00
* @license {@link https://opensource.org/licenses/MIT|MIT License}
2018-02-12 16:01:20 +00:00
*/
2018-02-07 17:10:01 +00:00
var Class = require('../utils/Class');
2017-11-24 14:21:09 +00:00
2018-02-07 15:27:21 +00:00
/**
* @classdesc
2018-02-07 21:58:23 +00:00
* An Image Collection is a special Tile Set containing multiple images, with no slicing into each image.
2018-02-07 15:27:21 +00:00
*
* Image Collections are normally created automatically when Tiled data is loaded.
*
2018-02-07 21:58:23 +00:00
* @class ImageCollection
2018-10-10 09:49:13 +00:00
* @memberof Phaser.Tilemaps
2018-02-07 15:27:21 +00:00
* @constructor
2018-02-07 21:58:23 +00:00
* @since 3.0.0
*
2018-02-07 15:27:21 +00:00
* @param {string} name - The name of the image collection in the map data.
* @param {integer} firstgid - The first image index this image collection contains.
* @param {integer} [width=32] - Width of widest image (in pixels).
* @param {integer} [height=32] - Height of tallest image (in pixels).
* @param {integer} [margin=0] - The margin around all images in the collection (in pixels).
* @param {integer} [spacing=0] - The spacing between each image in the collection (in pixels).
* @param {object} [properties={}] - Custom Image Collection properties.
*/
2017-11-24 14:21:09 +00:00
var ImageCollection = new Class({
initialize:
function ImageCollection (name, firstgid, width, height, margin, spacing, properties)
{
if (width === undefined || width <= 0) { width = 32; }
if (height === undefined || height <= 0) { height = 32; }
if (margin === undefined) { margin = 0; }
if (spacing === undefined) { spacing = 0; }
/**
2018-02-07 21:58:23 +00:00
* The name of the Image Collection.
*
* @name Phaser.Tilemaps.ImageCollection#name
* @type {string}
* @since 3.0.0
2017-11-24 14:21:09 +00:00
*/
this.name = name;
/**
2018-02-07 21:58:23 +00:00
* The Tiled firstgid value.
* This is the starting index of the first image index this Image Collection contains.
*
* @name Phaser.Tilemaps.ImageCollection#firstgid
* @type {integer}
* @since 3.0.0
*/
2017-11-24 14:21:09 +00:00
this.firstgid = firstgid | 0;
/**
2018-02-07 21:58:23 +00:00
* The width of the widest image (in pixels).
*
* @name Phaser.Tilemaps.ImageCollection#imageWidth
* @type {integer}
2018-10-09 12:40:00 +00:00
* @readonly
2018-02-07 21:58:23 +00:00
* @since 3.0.0
*/
2017-11-24 14:21:09 +00:00
this.imageWidth = width | 0;
/**
2018-02-07 21:58:23 +00:00
* The height of the tallest image (in pixels).
*
* @name Phaser.Tilemaps.ImageCollection#imageHeight
* @type {integer}
2018-10-09 12:40:00 +00:00
* @readonly
2018-02-07 21:58:23 +00:00
* @since 3.0.0
*/
2017-11-24 14:21:09 +00:00
this.imageHeight = height | 0;
/**
2018-02-07 21:58:23 +00:00
* The margin around the images in the collection (in pixels).
* Use `setSpacing` to change.
*
* @name Phaser.Tilemaps.ImageCollection#imageMarge
* @type {integer}
2018-10-09 12:40:00 +00:00
* @readonly
2018-02-07 21:58:23 +00:00
* @since 3.0.0
*/
2017-11-24 14:21:09 +00:00
this.imageMargin = margin | 0;
/**
2018-02-07 21:58:23 +00:00
* The spacing between each image in the collection (in pixels).
* Use `setSpacing` to change.
*
* @name Phaser.Tilemaps.ImageCollection#imageSpacing
* @type {integer}
2018-10-09 12:40:00 +00:00
* @readonly
2018-02-07 21:58:23 +00:00
* @since 3.0.0
*/
2017-11-24 14:21:09 +00:00
this.imageSpacing = spacing | 0;
/**
2018-02-07 21:58:23 +00:00
* Image Collection-specific properties that are typically defined in the Tiled editor.
*
* @name Phaser.Tilemaps.ImageCollection#properties
* @type {object}
* @since 3.0.0
*/
2017-11-24 14:21:09 +00:00
this.properties = properties || {};
/**
2018-02-07 21:58:23 +00:00
* The cached images that are a part of this collection.
*
* @name Phaser.Tilemaps.ImageCollection#images
* @type {array}
2018-10-09 12:40:00 +00:00
* @readonly
2018-02-07 21:58:23 +00:00
* @since 3.0.0
*/
2017-11-24 14:21:09 +00:00
this.images = [];
/**
2018-02-07 21:58:23 +00:00
* The total number of images in the image collection.
*
* @name Phaser.Tilemaps.ImageCollection#total
* @type {integer}
2018-10-09 12:40:00 +00:00
* @readonly
2018-02-07 21:58:23 +00:00
* @since 3.0.0
*/
2017-11-24 14:21:09 +00:00
this.total = 0;
},
/**
2018-02-07 21:58:23 +00:00
* Returns true if and only if this image collection contains the given image index.
*
* @method Phaser.Tilemaps.ImageCollection#containsImageIndex
* @since 3.0.0
*
* @param {integer} imageIndex - The image index to search for.
*
* @return {boolean} True if this Image Collection contains the given index.
*/
2017-11-24 14:21:09 +00:00
containsImageIndex: function (imageIndex)
{
2018-02-07 21:58:23 +00:00
return (imageIndex >= this.firstgid && imageIndex < (this.firstgid + this.total));
2017-11-24 14:21:09 +00:00
},
/**
2018-02-07 21:58:23 +00:00
* Add an image to this Image Collection.
*
* @method Phaser.Tilemaps.ImageCollection#addImage
* @since 3.0.0
*
* @param {integer} gid - The gid of the image in the Image Collection.
* @param {string} image - The the key of the image in the Image Collection and in the cache.
*
* @return {Phaser.Tilemaps.ImageCollection} This ImageCollection object.
*/
2017-11-24 14:21:09 +00:00
addImage: function (gid, image)
{
this.images.push({ gid: gid, image: image });
this.total++;
2018-02-07 21:58:23 +00:00
return this;
2017-11-24 14:21:09 +00:00
}
2018-02-07 21:58:23 +00:00
2017-11-24 14:21:09 +00:00
});
module.exports = ImageCollection;