EventMapper/assets/scripts/KaplayMap/floors.js
2024-06-04 17:54:40 -05:00

76 lines
1.6 KiB
JavaScript

export class FloorManager {
map;
ui;
gameobj;
events;
floors = new Map([]);
rooms = new Map([]);
#currentFloor = "";
#currentRoom = "";
constructor(map, ui, gameobj, events) {
this.map = map;
this.ui = ui;
this.ui.floormanager = this;
this.gameobj = gameobj;
this.events = events;
this.events.floormanager = this;
}
async getFloors(lang) {
let allFloorsReqSend = fetch(`/data/${lang}/map`);
this.ui.setLoading(true);
let allFloorsReq = await allFloorsReqSend;
let allFloors;
if (allFloorsReq.ok) allFloors = await allFloorsReq.json();
allFloors.forEach((floor) => this.floors.set(floor.id, floor));
this.ui.setLoading(false);
}
async getRooms(lang, floor) {
const curRoom = this.rooms.get(id);
if (curRoom != null) return curRoom;
let allRoomsReqSend = fetch(`/data/${lang}/map/${floor}`);
this.ui.setLoading(true);
let allRoomsReq = await allRoomsReqSend;
let allRooms;
if (allRoomsReq.ok) allRooms = await allRoomsReq.json();
const roomMap = new Map([]);
allRooms.forEach((room) => roomMap.set(room.id, room));
this.rooms.set(floor, roomMap);
this.ui.setLoading(false);
}
setCurrentFloor(id) {
const floor = this.floors.get(id);
if (floor != null) this.#currentFloor = id;
}
get currentFloor() {
return this.#currentFloor;
}
setCurrentRoom(id) {
if (id == null) this.#currentRoom = id;
else {
const room = this.rooms.get(this.#currentFloor).get(room);
if (room != null) this.#currentRoom = id;
}
}
get currentRoom() {
return this.#currentRoom;
}
}