Was working on a game using TileMaps. I sub classed TilemapLayer so that I could have the layer hold a list of Piece class objects (the Piece classes holds properties for what the tile represents, like a monster or item etc). Now I would like to alter the drawing of the tile layer, would like it to draw each one of the Pieces in the list, right now I just set the tile index to the top Piece in the list so I only see the top Piece drawn. I am not enough of a Javascript expert to figure if and how to replace the drawing code for the TileLayer. I see there is a file in the source code TilemapLayerCanvasRender.js (and one for WebGL) that sees to do the drawing job and it is hooked into the TilemapLayer via a list Mixins but I have no idea how to override that in my TilemapLayer subclass. Below is the TilemapLayer sub class code, not sure if the is useful but what is a post without code (I am doing Typescript because it was something new to learn)! Thanks for any thoughts.
export class TileLayerPieces extends Phaser.Tilemaps.TilemapLayer {
constructor(
scene: Phaser.Scene,
tilemap: Phaser.Tilemaps.Tilemap,
layerName: string,
tileset: string
) {
var layerData = new Phaser.Tilemaps.LayerData({
name: layerName,
tileWidth: tilemap.tileWidth,
tileHeight: tilemap.tileHeight,
width: tilemap.width,
height: tilemap.height,
});
tilemap.layers.push(layerData);
super(scene, tilemap, tilemap.getLayerIndexByName(layerName), tileset);
var row;
for (var tileY = 0; tileY < tilemap.height; tileY++) {
row = [];
for (var tileX = 0; tileX < tilemap.width; tileX++) {
row.push(
new Pieces(
layerData,
tileX,
tileY,
tilemap.tileWidth,
tilemap.tileHeight,
tilemap.tileWidth,
tilemap.tileHeight
)
);
}
layerData.data.push(row);
}
scene.sys.displayList.add(this);
}
public addPieceAt(piece: Piece, x: number, y: number) {
let pieces = this.layer.data[y][x] as Pieces;
pieces.pieces.push(piece);
pieces.index = piece.index;
piece.pieces = pieces;
}
public removePieceAt(piece: Piece, x: number, y: number) {
let pieces = this.layer.data[y][x] as Pieces;
let index = pieces.pieces.indexOf(piece);
if (index != -1) pieces.pieces.splice(index, 1);
pieces.index = -1;
if (pieces.pieces.length > 0) pieces.index = pieces.pieces[0].index;
}
public getPiecesAt(x: number, y: number): Pieces {
return this.layer.data[y][x] as Pieces;
}
public getTopPieceAt(x: number, y: number): Piece | undefined {
let pieces = this.layer.data[y][x] as Pieces;
if (pieces.pieces.length) return pieces.pieces[pieces.pieces.length - 1];
return undefined;
}
}