EventMapper/assets/scripts/KaplayMap/floors.js

85 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-06-02 05:39:23 +00:00
export class FloorManager {
map;
ui;
gameobj;
events;
lang;
floors = new Map([]);
rooms = new Map([]);
currentFloor = "";
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;
}
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();
this.rooms.set(id, allRooms);
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.ui.setLoading(false);
return floor;
}
}