Commit 1f9a5bc9 authored by Zak Stephens's avatar Zak Stephens

Load tiles from map file.

parent 4c4e7119
/* I'm not changing the Array prototype, I don't have the guts. */
module.exports = {
linearToGrid: (linear, rowWidth) => {
const grid = [];
const rows = Math.ceil(linear.length / rowWidth);
for (let row = 0; row < rows; ++row) {
grid[row] = linear.slice(row * rowWidth, (row+1) * rowWidth);
}
return grid;
}
}
......@@ -8,29 +8,31 @@
* assert freely here.) */
const assert = require('assert').strict;
const arrayUtil = require('./array-util');
function loadCollisions(tiledExport, map) {
map.collisions = [];
const collisionLayers = tiledExport.layers.filter(layer => layer.name === 'Collision');
assert(collisionLayers.length < 2, 'multiple collision layers are not supported');
if (collisionLayers.length === 0) {
map.collisions = [];
return;
}
const collisionLayer = collisionLayers[0];
const rows = collisionLayer.data.length / collisionLayer.width;
for (let row = 0; row < rows; ++row) {
map.collisions[row] =
collisionLayer.data
.slice(row * collisionLayer.width, (row+1) * collisionLayer.width)
.map(tile => tile > 0);
}
map.collisions = arrayUtil.linearToGrid(collisionLayer.data, collisionLayer.width)
.map(tile => tile > 0);
}
function loadTiles(tiledLayer, map) {
function loadTiles(tiledExport, map) {
map.tiles = [];
tiledExport.layers
.filter(layer => layer.name !== 'Collision')
.forEach(layer => {
map.tiles.push(arrayUtil.linearToGrid(layer.data, layer.width));
});
}
module.exports = (filePath, gameState) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment