support for group layer of Tiled (excludes 'start' layer)

This commit is contained in:
Lurkars 2021-02-27 15:23:57 +01:00 committed by David Négrier
parent f0d277af32
commit 9b6be3466b
3 changed files with 46 additions and 22 deletions

View File

@ -1,4 +1,4 @@
import {ITiledMap} from "../Map/ITiledMap";
import {ITiledMap, ITiledMapLayer} from "../Map/ITiledMap";
export type PropertyChangeCallback = (newValue: string | number | boolean | undefined, oldValue: string | number | boolean | undefined, allProps: Map<string, string | boolean | number>) => void;
@ -52,10 +52,15 @@ export class GameMap {
return this.lastProperties;
}
private getProperties(key: number): Map<string, string|boolean|number> {
const properties = new Map<string, string|boolean|number>();
// helper for recursive group layer support
private getPropertiesHelper(key: number, layers: ITiledMapLayer[], properties: Map<string, string|boolean|number>): Map<string, string|boolean|number> {
for (const layer of layers) {
if (layer.type === 'group') {
this.getPropertiesHelper(key, layer.layers, properties);
continue;
}
for (const layer of this.map.layers) {
if (layer.type !== 'tilelayer') {
continue;
}
@ -77,6 +82,11 @@ export class GameMap {
return properties;
}
private getProperties(key: number): Map<string, string|boolean|number> {
const properties = new Map<string, string|boolean|number>();
return this.getPropertiesHelper(key, this.map.layers, properties);
}
private trigger(propName: string, oldValue: string | number | boolean | undefined, newValue: string | number | boolean | undefined, allProps: Map<string, string | boolean | number>) {
const callbacksArray = this.callbacks.get(propName);
if (callbacksArray !== undefined) {

View File

@ -353,6 +353,31 @@ export class GameScene extends ResizableScene implements CenterListener {
}
}
// helper for recursive group layer support
private createHelper(that: this, layers: ITiledMapLayer[], depth: integer, prefix: string): integer {
for(const layer of layers) {
if(layer.type === 'tilelayer') {
that.addLayer(that.Map.createStaticLayer(prefix + layer.name, that.Terrains, 0, 0).setDepth(depth));
const exitSceneUrl = that.getExitSceneUrl(layer);
if(exitSceneUrl !== undefined) {
that.loadNextGame(exitSceneUrl);
}
const exitUrl = that.getExitUrl(layer);
if(exitUrl !== undefined) {
that.loadNextGame(exitUrl);
}
}
if(layer.type === 'group') {
that.createHelper(that, layer.layers, depth, prefix + layer.name + '/');
}
if(layer.type === 'objectgroup' && layer.name === 'floorLayer') {
depth = 10000;
}
}
return depth;
}
//hook create scene
create(): void {
gameManager.gameSceneIsCreated(this);
@ -386,24 +411,8 @@ export class GameScene extends ResizableScene implements CenterListener {
//add layer on map
this.Layers = new Array<Phaser.Tilemaps.StaticTilemapLayer>();
let depth = -2;
for (const layer of this.mapFile.layers) {
if (layer.type === 'tilelayer') {
this.addLayer(this.Map.createStaticLayer(layer.name, this.Terrains, 0, 0).setDepth(depth));
const exitSceneUrl = this.getExitSceneUrl(layer);
if (exitSceneUrl !== undefined) {
this.loadNextGame(exitSceneUrl);
}
const exitUrl = this.getExitUrl(layer);
if (exitUrl !== undefined) {
this.loadNextGame(exitUrl);
}
}
if (layer.type === 'objectgroup' && layer.name === 'floorLayer') {
depth = 10000;
}
}
let depth = this.createHelper(this, this.mapFile.layers, -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.');
}

View File

@ -61,6 +61,11 @@ export interface ITiledMapLayer {
*/
draworder: string;
objects: ITiledMapObject[];
/**
* Layers for group layer
*/
layers: this[];
}
export interface ITiledMapObject {