2017-11-17 19:16:39 +00:00
|
|
|
var MapData = require('../mapdata/MapData');
|
|
|
|
var LayerData = require('../mapdata/LayerData');
|
2017-11-11 03:38:42 +00:00
|
|
|
var Formats = require('../Formats');
|
|
|
|
var Tile = require('../Tile');
|
|
|
|
|
2017-11-14 21:02:19 +00:00
|
|
|
var Parse2DArray = function (key, 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
|
|
|
|
});
|
|
|
|
|
|
|
|
var mapData = new MapData({
|
|
|
|
name: key,
|
|
|
|
tileWidth: tileWidth,
|
|
|
|
tileHeight: tileHeight,
|
|
|
|
format: Formats.TILEMAP_2D_ARRAY,
|
|
|
|
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);
|
|
|
|
|
2017-11-17 19:16:39 +00:00
|
|
|
if (Number.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;
|