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

209 lines
7.5 KiB
JavaScript
Raw Permalink Normal View History

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";
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";
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) {
2021-09-14 20:11:01 +02:00
document.body.style.backgroundImage = 'url("../' + Setting.GRAPHICS_LOCATION + GraphicSet[level.getTilesetId()].backgroundImage + '")';
}
let image = new Image();
2021-09-14 20:11:01 +02:00
image.src = '../' + Setting.TILESET_LOCATION + GraphicSet[level.terrain.tileset.setId].tileset;
image.onload = function () {
document.body.style.backgroundColor = GraphicSet[level.terrain.tileset.setId].backgroundColor;
2020-01-30 20:51:29 +01:00
let map = document.getElementById('map');
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();
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 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);
let menuLevel = new MenuGroup('Level');
menuLevel.addMenuEntry(new MainMenuEntry('Gravitation...', TilorswiftMenuGravityClickedEvent));
mainbar.addMenuGroup(menuLevel);
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(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) {
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) {
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
}
}
}
);
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(level.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();
}
);
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();
}
);
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) {
level.terrain.addRows(event.beforeRow, event.rowCount);
2020-02-09 00:29:46 +01:00
}
);
window.addEventListener(
TilorswiftEvent.ADD_COLUMNS,
function (event) {
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) => {
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) {
2021-09-14 20:11:01 +02:00
document.body.style.backgroundImage = 'url("../' + Setting.GRAPHICS_LOCATION + GraphicSet[event.tilesetIndex].backgroundImage + '")';
} else {
document.body.style.backgroundImage = 'none';
}
2020-02-10 19:36:46 +01:00
map.innerHTML = '';
map.appendChild(level.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 () {
if (Tilorswift.saveLevelToFile(level)) {
2020-02-09 00:29:46 +01:00
window.dispatchEvent(new TilorswiftSavedEvent());
}
}
);
2020-01-29 00:21:53 +01:00
};