allow properties on tiles
# Conflicts: # front/src/Phaser/Game/GameMap.ts # front/src/Phaser/Map/ITiledMap.ts
This commit is contained in:
parent
61809aad21
commit
74dda8ab69
@ -1,5 +1,8 @@
|
|||||||
import type {ITiledMap, ITiledMapLayer} from "../Map/ITiledMap";
|
import type {ITiledMap, ITiledMapLayer} from "../Map/ITiledMap";
|
||||||
import {LayersIterator} from "../Map/LayersIterator";
|
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<string, string | boolean | number>) => void;
|
export type PropertyChangeCallback = (newValue: string | number | boolean | undefined, oldValue: string | number | boolean | undefined, allProps: Map<string, string | boolean | number>) => void;
|
||||||
|
|
||||||
@ -11,12 +14,37 @@ export class GameMap {
|
|||||||
private key: number|undefined;
|
private key: number|undefined;
|
||||||
private lastProperties = new Map<string, string|boolean|number>();
|
private lastProperties = new Map<string, string|boolean|number>();
|
||||||
private callbacks = new Map<string, Array<PropertyChangeCallback>>();
|
private callbacks = new Map<string, Array<PropertyChangeCallback>>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* tileset.firstgid => (Map<tileid,Array<ITiledMapLayerProperty>>)
|
||||||
|
*/
|
||||||
|
private tileSetPropertyMap = new Map<number, Map<number, Array<ITiledMapLayerProperty>>>()
|
||||||
public readonly layersIterator: LayersIterator;
|
public readonly layersIterator: LayersIterator;
|
||||||
|
|
||||||
|
public exitUrls: Array<string> = []
|
||||||
|
|
||||||
public constructor(private map: ITiledMap) {
|
public constructor(private map: ITiledMap) {
|
||||||
this.layersIterator = new LayersIterator(map);
|
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)
|
* Sets the position of the current player (in pixels)
|
||||||
* This will trigger events if properties are changing.
|
* This will trigger events if properties are changing.
|
||||||
@ -59,12 +87,15 @@ export class GameMap {
|
|||||||
const properties = new Map<string, string|boolean|number>();
|
const properties = new Map<string, string|boolean|number>();
|
||||||
|
|
||||||
for (const layer of this.layersIterator) {
|
for (const layer of this.layersIterator) {
|
||||||
|
|
||||||
|
let tileIndex: number | undefined = undefined;
|
||||||
if (layer.type !== 'tilelayer') {
|
if (layer.type !== 'tilelayer') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const tiles = layer.data as number[];
|
const tiles = layer.data as number[];
|
||||||
if (tiles[key] == 0) {
|
if (tiles[key] == 0) {
|
||||||
continue;
|
continue;
|
||||||
|
tileIndex = tiles[key]
|
||||||
}
|
}
|
||||||
// There is a tile in this layer, let's embed the properties
|
// There is a tile in this layer, let's embed the properties
|
||||||
if (layer.properties !== undefined) {
|
if (layer.properties !== undefined) {
|
||||||
@ -75,6 +106,23 @@ export class GameMap {
|
|||||||
properties.set(layerProperty.name, layerProperty.value);
|
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;
|
return properties;
|
||||||
|
@ -447,6 +447,11 @@ export class GameScene extends DirtyScene implements CenterListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.gameMap.exitUrls.forEach(exitUrl => {
|
||||||
|
this.loadNextGame(exitUrl)
|
||||||
|
})
|
||||||
|
|
||||||
if (depth === -2) {
|
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.');
|
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.');
|
||||||
}
|
}
|
||||||
|
@ -167,7 +167,7 @@ export interface ITiledTileSet {
|
|||||||
tilewidth: number;
|
tilewidth: number;
|
||||||
transparentcolor: string;
|
transparentcolor: string;
|
||||||
terrains: ITiledMapTerrain[];
|
terrains: ITiledMapTerrain[];
|
||||||
tiles: {[key: string]: { terrain: number[] }};
|
tiles?: Array<ITile>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Refers to external tileset file (should be JSON)
|
* Refers to external tileset file (should be JSON)
|
||||||
@ -175,6 +175,13 @@ export interface ITiledTileSet {
|
|||||||
source: string;
|
source: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ITile {
|
||||||
|
id: number,
|
||||||
|
type?: string
|
||||||
|
|
||||||
|
properties?: Array<ITiledMapLayerProperty>
|
||||||
|
}
|
||||||
|
|
||||||
export interface ITiledMapTerrain {
|
export interface ITiledMapTerrain {
|
||||||
name: string;
|
name: string;
|
||||||
tile: number;
|
tile: number;
|
||||||
|
Loading…
Reference in New Issue
Block a user