getMapProperty

This commit is contained in:
_Bastler
2021-11-24 14:32:45 +01:00
parent bc7d9a96fe
commit 16a73d4de6
5 changed files with 66 additions and 16 deletions
+13
View File
@@ -0,0 +1,13 @@
import * as tg from "generic-type-guard";
export const isGetPropertyEvent = new tg.IsInterface()
.withProperties({
layerName: tg.isString,
propertyName: tg.isString,
propertyValue: tg.isUnion(tg.isString, tg.isUnion(tg.isNumber, tg.isUnion(tg.isBoolean, tg.isUndefined))),
})
.get();
/**
* A message sent from the iFrame to the game to change the value of the property of the layer
*/
export type GetPropertyEvent = tg.GuardedType<typeof isGetPropertyEvent>;
+5
View File
@@ -30,6 +30,7 @@ import type {
import { isMessageReferenceEvent, isTriggerActionMessageEvent } from "./ui/TriggerActionMessageEvent"; import { isMessageReferenceEvent, isTriggerActionMessageEvent } from "./ui/TriggerActionMessageEvent";
import type { MenuRegisterEvent, UnregisterMenuEvent } from "./ui/MenuRegisterEvent"; import type { MenuRegisterEvent, UnregisterMenuEvent } from "./ui/MenuRegisterEvent";
import type { ChangeLayerEvent } from "./ChangeLayerEvent"; import type { ChangeLayerEvent } from "./ChangeLayerEvent";
import { isGetPropertyEvent } from "./GetPropertyEvent";
export interface TypedMessageEvent<T> extends MessageEvent { export interface TypedMessageEvent<T> extends MessageEvent {
data: T; data: T;
@@ -103,6 +104,10 @@ export const iframeQueryMapTypeGuards = {
query: tg.isUndefined, query: tg.isUndefined,
answer: isGameStateEvent, answer: isGameStateEvent,
}, },
getProperty: {
query: isGetPropertyEvent,
answer: isGetPropertyEvent,
},
getMapData: { getMapData: {
query: tg.isUndefined, query: tg.isUndefined,
answer: isMapDataEvent, answer: isMapDataEvent,
+11
View File
@@ -130,6 +130,17 @@ export class WorkadventureRoomCommands extends IframeApiContribution<Workadventu
}); });
} }
async getProperty(layerName: string, propertyName: string): Promise<string | number | boolean | undefined> {
const event = await queryWorkadventure({
type: "getProperty", data: {
layerName: layerName,
propertyName: propertyName,
propertyValue : undefined,
}
});
return event.propertyValue;
}
async getTiledMap(): Promise<ITiledMap> { async getTiledMap(): Promise<ITiledMap> {
const event = await queryWorkadventure({ type: "getMapData", data: undefined }); const event = await queryWorkadventure({ type: "getMapData", data: undefined });
return event.data as ITiledMap; return event.data as ITiledMap;
+16
View File
@@ -323,6 +323,22 @@ export class GameMap {
return this.tileNameMap.get(tile); return this.tileNameMap.get(tile);
} }
public getLayerProperty(
layerName: string,
propertyName: string
) : 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);
return property?.value;
}
public setLayerProperty( public setLayerProperty(
layerName: string, layerName: string,
propertyName: string, propertyName: string,
+21 -16
View File
@@ -270,7 +270,7 @@ export class GameScene extends DirtyScene {
// So if we are in https, we can still try to load a HTTP local resource (can be useful for testing purposes) // So if we are in https, we can still try to load a HTTP local resource (can be useful for testing purposes)
// See https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts#when_is_a_context_considered_secure // See https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts#when_is_a_context_considered_secure
const url = new URL(file.src); const url = new URL(file.src);
const host = url.host.split(":")[0]; const host = url.host.split(":")[ 0 ];
if ( if (
window.location.protocol === "https:" && window.location.protocol === "https:" &&
file.src === this.MapUrlFile && file.src === this.MapUrlFile &&
@@ -326,8 +326,8 @@ export class GameScene extends DirtyScene {
//eslint-disable-next-line @typescript-eslint/no-explicit-any //eslint-disable-next-line @typescript-eslint/no-explicit-any
(this.load as any).rexWebFont({ (this.load as any).rexWebFont({
custom: { custom: {
families: ["Press Start 2P"], families: [ "Press Start 2P" ],
urls: ["/resources/fonts/fonts.css"], urls: [ "/resources/fonts/fonts.css" ],
testString: "abcdefg", testString: "abcdefg",
}, },
}); });
@@ -373,7 +373,7 @@ export class GameScene extends DirtyScene {
} }
} }
for (const [itemType, objectsOfType] of this.objectsByType) { for (const [ itemType, objectsOfType ] of this.objectsByType) {
// FIXME: we would ideally need for the loader to WAIT for the import to be performed, which means writing our own loader plugin. // FIXME: we would ideally need for the loader to WAIT for the import to be performed, which means writing our own loader plugin.
let itemFactory: ItemFactoryInterface; let itemFactory: ItemFactoryInterface;
@@ -404,7 +404,7 @@ export class GameScene extends DirtyScene {
// TODO: we should pass here a factory to create sprites (maybe?) // TODO: we should pass here a factory to create sprites (maybe?)
// Do we have a state for this object? // Do we have a state for this object?
const state = roomJoinedAnswer.items[object.id]; const state = roomJoinedAnswer.items[ object.id ];
const actionableItem = itemFactory.factory(this, object, state); const actionableItem = itemFactory.factory(this, object, state);
this.actionableItems.set(actionableItem.getId(), actionableItem); this.actionableItems.set(actionableItem.getId(), actionableItem);
@@ -634,7 +634,7 @@ export class GameScene extends DirtyScene {
} }
}); });
Promise.all([this.connectionAnswerPromise as Promise<unknown>, ...scriptPromises]).then(() => { Promise.all([ this.connectionAnswerPromise as Promise<unknown>, ...scriptPromises ]).then(() => {
this.scene.wake(); this.scene.wake();
}); });
} }
@@ -722,8 +722,8 @@ export class GameScene extends DirtyScene {
if (item === undefined) { if (item === undefined) {
console.warn( console.warn(
'Received an event about object "' + 'Received an event about object "' +
message.itemId + message.itemId +
'" but cannot find this item on the map.' '" but cannot find this item on the map.'
); );
return; return;
} }
@@ -931,8 +931,8 @@ export class GameScene extends DirtyScene {
} else { } else {
console.error( console.error(
"Error while opening a popup. Cannot find an object on the map with name '" + "Error while opening a popup. Cannot find an object on the map with name '" +
openPopupEvent.targetObject + openPopupEvent.targetObject +
"'. The first parameter of WA.openPopup() must be the name of a rectangle object in your map." "'. The first parameter of WA.openPopup() must be the name of a rectangle object in your map."
); );
return; return;
} }
@@ -1151,6 +1151,11 @@ export class GameScene extends DirtyScene {
}); });
}); });
iframeListener.registerAnswerer("getProperty", (data) => {
data.propertyValue = this.gameMap.getLayerProperty(data.layerName, data.propertyName);
return data;
});
iframeListener.registerAnswerer("getMapData", () => { iframeListener.registerAnswerer("getMapData", () => {
return { return {
data: this.gameMap.getMap(), data: this.gameMap.getMap(),
@@ -1191,7 +1196,7 @@ export class GameScene extends DirtyScene {
const jsonTilesetDir = eventTileset.url.substr(0, eventTileset.url.lastIndexOf("/")); const jsonTilesetDir = eventTileset.url.substr(0, eventTileset.url.lastIndexOf("/"));
//Initialise the firstgid to 1 because if there is no tileset in the tilemap, the firstgid will be 1 //Initialise the firstgid to 1 because if there is no tileset in the tilemap, the firstgid will be 1
let newFirstgid = 1; let newFirstgid = 1;
const lastTileset = this.mapFile.tilesets[this.mapFile.tilesets.length - 1]; const lastTileset = this.mapFile.tilesets[ this.mapFile.tilesets.length - 1 ];
if (lastTileset) { if (lastTileset) {
//If there is at least one tileset in the tilemap then calculate the firstgid of the new tileset //If there is at least one tileset in the tilemap then calculate the firstgid of the new tileset
newFirstgid = lastTileset.firstgid + lastTileset.tilecount; newFirstgid = lastTileset.firstgid + lastTileset.tilecount;
@@ -1291,14 +1296,14 @@ export class GameScene extends DirtyScene {
if (phaserLayers === []) { if (phaserLayers === []) {
console.warn( console.warn(
'Could not find layer with name that contains "' + 'Could not find layer with name that contains "' +
layerName + layerName +
'" when calling WA.hideLayer / WA.showLayer' '" when calling WA.hideLayer / WA.showLayer'
); );
return; return;
} }
for (let i = 0; i < phaserLayers.length; i++) { for (let i = 0; i < phaserLayers.length; i++) {
phaserLayers[i].setVisible(visible); phaserLayers[ i ].setVisible(visible);
phaserLayers[i].setCollisionByProperty({ collides: true }, visible); phaserLayers[ i ].setCollisionByProperty({ collides: true }, visible);
} }
} }
this.markDirty(); this.markDirty();
@@ -1904,7 +1909,7 @@ export class GameScene extends DirtyScene {
const silent = this.gameMap.getCurrentProperties().get("silent"); const silent = this.gameMap.getCurrentProperties().get("silent");
this.connection?.setSilent(!!silent); this.connection?.setSilent(!!silent);
mediaManager.showGameOverlay(); mediaManager.showGameOverlay();
} }
analyticsClient.enteredJitsi(roomName, this.room.id); analyticsClient.enteredJitsi(roomName, this.room.id);