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