2021-07-16 11:22:36 +02:00
|
|
|
import { Observable, Subject } from "rxjs";
|
2021-06-23 20:19:38 +02:00
|
|
|
|
2021-06-25 18:14:40 +02:00
|
|
|
import { EnterLeaveEvent, isEnterLeaveEvent } from "../Events/EnterLeaveEvent";
|
2021-06-23 20:19:38 +02:00
|
|
|
|
2021-07-07 14:42:17 +02:00
|
|
|
import { IframeApiContribution, queryWorkadventure, sendToWorkadventure } from "./IframeApiContribution";
|
2021-06-21 11:48:39 +02:00
|
|
|
import { apiCallback } from "./registeredCallbacks";
|
2021-06-23 20:19:38 +02:00
|
|
|
|
2021-06-25 18:14:40 +02:00
|
|
|
import type { ITiledMap } from "../../Phaser/Map/ITiledMap";
|
2021-05-25 13:47:41 +02:00
|
|
|
|
2021-05-28 00:24:08 +02:00
|
|
|
const enterStreams: Map<string, Subject<EnterLeaveEvent>> = new Map<string, Subject<EnterLeaveEvent>>();
|
|
|
|
const leaveStreams: Map<string, Subject<EnterLeaveEvent>> = new Map<string, Subject<EnterLeaveEvent>>();
|
2021-06-23 11:32:11 +02:00
|
|
|
|
2021-06-23 12:02:29 +02:00
|
|
|
interface TileDescriptor {
|
2021-07-02 14:35:28 +02:00
|
|
|
x: number;
|
|
|
|
y: number;
|
2021-07-07 14:42:17 +02:00
|
|
|
tile: number | string | null;
|
2021-07-02 14:35:28 +02:00
|
|
|
layer: string;
|
2021-06-23 12:02:29 +02:00
|
|
|
}
|
|
|
|
|
2021-07-16 11:22:36 +02:00
|
|
|
let roomId: string | undefined;
|
2021-07-05 11:53:33 +02:00
|
|
|
|
|
|
|
export const setRoomId = (id: string) => {
|
|
|
|
roomId = id;
|
2021-07-16 11:22:36 +02:00
|
|
|
};
|
2021-06-23 11:32:11 +02:00
|
|
|
|
2021-07-16 11:22:36 +02:00
|
|
|
let mapURL: string | undefined;
|
2021-07-05 11:53:33 +02:00
|
|
|
|
|
|
|
export const setMapURL = (url: string) => {
|
|
|
|
mapURL = url;
|
2021-07-16 11:22:36 +02:00
|
|
|
};
|
2021-05-25 13:47:41 +02:00
|
|
|
|
2021-06-30 10:15:55 +02:00
|
|
|
export class WorkadventureRoomCommands extends IframeApiContribution<WorkadventureRoomCommands> {
|
2021-05-28 00:24:08 +02:00
|
|
|
callbacks = [
|
|
|
|
apiCallback({
|
|
|
|
callback: (payloadData: EnterLeaveEvent) => {
|
|
|
|
enterStreams.get(payloadData.name)?.next();
|
|
|
|
},
|
|
|
|
type: "enterEvent",
|
2021-06-25 18:14:40 +02:00
|
|
|
typeChecker: isEnterLeaveEvent,
|
2021-05-28 00:24:08 +02:00
|
|
|
}),
|
|
|
|
apiCallback({
|
|
|
|
type: "leaveEvent",
|
|
|
|
typeChecker: isEnterLeaveEvent,
|
|
|
|
callback: (payloadData) => {
|
|
|
|
leaveStreams.get(payloadData.name)?.next();
|
2021-06-25 18:14:40 +02:00
|
|
|
},
|
2021-06-23 11:32:11 +02:00
|
|
|
}),
|
2021-06-25 18:14:40 +02:00
|
|
|
];
|
2021-05-25 13:47:41 +02:00
|
|
|
|
2021-05-28 00:24:08 +02:00
|
|
|
onEnterZone(name: string, callback: () => void): void {
|
|
|
|
let subject = enterStreams.get(name);
|
|
|
|
if (subject === undefined) {
|
|
|
|
subject = new Subject<EnterLeaveEvent>();
|
|
|
|
enterStreams.set(name, subject);
|
|
|
|
}
|
|
|
|
subject.subscribe(callback);
|
|
|
|
}
|
|
|
|
onLeaveZone(name: string, callback: () => void): void {
|
|
|
|
let subject = leaveStreams.get(name);
|
|
|
|
if (subject === undefined) {
|
|
|
|
subject = new Subject<EnterLeaveEvent>();
|
|
|
|
leaveStreams.set(name, subject);
|
|
|
|
}
|
|
|
|
subject.subscribe(callback);
|
|
|
|
}
|
2021-06-23 11:32:11 +02:00
|
|
|
showLayer(layerName: string): void {
|
2021-06-25 18:14:40 +02:00
|
|
|
sendToWorkadventure({ type: "showLayer", data: { name: layerName } });
|
2021-06-23 11:32:11 +02:00
|
|
|
}
|
|
|
|
hideLayer(layerName: string): void {
|
2021-06-25 18:14:40 +02:00
|
|
|
sendToWorkadventure({ type: "hideLayer", data: { name: layerName } });
|
2021-06-23 11:32:11 +02:00
|
|
|
}
|
|
|
|
setProperty(layerName: string, propertyName: string, propertyValue: string | number | boolean | undefined): void {
|
|
|
|
sendToWorkadventure({
|
2021-06-25 18:14:40 +02:00
|
|
|
type: "setProperty",
|
2021-06-23 11:32:11 +02:00
|
|
|
data: {
|
2021-06-25 18:14:40 +02:00
|
|
|
layerName: layerName,
|
|
|
|
propertyName: propertyName,
|
|
|
|
propertyValue: propertyValue,
|
|
|
|
},
|
|
|
|
});
|
2021-06-23 11:32:11 +02:00
|
|
|
}
|
2021-07-05 11:53:33 +02:00
|
|
|
async getMap(): Promise<ITiledMap> {
|
|
|
|
const event = await queryWorkadventure({ type: "getMapData", data: undefined });
|
|
|
|
return event.data as ITiledMap;
|
2021-06-23 11:32:11 +02:00
|
|
|
}
|
2021-06-28 14:58:49 +02:00
|
|
|
setTiles(tiles: TileDescriptor[]) {
|
2021-06-23 12:02:29 +02:00
|
|
|
sendToWorkadventure({
|
2021-07-02 14:35:28 +02:00
|
|
|
type: "setTiles",
|
|
|
|
data: tiles,
|
|
|
|
});
|
2021-06-23 12:02:29 +02:00
|
|
|
}
|
2021-07-02 11:31:44 +02:00
|
|
|
|
2021-07-16 11:22:36 +02:00
|
|
|
get id(): string {
|
2021-07-05 11:53:33 +02:00
|
|
|
if (roomId === undefined) {
|
2021-07-16 11:22:36 +02:00
|
|
|
throw new Error("Room id not initialized yet. You should call WA.room.id within a WA.onInit callback.");
|
2021-07-05 11:53:33 +02:00
|
|
|
}
|
|
|
|
return roomId;
|
|
|
|
}
|
|
|
|
|
2021-07-16 11:22:36 +02:00
|
|
|
get mapURL(): string {
|
2021-07-05 11:53:33 +02:00
|
|
|
if (mapURL === undefined) {
|
2021-07-16 11:22:36 +02:00
|
|
|
throw new Error(
|
|
|
|
"mapURL is not initialized yet. You should call WA.room.mapURL within a WA.onInit callback."
|
|
|
|
);
|
2021-07-05 11:53:33 +02:00
|
|
|
}
|
|
|
|
return mapURL;
|
|
|
|
}
|
2021-05-28 00:24:08 +02:00
|
|
|
}
|
2021-05-25 13:47:41 +02:00
|
|
|
|
2021-06-18 17:22:56 +02:00
|
|
|
export default new WorkadventureRoomCommands();
|