EventMapper/assets/scripts/KaplayMap/floors.js

122 lines
2.5 KiB
JavaScript

export class FloorManager {
map;
ui;
gameobj;
events;
lang;
floors = new Map([]);
rooms = new Map([]);
#currentFloor = "";
#currentRoom = "";
constructor(map, ui, gameobj, events, lang) {
this.map = map;
this.ui = ui;
this.ui.floormanager = this;
this.gameobj = gameobj;
this.gameobj.floormanager = this;
this.events = events;
this.events.floormanager = this;
this.lang = lang;
}
set currentFloor(floor) {
this.#currentFloor = floor;
}
get currentFloor() {
return this.#currentFloor;
}
get currentFloorItem() {
return this.floors.get(this.#currentFloor);
}
setCurrentRoom(room, maximize = true) {
this.#currentRoom = room;
this.ui.__updateEvents();
if (maximize) this.ui.eventsMaximize();
if (room == "") this.gameobj.zoomToFloor();
else this.gameobj.zoomToRoom(room);
}
get currentRoom() {
return this.#currentRoom;
}
get currentRoomItem() {
return this.currentFloorRooms.get(this.#currentRoom);
}
get currentFloorRooms() {
return this.rooms.get(this.#currentFloor);
}
async getFloors() {
let allFloorsReqSend = fetch(`/data/${this.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.changeFloor(allFloors[0].id);
this.ui.setLoading(false);
return this.floors;
}
async getRooms(id) {
const curRoom = this.rooms.get(id);
if (curRoom != null) return curRoom;
let allRoomsReqSend = fetch(`/data/${this.lang}/map/${id}`);
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(id, roomMap);
this.ui.setLoading(false);
return this.rooms;
}
async changeFloor(id) {
const floor = this.floors.get(id);
if (floor != null) this.#currentFloor = id;
if (this.ui.floorsEmpty) this.ui.__updateFloorsHard();
else this.ui.__updateFloorsSoft();
this.ui.setLoading(true);
this.gameobj.generateFloor();
await this.getRooms(id);
this.gameobj.generateRooms();
await this.events.getFloorEvents();
this.ui.__updateEvents();
this.setCurrentRoom("", false);
this.ui.setLoading(false);
return floor;
}
}