import Terrain from "./Terrain.js"; import FileLoader from "../../js/FileLoader.js"; import TilorswiftEvent from "./events/TilorswiftEvent.js"; import Mouse from "./Mouse.js"; import Tileset from "./Tileset.js"; import ButtonTile from "./ButtonTile.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"; let loader = new FileLoader('../levels/level.json'); let image = new Image(); image.src = '../graphics/tileset-landscape01.jpg'; image.onload = function () { let terrain = Terrain.createFromJson(loader.getContent()); let map = document.getElementById('map'); map.appendChild(terrain.getElement()); let menuTileset = document.getElementById('tileset'); let tileset = new Tileset(image, 8, 3); for (let t = 0; t < tileset.tiles; t++) { let button = new ButtonTile(tileset, t); menuTileset.appendChild(button.getElement()); } let mouse = new Mouse(); let mainbar = new MainMenu('mainbar'); let menuFile = new MenuGroup('Datei'); menuFile.addMenuEntry( new MainMenuEntry('Speichern', TilorswiftMenuSaveClickedEvent) ); mainbar.addMenuGroup(menuFile); document.body.appendChild(mainbar.getElement()); window.addEventListener( TilorswiftEvent.FIELD_CLICKED, (event) => { console.log(event); switch (event.button) { case 0: event.getField().setIndex(terrain.brushTileIndex); break; case 2: event.getField().setIndex(-1); break; } } ); window.addEventListener( 'contextmenu', (event) => { event.preventDefault(); } ); window.addEventListener( TilorswiftEvent.FIELD_ENTERED, (event) => { if (mouse.isPressedLeft) { event.getField().setIndex(terrain.brushTileIndex); } else if (mouse.isPressedRight) { event.getField().setIndex(-1); } } ); document.addEventListener( 'dragstart', function (event) { event.preventDefault(); } ); window.addEventListener( 'keydown', function (event) { if (event.code === 'KeyS') { Tilorswift.saveTerrainToFile(terrain); window.dispatchEvent(new TilorswiftSavedEvent()); } } ); window.addEventListener( TilorswiftEvent.MENU_SAVE_CLICKED, function () { Tilorswift.saveTerrainToFile(terrain); window.dispatchEvent(new TilorswiftSavedEvent()); } ); };