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