Skip to content

Commit 1f76340

Browse files
committed
Merge pull request phaserjs#1879 from asyed94/dev
Implemented Tiled Image Collection support.
2 parents a89d48b + 6771c65 commit 1f76340

3 files changed

Lines changed: 151 additions & 3 deletions

File tree

src/tilemap/ImageCollection.js

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

src/tilemap/Tilemap.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ Phaser.Tilemap = function (game, key, tileWidth, tileHeight, width, height) {
100100
* @property {array} tilesets - An array of Tilesets.
101101
*/
102102
this.tilesets = data.tilesets;
103+
104+
/**
105+
* @property {array} imagecollections - An array of Image Collections.
106+
*/
107+
this.imagecollections = data.imagecollections;
103108

104109
/**
105110
* @property {array} tiles - The super array of Tiles.

src/tilemap/TilemapParser.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,9 @@ Phaser.TilemapParser = {
376376

377377
map.images = images;
378378

379-
// Tilesets
379+
// Tilesets & Image Collections
380380
var tilesets = [];
381+
var imagecollections = [];
381382

382383
for (var i = 0; i < json.tilesets.length; i++)
383384
{
@@ -400,13 +401,22 @@ Phaser.TilemapParser = {
400401
}
401402
else
402403
{
403-
// TODO: Handle Tileset Image Collections (multiple images in a tileset, no slicing into each image)
404-
console.warn("Phaser.TilemapParser - Image Collection Tilesets are not support");
404+
var newCollection = new Phaser.ImageCollection(set.name, set.firstgid, set.tilewidth, set.tileheight, set.margin, set.spacing, set.properties);
405+
406+
for (var i in set.tiles)
407+
{
408+
var image = set.tiles[i].image;
409+
var gid = set.firstgid + parseInt(i, 10);
410+
newCollection.addImage(gid, image);
411+
}
412+
413+
imagecollections.push(newCollection);
405414
}
406415

407416
}
408417

409418
map.tilesets = tilesets;
419+
map.imagecollections = imagecollections;
410420

411421
// Objects & Collision Data (polylines, etc)
412422
var objects = {};

0 commit comments

Comments
 (0)