Corrects scale managing and camera event listening

This commit is contained in:
Benedicte Quimbert 2021-12-06 18:44:37 +01:00
parent 7eda8a45a7
commit 4b4f5520db
4 changed files with 50 additions and 25 deletions

View File

@ -6,6 +6,7 @@ export const isWasCameraUpdatedEvent = new tg.IsInterface()
y: tg.isNumber, y: tg.isNumber,
width: tg.isNumber, width: tg.isNumber,
height: tg.isNumber, height: tg.isNumber,
zoom: tg.isNumber,
}) })
.get(); .get();

View File

@ -85,6 +85,9 @@ class IframeListener {
private readonly _loadSoundStream: Subject<LoadSoundEvent> = new Subject(); private readonly _loadSoundStream: Subject<LoadSoundEvent> = new Subject();
public readonly loadSoundStream = this._loadSoundStream.asObservable(); public readonly loadSoundStream = this._loadSoundStream.asObservable();
private readonly _trackCameraUpdateStream: Subject<LoadSoundEvent> = new Subject();
public readonly trackCameraUpdateStream = this._trackCameraUpdateStream.asObservable();
private readonly _setTilesStream: Subject<SetTilesEvent> = new Subject(); private readonly _setTilesStream: Subject<SetTilesEvent> = new Subject();
public readonly setTilesStream = this._setTilesStream.asObservable(); public readonly setTilesStream = this._setTilesStream.asObservable();
@ -95,7 +98,6 @@ class IframeListener {
private readonly iframeCloseCallbacks = new Map<HTMLIFrameElement, (() => void)[]>(); private readonly iframeCloseCallbacks = new Map<HTMLIFrameElement, (() => void)[]>();
private readonly scripts = new Map<string, HTMLIFrameElement>(); private readonly scripts = new Map<string, HTMLIFrameElement>();
private sendPlayerMove: boolean = false; private sendPlayerMove: boolean = false;
private sendCameraUpdate: boolean = false;
// Note: we are forced to type this in unknown and later cast with "as" because of https://github.com/microsoft/TypeScript/issues/31904 // Note: we are forced to type this in unknown and later cast with "as" because of https://github.com/microsoft/TypeScript/issues/31904
private answerers: { private answerers: {
@ -228,7 +230,7 @@ class IframeListener {
} else if (payload.type == "onPlayerMove") { } else if (payload.type == "onPlayerMove") {
this.sendPlayerMove = true; this.sendPlayerMove = true;
} else if (payload.type == "onCameraUpdate") { } else if (payload.type == "onCameraUpdate") {
this.sendCameraUpdate = true; this._trackCameraUpdateStream.next();
} else if (payload.type == "setTiles" && isSetTilesEvent(payload.data)) { } else if (payload.type == "setTiles" && isSetTilesEvent(payload.data)) {
this._setTilesStream.next(payload.data); this._setTilesStream.next(payload.data);
} else if (payload.type == "modifyEmbeddedWebsite" && isEmbeddedWebsiteEvent(payload.data)) { } else if (payload.type == "modifyEmbeddedWebsite" && isEmbeddedWebsiteEvent(payload.data)) {
@ -428,12 +430,10 @@ class IframeListener {
} }
sendCameraUpdated(event: WasCameraUpdatedEvent) { sendCameraUpdated(event: WasCameraUpdatedEvent) {
if (this.sendCameraUpdate) { this.postMessage({
this.postMessage({ type: "wasCameraUpdated",
type: "wasCameraUpdated", data: event,
data: event, });
});
}
} }
sendButtonClickedEvent(popupId: number, buttonId: number): void { sendButtonClickedEvent(popupId: number, buttonId: number): void {

View File

@ -16,7 +16,8 @@ export class EmbeddedWebsiteManager {
if (website === undefined) { if (website === undefined) {
throw new Error('Cannot find embedded website with name "' + name + '"'); throw new Error('Cannot find embedded website with name "' + name + '"');
} }
const rect = website.iframe.getBoundingClientRect();
const scale = website.scale ?? 1;
return { return {
url: website.url, url: website.url,
name: website.name, name: website.name,
@ -26,8 +27,8 @@ export class EmbeddedWebsiteManager {
position: { position: {
x: website.phaserObject.x, x: website.phaserObject.x,
y: website.phaserObject.y, y: website.phaserObject.y,
width: website.phaserObject.width, width: website.phaserObject.width * scale,
height: website.phaserObject.height, height: website.phaserObject.height * scale,
}, },
origin: website.origin, origin: website.origin,
scale: website.scale, scale: website.scale,

View File

@ -206,6 +206,8 @@ export class GameScene extends DirtyScene {
private objectsByType = new Map<string, ITiledMapObject[]>(); private objectsByType = new Map<string, ITiledMapObject[]>();
private embeddedWebsiteManager!: EmbeddedWebsiteManager; private embeddedWebsiteManager!: EmbeddedWebsiteManager;
private loader: Loader; private loader: Loader;
private lastCameraEvent: WasCameraUpdatedEvent | undefined;
private firstCameraUpdateSent: boolean = false;
constructor(private room: Room, MapUrlFile: string, customKey?: string | undefined) { constructor(private room: Room, MapUrlFile: string, customKey?: string | undefined) {
super({ super({
@ -752,17 +754,6 @@ export class GameScene extends DirtyScene {
this.gameMap.setPosition(event.x, event.y); this.gameMap.setPosition(event.x, event.y);
}); });
//listen event to share the actual worldView when the camera is updated
this.cameras.main.on("followupdate", (camera: Camera) => {
const cameraEvent: WasCameraUpdatedEvent = {
x: camera.worldView.x,
y: camera.worldView.y,
width: camera.worldView.width,
height: camera.worldView.height,
};
iframeListener.sendCameraUpdated(cameraEvent);
});
// Set up variables manager // Set up variables manager
this.sharedVariablesManager = new SharedVariablesManager( this.sharedVariablesManager = new SharedVariablesManager(
this.connection, this.connection,
@ -1045,9 +1036,33 @@ ${escapedMessage}
); );
this.iframeSubscriptionList.push( this.iframeSubscriptionList.push(
iframeListener.stopSoundStream.subscribe((stopSoundEvent) => { iframeListener.trackCameraUpdateStream.subscribe(() => {
const url = new URL(stopSoundEvent.url, this.MapUrlFile); if (!this.firstCameraUpdateSent) {
soundManager.stopSound(this.sound, url.toString()); this.cameras.main.on("followupdate", (camera: Camera) => {
const cameraEvent: WasCameraUpdatedEvent = {
x: camera.worldView.x,
y: camera.worldView.y,
width: camera.worldView.width,
height: camera.worldView.height,
zoom: camera.scaleManager.zoom,
};
if (
this.lastCameraEvent?.x == cameraEvent.x &&
this.lastCameraEvent?.y == cameraEvent.y &&
this.lastCameraEvent?.width == cameraEvent.width &&
this.lastCameraEvent?.height == cameraEvent.height &&
this.lastCameraEvent?.zoom == cameraEvent.zoom
) {
return;
}
this.lastCameraEvent = cameraEvent;
iframeListener.sendCameraUpdated(cameraEvent);
this.firstCameraUpdateSent = true;
});
iframeListener.sendCameraUpdated(this.cameras.main);
}
}) })
); );
@ -1110,6 +1125,12 @@ ${escapedMessage}
}) })
); );
this.iframeSubscriptionList.push(
iframeListener.setPropertyStream.subscribe((setProperty) => {
this.setPropertyLayer(setProperty.layerName, setProperty.propertyName, setProperty.propertyValue);
})
);
iframeListener.registerAnswerer("openCoWebsite", async (openCoWebsite, source) => { iframeListener.registerAnswerer("openCoWebsite", async (openCoWebsite, source) => {
if (!source) { if (!source) {
throw new Error("Unknown query source"); throw new Error("Unknown query source");
@ -1877,6 +1898,7 @@ ${escapedMessage}
this.loader.resize(); this.loader.resize();
} }
private getObjectLayerData(objectName: string): ITiledMapObject | undefined { private getObjectLayerData(objectName: string): ITiledMapObject | undefined {
for (const layer of this.mapFile.layers) { for (const layer of this.mapFile.layers) {
if (layer.type === "objectgroup" && layer.name === "floorLayer") { if (layer.type === "objectgroup" && layer.name === "floorLayer") {
@ -1889,6 +1911,7 @@ ${escapedMessage}
} }
return undefined; return undefined;
} }
private reposition(): void { private reposition(): void {
// Recompute camera offset if needed // Recompute camera offset if needed
biggestAvailableAreaStore.recompute(); biggestAvailableAreaStore.recompute();