2020-01-29 00:21:53 +01:00
|
|
|
import Terrain from "./Terrain.js";
|
|
|
|
import FileLoader from "../../js/FileLoader.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";
|
|
|
|
import BrushMode from "./BrushMode.js";
|
2020-01-30 23:40:42 +01:00
|
|
|
|
|
|
|
let loader = new FileLoader('../levels/level.json');
|
2020-01-29 00:21:53 +01:00
|
|
|
let image = new Image();
|
|
|
|
image.src = '../graphics/tileset-landscape01.jpg';
|
2020-02-04 21:42:14 +01:00
|
|
|
|
2020-01-29 00:21:53 +01:00
|
|
|
image.onload = function () {
|
|
|
|
let terrain = Terrain.createFromJson(loader.getContent());
|
2020-01-30 20:51:29 +01:00
|
|
|
let map = document.getElementById('map');
|
|
|
|
map.appendChild(terrain.getElement());
|
|
|
|
|
2020-01-30 23:40:42 +01:00
|
|
|
let tileset = new Tileset(image, 8, 3);
|
2020-02-04 21:42:14 +01:00
|
|
|
let tilesetPicker = new TilesetPickerWidget(tileset);
|
2020-01-30 23:40:42 +01:00
|
|
|
|
2020-02-04 21:42:14 +01:00
|
|
|
let widgetBar = new WidgetBar('widget-bar');
|
|
|
|
widgetBar.addWidget(tilesetPicker);
|
|
|
|
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');
|
|
|
|
menuFile.addMenuEntry(
|
|
|
|
new MainMenuEntry('Speichern', TilorswiftMenuSaveClickedEvent)
|
|
|
|
);
|
|
|
|
|
|
|
|
mainbar.addMenuGroup(menuFile);
|
|
|
|
|
|
|
|
document.body.appendChild(mainbar.getElement());
|
|
|
|
|
2020-01-30 20:51:29 +01:00
|
|
|
window.addEventListener(
|
|
|
|
TilorswiftEvent.FIELD_CLICKED,
|
|
|
|
(event) => {
|
2020-01-30 23:40:42 +01:00
|
|
|
switch (event.button) {
|
|
|
|
case 0:
|
|
|
|
event.getField().setIndex(terrain.brushTileIndex);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
event.getField().setIndex(-1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
window.addEventListener(
|
|
|
|
'contextmenu',
|
|
|
|
(event) => {
|
|
|
|
event.preventDefault();
|
2020-01-30 20:51:29 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
window.addEventListener(
|
|
|
|
TilorswiftEvent.FIELD_ENTERED,
|
|
|
|
(event) => {
|
|
|
|
if (mouse.isPressedLeft) {
|
|
|
|
event.getField().setIndex(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-04 21:42:14 +01:00
|
|
|
/* Prevent Firefox's default drag and drop for images */
|
2020-02-02 23:00:14 +01:00
|
|
|
document.addEventListener(
|
|
|
|
'dragstart',
|
|
|
|
function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
window.addEventListener(
|
|
|
|
TilorswiftEvent.MENU_SAVE_CLICKED,
|
|
|
|
function () {
|
|
|
|
Tilorswift.saveTerrainToFile(terrain);
|
|
|
|
window.dispatchEvent(new TilorswiftSavedEvent());
|
|
|
|
}
|
|
|
|
);
|
2020-01-29 00:21:53 +01:00
|
|
|
};
|