From b182a08ca2c6290e37570522ec49037fec45beb7 Mon Sep 17 00:00:00 2001 From: GRL Date: Mon, 28 Jun 2021 09:33:13 +0200 Subject: [PATCH] correction from review --- docs/maps/api-room.md | 9 ++++++ front/src/Api/IframeListener.ts | 2 -- front/src/Phaser/Game/GameMap.ts | 48 ++++++++++++++++++++++++++--- front/src/Phaser/Game/GameScene.ts | 30 +----------------- maps/tests/Metadata/changeTile.json | 29 +++++++++++++---- 5 files changed, 76 insertions(+), 42 deletions(-) diff --git a/docs/maps/api-room.md b/docs/maps/api-room.md index 3bb01cc0..74eafee7 100644 --- a/docs/maps/api-room.md +++ b/docs/maps/api-room.md @@ -118,6 +118,14 @@ WA.room.getCurrentUser().then((user) => { WA.room.changeTile(tiles: TileDescriptor[]): void ``` Replace the tile at the `x` and `y` coordinates in the layer named `layer` by the tile with the id `tile`. + +If `tile` is a string, it's not the id of the tile but the value of the property `name`. +
+
+ +
+
+ `TileDescriptor` has the following attributes : * **x (number) :** The coordinate x of the tile that you want to replace. * **y (number) :** The coordinate y of the tile that you want to replace. @@ -126,6 +134,7 @@ Replace the tile at the `x` and `y` coordinates in the layer named `layer` by th **Important !** : If you use `tile` as a number, be sure to add the `firstgid` of the tileset of the tile that you want to the id of the tile in Tiled Editor. + Example : ```javascript WA.room.changeTile([ diff --git a/front/src/Api/IframeListener.ts b/front/src/Api/IframeListener.ts index ac3723b7..cbe4dcf3 100644 --- a/front/src/Api/IframeListener.ts +++ b/front/src/Api/IframeListener.ts @@ -193,8 +193,6 @@ class IframeListener { this._unregisterMenuCommandStream.next(data); }) handleMenuItemRegistrationEvent(payload.data) - } else if (payload.type == "registerMenuCommand" && isMenuItemRegisterEvent(payload.data)) { - this._registerMenuCommandStream.next(payload.data.menutItem) } else if (payload.type == "changeTile" && isChangeTileEvent(payload.data)) { this._changeTileStream.next(payload.data); } diff --git a/front/src/Phaser/Game/GameMap.ts b/front/src/Phaser/Game/GameMap.ts index ab3e6010..aeb4cb62 100644 --- a/front/src/Phaser/Game/GameMap.ts +++ b/front/src/Phaser/Game/GameMap.ts @@ -13,8 +13,9 @@ export class GameMap { private key: number | undefined; private lastProperties = new Map(); private callbacks = new Map>(); + private tileNameMap = new Map(); - private tileSetPropertyMap: { [tile_index: number]: Array } = {} + private tileSetPropertyMap: { [tile_index: number]: Array } = {}; public readonly flatLayers: ITiledMapLayer[]; public readonly phaserLayers: TilemapLayer[] = []; @@ -36,6 +37,9 @@ export class GameMap { if (tile.properties) { this.tileSetPropertyMap[tileset.firstgid + tile.id] = tile.properties tile.properties.forEach(prop => { + if (prop.name == 'name' && typeof prop.value == "string") { + this.tileNameMap.set(prop.value, tileset.firstgid + tile.id); + } if (prop.name == "exitUrl" && typeof prop.value == "string") { this.exitUrls.push(prop.value); } @@ -130,8 +134,8 @@ export class GameMap { return this.map; } - public getTilesetProperties(): { [tile_index: number]: Array } { - return this.tileSetPropertyMap; + private getTileProperty(index: number): Array { + return this.tileSetPropertyMap[index]; } private trigger(propName: string, oldValue: string | number | boolean | undefined, newValue: string | number | boolean | undefined, allProps: Map) { @@ -169,13 +173,47 @@ export class GameMap { } } - public putTileInFlatLayer(index: number, x: number, y: number, layer: string): void { + private putTileInFlatLayer(index: number, x: number, y: number, layer: string): void { const fLayer = this.findLayer(layer); - if (fLayer?.type !== 'tilelayer') { + if ( fLayer == undefined ) { + console.error("The layer that you want to change doesn't exist."); + return; + } + if (fLayer.type !== 'tilelayer') { + console.error("The layer that you want to change is not a tilelayer. Tile can only be put in tilelayer."); return; } // @ts-ignore fLayer.data[x+y*fLayer.height] = index; } + public putTile(tile: string | number, x: number, y: number, layer: string): void { + const phaserLayer = this.findPhaserLayer(layer); + if ( phaserLayer ) { + const tileIndex = this.getIndexForTileType(tile); + if ( tileIndex !== undefined ) { + this.putTileInFlatLayer(tileIndex, x, y, layer); + const phaserTile = phaserLayer.putTileAt(tileIndex, x, y); + for (const property of this.getTileProperty(tileIndex)) { + if ( property.name === "collides" ) { + phaserTile.setCollision(true); + } + } + } + else { + console.error("The tile that you want to place doesn't exist."); + } + } + else { + console.error("The layer that you want to change is not a tilelayer. Tile can only be put in tilelayer."); + } + } + + private getIndexForTileType(tile: string | number): number | undefined { + if (typeof tile == "number") { + return tile; + } + return this.tileNameMap.get(tile); + } + } diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 9a46991a..a3a890e6 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -954,19 +954,7 @@ ${escapedMessage} })); this.iframeSubscriptionList.push(iframeListener.changeTileStream.subscribe((eventTiles) => { for (const eventTile of eventTiles) { - const layer = this.gameMap.findPhaserLayer(eventTile.layer); - if ( layer ) { - const tileIndex = this.getIndexForTileType(eventTile.tile); - if ( tileIndex ) { - this.gameMap.putTileInFlatLayer(tileIndex, eventTile.x, eventTile.y, eventTile.layer); - const tile = layer.putTileAt(tileIndex, eventTile.x, eventTile.y); - for (const property of this.gameMap.getTilesetProperties()[tileIndex]) { - if ( property.name === "collides" ) { - tile.setCollision(true); - } - } - } - } + this.gameMap.putTile(eventTile.tile, eventTile.x, eventTile.y, eventTile.layer); } })) @@ -997,22 +985,6 @@ ${escapedMessage} this.dirty = true; } - private getIndexForTileType(tileType: string | number): number | null { - if (typeof tileType == "number") { - return tileType; - } - for (const tileset of this.mapFile.tilesets) { - if (tileset.tiles) { - for (const tilesetTile of tileset.tiles) { - if (tilesetTile.type == tileType) { - return tileset.firstgid + tilesetTile.id - } - } - } - } - return null - } - private getMapDirUrl(): string { return this.MapUrlFile.substr(0, this.MapUrlFile.lastIndexOf('/')); } diff --git a/maps/tests/Metadata/changeTile.json b/maps/tests/Metadata/changeTile.json index 834ea9a1..bdf34704 100644 --- a/maps/tests/Metadata/changeTile.json +++ b/maps/tests/Metadata/changeTile.json @@ -82,12 +82,12 @@ { "fontfamily":"Sans Serif", "pixelsize":9, - "text":"Test : \nWalk on the grass\n\nResult : \nThe Yellow Tile open a jitsi with Trigger.\n\nThe Red Tile open cowebsite Wikip\u00e9dia. The highest Red Tile is find by 'string' index, the lowest by 'number' index.\n\nThe White Tile are silent tile. You can not open a bubble in it. (Even if the other player didn't activate the script.)\n\nThe Pale Tile (Lowest) is an exitUrl tile to customMenu.json.\n\nThe Blue Tile are 'collides' tile. The two tile in the center are 'number' index. The others are 'string' index.\n", + "text":"Test : \nWalk on the grass\n\nResult : \nThe Yellow Tile open a jitsi with Trigger.\n\nThe Red Tile open cowebsite Wikip\u00e9dia. The highest Red Tile is find by 'name' property index, the lowest by 'number' index.\n\nThe White Tile are silent tile. You can not open a bubble in it. (Even if the other player didn't activate the script.)\n\nThe Pale Tile (Lowest) is an exitUrl tile to customMenu.json.\n\nThe Blue Tile are 'collides' tile. The two tile in the center are 'number' index. The others are 'name' property index.\n", "wrap":true }, "type":"", "visible":true, - "width":287.674838251912, + "width":274.674838251912, "x":32.5473600365393, "y":128.305680721763 }], @@ -296,12 +296,25 @@ { "id":34, "properties":[ + { + "name":"name", + "type":"string", + "value":"Red" + }, { "name":"openWebsite", "type":"string", "value":"https:\/\/fr.wikipedia.org\/wiki\/Wikip%C3%A9dia:Accueil_principal" - }], - "type":"Red" + }] + }, + { + "id":40, + "properties":[ + { + "name":"name", + "type":"string", + "value":"" + }] }, { "id":44, @@ -310,8 +323,12 @@ "name":"collides", "type":"bool", "value":true - }], - "type":"blue" + }, + { + "name":"name", + "type":"string", + "value":"blue" + }] }, { "id":52,