From 74dda8ab69715733c347c007c2668729afac1728 Mon Sep 17 00:00:00 2001 From: jonny Date: Sat, 19 Jun 2021 15:17:28 +0200 Subject: [PATCH] allow properties on tiles # Conflicts: # front/src/Phaser/Game/GameMap.ts # front/src/Phaser/Map/ITiledMap.ts --- front/src/Phaser/Game/GameMap.ts | 48 ++++++++++++++++++++++++++++++ front/src/Phaser/Game/GameScene.ts | 5 ++++ front/src/Phaser/Map/ITiledMap.ts | 9 +++++- 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/front/src/Phaser/Game/GameMap.ts b/front/src/Phaser/Game/GameMap.ts index 7c93a702..dd567585 100644 --- a/front/src/Phaser/Game/GameMap.ts +++ b/front/src/Phaser/Game/GameMap.ts @@ -1,5 +1,8 @@ import type {ITiledMap, ITiledMapLayer} from "../Map/ITiledMap"; import {LayersIterator} from "../Map/LayersIterator"; +import { CustomVector, Vector2 } from '../../utility/vector'; +import type { ITiledMap, ITiledMapLayerProperty } from "../Map/ITiledMap"; +import { LayersIterator } from "../Map/LayersIterator"; export type PropertyChangeCallback = (newValue: string | number | boolean | undefined, oldValue: string | number | boolean | undefined, allProps: Map) => void; @@ -11,12 +14,37 @@ export class GameMap { private key: number|undefined; private lastProperties = new Map(); private callbacks = new Map>(); + + /** + * tileset.firstgid => (Map>) + */ + private tileSetPropertyMap = new Map>>() public readonly layersIterator: LayersIterator; + public exitUrls: Array = [] + public constructor(private map: ITiledMap) { this.layersIterator = new LayersIterator(map); + + for (const tileset of map.tilesets) { + if (!this.tileSetPropertyMap.has(tileset.firstgid)) { + this.tileSetPropertyMap.set(tileset.firstgid, new Map()) + } + tileset?.tiles?.forEach(tile => { + if (tile.properties) { + this.tileSetPropertyMap.get(tileset.firstgid)?.set(tile.id, tile.properties) + tile.properties.forEach(prop => { + if (prop.name == "exitUrl" && typeof prop.value == "string") { + this.exitUrls.push(prop.value); + } + }) + } + }) + } } + + /** * Sets the position of the current player (in pixels) * This will trigger events if properties are changing. @@ -59,12 +87,15 @@ export class GameMap { const properties = new Map(); for (const layer of this.layersIterator) { + + let tileIndex: number | undefined = undefined; if (layer.type !== 'tilelayer') { continue; } const tiles = layer.data as number[]; if (tiles[key] == 0) { continue; + tileIndex = tiles[key] } // There is a tile in this layer, let's embed the properties if (layer.properties !== undefined) { @@ -75,6 +106,23 @@ export class GameMap { properties.set(layerProperty.name, layerProperty.value); } } + + if (tileIndex) { + const tileset = this.map.tilesets.find(tileset => tileset.firstgid + tileset.tilecount > (tileIndex as number)) + if (tileset) { + const tileProperties = this.tileSetPropertyMap.get(tileset?.firstgid)?.get(tileIndex - tileset.firstgid) + if (tileProperties) { + for (const property of tileProperties) { + if (property.value) { + properties.set(property.name, property.value) + } else if (properties.has(property.name)) { + properties.delete(property.name) + } + } + } + } + + } } return properties; diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index b5876d5a..7d01de46 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -447,6 +447,11 @@ export class GameScene extends DirtyScene implements CenterListener { } } } + + this.gameMap.exitUrls.forEach(exitUrl => { + this.loadNextGame(exitUrl) + }) + if (depth === -2) { throw new Error('Your map MUST contain a layer of type "objectgroup" whose name is "floorLayer" that represents the layer characters are drawn at. This layer cannot be contained in a group.'); } diff --git a/front/src/Phaser/Map/ITiledMap.ts b/front/src/Phaser/Map/ITiledMap.ts index c4828911..91a29397 100644 --- a/front/src/Phaser/Map/ITiledMap.ts +++ b/front/src/Phaser/Map/ITiledMap.ts @@ -167,7 +167,7 @@ export interface ITiledTileSet { tilewidth: number; transparentcolor: string; terrains: ITiledMapTerrain[]; - tiles: {[key: string]: { terrain: number[] }}; + tiles?: Array; /** * Refers to external tileset file (should be JSON) @@ -175,6 +175,13 @@ export interface ITiledTileSet { source: string; } +export interface ITile { + id: number, + type?: string + + properties?: Array +} + export interface ITiledMapTerrain { name: string; tile: number;