-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Parse.js
64 lines (59 loc) · 2.6 KB
/
Parse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Formats = require('../Formats');
var Parse2DArray = require('./Parse2DArray');
var ParseCSV = require('./ParseCSV');
var ParseJSONTiled = require('./tiled/ParseJSONTiled');
var ParseWeltmeister = require('./impact/ParseWeltmeister');
/**
* Parses raw data of a given Tilemap format into a new MapData object. If no recognized data format
* is found, returns `null`. When loading from CSV or a 2D array, you should specify the tileWidth &
* tileHeight. When parsing from a map from Tiled, the tileWidth & tileHeight will be pulled from
* the map data.
*
* @function Phaser.Tilemaps.Parsers.Parse
* @since 3.0.0
*
* @param {string} name - The name of the tilemap, used to set the name on the MapData.
* @param {number} mapFormat - See ../Formats.js.
* @param {(number[][]|string|object)} data - 2D array, CSV string or Tiled JSON object.
* @param {number} tileWidth - The width of a tile in pixels. Required for 2D array and CSV, but
* ignored for Tiled JSON.
* @param {number} tileHeight - The height of a tile in pixels. Required for 2D array and CSV, but
* ignored for Tiled JSON.
* @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.
*
* @return {Phaser.Tilemaps.MapData} The created `MapData` object.
*/
var Parse = function (name, mapFormat, data, tileWidth, tileHeight, insertNull)
{
var newMap;
switch (mapFormat)
{
case (Formats.ARRAY_2D):
newMap = Parse2DArray(name, data, tileWidth, tileHeight, insertNull);
break;
case (Formats.CSV):
newMap = ParseCSV(name, data, tileWidth, tileHeight, insertNull);
break;
case (Formats.TILED_JSON):
newMap = ParseJSONTiled(name, data, insertNull);
break;
case (Formats.WELTMEISTER):
newMap = ParseWeltmeister(name, data, insertNull);
break;
default:
console.warn('Unrecognized tilemap data format: ' + mapFormat);
newMap = null;
}
return newMap;
};
module.exports = Parse;