Skip to content

Commit fa8178f

Browse files
committed
Port of ImageCollection from v2
1 parent ce28eaa commit fa8178f

3 files changed

Lines changed: 128 additions & 0 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* An Image Collection is a special tileset containing mulitple images, with no slicing into each image.
3+
*
4+
* Image Collections are normally created automatically when Tiled data is loaded.
5+
*
6+
* @class Phaser.ImageCollection
7+
* @constructor
8+
* @param {string} name - The name of the image collection in the map data.
9+
* @param {integer} firstgid - The first image index this image collection contains.
10+
* @param {integer} [width=32] - Width of widest image (in pixels).
11+
* @param {integer} [height=32] - Height of tallest image (in pixels).
12+
* @param {integer} [margin=0] - The margin around all images in the collection (in pixels).
13+
* @param {integer} [spacing=0] - The spacing between each image in the collection (in pixels).
14+
* @param {object} [properties={}] - Custom Image Collection properties.
15+
*/
16+
17+
var Class = require('../../utils/Class');
18+
19+
var ImageCollection = new Class({
20+
21+
initialize:
22+
23+
function ImageCollection (name, firstgid, width, height, margin, spacing, properties)
24+
{
25+
if (width === undefined || width <= 0) { width = 32; }
26+
if (height === undefined || height <= 0) { height = 32; }
27+
if (margin === undefined) { margin = 0; }
28+
if (spacing === undefined) { spacing = 0; }
29+
30+
/**
31+
* The name of the Image Collection.
32+
* @property {string} name
33+
*/
34+
this.name = name;
35+
36+
/**
37+
* The Tiled firstgid value.
38+
* This is the starting index of the first image index this Image Collection contains.
39+
* @property {integer} firstgid
40+
*/
41+
this.firstgid = firstgid | 0;
42+
43+
/**
44+
* The width of the widest image (in pixels).
45+
* @property {integer} imageWidth
46+
* @readonly
47+
*/
48+
this.imageWidth = width | 0;
49+
50+
/**
51+
* The height of the tallest image (in pixels).
52+
* @property {integer} imageHeight
53+
* @readonly
54+
*/
55+
this.imageHeight = height | 0;
56+
57+
/**
58+
* The margin around the images in the collection (in pixels).
59+
* Use `setSpacing` to change.
60+
* @property {integer} imageMarge
61+
* @readonly
62+
*/
63+
// Modified internally
64+
this.imageMargin = margin | 0;
65+
66+
/**
67+
* The spacing between each image in the collection (in pixels).
68+
* Use `setSpacing` to change.
69+
* @property {integer} imageSpacing
70+
* @readonly
71+
*/
72+
this.imageSpacing = spacing | 0;
73+
74+
/**
75+
* Image Collection-specific properties that are typically defined in the Tiled editor.
76+
* @property {object} properties
77+
*/
78+
this.properties = properties || {};
79+
80+
/**
81+
* The cached images that are a part of this collection.
82+
* @property {array} images
83+
* @readonly
84+
*/
85+
// Modified internally
86+
this.images = [];
87+
88+
/**
89+
* The total number of images in the image collection.
90+
* @property {integer} total
91+
* @readonly
92+
*/
93+
// Modified internally
94+
this.total = 0;
95+
},
96+
97+
/**
98+
* Returns true if and only if this image collection contains the given image index.
99+
*
100+
* @method Phaser.ImageCollection#containsImageIndex
101+
* @param {integer} imageIndex - The image index to search for.
102+
* @return {boolean} True if this Image Collection contains the given index.
103+
*/
104+
containsImageIndex: function (imageIndex)
105+
{
106+
return (
107+
imageIndex >= this.firstgid && imageIndex < (this.firstgid + this.total)
108+
);
109+
110+
},
111+
112+
/**
113+
* Add an image to this Image Collection.
114+
*
115+
* @method Phaser.ImageCollection#addImage
116+
* @param {integer} gid - The gid of the image in the Image Collection.
117+
* @param {string} image - The the key of the image in the Image Collection and in the cache.
118+
*/
119+
addImage: function (gid, image)
120+
{
121+
this.images.push({ gid: gid, image: image });
122+
this.total++;
123+
}
124+
});
125+
126+
module.exports = ImageCollection;

v3/src/gameobjects/tilemap/Tilemap.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var Tilemap = new Class({
2828
this.properties = mapData.properties;
2929
this.widthInPixels = mapData.widthInPixels;
3030
this.heightInPixels = mapData.heightInPixels;
31+
this.imageCollections = mapData.imageCollections;
3132
this.layers = mapData.layers;
3233
this.tilesets = mapData.tilesets;
3334
this.tiles = mapData.tiles;

v3/src/gameobjects/tilemap/mapdata/MapData.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var MapData = new Class({
2525
this.objects = GetFastValue(config, 'objects', {});
2626
this.collision = GetFastValue(config, 'collision', {});
2727
this.tilesets = GetFastValue(config, 'tilesets', []);
28+
this.imageCollections = GetFastValue(config, 'imageCollections', []);
2829
this.tiles = GetFastValue(config, 'tiles', []);
2930
}
3031

0 commit comments

Comments
 (0)