mirror of
https://git.leinelab.org/Mal/mr-crocs-adventures.git
synced 2025-09-11 02:57:36 +02:00
Main menu and save function to json file implemented.
This commit is contained in:
40
tilorswift/js/Tilorswift.js
Normal file
40
tilorswift/js/Tilorswift.js
Normal file
@@ -0,0 +1,40 @@
|
||||
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,
|
||||
backgroundColor: terrain.backgroundColor,
|
||||
matrix: matrix,
|
||||
};
|
||||
|
||||
return JSON.stringify(data);
|
||||
}
|
||||
|
||||
static saveTerrainToFile(terrain)
|
||||
{
|
||||
let json = Tilorswift.getTerrainAsJson(terrain);
|
||||
let download = document.createElement('a');
|
||||
|
||||
download.setAttribute('download', 'terrain.json');
|
||||
download.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(json));
|
||||
download.click();
|
||||
download.remove();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user