correction from review

This commit is contained in:
GRL 2021-06-28 09:33:13 +02:00
parent c79603d7fa
commit b182a08ca2
5 changed files with 76 additions and 42 deletions

View File

@ -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`.
<div class="row">
<div class="col">
<img src="https://workadventu.re/img/docs/nameIndexProperty.png" class="figure-img img-fluid rounded" alt="" />
</div>
</div>
`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([

View File

@ -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);
}

View File

@ -13,8 +13,9 @@ export class GameMap {
private key: number | undefined;
private lastProperties = new Map<string, string | boolean | number>();
private callbacks = new Map<string, Array<PropertyChangeCallback>>();
private tileNameMap = new Map<string, number>();
private tileSetPropertyMap: { [tile_index: number]: Array<ITiledMapLayerProperty> } = {}
private tileSetPropertyMap: { [tile_index: number]: Array<ITiledMapLayerProperty> } = {};
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<ITiledMapLayerProperty> } {
return this.tileSetPropertyMap;
private getTileProperty(index: number): Array<ITiledMapLayerProperty> {
return this.tileSetPropertyMap[index];
}
private trigger(propName: string, oldValue: string | number | boolean | undefined, newValue: string | number | boolean | undefined, allProps: Map<string, string | boolean | number>) {
@ -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);
}
}

View File

@ -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('/'));
}

View File

@ -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,