Tilorswift levels will be loaded asynchronously now

This commit is contained in:
Mal
2023-09-19 11:44:56 +02:00
parent c6b0baa533
commit f02fb4d411
5 changed files with 117 additions and 98 deletions

View File

@@ -69,6 +69,92 @@ export default class Tilorswift
this.addEventListeners();
}
getLevelAsJson()
{
let matrix = [];
for (let y = 0; y < this.level.terrain.fields.length; y++) {
let row = [];
for (let x = 0; x < this.level.terrain.fields[y].length; x++) {
row.push(this.level.terrain.fields[y][x].index);
}
matrix.push(row);
}
let data = {
tileset: this.level.getTilesetId(),
rows: this.level.getRows(),
columns: this.level.getColumns(),
startX: this.level.getStartX(),
startY: this.level.getStartY(),
targetX: this.level.getTargetX(),
targetY: this.level.getTargetY(),
gravity: this.level.gravity,
matrix: matrix,
};
return JSON.stringify(data);
}
openLevelFromFile()
{
const dialog = new LoadLevelDialog();
dialog.onLoad = (json) => {
this.tileset = new Tileset(JSON.parse(json).tileset);
this.level = Level.createFromJson(json);
this.loadLevel();
dialog.close();
}
dialog.openFileBrowser();
}
loadLevel()
{
this.tileset = new Tileset(this.level.terrain.tileset.setId);
document.body.style.backgroundColor = this.level.getBackgroundColor();
if (GraphicSet[this.level.terrain.tileset.setId].backgroundImage !== null) {
document.body.style.backgroundImage = 'url("../' + Setting.GRAPHICS_LOCATION + GraphicSet[this.level.terrain.tileset.setId].backgroundImage + '")';
} else {
document.body.style.backgroundImage = 'none';
}
this.map.innerHTML = '';
this.map.appendChild(this.level.terrain.getElement());
this.tilesetPicker.reloadTileset(this.tileset);
}
saveLevelToFile()
{
if (!this.level.hasEntrancePoint()) {
alert('Es muss ein Startpunkt definiert sein!');
return false;
}
if (!this.level.hasTargetPoint()) {
alert('Es muss ein Zielpunkt definiert sein!');
return false;
}
const filename = prompt('Dateiname', 'terrain.json');
if (filename === null) {
return false;
}
const json = this.getLevelAsJson(level);
const 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;
}
addEventListeners()
{
window.addEventListener(
@@ -204,95 +290,11 @@ export default class Tilorswift
window.addEventListener(
TilorswiftEvent.MENU_SAVE_CLICKED,
()=> {
() => {
if (this.saveLevelToFile()) {
window.dispatchEvent(new TilorswiftSavedEvent());
}
}
);
}
getLevelAsJson()
{
let matrix = [];
for (let y = 0; y < this.level.terrain.fields.length; y++) {
let row = [];
for (let x = 0; x < this.level.terrain.fields[y].length; x++) {
row.push(this.level.terrain.fields[y][x].index);
}
matrix.push(row);
}
let data = {
tileset: this.level.getTilesetId(),
rows: this.level.getRows(),
columns: this.level.getColumns(),
startX: this.level.getStartX(),
startY: this.level.getStartY(),
targetX: this.level.getTargetX(),
targetY: this.level.getTargetY(),
gravity: this.level.gravity,
matrix: matrix,
};
return JSON.stringify(data);
}
openLevelFromFile()
{
const dialog = new LoadLevelDialog();
dialog.onLoad = (json) => {
this.level = Level.createFromJson(json);
this.loadLevel();
dialog.close();
}
}
loadLevel()
{
this.tileset = new Tileset(this.level.terrain.tileset.setId);
document.body.style.backgroundColor = this.level.getBackgroundColor();
if (GraphicSet[this.level.terrain.tileset.setId].backgroundImage !== null) {
document.body.style.backgroundImage = 'url("../' + Setting.GRAPHICS_LOCATION + GraphicSet[this.level.terrain.tileset.setId].backgroundImage + '")';
} else {
document.body.style.backgroundImage = 'none';
}
this.map.innerHTML = '';
this.map.appendChild(this.level.terrain.getElement());
this.tilesetPicker.reloadTileset(this.tileset);
}
saveLevelToFile()
{
if (!this.level.hasEntrancePoint()) {
alert('Es muss ein Startpunkt definiert sein!');
return false;
}
if (!this.level.hasTargetPoint()) {
alert('Es muss ein Zielpunkt definiert sein!');
return false;
}
const filename = prompt('Dateiname', 'terrain.json');
if (filename === null) {
return false;
}
const json = this.getLevelAsJson(level);
const 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;
}
}

View File

@@ -4,12 +4,20 @@ import ImageLoader from "../../js/ImageLoader.js";
import GraphicSet from "../../js/GraphicSet.js";
import Setting from "../../js/Setting.js";
const level = Level.createFromFile('../levels/moonbase.json');
const imageLoader = new ImageLoader();
imageLoader.addImage('../' + Setting.TILESET_LOCATION + GraphicSet[level.terrain.tileset.setId].tileset);
for (const graphicSet of GraphicSet) {
imageLoader.addImage('../' + Setting.TILESET_LOCATION + graphicSet.tileset);
}
imageLoader.onLoad = () => {
const tilorswift = new Tilorswift(level);
tilorswift.init();
tilorswift.loadLevel();
Level.createFromFile(
'../levels/moonbase.json',
(level) => {
const tilorswift = new Tilorswift(level);
tilorswift.loadLevel();
tilorswift.init();
}
);
}
imageLoader.load();