2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2019-01-15 16:20:22 +00:00
|
|
|
* @copyright 2019 Photon Storm Ltd.
|
2018-02-12 16:01:20 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-11-10 02:50:25 +00:00
|
|
|
var Formats = require('../Formats');
|
2017-11-11 03:38:42 +00:00
|
|
|
var Parse2DArray = require('./Parse2DArray');
|
2017-11-10 02:50:25 +00:00
|
|
|
|
2017-11-29 20:02:45 +00:00
|
|
|
/**
|
|
|
|
* Parses a CSV string of tile indexes into a new MapData object with a single layer.
|
|
|
|
*
|
2018-02-10 01:50:48 +00:00
|
|
|
* @function Phaser.Tilemaps.Parsers.ParseCSV
|
|
|
|
* @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.
|
|
|
|
* @param {string} data - CSV string of tile indexes.
|
2017-11-29 21:07:56 +00:00
|
|
|
* @param {integer} tileWidth - The width of a tile in pixels.
|
|
|
|
* @param {integer} 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
|
|
|
*
|
2018-10-01 10:35:01 +00:00
|
|
|
* @return {Phaser.Tilemaps.MapData} The resulting MapData object.
|
2017-11-29 20:02:45 +00:00
|
|
|
*/
|
|
|
|
var ParseCSV = function (name, data, tileWidth, tileHeight, insertNull)
|
2017-11-10 02:50:25 +00:00
|
|
|
{
|
2017-11-11 03:38:42 +00:00
|
|
|
var array2D = data
|
|
|
|
.trim()
|
|
|
|
.split('\n')
|
|
|
|
.map(function (row) { return row.split(','); });
|
2017-11-10 02:50:25 +00:00
|
|
|
|
2017-11-29 20:02:45 +00:00
|
|
|
var map = Parse2DArray(name, array2D, tileWidth, tileHeight, insertNull);
|
2018-01-18 00:30:22 +00:00
|
|
|
map.format = Formats.CSV;
|
2017-11-10 02:50:25 +00:00
|
|
|
|
|
|
|
return map;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ParseCSV;
|