EventMapper/assets/scripts/KaplayMap/mapper.js

351 lines
8.3 KiB
JavaScript
Raw Normal View History

2024-06-05 07:30:05 +00:00
import { EventManager } from "./events.js";
import { FloorManager } from "./floors.js";
import { GameObjManager } from "./gameobj.js";
import { LangManager } from "./lang.js";
2024-06-09 12:23:14 +00:00
import { TagManager } from "./tags.js";
2024-06-05 07:30:05 +00:00
import { UIManager } from "./ui.js";
export class EventMapperManager {
#currentFocus = [null, null, null];
map;
langmanager;
eventmanager;
floormanager;
uimanager;
gameobjmanager;
2024-06-08 16:36:42 +00:00
constructor(
map,
mapUi,
opts = {
gameobj: {},
}
) {
2024-06-05 07:30:05 +00:00
this.map = map;
this.langmanager = new LangManager("en-US");
this.langmanager.mainmanager = this;
this.eventmanager = new EventManager();
this.eventmanager.mainmanager = this;
this.floormanager = new FloorManager();
this.floormanager.mainmanager = this;
2024-06-09 12:23:14 +00:00
this.tagmanager = new TagManager();
this.tagmanager.mainmanager = this;
2024-06-05 07:30:05 +00:00
this.uimanager = new UIManager(mapUi);
this.uimanager.mainmanager = this;
2024-06-08 16:36:42 +00:00
this.gameobjmanager = new GameObjManager(this.map, opts.gameobj);
2024-06-05 07:30:05 +00:00
this.gameobjmanager.mainmanager = this;
}
// Init
__initFloor() {
const allFloors = Array.from(this.getAllFocusObject("floor").keys());
location.hash = allFloors[0];
}
2024-06-05 07:30:05 +00:00
async load() {
this.uimanager.setLoading(true);
await this.floormanager.getFloors();
this.uimanager.mainmanagerInit();
this.uimanager.__updateFloorsHard();
this.uimanager.setLoading(false);
this.uimanager.setLoading(true);
2024-06-09 12:23:14 +00:00
await this.tagmanager.getTags();
this.uimanager.__updateSearchUI();
this.uimanager.setLoading(false);
this.uimanager.setLoading(true);
2024-06-05 07:30:05 +00:00
window.addEventListener("hashchange", () => this.hashchange());
this.__initFloor();
2024-06-05 07:30:05 +00:00
await this.hashchange();
this.uimanager.setLoading(false);
}
async hashchange() {
const prevID = this.#currentFocus;
let hash = location.hash;
if (hash.includes("#")) hash = hash.replace("#", "");
else return;
2024-06-05 07:30:05 +00:00
const floorFocus = this.convertIdFocus(hash, "floor");
const roomFocus = this.convertIdFocus(hash, "room");
const eventFocus = this.convertIdFocus(hash, "event");
this.uimanager.setLoading(true);
2024-06-05 07:30:05 +00:00
if (floorFocus != this.convertFocus(prevID, "floor"))
await this.selectFloor(floorFocus);
this.uimanager.setLoading(true);
this.uimanager.setLoading(false);
2024-06-05 07:30:05 +00:00
if (roomFocus != this.convertFocus(prevID, "room"))
await this.selectRoom(roomFocus);
this.uimanager.setLoading(true);
this.uimanager.setLoading(false);
2024-06-05 07:30:05 +00:00
if (eventFocus != this.convertFocus(prevID, "event"))
await this.selectEvent(eventFocus);
this.uimanager.setLoading(false);
2024-06-05 07:30:05 +00:00
}
async selectFloor(id) {
this.floormanager.setCurrentFloor(id);
this.uimanager.__updateFloorsSoft();
this.uimanager.__updateEventsSoft();
this.gameobjmanager.generateFloor();
this.gameobjmanager.zoomToFloor();
this.uimanager.setLoading(true);
await this.floormanager.getRooms();
this.gameobjmanager.generateRooms();
this.uimanager.setLoading(false);
this.uimanager.setLoading(true);
await this.eventmanager.getEvents();
this.uimanager.updateEvents();
if (!this.uimanager.getEventsEmpty())
this.uimanager.setEventsMinimized(false);
2024-06-09 12:23:14 +00:00
// this.uimanager.setSearchDialogOpen(false);
await this.uimanager.__updateSearch();
2024-06-05 07:30:05 +00:00
this.uimanager.setLoading(false);
}
async selectRoom(id) {
this.floormanager.setCurrentRoom(id);
if (id == null) this.gameobjmanager.zoomToFloor();
else this.gameobjmanager.zoomToRoom(id);
this.uimanager.updateEvents();
if (!this.uimanager.getEventsEmpty())
this.uimanager.setEventsMinimized(false);
2024-06-09 12:23:14 +00:00
if (id != null) {
this.uimanager.setSearchDialogOpen(false);
}
2024-06-05 07:30:05 +00:00
this.uimanager.setEventsInspector(false);
}
async selectEvent(id) {
this.eventmanager.setCurrentEvent(id);
if (id != null) {
this.gameobjmanager.zoomToRoom(this.convertIdFocus(id, "room"));
this.uimanager.updateInspector();
2024-06-09 12:23:14 +00:00
this.uimanager.setSearchDialogOpen(false);
2024-06-05 07:30:05 +00:00
}
this.uimanager.setEventsInspector(id != null);
}
// Focus
convertFocus(id, focus) {
let res;
switch (focus) {
case "floor":
return id.slice(0, 1).toString();
case "room":
res = id.slice(0, 2);
if (res.length < 2) return null;
return res.filter(Boolean).join("_");
case "event":
res = id.slice(0, 3);
if (res.length < 3) return null;
return res.join("_");
}
}
convertIdFocus(id, focus) {
const focusFromId = id.split("_");
let res;
switch (focus) {
case "floor":
return focusFromId.slice(0, 1).toString();
case "room":
res = focusFromId.slice(0, 2);
if (res.length < 2) return null;
return res.filter(Boolean).join("_");
case "event":
res = focusFromId.slice(0, 3);
if (res.length < 3) return null;
return res.join("_");
}
}
splitIdFocus(id, focus) {
const focusFromId = id.split("_");
switch (focus) {
case "floor":
return focusFromId.slice(0, 1);
case "room":
return focusFromId.slice(0, 2);
case "event":
return focusFromId;
}
}
setCurrentFocus(focus, id) {
if (id == null) {
switch (focus) {
case "floor":
this.#currentFocus[0] = id;
break;
case "room":
this.#currentFocus[1] = id;
break;
case "event":
this.#currentFocus[2] = id;
break;
}
} else this.#currentFocus = this.splitIdFocus(id, focus);
}
getCurrentFocus(focus) {
switch (focus) {
case "floor":
return this.#currentFocus.slice(0, 1).toString();
case "room":
return this.#currentFocus.slice(0, 2).filter(Boolean).join("_");
case "event":
return this.currentFocusId;
}
}
getIsFocused(focus) {
switch (focus) {
case "floor":
return this.#currentFocus[0] != null;
case "room":
return this.#currentFocus[1] != null;
case "event":
return this.#currentFocus[2] != null;
}
}
set currentFocusId(id) {
this.#currentFocus = id.split("_");
}
get currentFocusId() {
return this.#currentFocus.filter(Boolean).join("_");
}
// IMC shorthand (focus)
getAllFocusObject(focus) {
switch (focus) {
case "floor":
return this.floormanager.allFloors;
case "room":
return this.floormanager.currentFloorRooms;
case "event":
return this.eventmanager.currentRoomEvents;
}
}
getCurrentFocusObject(focus) {
switch (focus) {
case "floor":
return this.floormanager.currentFloor;
case "room":
return this.floormanager.currentRoom;
case "event":
return this.eventmanager.currentEvent;
}
}
getCurrentLocalization() {
return this.langmanager.currentLocalization;
}
getCurrentLangCode() {
return this.langmanager.langCode;
}
// Inter-manager comms
// I don't know if this is Horrible or Fine
addManagerEventListener(manager, event, callback) {
switch (manager) {
case "map":
return this.map.events.addEventListener(event, callback);
}
}
removeManagerEventListener(manager, event, callback) {
switch (manager) {
case "map":
return this.map.events.removeEventListener(event, callback);
}
}
callManagerFunction(manager, func, ...args) {
switch (manager) {
case "map":
return this.map[func](...args);
case "lang":
return this.langmanager[func](...args);
case "event":
return this.eventmanager[func](...args);
case "floor":
return this.floormanager[func](...args);
2024-06-09 12:23:14 +00:00
case "tag":
return this.tagmanager[func](...args);
2024-06-05 07:30:05 +00:00
case "ui":
return this.uimanager[func](...args);
case "gameobj":
return this.gameobjmanager[func](...args);
}
}
getManagerVariable(manager, variable) {
switch (manager) {
case "map":
return this.map[variable];
case "lang":
return this.langmanager[variable];
case "event":
return this.eventmanager[variable];
case "floor":
return this.floormanager[variable];
2024-06-09 12:23:14 +00:00
case "tag":
return this.tagmanager[variable];
2024-06-05 07:30:05 +00:00
case "ui":
return this.uimanager[variable];
case "gameobj":
return this.gameobjmanager[variable];
}
}
}