From 4536a63e69abfb3974b08d27fa91fd8c8d6f0ed3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Fri, 27 Aug 2021 15:05:14 +0200 Subject: [PATCH] Properties changed via the Iframe API now trigger changes directly Changes performed in WA.room.setPropertyLayer now have a real-time impact. If the property is changed on a layer the current player is on, the changes will be triggered. --- front/src/Phaser/Game/GameMap.ts | 35 +++++++++++++++++++++++++++++- front/src/Phaser/Game/GameScene.ts | 22 +------------------ 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/front/src/Phaser/Game/GameMap.ts b/front/src/Phaser/Game/GameMap.ts index 98583cba..23e5c4e9 100644 --- a/front/src/Phaser/Game/GameMap.ts +++ b/front/src/Phaser/Game/GameMap.ts @@ -80,8 +80,11 @@ export class GameMap { return; } this.key = key; + this.triggerAll(); + } - const newProps = this.getProperties(key); + private triggerAll(): void { + const newProps = this.getProperties(this.key ?? 0); const oldProps = this.lastProperties; this.lastProperties = newProps; @@ -253,4 +256,34 @@ export class GameMap { } return this.tileNameMap.get(tile); } + + public setLayerProperty( + layerName: string, + propertyName: string, + propertyValue: string | number | undefined | boolean + ) { + const layer = this.findLayer(layerName); + if (layer === undefined) { + console.warn('Could not find layer "' + layerName + '" when calling setProperty'); + return; + } + if (layer.properties === undefined) { + layer.properties = []; + } + const property = layer.properties.find((property) => property.name === propertyName); + if (property === undefined) { + if (propertyValue === undefined) { + return; + } + layer.properties.push({ name: propertyName, type: typeof propertyValue, value: propertyValue }); + return; + } + if (propertyValue === undefined) { + const index = layer.properties.indexOf(property); + layer.properties.splice(index, 1); + } + property.value = propertyValue; + + this.triggerAll(); + } } diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index bd6890dc..da149690 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -1241,30 +1241,10 @@ ${escapedMessage} propertyName: string, propertyValue: string | number | boolean | undefined ): void { - const layer = this.gameMap.findLayer(layerName); - if (layer === undefined) { - console.warn('Could not find layer "' + layerName + '" when calling setProperty'); - return; - } if (propertyName === "exitUrl" && typeof propertyValue === "string") { this.loadNextGameFromExitUrl(propertyValue); } - if (layer.properties === undefined) { - layer.properties = []; - } - const property = layer.properties.find((property) => property.name === propertyName); - if (property === undefined) { - if (propertyValue === undefined) { - return; - } - layer.properties.push({ name: propertyName, type: typeof propertyValue, value: propertyValue }); - return; - } - if (propertyValue === undefined) { - const index = layer.properties.indexOf(property); - layer.properties.splice(index, 1); - } - property.value = propertyValue; + this.gameMap.setLayerProperty(layerName, propertyName, propertyValue); } private setLayerVisibility(layerName: string, visible: boolean): void {