62 lines
1.6 KiB
JavaScript
62 lines
1.6 KiB
JavaScript
export default class Tilorswift
|
|
{
|
|
static getTerrainAsJson(terrain)
|
|
{
|
|
console.log('Save da shit.');
|
|
let matrix = [];
|
|
|
|
console.log(terrain.fields.length);
|
|
|
|
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.setId,
|
|
rows: terrain.tilesY,
|
|
columns: terrain.tilesX,
|
|
startX: terrain.entranceTileX,
|
|
startY: terrain.entranceTileY,
|
|
targetX: terrain.targetTileX,
|
|
targetY: terrain.targetTileY,
|
|
matrix: matrix,
|
|
};
|
|
|
|
return JSON.stringify(data);
|
|
}
|
|
|
|
static saveTerrainToFile(terrain)
|
|
{
|
|
if (!terrain.hasEntrancePoint()) {
|
|
alert('Es muss ein Startpunkt definiert sein!');
|
|
return false;
|
|
}
|
|
|
|
if (!terrain.hasTargetPoint()) {
|
|
alert('Es muss ein Zielpunkt definiert sein!');
|
|
return false;
|
|
}
|
|
|
|
let filename = prompt('Dateiname', 'terrain.json');
|
|
|
|
if (filename === null) {
|
|
return false;
|
|
}
|
|
|
|
let json = Tilorswift.getTerrainAsJson(terrain);
|
|
let download = document.createElement('a');
|
|
|
|
download.setAttribute('download', filename);
|
|
download.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(json));
|
|
download.click();
|
|
download.remove();
|
|
|
|
return true;
|
|
}
|
|
} |