From 9bf39ab1e40cb0daacaef42007bd687823c3368d Mon Sep 17 00:00:00 2001 From: Johannes Baum Date: Tue, 8 Aug 2023 19:41:01 +0200 Subject: [PATCH] #408 throw error --- src/GridTilemap/Phaser/PhaserTilemap.test.ts | 28 ++++++++++++++++++++ src/GridTilemap/Phaser/PhaserTilemap.ts | 10 ++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/GridTilemap/Phaser/PhaserTilemap.test.ts b/src/GridTilemap/Phaser/PhaserTilemap.test.ts index 9f9281a7..ce8ef562 100644 --- a/src/GridTilemap/Phaser/PhaserTilemap.test.ts +++ b/src/GridTilemap/Phaser/PhaserTilemap.test.ts @@ -114,4 +114,32 @@ describe("PhaserTilemap", () => { ); } }); + + it("should throw error if initialized without tilemapLayer", () => { + const tilemap = createPhaserTilemapStub( + new Map([["layer_name", ["..", ".."]]]) + ); + const tiledProject = { + propertyTypes: [ + { + name: "SomeTiledClass", + type: "class", + members: [ + { + name: "testProp", + type: "boolean", + value: true, + }, + ], + }, + ], + }; + // @ts-ignore + tilemap.layers[0].tilemapLayer = undefined; + expect(() => new PhaserTilemap(tilemap, tiledProject)).toThrow( + new Error( + "Error initializing tilemap. Layer 'layer_name' has no 'tilemapLayer'. This can happen if you call 'createLayer' with the wrong layer ID." + ) + ); + }); }); diff --git a/src/GridTilemap/Phaser/PhaserTilemap.ts b/src/GridTilemap/Phaser/PhaserTilemap.ts index 10007091..05bfa6f2 100644 --- a/src/GridTilemap/Phaser/PhaserTilemap.ts +++ b/src/GridTilemap/Phaser/PhaserTilemap.ts @@ -19,7 +19,15 @@ export class PhaserTilemap implements Tilemap { constructor( private phaserTilemap: Phaser.Tilemaps.Tilemap, private tiledProject?: TiledProject - ) {} + ) { + for (const l of this.phaserTilemap.layers) { + if (l.tilemapLayer == null) { + throw new Error( + `Error initializing tilemap. Layer '${l.name}' has no 'tilemapLayer'. This can happen if you call 'createLayer' with the wrong layer ID.` + ); + } + } + } getTileWidth(): number { return this.phaserTilemap.tileWidth;