2020-02-16 00:53:03 +01:00
|
|
|
import FileLoader from "./FileLoader.js";
|
|
|
|
import Terrain from "../tilorswift/js/Terrain.js";
|
|
|
|
|
|
|
|
export default class Level
|
|
|
|
{
|
2024-08-02 22:00:43 +02:00
|
|
|
static FACTOR_GRAVITY = 4.903;
|
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
constructor(terrain)
|
|
|
|
{
|
|
|
|
this.terrain = terrain;
|
|
|
|
this.gravity = 2.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTilesetId()
|
|
|
|
{
|
|
|
|
return this.terrain.tileset.setId;
|
|
|
|
}
|
|
|
|
|
|
|
|
getBackgroundColor()
|
|
|
|
{
|
|
|
|
return this.terrain.backgroundColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
getBackgroundImage()
|
|
|
|
{
|
|
|
|
return this.terrain.backgroundImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
getRows()
|
|
|
|
{
|
|
|
|
return this.terrain.tilesY;
|
|
|
|
}
|
|
|
|
|
|
|
|
getColumns()
|
|
|
|
{
|
|
|
|
return this.terrain.tilesX;
|
|
|
|
}
|
|
|
|
|
|
|
|
getStartX()
|
|
|
|
{
|
|
|
|
return this.terrain.entranceTileX;
|
|
|
|
}
|
|
|
|
|
|
|
|
getStartY()
|
|
|
|
{
|
|
|
|
return this.terrain.entranceTileY;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTargetX()
|
|
|
|
{
|
|
|
|
return this.terrain.targetTileX;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTargetY()
|
|
|
|
{
|
|
|
|
return this.terrain.targetTileY;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTilesetMatrix()
|
|
|
|
{
|
|
|
|
return this.terrain.fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
hasEntrancePoint()
|
|
|
|
{
|
|
|
|
return this.terrain.hasEntrancePoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
hasTargetPoint()
|
|
|
|
{
|
|
|
|
return this.terrain.hasTargetPoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
setGravity(gravity)
|
|
|
|
{
|
|
|
|
this.gravity = gravity;
|
|
|
|
}
|
|
|
|
|
2023-09-19 11:44:56 +02:00
|
|
|
static createFromFile(filename, callback = () => {})
|
2020-02-16 00:53:03 +01:00
|
|
|
{
|
|
|
|
let loader = new FileLoader(filename);
|
2023-09-19 11:44:56 +02:00
|
|
|
loader.onLoad = (data) => {
|
|
|
|
const json = JSON.parse(data);
|
2023-09-24 01:48:01 +02:00
|
|
|
const level = new Level(Terrain.createFromJson(json));
|
2024-08-02 22:00:43 +02:00
|
|
|
level.setGravity(json.gravity / Level.FACTOR_GRAVITY);
|
2023-09-19 11:44:56 +02:00
|
|
|
|
|
|
|
callback(level);
|
|
|
|
}
|
|
|
|
loader.loadContent();
|
2020-02-16 00:53:03 +01:00
|
|
|
}
|
2023-09-17 14:18:21 +02:00
|
|
|
|
|
|
|
static createFromJson(json)
|
|
|
|
{
|
|
|
|
const data = JSON.parse(json);
|
|
|
|
|
|
|
|
const terrain = Terrain.createFromJson(data);
|
|
|
|
const level = new Level(terrain);
|
2024-08-02 22:00:43 +02:00
|
|
|
level.setGravity(data.gravity / Level.FACTOR_GRAVITY);
|
2023-09-17 14:18:21 +02:00
|
|
|
|
|
|
|
return level;
|
|
|
|
}
|
|
|
|
}
|