phaser/src/tilemaps/parsers/tiled/ParseJSONTiled.js

77 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Formats = require('../../Formats');
var MapData = require('../../mapdata/MapData');
var ParseTileLayers = require('./ParseTileLayers');
var ParseImageLayers = require('./ParseImageLayers');
var ParseTilesets = require('./ParseTilesets');
var ParseObjectLayers = require('./ParseObjectLayers');
var BuildTilesetIndex = require('./BuildTilesetIndex');
var AssignTileProperties = require('./AssignTileProperties');
2018-06-30 07:47:43 +00:00
var GetFastValue = require('../../../utils/object/GetFastValue');
2018-04-16 15:02:27 +00:00
/**
* @namespace Phaser.Tilemaps.Parsers.Tiled
*/
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-03-20 11:36:35 +00:00
* @return {?Phaser.Tilemaps.MapData} [description]
2017-11-29 20:02:45 +00:00
*/
var ParseJSONTiled = function (name, json, insertNull)
{
if (json.orientation !== 'orthogonal')
{
console.warn('Only orthogonal map types are supported in this version of Phaser');
return null;
}
// 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,
tileWidth: json.tilewidth,
tileHeight: json.tileheight,
orientation: json.orientation,
2018-01-18 00:30:22 +00:00
format: Formats.TILED_JSON,
version: json.version,
2018-06-30 07:47:43 +00:00
properties: json.properties,
infinite: GetFastValue(json, 'infinite', false)
});
mapData.layers = ParseTileLayers(json, insertNull);
mapData.images = ParseImageLayers(json);
var sets = ParseTilesets(json);
mapData.tilesets = sets.tilesets;
mapData.imageCollections = sets.imageCollections;
mapData.objects = ParseObjectLayers(json);
mapData.tiles = BuildTilesetIndex(mapData);
AssignTileProperties(mapData);
return mapData;
};
module.exports = ParseJSONTiled;