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
|
|
|
*/
|
|
|
|
|
2017-11-11 03:38:42 +00:00
|
|
|
var Formats = require('../Formats');
|
2018-02-09 18:44:16 +00:00
|
|
|
var LayerData = require('../mapdata/LayerData');
|
|
|
|
var MapData = require('../mapdata/MapData');
|
2017-11-11 03:38:42 +00:00
|
|
|
var Tile = require('../Tile');
|
|
|
|
|
2017-11-29 20:02:45 +00:00
|
|
|
/**
|
|
|
|
* Parses a 2D array of tile indexes into a new MapData object with a single layer.
|
|
|
|
*
|
2018-02-10 01:50:48 +00:00
|
|
|
* @function Phaser.Tilemaps.Parsers.Parse2DArray
|
|
|
|
* @since 3.0.0
|
2018-03-19 01:03:17 +00:00
|
|
|
*
|
2017-11-29 20:02:45 +00:00
|
|
|
* @param {string} name - The name of the tilemap, used to set the name on the MapData.
|
2020-11-23 10:24:10 +00:00
|
|
|
* @param {number[][]} data - 2D array, CSV string or Tiled JSON object.
|
2020-11-23 10:22:13 +00:00
|
|
|
* @param {number} tileWidth - The width of a tile in pixels.
|
|
|
|
* @param {number} tileHeight - The height of a tile in pixels.
|
2017-11-29 20:02:45 +00:00
|
|
|
* @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-03-19 01:03:17 +00:00
|
|
|
*
|
2020-04-27 15:13:40 +00:00
|
|
|
* @return {Phaser.Tilemaps.MapData} The MapData object.
|
2017-11-29 20:02:45 +00:00
|
|
|
*/
|
|
|
|
var Parse2DArray = function (name, data, tileWidth, tileHeight, insertNull)
|
2017-11-11 03:38:42 +00:00
|
|
|
{
|
2017-11-17 19:16:39 +00:00
|
|
|
var layerData = new LayerData({
|
|
|
|
tileWidth: tileWidth,
|
|
|
|
tileHeight: tileHeight
|
|
|
|
});
|
2022-02-28 14:29:51 +00:00
|
|
|
|
2017-11-17 19:16:39 +00:00
|
|
|
var mapData = new MapData({
|
2017-11-29 20:02:45 +00:00
|
|
|
name: name,
|
2017-11-17 19:16:39 +00:00
|
|
|
tileWidth: tileWidth,
|
|
|
|
tileHeight: tileHeight,
|
2018-01-18 00:30:22 +00:00
|
|
|
format: Formats.ARRAY_2D,
|
2017-11-17 19:16:39 +00:00
|
|
|
layers: [ layerData ]
|
|
|
|
});
|
|
|
|
|
|
|
|
var tiles = [];
|
2017-11-11 03:38:42 +00:00
|
|
|
var height = data.length;
|
|
|
|
var width = 0;
|
|
|
|
|
|
|
|
for (var y = 0; y < data.length; y++)
|
|
|
|
{
|
2017-11-17 19:16:39 +00:00
|
|
|
tiles[y] = [];
|
2017-11-11 03:38:42 +00:00
|
|
|
var row = data[y];
|
|
|
|
|
|
|
|
for (var x = 0; x < row.length; x++)
|
|
|
|
{
|
|
|
|
var tileIndex = parseInt(row[x], 10);
|
|
|
|
|
2018-06-11 08:05:52 +00:00
|
|
|
if (isNaN(tileIndex) || tileIndex === -1)
|
2017-11-14 21:02:19 +00:00
|
|
|
{
|
2017-11-17 19:16:39 +00:00
|
|
|
tiles[y][x] = insertNull
|
2017-11-14 21:02:19 +00:00
|
|
|
? null
|
2017-11-17 19:16:39 +00:00
|
|
|
: new Tile(layerData, -1, x, y, tileWidth, tileHeight);
|
2017-11-14 21:02:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-17 19:16:39 +00:00
|
|
|
tiles[y][x] = new Tile(layerData, tileIndex, x, y, tileWidth, tileHeight);
|
2017-11-14 21:02:19 +00:00
|
|
|
}
|
2017-11-11 03:38:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (width === 0)
|
|
|
|
{
|
|
|
|
width = row.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 19:16:39 +00:00
|
|
|
mapData.width = layerData.width = width;
|
|
|
|
mapData.height = layerData.height = height;
|
|
|
|
mapData.widthInPixels = layerData.widthInPixels = width * tileWidth;
|
|
|
|
mapData.heightInPixels = layerData.heightInPixels = height * tileHeight;
|
|
|
|
layerData.data = tiles;
|
2017-11-11 03:38:42 +00:00
|
|
|
|
2017-11-17 19:16:39 +00:00
|
|
|
return mapData;
|
2017-11-11 03:38:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Parse2DArray;
|