2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2022-02-28 14:29:51 +00:00
|
|
|
* @copyright 2022 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
|
|
|
*/
|
|
|
|
|
2020-10-02 11:02:48 +00:00
|
|
|
var AssignTileProperties = require('./AssignTileProperties');
|
|
|
|
var BuildTilesetIndex = require('./BuildTilesetIndex');
|
2020-11-23 15:29:08 +00:00
|
|
|
var CONST = require('../../const/ORIENTATION_CONST');
|
2017-11-24 14:22:55 +00:00
|
|
|
var Formats = require('../../Formats');
|
2020-12-07 10:11:47 +00:00
|
|
|
var FromOrientationString = require('../FromOrientationString');
|
2017-11-24 14:22:55 +00:00
|
|
|
var MapData = require('../../mapdata/MapData');
|
2017-12-01 19:24:37 +00:00
|
|
|
var ParseImageLayers = require('./ParseImageLayers');
|
|
|
|
var ParseObjectLayers = require('./ParseObjectLayers');
|
2020-10-02 11:02:48 +00:00
|
|
|
var ParseTileLayers = require('./ParseTileLayers');
|
|
|
|
var ParseTilesets = require('./ParseTilesets');
|
2017-11-24 14:22:55 +00:00
|
|
|
|
2017-11-29 20:02:45 +00:00
|
|
|
/**
|
|
|
|
* Parses a Tiled JSON object into a new MapData object.
|
|
|
|
*
|
2018-02-10 01:50:48 +00:00
|
|
|
* @function Phaser.Tilemaps.Parsers.Tiled.ParseJSONTiled
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2017-11-29 20:02:45 +00:00
|
|
|
* @param {string} name - The name of the tilemap, used to set the name on the MapData.
|
|
|
|
* @param {object} json - The Tiled JSON object.
|
|
|
|
* @param {boolean} insertNull - Controls how empty tiles, tiles with an index of -1, in the map
|
|
|
|
* data are handled. If `true`, empty locations will get a value of `null`. If `false`, empty
|
|
|
|
* location will get a Tile object with an index of -1. If you've a large sparsely populated map and
|
|
|
|
* the tile data doesn't need to change then setting this value to `true` will help with memory
|
|
|
|
* consumption. However if your map is small or you need to update the tiles dynamically, then leave
|
|
|
|
* the default value set.
|
2018-02-10 01:50:48 +00:00
|
|
|
*
|
2018-10-01 10:35:01 +00:00
|
|
|
* @return {?Phaser.Tilemaps.MapData} The created MapData object, or `null` if the data can't be parsed.
|
2017-11-29 20:02:45 +00:00
|
|
|
*/
|
|
|
|
var ParseJSONTiled = function (name, json, insertNull)
|
2017-11-24 14:22:55 +00:00
|
|
|
{
|
|
|
|
// Map data will consist of: layers, objects, images, tilesets, sizes
|
|
|
|
var mapData = new MapData({
|
|
|
|
width: json.width,
|
|
|
|
height: json.height,
|
2017-11-29 20:02:45 +00:00
|
|
|
name: name,
|
2017-11-24 14:22:55 +00:00
|
|
|
tileWidth: json.tilewidth,
|
|
|
|
tileHeight: json.tileheight,
|
2020-11-23 15:29:08 +00:00
|
|
|
orientation: FromOrientationString(json.orientation),
|
2018-01-18 00:30:22 +00:00
|
|
|
format: Formats.TILED_JSON,
|
2017-11-24 14:22:55 +00:00
|
|
|
version: json.version,
|
2018-06-30 07:47:43 +00:00
|
|
|
properties: json.properties,
|
2018-12-20 16:51:42 +00:00
|
|
|
renderOrder: json.renderorder,
|
|
|
|
infinite: json.infinite
|
2017-11-24 14:22:55 +00:00
|
|
|
});
|
|
|
|
|
2020-09-19 08:56:05 +00:00
|
|
|
if (mapData.orientation === CONST.HEXAGONAL)
|
|
|
|
{
|
|
|
|
mapData.hexSideLength = json.hexsidelength;
|
|
|
|
}
|
|
|
|
|
2017-12-01 19:24:37 +00:00
|
|
|
mapData.layers = ParseTileLayers(json, insertNull);
|
|
|
|
mapData.images = ParseImageLayers(json);
|
2017-11-24 14:22:55 +00:00
|
|
|
|
2017-12-01 19:24:37 +00:00
|
|
|
var sets = ParseTilesets(json);
|
2020-10-02 11:02:48 +00:00
|
|
|
|
2017-12-01 19:24:37 +00:00
|
|
|
mapData.tilesets = sets.tilesets;
|
|
|
|
mapData.imageCollections = sets.imageCollections;
|
2017-11-24 14:22:55 +00:00
|
|
|
|
2018-01-27 14:05:45 +00:00
|
|
|
mapData.objects = ParseObjectLayers(json);
|
2017-11-24 14:22:55 +00:00
|
|
|
|
2017-12-01 19:24:37 +00:00
|
|
|
mapData.tiles = BuildTilesetIndex(mapData);
|
2017-11-24 14:22:55 +00:00
|
|
|
|
2017-12-01 19:24:37 +00:00
|
|
|
AssignTileProperties(mapData);
|
2017-11-24 14:22:55 +00:00
|
|
|
|
|
|
|
return mapData;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ParseJSONTiled;
|