2020-01-29 00:21:53 +01:00
|
|
|
import Terrain from "./Terrain.js";
|
2020-01-30 20:51:29 +01:00
|
|
|
import TilorswiftEvent from "./events/TilorswiftEvent.js";
|
|
|
|
import Mouse from "./Mouse.js";
|
2020-01-30 23:40:42 +01:00
|
|
|
import Tileset from "./Tileset.js";
|
2020-02-02 23:00:14 +01:00
|
|
|
import Tilorswift from "./Tilorswift.js";
|
|
|
|
import TilorswiftSavedEvent from "./events/TilorswiftSavedEvent.js";
|
|
|
|
import MainMenu from "./menu/MainMenu.js";
|
|
|
|
import MenuGroup from "./menu/MenuGroup.js";
|
|
|
|
import MainMenuEntry from "./menu/MainMenuEntry.js";
|
|
|
|
import TilorswiftMenuSaveClickedEvent from "./events/TilorswiftMenuSaveClickedEvent.js";
|
2020-02-04 21:42:14 +01:00
|
|
|
import TilesetPickerWidget from "./menu/TilesetPickerWidget.js";
|
|
|
|
import WidgetBar from "./menu/WidgetBar.js";
|
2020-02-04 23:03:33 +01:00
|
|
|
import EntrancePointWidget from "./menu/EntrancePointWidget.js";
|
|
|
|
import Brush from "./Brush.js";
|
2020-02-04 21:42:14 +01:00
|
|
|
import BrushMode from "./BrushMode.js";
|
2020-02-09 00:29:46 +01:00
|
|
|
import DialogAddRows from "./dialog/DialogAddRows.js";
|
|
|
|
import DialogAddColumns from "./dialog/DialogAddColumns.js";
|
|
|
|
import TilorswiftAddRowsClickedEvent from "./events/TilorswiftAddRowsClickedEvent.js";
|
|
|
|
import TilorswiftAddColumnsClickedEvent from "./events/TilorswiftAddColumnsClickedEvent.js";
|
2020-02-10 19:36:46 +01:00
|
|
|
import TilorswiftMenuNewTerrainClickedEvent from "./events/TilorswiftMenuNewTerrainClickedEvent.js";
|
|
|
|
import DialogNewTerrain from "./dialog/DialogNewTerrain.js";
|
2020-02-11 21:20:11 +01:00
|
|
|
import TargetPointWidget from "./menu/TargetPointWidget.js";
|
2020-02-14 00:12:24 +01:00
|
|
|
import GraphicSet from "../../js/GraphicSet.js";
|
|
|
|
import Setting from "../../js/Setting.js";
|
2020-02-16 00:53:03 +01:00
|
|
|
import Level from "../../js/Level.js";
|
|
|
|
import TilorswiftMenuGravityClickedEvent from "./events/TilorswiftMenuGravityClickedEvent.js";
|
|
|
|
import DialogGravity from "./dialog/DialogGravity.js";
|
2020-01-30 23:40:42 +01:00
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
let level = Level.createFromFile('../levels/moonbase.json');
|
2020-02-14 00:12:24 +01:00
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
if (GraphicSet[level.terrain.tileset.setId].backgroundImage !== null) {
|
2021-09-14 20:11:01 +02:00
|
|
|
document.body.style.backgroundImage = 'url("../' + Setting.GRAPHICS_LOCATION + GraphicSet[level.getTilesetId()].backgroundImage + '")';
|
2020-02-16 00:53:03 +01:00
|
|
|
}
|
2020-02-14 00:12:24 +01:00
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
let image = new Image();
|
2021-09-14 20:11:01 +02:00
|
|
|
image.src = '../' + Setting.TILESET_LOCATION + GraphicSet[level.terrain.tileset.setId].tileset;
|
2020-02-14 00:12:24 +01:00
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
image.onload = function () {
|
|
|
|
document.body.style.backgroundColor = GraphicSet[level.terrain.tileset.setId].backgroundColor;
|
2020-02-14 00:12:24 +01:00
|
|
|
|
2020-01-30 20:51:29 +01:00
|
|
|
let map = document.getElementById('map');
|
2020-02-16 00:53:03 +01:00
|
|
|
map.appendChild(level.terrain.getElement());
|
2020-01-30 20:51:29 +01:00
|
|
|
|
2020-02-09 00:29:46 +01:00
|
|
|
let brush = new Brush();
|
2020-02-16 00:53:03 +01:00
|
|
|
let tileset = new Tileset(level.terrain.tileset.setId);
|
2020-02-04 21:42:14 +01:00
|
|
|
let widgetBar = new WidgetBar('widget-bar');
|
2020-02-04 23:03:33 +01:00
|
|
|
|
|
|
|
let tilesetPicker = new TilesetPickerWidget(tileset, brush);
|
2020-02-04 21:42:14 +01:00
|
|
|
widgetBar.addWidget(tilesetPicker);
|
2020-02-04 23:03:33 +01:00
|
|
|
|
|
|
|
let entrancePicker = new EntrancePointWidget(widgetBar, brush);
|
|
|
|
widgetBar.addWidget(entrancePicker);
|
2020-02-09 00:29:46 +01:00
|
|
|
|
2020-02-11 21:20:11 +01:00
|
|
|
let targetPicker = new TargetPointWidget(widgetBar, brush);
|
|
|
|
widgetBar.addWidget(targetPicker);
|
|
|
|
|
2020-02-04 21:42:14 +01:00
|
|
|
document.body.appendChild(widgetBar.getElement());
|
2020-01-30 23:40:42 +01:00
|
|
|
|
2020-01-30 20:51:29 +01:00
|
|
|
let mouse = new Mouse();
|
2020-02-02 23:00:14 +01:00
|
|
|
let mainbar = new MainMenu('mainbar');
|
|
|
|
|
|
|
|
let menuFile = new MenuGroup('Datei');
|
2020-02-10 19:36:46 +01:00
|
|
|
menuFile.addMenuEntry(new MainMenuEntry('Neu...', TilorswiftMenuNewTerrainClickedEvent));
|
|
|
|
menuFile.addMenuEntry(new MainMenuEntry('Speichern...', TilorswiftMenuSaveClickedEvent));
|
2020-02-02 23:00:14 +01:00
|
|
|
mainbar.addMenuGroup(menuFile);
|
|
|
|
|
2020-02-09 00:29:46 +01:00
|
|
|
let menuEdit = new MenuGroup('Bearbeiten');
|
|
|
|
menuEdit.addMenuEntry(new MainMenuEntry('Zeilen einfügen...', TilorswiftAddRowsClickedEvent));
|
|
|
|
menuEdit.addMenuEntry(new MainMenuEntry('Spalten einfügen...', TilorswiftAddColumnsClickedEvent));
|
|
|
|
mainbar.addMenuGroup(menuEdit);
|
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
let menuLevel = new MenuGroup('Level');
|
|
|
|
menuLevel.addMenuEntry(new MainMenuEntry('Gravitation...', TilorswiftMenuGravityClickedEvent));
|
|
|
|
mainbar.addMenuGroup(menuLevel);
|
|
|
|
|
2020-02-02 23:00:14 +01:00
|
|
|
document.body.appendChild(mainbar.getElement());
|
|
|
|
|
2020-01-30 20:51:29 +01:00
|
|
|
window.addEventListener(
|
|
|
|
TilorswiftEvent.FIELD_CLICKED,
|
|
|
|
(event) => {
|
2020-02-04 23:03:33 +01:00
|
|
|
if (brush.mode === BrushMode.TERRAIN && !event.getField().isEntrancePoint) {
|
|
|
|
switch (event.button) {
|
|
|
|
case 0:
|
2020-02-16 00:53:03 +01:00
|
|
|
event.getField().setIndex(level.terrain.brushTileIndex);
|
2020-02-04 23:03:33 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
event.getField().setIndex(-1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (brush.mode === BrushMode.ENTRANCE) {
|
|
|
|
if (event.getField().index === -1) {
|
2020-02-16 00:53:03 +01:00
|
|
|
let coordinates = level.terrain.getFieldCoordinates(event.getField());
|
|
|
|
level.terrain.setEntrancePoint(coordinates.x, coordinates.y);
|
2020-02-04 23:03:33 +01:00
|
|
|
brush.mode = BrushMode.TERRAIN;
|
|
|
|
widgetBar.enableWidgets();
|
2020-02-11 21:20:11 +01:00
|
|
|
}
|
|
|
|
} else if (brush.mode === BrushMode.EXIT) {
|
|
|
|
if (event.getField().index === -1) {
|
2020-02-16 00:53:03 +01:00
|
|
|
let coordinates = level.terrain.getFieldCoordinates(event.getField());
|
|
|
|
level.terrain.setTargetPoint(coordinates.x, coordinates.y);
|
2020-02-11 21:20:11 +01:00
|
|
|
brush.mode = BrushMode.TERRAIN;
|
|
|
|
widgetBar.enableWidgets();
|
2020-02-04 23:03:33 +01:00
|
|
|
}
|
2020-01-30 23:40:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
window.addEventListener(
|
|
|
|
'contextmenu',
|
|
|
|
(event) => {
|
|
|
|
event.preventDefault();
|
2020-01-30 20:51:29 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
window.addEventListener(
|
|
|
|
TilorswiftEvent.FIELD_ENTERED,
|
|
|
|
(event) => {
|
|
|
|
if (mouse.isPressedLeft) {
|
2020-02-16 00:53:03 +01:00
|
|
|
event.getField().setIndex(level.terrain.brushTileIndex);
|
2020-01-30 23:40:42 +01:00
|
|
|
} else if (mouse.isPressedRight) {
|
|
|
|
event.getField().setIndex(-1);
|
2020-01-30 20:51:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2020-02-02 23:00:14 +01:00
|
|
|
|
2020-02-10 19:36:46 +01:00
|
|
|
window.addEventListener(
|
|
|
|
TilorswiftEvent.NEW_TERRAIN_CLICKED,
|
|
|
|
() => {
|
|
|
|
new DialogNewTerrain();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
window.addEventListener(
|
|
|
|
TilorswiftEvent.MENU_GRAVITY_CLICKED,
|
|
|
|
() => {
|
|
|
|
new DialogGravity(level.gravity);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-02-09 00:29:46 +01:00
|
|
|
window.addEventListener(
|
|
|
|
TilorswiftEvent.ADD_ROWS_CLICKED,
|
|
|
|
() => {
|
|
|
|
new DialogAddRows();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
window.addEventListener(
|
|
|
|
TilorswiftEvent.ADD_COLUMNS_CLICKED,
|
|
|
|
() => {
|
|
|
|
new DialogAddColumns();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
window.addEventListener(
|
|
|
|
TilorswiftEvent.GRAVITY_UPDATED,
|
|
|
|
(event) => {
|
|
|
|
level.gravity = event.gravity;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-02-09 00:29:46 +01:00
|
|
|
window.addEventListener(
|
|
|
|
TilorswiftEvent.ADD_ROWS,
|
|
|
|
function (event) {
|
2020-02-16 00:53:03 +01:00
|
|
|
level.terrain.addRows(event.beforeRow, event.rowCount);
|
2020-02-09 00:29:46 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
window.addEventListener(
|
|
|
|
TilorswiftEvent.ADD_COLUMNS,
|
|
|
|
function (event) {
|
2020-02-16 00:53:03 +01:00
|
|
|
level.terrain.addColumns(event.beforeColumn, event.columnCount);
|
2020-02-09 00:29:46 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-02-10 19:36:46 +01:00
|
|
|
window.addEventListener(
|
|
|
|
TilorswiftEvent.NEW_TERRAIN,
|
|
|
|
(event) => {
|
2020-02-14 00:12:24 +01:00
|
|
|
let tileset = new Tileset(event.tilesetIndex);
|
2020-02-16 00:53:03 +01:00
|
|
|
level.terrain = new Terrain(tileset, event.tilesX, event.tilesY, GraphicSet[event.tilesetIndex].backgroundColor);
|
|
|
|
document.body.style.backgroundColor = level.getBackgroundColor();
|
2020-02-14 00:12:24 +01:00
|
|
|
if (GraphicSet[event.tilesetIndex].backgroundImage !== null) {
|
2021-09-14 20:11:01 +02:00
|
|
|
document.body.style.backgroundImage = 'url("../' + Setting.GRAPHICS_LOCATION + GraphicSet[event.tilesetIndex].backgroundImage + '")';
|
2020-02-16 00:53:03 +01:00
|
|
|
} else {
|
|
|
|
document.body.style.backgroundImage = 'none';
|
2020-02-14 00:12:24 +01:00
|
|
|
}
|
2020-02-10 19:36:46 +01:00
|
|
|
map.innerHTML = '';
|
2020-02-16 00:53:03 +01:00
|
|
|
map.appendChild(level.terrain.getElement());
|
2020-02-14 00:12:24 +01:00
|
|
|
tilesetPicker.reloadTileset(tileset);
|
2020-02-10 19:36:46 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-02-09 00:29:46 +01:00
|
|
|
/* Prevents Firefox's annoying default drag and drop behavior for images */
|
2020-02-02 23:00:14 +01:00
|
|
|
document.addEventListener(
|
|
|
|
'dragstart',
|
|
|
|
function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
window.addEventListener(
|
|
|
|
TilorswiftEvent.MENU_SAVE_CLICKED,
|
|
|
|
function () {
|
2020-02-16 00:53:03 +01:00
|
|
|
if (Tilorswift.saveLevelToFile(level)) {
|
2020-02-09 00:29:46 +01:00
|
|
|
window.dispatchEvent(new TilorswiftSavedEvent());
|
|
|
|
}
|
2020-02-02 23:00:14 +01:00
|
|
|
}
|
|
|
|
);
|
2020-01-29 00:21:53 +01:00
|
|
|
};
|