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

103 lines
2.9 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 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');
2020-01-29 00:21:53 +01:00
let image = new Image();
image.src = '../graphics/tileset-landscape01.jpg';
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());
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());
}
2020-01-30 20:51:29 +01:00
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());
2020-01-30 20:51:29 +01:00
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();
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
}
}
);
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());
}
);
2020-01-29 00:21:53 +01:00
};