2020-02-02 23:00:14 +01:00
|
|
|
export default class Tilorswift
|
|
|
|
{
|
2020-02-16 00:53:03 +01:00
|
|
|
static getLevelAsJson(level)
|
2020-02-02 23:00:14 +01:00
|
|
|
{
|
|
|
|
let matrix = [];
|
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
for (let y = 0; y < level.terrain.fields.length; y++) {
|
2020-02-02 23:00:14 +01:00
|
|
|
let row = [];
|
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
for (let x = 0; x < level.terrain.fields[y].length; x++) {
|
|
|
|
row.push(level.terrain.fields[y][x].index);
|
2020-02-02 23:00:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
matrix.push(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = {
|
2020-02-16 00:53:03 +01:00
|
|
|
tileset: level.getTilesetId(),
|
|
|
|
rows: level.getRows(),
|
|
|
|
columns: level.getColumns(),
|
|
|
|
startX: level.getStartX(),
|
|
|
|
startY: level.getStartY(),
|
|
|
|
targetX: level.getTargetX(),
|
|
|
|
targetY: level.getTargetY(),
|
|
|
|
gravity: level.gravity,
|
2020-02-02 23:00:14 +01:00
|
|
|
matrix: matrix,
|
|
|
|
};
|
|
|
|
|
|
|
|
return JSON.stringify(data);
|
|
|
|
}
|
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
static saveLevelToFile(level)
|
2020-02-02 23:00:14 +01:00
|
|
|
{
|
2020-02-16 00:53:03 +01:00
|
|
|
if (!level.hasEntrancePoint()) {
|
2020-02-04 23:03:33 +01:00
|
|
|
alert('Es muss ein Startpunkt definiert sein!');
|
2020-02-09 00:29:46 +01:00
|
|
|
return false;
|
2020-02-04 23:03:33 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
if (!level.hasTargetPoint()) {
|
2020-02-11 21:20:11 +01:00
|
|
|
alert('Es muss ein Zielpunkt definiert sein!');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-04 21:42:14 +01:00
|
|
|
let filename = prompt('Dateiname', 'terrain.json');
|
|
|
|
|
|
|
|
if (filename === null) {
|
2020-02-09 00:29:46 +01:00
|
|
|
return false;
|
2020-02-04 21:42:14 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
let json = Tilorswift.getLevelAsJson(level);
|
2020-02-02 23:00:14 +01:00
|
|
|
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();
|
2020-02-09 00:29:46 +01:00
|
|
|
|
|
|
|
return true;
|
2020-02-02 23:00:14 +01:00
|
|
|
}
|
|
|
|
}
|