EventMapper/assets/scripts/KaplayMap/floors.js

77 lines
1.6 KiB
JavaScript
Raw Normal View History

2024-06-02 05:39:23 +00:00
export class FloorManager {
map;
ui;
gameobj;
events;
floors = new Map([]);
rooms = new Map([]);
2024-06-03 03:54:02 +00:00
#currentFloor = "";
#currentRoom = "";
2024-06-02 05:39:23 +00:00
2024-06-04 22:54:40 +00:00
constructor(map, ui, gameobj, events) {
2024-06-02 05:39:23 +00:00
this.map = map;
this.ui = ui;
this.ui.floormanager = this;
this.gameobj = gameobj;
this.events = events;
this.events.floormanager = this;
}
2024-06-04 22:54:40 +00:00
async getFloors(lang) {
let allFloorsReqSend = fetch(`/data/${lang}/map`);
2024-06-02 05:39:23 +00:00
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);
}
2024-06-04 22:54:40 +00:00
async getRooms(lang, floor) {
2024-06-02 05:39:23 +00:00
const curRoom = this.rooms.get(id);
if (curRoom != null) return curRoom;
2024-06-04 22:54:40 +00:00
let allRoomsReqSend = fetch(`/data/${lang}/map/${floor}`);
2024-06-02 05:39:23 +00:00
this.ui.setLoading(true);
let allRoomsReq = await allRoomsReqSend;
let allRooms;
if (allRoomsReq.ok) allRooms = await allRoomsReq.json();
2024-06-03 03:54:02 +00:00
const roomMap = new Map([]);
allRooms.forEach((room) => roomMap.set(room.id, room));
2024-06-04 22:54:40 +00:00
this.rooms.set(floor, roomMap);
2024-06-02 05:39:23 +00:00
this.ui.setLoading(false);
}
2024-06-04 22:54:40 +00:00
setCurrentFloor(id) {
2024-06-02 05:39:23 +00:00
const floor = this.floors.get(id);
2024-06-03 03:54:02 +00:00
if (floor != null) this.#currentFloor = id;
2024-06-04 22:54:40 +00:00
}
2024-06-02 05:39:23 +00:00
2024-06-04 22:54:40 +00:00
get currentFloor() {
return this.#currentFloor;
}
2024-06-03 03:54:02 +00:00
2024-06-04 22:54:40 +00:00
setCurrentRoom(id) {
if (id == null) this.#currentRoom = id;
else {
const room = this.rooms.get(this.#currentFloor).get(room);
if (room != null) this.#currentRoom = id;
}
}
2024-06-02 05:39:23 +00:00
2024-06-04 22:54:40 +00:00
get currentRoom() {
return this.#currentRoom;
2024-06-02 05:39:23 +00:00
}
}