mr-crocs-adventures/tilorswift/js/module.js

189 lines
6.8 KiB
JavaScript
Raw Normal View History

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";
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";
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";
import GraphicSet from "../../js/GraphicSet.js";
import Setting from "../../js/Setting.js";
2020-02-11 21:20:11 +01:00
let loader = new FileLoader('../levels/level01.json');
2020-02-09 22:06:59 +01:00
let terrainData = JSON.parse(loader.getContent());
2020-01-29 00:21:53 +01:00
let image = new Image();
image.src = Setting.TILESET_LOCATION + GraphicSet[terrainData.tileset].tileset;
2020-02-04 21:42:14 +01:00
2020-01-29 00:21:53 +01:00
image.onload = function () {
2020-02-09 22:06:59 +01:00
let terrain = Terrain.createFromJson(terrainData);
if (GraphicSet[terrainData.tileset].backgroundImage !== null) {
document.body.style.backgroundImage = 'url("' + Setting.GRAPHICS_LOCATION + GraphicSet[terrainData.tileset].backgroundImage + '")';
}
document.body.style.backgroundColor = GraphicSet[terrainData.tileset].backgroundColor;
2020-01-30 20:51:29 +01:00
let map = document.getElementById('map');
map.appendChild(terrain.getElement());
2020-02-09 00:29:46 +01:00
let brush = new Brush();
let tileset = new Tileset(terrainData.tileset);
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 20:51:29 +01:00
let mouse = new Mouse();
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));
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);
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:
event.getField().setIndex(terrain.brushTileIndex);
break;
case 2:
event.getField().setIndex(-1);
break;
}
} else if (brush.mode === BrushMode.ENTRANCE) {
if (event.getField().index === -1) {
let coordinates = terrain.getFieldCoordinates(event.getField());
terrain.setEntrancePoint(coordinates.x, coordinates.y);
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) {
let coordinates = terrain.getFieldCoordinates(event.getField());
terrain.setTargetPoint(coordinates.x, coordinates.y);
brush.mode = BrushMode.TERRAIN;
widgetBar.enableWidgets();
2020-02-04 23:03:33 +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) {
event.getField().setIndex(terrain.brushTileIndex);
} else if (mouse.isPressedRight) {
event.getField().setIndex(-1);
2020-01-30 20:51:29 +01:00
}
}
);
2020-02-10 19:36:46 +01:00
window.addEventListener(
TilorswiftEvent.NEW_TERRAIN_CLICKED,
() => {
new DialogNewTerrain();
}
);
2020-02-09 00:29:46 +01:00
window.addEventListener(
TilorswiftEvent.ADD_ROWS_CLICKED,
() => {
new DialogAddRows();
}
);
window.addEventListener(
TilorswiftEvent.ADD_COLUMNS_CLICKED,
() => {
new DialogAddColumns();
}
);
window.addEventListener(
TilorswiftEvent.ADD_ROWS,
function (event) {
terrain.addRows(event.beforeRow, event.rowCount);
}
);
window.addEventListener(
TilorswiftEvent.ADD_COLUMNS,
function (event) {
terrain.addColumns(event.beforeColumn, event.columnCount);
}
);
2020-02-10 19:36:46 +01:00
window.addEventListener(
TilorswiftEvent.NEW_TERRAIN,
(event) => {
let tileset = new Tileset(event.tilesetIndex);
terrain = new Terrain(tileset, event.tilesX, event.tilesY, GraphicSet[event.tilesetIndex].backgroundColor);
if (GraphicSet[event.tilesetIndex].backgroundImage !== null) {
document.body.style.backgroundImage = 'url("' + Setting.GRAPHICS_LOCATION + GraphicSet[event.tilesetIndex].backgroundImage + '")';
}
2020-02-10 19:36:46 +01:00
map.innerHTML = '';
map.appendChild(terrain.getElement());
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 */
document.addEventListener(
'dragstart',
function (event) {
event.preventDefault();
}
);
window.addEventListener(
TilorswiftEvent.MENU_SAVE_CLICKED,
function () {
2020-02-09 00:29:46 +01:00
if (Tilorswift.saveTerrainToFile(terrain)) {
window.dispatchEvent(new TilorswiftSavedEvent());
}
}
);
2020-01-29 00:21:53 +01:00
};