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;