2020-02-02 23:00:14 +01:00
|
|
|
export default class Tilorswift
|
|
|
|
{
|
|
|
|
static getTerrainAsJson(terrain)
|
|
|
|
{
|
|
|
|
let matrix = [];
|
|
|
|
|
|
|
|
for (let y = 0; y < terrain.fields.length; y++) {
|
|
|
|
let row = [];
|
|
|
|
|
|
|
|
for (let x = 0; x < terrain.fields[y].length; x++) {
|
|
|
|
row.push(terrain.fields[y][x].index);
|
|
|
|
}
|
|
|
|
|
|
|
|
matrix.push(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = {
|
|
|
|
tileset: terrain.tileset.image.src,
|
|
|
|
tiles: terrain.tileset.tiles,
|
|
|
|
scale: terrain.tileset.scale,
|
|
|
|
rows: terrain.tilesY,
|
|
|
|
columns: terrain.tilesX,
|
2020-02-04 23:03:33 +01:00
|
|
|
startX: terrain.entranceTileX,
|
|
|
|
startY: terrain.entranceTileY,
|
2020-02-02 23:00:14 +01:00
|
|
|
backgroundColor: terrain.backgroundColor,
|
|
|
|
matrix: matrix,
|
|
|
|
};
|
|
|
|
|
|
|
|
return JSON.stringify(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static saveTerrainToFile(terrain)
|
|
|
|
{
|
2020-02-04 23:03:33 +01:00
|
|
|
if (!terrain.hasEntrancePoint()) {
|
|
|
|
alert('Es muss ein Startpunkt definiert sein!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-04 21:42:14 +01:00
|
|
|
let filename = prompt('Dateiname', 'terrain.json');
|
|
|
|
|
|
|
|
if (filename === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-02 23:00:14 +01:00
|
|
|
let json = Tilorswift.getTerrainAsJson(terrain);
|
|
|
|
let download = document.createElement('a');
|
|
|
|
|
2020-02-04 21:42:14 +01:00
|
|
|
download.setAttribute('download', filename);
|
2020-02-02 23:00:14 +01:00
|
|
|
download.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(json));
|
|
|
|
download.click();
|
|
|
|
download.remove();
|
|
|
|
}
|
|
|
|
}
|