From 6dcb0d3750f78838ac03f89131bce9132489869c Mon Sep 17 00:00:00 2001 From: GRL Date: Thu, 27 May 2021 17:06:39 +0200 Subject: [PATCH] first step to change tile --- front/src/Api/Events/ChangeTileEvent.ts | 14 +- front/src/Phaser/Game/GameScene.ts | 17 +- front/src/iframe_api.ts | 21 +- maps/tests/Metadata/changeTile.html | 28 +++ maps/tests/Metadata/changeTile.json | 275 ++++++++++++++++++++++++ 5 files changed, 334 insertions(+), 21 deletions(-) create mode 100644 maps/tests/Metadata/changeTile.html create mode 100644 maps/tests/Metadata/changeTile.json diff --git a/front/src/Api/Events/ChangeTileEvent.ts b/front/src/Api/Events/ChangeTileEvent.ts index 23599881..4a071403 100644 --- a/front/src/Api/Events/ChangeTileEvent.ts +++ b/front/src/Api/Events/ChangeTileEvent.ts @@ -1,12 +1,14 @@ import * as tg from "generic-type-guard"; export const isChangeTileEvent = - new tg.IsInterface().withProperties({ - x: tg.isNumber, - y: tg.isNumber, - tile: tg.isUnion(tg.isNumber, tg.isString), - layer: tg.isUnion(tg.isNumber, tg.isString) - }).get(); + tg.isArray( + new tg.IsInterface().withProperties({ + x: tg.isNumber, + y: tg.isNumber, + tile: tg.isUnion(tg.isNumber, tg.isString), + layer: tg.isString + }).get() + ); /** * A message sent from the game to the iFrame when a user enters or leaves a zone marked with the "zone" property. */ diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index b314fe3b..cb820ba4 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -890,19 +890,12 @@ ${escapedMessage} this.userInputManager.restoreControls(); })) -/* this.iframeSubscriptionList.push(iframeListener.loadPageStream.subscribe((url: string) => { - this.loadNextGame(url).then(() => { - this.events.once(EVENT_TYPE.POST_UPDATE, () => { - this.onMapExit(url); - }) - }) - }))*/ - - this.iframeSubscriptionList.push(iframeListener.updateTileEvent.subscribe(event => { + this.iframeSubscriptionList.push(iframeListener.changeTileStream.subscribe(event => { for (const eventTile of event) { const layer = this.gameMap.findPhaserLayer(eventTile.layer); if (layer) { - const tile = layer.getTileAt(eventTile.x, eventTile.y) + console.log('layer : ', layer); + const tile = layer.getTileAt(eventTile.x, eventTile.y, true) if (typeof eventTile.tile == "string") { const tileIndex = this.getIndexForTileType(eventTile.tile); if (tileIndex) { @@ -911,11 +904,11 @@ ${escapedMessage} return } } else { - tile.index = eventTile.tile + tile.index = eventTile.tile //+ firsrtgid du layer } } } - this.scene.scene.sys.game.events.emit("contextrestored") + //this.dirty = true; })) let scriptedBubbleSprite: Sprite; diff --git a/front/src/iframe_api.ts b/front/src/iframe_api.ts index a390e616..b8b5ccdf 100644 --- a/front/src/iframe_api.ts +++ b/front/src/iframe_api.ts @@ -44,9 +44,10 @@ interface WorkAdventureApi { displayBubble(): void; removeBubble(): void; loadSound(url : string): Sound; - registerMenuCommand(commandDescriptor: string, callback: (commandDescriptor: string) => void): void - getCurrentUser(): Promise - getCurrentRoom(): Promise + registerMenuCommand(commandDescriptor: string, callback: (commandDescriptor: string) => void): void; + getCurrentUser(): Promise; + getCurrentRoom(): Promise; + changeTile(tiles: TileDescriptor[]): void; //loadTileset(name: string, imgUrl : string, tilewidth : number, tileheight : number, margin : number, spacing : number): void; onPlayerMove(callback: (playerMovedEvent: HasPlayerMovedEvent) => void): void @@ -65,6 +66,13 @@ interface Room { startLayer: string | null } +interface TileDescriptor { + x: number + y: number + tile: number | string + layer: string +} + declare global { // eslint-disable-next-line no-var var WA: WorkAdventureApi @@ -221,6 +229,13 @@ window.WA = { }) }, + changeTile(tiles: TileDescriptor[]) { + postToParent({ + type: 'changeTile', + data: tiles + }) + }, + /** * Send a message in the chat. * Only the local user will receive this message. diff --git a/maps/tests/Metadata/changeTile.html b/maps/tests/Metadata/changeTile.html new file mode 100644 index 00000000..214908a9 --- /dev/null +++ b/maps/tests/Metadata/changeTile.html @@ -0,0 +1,28 @@ + + + + + + + + + \ No newline at end of file diff --git a/maps/tests/Metadata/changeTile.json b/maps/tests/Metadata/changeTile.json new file mode 100644 index 00000000..48f57e0f --- /dev/null +++ b/maps/tests/Metadata/changeTile.json @@ -0,0 +1,275 @@ +{ "compressionlevel":-1, + "height":10, + "infinite":false, + "layers":[ + { + "data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + "height":10, + "id":1, + "name":"start", + "opacity":1, + "type":"tilelayer", + "visible":true, + "width":10, + "x":0, + "y":0 + }, + { + "data":[33, 34, 34, 34, 34, 34, 34, 34, 34, 35, 41, 42, 42, 42, 42, 42, 42, 42, 42, 43, 41, 42, 42, 42, 42, 42, 42, 42, 42, 43, 41, 42, 42, 42, 42, 42, 42, 42, 42, 43, 41, 42, 42, 42, 42, 42, 42, 42, 42, 43, 41, 42, 42, 42, 42, 42, 42, 42, 42, 43, 41, 42, 42, 42, 42, 42, 42, 42, 42, 43, 41, 42, 42, 42, 42, 42, 42, 42, 42, 43, 41, 42, 42, 42, 42, 42, 42, 42, 42, 43, 49, 50, 50, 50, 50, 50, 50, 50, 50, 51], + "height":10, + "id":2, + "name":"bottom", + "opacity":1, + "type":"tilelayer", + "visible":true, + "width":10, + "x":0, + "y":0 + }, + { + "data":[0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + "height":10, + "id":4, + "name":"metadata", + "opacity":1, + "properties":[ + { + "name":"openWebsite", + "type":"string", + "value":"changeTile.html" + }, + { + "name":"openWebsiteAllowApi", + "type":"bool", + "value":true + }], + "type":"tilelayer", + "visible":true, + "width":10, + "x":0, + "y":0 + }, + { + "data":[65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65, 0, 0, 0, 0, 0, 65, 65, 65, 65, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0], + "height":10, + "id":8, + "name":"changeTile", + "opacity":1, + "type":"tilelayer", + "visible":true, + "width":10, + "x":0, + "y":0 + }, + { + "draworder":"topdown", + "id":5, + "name":"floorLayer", + "objects":[ + { + "height":159.866671635267, + "id":1, + "name":"", + "rotation":0, + "text": + { + "fontfamily":"Sans Serif", + "pixelsize":9, + "text":"Test : \nWalk on the grass\n\nResult : \nTiles of the first left colum become red tile (tile find by Number)\nTiles of the below the grass become blue (tile find by String)\n", + "wrap":true + }, + "type":"", + "visible":true, + "width":287.674838251912, + "x":32.5473600365393, + "y":160.305680721763 + }], + "opacity":1, + "type":"objectgroup", + "visible":true, + "x":0, + "y":0 + }], + "nextlayerid":9, + "nextobjectid":2, + "orientation":"orthogonal", + "renderorder":"right-down", + "tiledversion":"1.4.3", + "tileheight":32, + "tilesets":[ + { + "columns":8, + "firstgid":1, + "image":"tileset_dungeon.png", + "imageheight":256, + "imagewidth":256, + "margin":0, + "name":"TDungeon", + "spacing":0, + "tilecount":64, + "tileheight":32, + "tiles":[ + { + "id":0, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":1, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":2, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":3, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":4, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":8, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":9, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":10, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":11, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":12, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":16, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":17, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":18, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":19, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }, + { + "id":20, + "properties":[ + { + "name":"collides", + "type":"bool", + "value":true + }] + }], + "tilewidth":32 + }, + { + "columns":8, + "firstgid":65, + "image":"floortileset.png", + "imageheight":288, + "imagewidth":256, + "margin":0, + "name":"Floor", + "spacing":0, + "tilecount":72, + "tileheight":32, + "tiles":[ + { + "id":34, + "type":"Red" + }, + { + "id":44, + "type":"blue" + }], + "tilewidth":32 + }], + "tilewidth":32, + "type":"map", + "version":1.4, + "width":10 +} \ No newline at end of file