From f536d538ead53f43885c319e6393f5863ec8f3c7 Mon Sep 17 00:00:00 2001 From: jonny Date: Fri, 25 Jun 2021 17:35:42 +0200 Subject: [PATCH] added backwards compatible check and maps --- front/src/Phaser/Game/GameMap.ts | 4 ++ front/src/Phaser/Game/GameScene.ts | 21 +++---- maps/tests/function_tiles.json | 33 ++++++++++ maps/tests/function_tiles.png | Bin 0 -> 1313 bytes maps/tests/start-tile.json | 95 +++++++++++++++++++++++++++++ 5 files changed, 142 insertions(+), 11 deletions(-) create mode 100644 maps/tests/function_tiles.json create mode 100644 maps/tests/function_tiles.png create mode 100644 maps/tests/start-tile.json diff --git a/front/src/Phaser/Game/GameMap.ts b/front/src/Phaser/Game/GameMap.ts index b84ec477..7c446dbf 100644 --- a/front/src/Phaser/Game/GameMap.ts +++ b/front/src/Phaser/Game/GameMap.ts @@ -17,6 +17,8 @@ export class GameMap { public exitUrls: Array = [] + public hasStartTile = false; + public constructor(private map: ITiledMap) { this.layersIterator = new LayersIterator(map); @@ -27,6 +29,8 @@ export class GameMap { tile.properties.forEach(prop => { if (prop.name == "exitUrl" && typeof prop.value == "string") { this.exitUrls.push(prop.value); + } else if (prop.name == "start") { + this.hasStartTile = true } }) } diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 30b92a91..98e990ef 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -1043,7 +1043,7 @@ ${escapedMessage} } else { // Now, let's find the start layer if (this.startLayerName) { - this.initPositionFromLayerName(this.startLayerName, null); + this.initPositionFromLayerName(this.startLayerName, this.startLayerName); } if (this.startX === undefined) { // If we have no start layer specified or if the hash passed does not exist, let's go with the default start position. @@ -1059,10 +1059,10 @@ ${escapedMessage} } } - private initPositionFromLayerName(layerName: string, startLayerName: string | null) { + private initPositionFromLayerName(selectedOrdDefaultLayer: string, selectedLayer: string | null) { for (const layer of this.gameMap.layersIterator) { - if ((layerName === layer.name || layer.name.endsWith('/' + layerName)) && layer.type === 'tilelayer' && (layerName === defaultStartLayerName || this.isStartLayer(layer))) { - const startPosition = this.startUser(layer, startLayerName); + if ((selectedOrdDefaultLayer === layer.name || layer.name.endsWith('/' + selectedOrdDefaultLayer)) && layer.type === 'tilelayer' && (selectedOrdDefaultLayer === defaultStartLayerName || this.isStartLayer(layer))) { + const startPosition = this.startUser(layer, selectedLayer); this.startX = startPosition.x + this.mapFile.tilewidth / 2; this.startY = startPosition.y + this.mapFile.tileheight / 2; } @@ -1116,8 +1116,8 @@ ${escapedMessage} return gameManager.loadMap(room, this.scene).catch(() => { }); } - private startUser(layer: ITiledMapTileLayer, startName: string | null): PositionInterface { - const tiles = layer.data; + private startUser(selectedOrDefaultLayer: ITiledMapTileLayer, startName: string | null): PositionInterface { + const tiles = selectedOrDefaultLayer.data; if (typeof (tiles) === 'string') { throw new Error('The content of a JSON map must be filled as a JSON array, not as a string'); } @@ -1126,21 +1126,20 @@ ${escapedMessage} if (objectKey === 0) { return; } - const y = Math.floor(key / layer.width); - const x = key % layer.width; + const y = Math.floor(key / selectedOrDefaultLayer.width); + const x = key % selectedOrDefaultLayer.width; - if (startName) { + if (startName && this.gameMap.hasStartTile) { const properties = this.gameMap.getPropertiesForIndex(objectKey); if (!properties.length || !properties.some(property => property.name == "start" && property.value == startName)) { return } } - possibleStartPositions.push({ x: x * this.mapFile.tilewidth, y: y * this.mapFile.tilewidth }); }); // Get a value at random amongst allowed values if (possibleStartPositions.length === 0) { - console.warn('The start layer "' + layer.name + '" for this map is empty.'); + console.warn('The start layer "' + selectedOrDefaultLayer.name + '" for this map is empty.'); return { x: 0, y: 0 diff --git a/maps/tests/function_tiles.json b/maps/tests/function_tiles.json new file mode 100644 index 00000000..9bc374eb --- /dev/null +++ b/maps/tests/function_tiles.json @@ -0,0 +1,33 @@ +{ "columns":2, + "image":"function_tiles.png", + "imageheight":64, + "imagewidth":64, + "margin":0, + "name":"function_tiles", + "spacing":0, + "tilecount":4, + "tiledversion":"1.6.0", + "tileheight":32, + "tiles":[ + { + "id":0, + "properties":[ + { + "name":"start", + "type":"string", + "value":"S1" + }] + }, + { + "id":1, + "properties":[ + { + "name":"start", + "type":"string", + "value":"S2" + }] + }], + "tilewidth":32, + "type":"tileset", + "version":"1.6" +} \ No newline at end of file diff --git a/maps/tests/function_tiles.png b/maps/tests/function_tiles.png new file mode 100644 index 0000000000000000000000000000000000000000..147eb61942894f428ec11dd1bb40271a2c9d7bdd GIT binary patch literal 1313 zcmV++1>X9JP)WFU8GbZ8()Nlj2>E@cM*00f0eL_t(|+U=W7j1)x_ z$A2@7kwl1r>;Nu!@CJI&17HkXICwDxv+S3$#&}T!CyyqCgd<0ecu^B}$ujH)Tw~xu z0tXT}5hCCPBd#C7u4_yrh7Wg}L%qt>7G2%dJ>9+Y(f?%9+tu~)>Q%judi55v!XE2A z@NQYL3kHAfU)H)R&kuC7oI8!lVehkwd z$9MDh91a5Sc=G#TW@N{15PMx*TsBEu4@~${Pz@=27nt_#pk|F;iFoe?ya`-#2mjsSMatSS#X8+74a<{%(Z_ z75P0#+QAv%g`#&(|Gw5;P zKj3#@cM{*i`o8FB*H`ZHME0Ondy&R93Ph{hSTEliS#qH2;7ZNfXcNGKY@m@W38*>B z>%gc*L@s__l+n0o{5_Y~CUg;S8kh&x$;_*H`BSwB?N@Ou$uKb0HDXwzL|fuJ;4$#6 zQMMJhTEyAvCU5~*ZRz}5z#B0H3^*JXvuuAH0k?r8DSf&}z{ZI0uK`bexG)ppK1C6r zHRT3& zp+aZVMG!DA1C7anR3B6~6?HH{SRKfLR4+l{QlOoSf5k^AoPZzYxi-(Zj*Ean*^K*) zeNiX@Q^23V_r_987Da%L1#C9nr^V;%D?$;~f%ihBBA2DG31|Sn0p}y6qLHT_h%(P} z^xl17i4rABtSkm-W*-C|r)-}(zSxP1mCZ_+;Vvp>w#%ybHt-ejv(%df&H_8KV8A1p z#KshDTVcRWX?p>c8nRj(gb|u)A{tkKPk>iZNgQuUSpfXhDg#EX)x0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