2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2020-01-15 12:07:09 +00:00
|
|
|
* @copyright 2020 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
|
|
|
*/
|
|
|
|
|
2017-12-01 19:24:37 +00:00
|
|
|
var Tileset = require('../../Tileset');
|
|
|
|
var ImageCollection = require('../../ImageCollection');
|
|
|
|
var ParseObject = require('./ParseObject');
|
2020-09-10 20:22:06 +00:00
|
|
|
var ParseWangsets = require('./ParseWangsets');
|
2017-12-01 19:24:37 +00:00
|
|
|
|
2018-02-10 01:50:48 +00:00
|
|
|
/**
|
2019-06-12 11:35:41 +00:00
|
|
|
* Tilesets and Image Collections
|
2018-02-10 01:50:48 +00:00
|
|
|
*
|
|
|
|
* @function Phaser.Tilemaps.Parsers.Tiled.ParseTilesets
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2020-04-27 15:13:40 +00:00
|
|
|
* @param {object} json - The Tiled JSON data.
|
2018-02-10 01:50:48 +00:00
|
|
|
*
|
2020-04-27 15:13:40 +00:00
|
|
|
* @return {object} An object containing the tileset and image collection data.
|
2018-02-10 01:50:48 +00:00
|
|
|
*/
|
2017-12-01 19:24:37 +00:00
|
|
|
var ParseTilesets = function (json)
|
|
|
|
{
|
|
|
|
var tilesets = [];
|
|
|
|
var imageCollections = [];
|
|
|
|
var lastSet = null;
|
|
|
|
var stringID;
|
|
|
|
|
|
|
|
for (var i = 0; i < json.tilesets.length; i++)
|
|
|
|
{
|
|
|
|
// name, firstgid, width, height, margin, spacing, properties
|
|
|
|
var set = json.tilesets[i];
|
|
|
|
|
|
|
|
if (set.source)
|
|
|
|
{
|
2020-10-12 12:52:31 +00:00
|
|
|
console.warn('External tilesets unsupported. Use Embed Tileset and re-export');
|
2017-12-01 19:24:37 +00:00
|
|
|
}
|
|
|
|
else if (set.image)
|
|
|
|
{
|
|
|
|
var newSet = new Tileset(set.name, set.firstgid, set.tilewidth, set.tileheight, set.margin, set.spacing);
|
|
|
|
|
2018-09-26 23:41:11 +00:00
|
|
|
if (json.version > 1)
|
2017-12-01 19:24:37 +00:00
|
|
|
{
|
2020-09-10 20:22:06 +00:00
|
|
|
var datas = undefined;
|
|
|
|
var props = undefined;
|
2018-09-26 23:41:11 +00:00
|
|
|
if (Array.isArray(set.tiles))
|
|
|
|
{
|
2020-09-10 20:22:06 +00:00
|
|
|
datas = datas || {};
|
|
|
|
props = props || {};
|
2018-09-26 23:41:11 +00:00
|
|
|
|
2020-09-10 20:22:06 +00:00
|
|
|
// Tiled 1.2+
|
2018-09-26 23:41:11 +00:00
|
|
|
for (var t = 0; t < set.tiles.length; t++)
|
|
|
|
{
|
|
|
|
var tile = set.tiles[t];
|
|
|
|
|
2020-09-10 20:22:06 +00:00
|
|
|
// Convert tileproperties.
|
2018-09-26 23:41:11 +00:00
|
|
|
if (tile.properties)
|
|
|
|
{
|
|
|
|
var newPropData = {};
|
|
|
|
|
|
|
|
tile.properties.forEach(function (propData)
|
|
|
|
{
|
|
|
|
newPropData[propData['name']] = propData['value'];
|
|
|
|
});
|
|
|
|
|
|
|
|
props[tile.id] = newPropData;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert objectgroup
|
|
|
|
if (tile.objectgroup)
|
|
|
|
{
|
2020-09-10 20:22:06 +00:00
|
|
|
(datas[tile.id] || (datas[tile.id] = {})).objectgroup = tile.objectgroup;
|
2017-12-01 19:24:37 +00:00
|
|
|
|
2018-09-26 23:41:11 +00:00
|
|
|
if (tile.objectgroup.objects)
|
|
|
|
{
|
2020-10-12 12:52:31 +00:00
|
|
|
var parsedObjects2 = tile.objectgroup.objects.map(function (obj)
|
|
|
|
{
|
|
|
|
return ParseObject(obj);
|
|
|
|
});
|
2018-09-26 23:41:11 +00:00
|
|
|
|
2020-09-10 20:22:06 +00:00
|
|
|
datas[tile.id].objectgroup.objects = parsedObjects2;
|
2018-09-26 23:41:11 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-17 13:12:41 +00:00
|
|
|
|
|
|
|
// Copy animation data
|
2018-10-19 12:51:32 +00:00
|
|
|
if (tile.animation)
|
|
|
|
{
|
2020-09-10 20:22:06 +00:00
|
|
|
(datas[tile.id] || (datas[tile.id] = {})).animation = tile.animation;
|
2018-10-17 13:12:41 +00:00
|
|
|
}
|
2018-09-26 23:41:11 +00:00
|
|
|
|
2020-09-10 20:22:06 +00:00
|
|
|
// Copy tile `type` field
|
|
|
|
// (see https://doc.mapeditor.org/en/latest/manual/custom-properties/#typed-tiles).
|
|
|
|
if (tile.type)
|
|
|
|
{
|
|
|
|
(datas[tile.id] || (datas[tile.id] = {})).type = tile.type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Array.isArray(set.wangsets))
|
|
|
|
{
|
|
|
|
datas = datas || {};
|
|
|
|
props = props || {};
|
|
|
|
ParseWangsets(set.wangsets, datas);
|
|
|
|
}
|
|
|
|
if (datas) // Implies also props is set.
|
|
|
|
{
|
|
|
|
newSet.tileData = datas;
|
2018-09-26 23:41:11 +00:00
|
|
|
newSet.tileProperties = props;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2017-12-01 19:24:37 +00:00
|
|
|
{
|
2018-09-26 23:41:11 +00:00
|
|
|
// Tiled 1
|
2017-12-01 19:24:37 +00:00
|
|
|
|
2018-09-26 23:41:11 +00:00
|
|
|
// Properties stored per-tile in object with string indexes starting at "0"
|
|
|
|
if (set.tileproperties)
|
2017-12-01 19:24:37 +00:00
|
|
|
{
|
2018-09-26 23:41:11 +00:00
|
|
|
newSet.tileProperties = set.tileproperties;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Object & terrain shapes stored per-tile in object with string indexes starting at "0"
|
|
|
|
if (set.tiles)
|
|
|
|
{
|
|
|
|
newSet.tileData = set.tiles;
|
|
|
|
|
|
|
|
// Parse the objects into Phaser format to match handling of other Tiled objects
|
|
|
|
for (stringID in newSet.tileData)
|
2017-12-01 19:24:37 +00:00
|
|
|
{
|
2018-09-26 23:41:11 +00:00
|
|
|
var objectGroup = newSet.tileData[stringID].objectgroup;
|
2020-10-12 12:52:31 +00:00
|
|
|
|
2018-09-26 23:41:11 +00:00
|
|
|
if (objectGroup && objectGroup.objects)
|
|
|
|
{
|
2020-10-12 12:52:31 +00:00
|
|
|
var parsedObjects1 = objectGroup.objects.map(function (obj)
|
|
|
|
{
|
|
|
|
return ParseObject(obj);
|
|
|
|
});
|
|
|
|
|
2018-09-26 23:41:11 +00:00
|
|
|
newSet.tileData[stringID].objectgroup.objects = parsedObjects1;
|
|
|
|
}
|
2017-12-01 19:24:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For a normal sliced tileset the row/count/size information is computed when updated.
|
|
|
|
// This is done (again) after the image is set.
|
|
|
|
newSet.updateTileData(set.imagewidth, set.imageheight);
|
|
|
|
|
|
|
|
tilesets.push(newSet);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-12 12:52:31 +00:00
|
|
|
var newCollection = new ImageCollection(set.name, set.firstgid, set.tilewidth, set.tileheight, set.margin, set.spacing, set.properties);
|
2017-12-01 19:24:37 +00:00
|
|
|
|
2020-10-12 12:52:31 +00:00
|
|
|
var maxId = 0;
|
|
|
|
|
|
|
|
for (t = 0; t < set.tiles.length; t++)
|
2017-12-01 19:24:37 +00:00
|
|
|
{
|
2020-10-12 12:52:31 +00:00
|
|
|
tile = set.tiles[t];
|
|
|
|
|
|
|
|
var image = tile.image;
|
|
|
|
var tileId = parseInt(tile.id, 10);
|
|
|
|
var gid = set.firstgid + tileId;
|
2017-12-01 19:24:37 +00:00
|
|
|
newCollection.addImage(gid, image);
|
2020-10-12 12:52:31 +00:00
|
|
|
|
|
|
|
maxId = Math.max(tileId, maxId);
|
2017-12-01 19:24:37 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 12:52:31 +00:00
|
|
|
newCollection.maxId = maxId;
|
|
|
|
|
2017-12-01 19:24:37 +00:00
|
|
|
imageCollections.push(newCollection);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've got a new Tileset, so set the lastgid into the previous one
|
|
|
|
if (lastSet)
|
|
|
|
{
|
|
|
|
lastSet.lastgid = set.firstgid - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastSet = set;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { tilesets: tilesets, imageCollections: imageCollections };
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ParseTilesets;
|