phaser/src/tilemaps/parsers/ParseCSV.js

43 lines
1.6 KiB
JavaScript
Raw Normal View History

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}
*/
var Formats = require('../Formats');
var Parse2DArray = require('./Parse2DArray');
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
*
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.
* @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.
*
* @return {Phaser.Tilemaps.MapData} The resulting MapData object.
2017-11-29 20:02:45 +00:00
*/
var ParseCSV = function (name, data, tileWidth, tileHeight, insertNull)
{
var array2D = data
.trim()
.split('\n')
.map(function (row) { return row.split(','); });
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;
return map;
};
module.exports = ParseCSV;