2021-05-28 12:13:10 +02:00
|
|
|
import type { ITiledMap, ITiledMapLayer } from "../Map/ITiledMap";
|
2021-05-12 14:30:12 +02:00
|
|
|
import { flattenGroupLayersMap } from "../Map/LayersFlattener";
|
2021-05-18 15:41:16 +02:00
|
|
|
import TilemapLayer = Phaser.Tilemaps.TilemapLayer;
|
2021-05-28 12:13:10 +02:00
|
|
|
import { DEPTH_OVERLAY_INDEX } from "./DepthIndexes";
|
2020-08-30 15:44:22 +02:00
|
|
|
|
2020-10-16 19:13:26 +02:00
|
|
|
export type PropertyChangeCallback = (newValue: string | number | boolean | undefined, oldValue: string | number | boolean | undefined, allProps: Map<string, string | boolean | number>) => void;
|
2020-08-30 15:44:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A wrapper around a ITiledMap interface to provide additional capabilities.
|
|
|
|
* It is used to handle layer properties.
|
|
|
|
*/
|
|
|
|
export class GameMap {
|
|
|
|
private key: number|undefined;
|
|
|
|
private lastProperties = new Map<string, string|boolean|number>();
|
|
|
|
private callbacks = new Map<string, Array<PropertyChangeCallback>>();
|
2021-05-12 14:30:12 +02:00
|
|
|
public readonly flatLayers: ITiledMapLayer[];
|
2021-05-18 15:41:16 +02:00
|
|
|
public readonly phaserLayers: TilemapLayer[] = [];
|
2020-08-30 15:44:22 +02:00
|
|
|
|
2021-05-12 14:30:12 +02:00
|
|
|
public constructor(private map: ITiledMap, phaserMap: Phaser.Tilemaps.Tilemap, terrains: Array<Phaser.Tilemaps.Tileset>) {
|
|
|
|
this.flatLayers = flattenGroupLayersMap(map);
|
|
|
|
let depth = -2;
|
|
|
|
for (const layer of this.flatLayers) {
|
|
|
|
if(layer.type === 'tilelayer'){
|
2021-05-18 15:41:16 +02:00
|
|
|
this.phaserLayers.push(phaserMap.createLayer(layer.name, terrains, 0, 0).setDepth(depth));
|
2021-05-12 14:30:12 +02:00
|
|
|
}
|
|
|
|
if (layer.type === 'objectgroup' && layer.name === 'floorLayer') {
|
2021-05-26 10:58:25 +02:00
|
|
|
depth = DEPTH_OVERLAY_INDEX;
|
2021-05-12 14:30:12 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-30 15:44:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the position of the current player (in pixels)
|
|
|
|
* This will trigger events if properties are changing.
|
|
|
|
*/
|
|
|
|
public setPosition(x: number, y: number) {
|
|
|
|
const xMap = Math.floor(x / this.map.tilewidth);
|
|
|
|
const yMap = Math.floor(y / this.map.tileheight);
|
|
|
|
const key = xMap + yMap * this.map.width;
|
|
|
|
if (key === this.key) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.key = key;
|
|
|
|
|
|
|
|
const newProps = this.getProperties(key);
|
|
|
|
const oldProps = this.lastProperties;
|
2021-02-18 11:32:37 +01:00
|
|
|
this.lastProperties = newProps;
|
2020-08-30 15:44:22 +02:00
|
|
|
|
|
|
|
// Let's compare the 2 maps:
|
|
|
|
// First new properties vs oldProperties
|
|
|
|
for (const [newPropName, newPropValue] of newProps.entries()) {
|
|
|
|
const oldPropValue = oldProps.get(newPropName);
|
|
|
|
if (oldPropValue !== newPropValue) {
|
2020-10-16 19:13:26 +02:00
|
|
|
this.trigger(newPropName, oldPropValue, newPropValue, newProps);
|
2020-08-30 15:44:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const [oldPropName, oldPropValue] of oldProps.entries()) {
|
|
|
|
if (!newProps.has(oldPropName)) {
|
|
|
|
// We found a property that disappeared
|
2020-10-16 19:13:26 +02:00
|
|
|
this.trigger(oldPropName, oldPropValue, undefined, newProps);
|
2020-08-30 15:44:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 11:08:57 +01:00
|
|
|
public getCurrentProperties(): Map<string, string|boolean|number> {
|
|
|
|
return this.lastProperties;
|
|
|
|
}
|
|
|
|
|
2021-04-15 22:39:35 +02:00
|
|
|
private getProperties(key: number): Map<string, string|boolean|number> {
|
|
|
|
const properties = new Map<string, string|boolean|number>();
|
2020-08-30 15:44:22 +02:00
|
|
|
|
2021-05-12 14:30:12 +02:00
|
|
|
for (const layer of this.flatLayers) {
|
2020-08-30 15:44:22 +02:00
|
|
|
if (layer.type !== 'tilelayer') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const tiles = layer.data as number[];
|
|
|
|
if (tiles[key] == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// There is a tile in this layer, let's embed the properties
|
|
|
|
if (layer.properties !== undefined) {
|
|
|
|
for (const layerProperty of layer.properties) {
|
|
|
|
if (layerProperty.value === undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
properties.set(layerProperty.name, layerProperty.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return properties;
|
|
|
|
}
|
|
|
|
|
2021-05-18 15:41:16 +02:00
|
|
|
public getMap(): ITiledMap{
|
|
|
|
return this.map;
|
|
|
|
}
|
|
|
|
|
2020-10-16 19:13:26 +02:00
|
|
|
private trigger(propName: string, oldValue: string | number | boolean | undefined, newValue: string | number | boolean | undefined, allProps: Map<string, string | boolean | number>) {
|
2020-08-30 17:40:04 +02:00
|
|
|
const callbacksArray = this.callbacks.get(propName);
|
2020-08-30 15:44:22 +02:00
|
|
|
if (callbacksArray !== undefined) {
|
|
|
|
for (const callback of callbacksArray) {
|
2020-10-16 19:13:26 +02:00
|
|
|
callback(newValue, oldValue, allProps);
|
2020-08-30 15:44:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a callback called when the user moves to a tile where the property propName is different from the last tile the user was on.
|
|
|
|
*/
|
|
|
|
public onPropertyChange(propName: string, callback: PropertyChangeCallback) {
|
|
|
|
let callbacksArray = this.callbacks.get(propName);
|
|
|
|
if (callbacksArray === undefined) {
|
|
|
|
callbacksArray = new Array<PropertyChangeCallback>();
|
|
|
|
this.callbacks.set(propName, callbacksArray);
|
|
|
|
}
|
|
|
|
callbacksArray.push(callback);
|
|
|
|
}
|
2021-05-12 14:30:12 +02:00
|
|
|
|
|
|
|
public findLayer(layerName: string): ITiledMapLayer | undefined {
|
2021-05-28 12:13:10 +02:00
|
|
|
return this.flatLayers.find((layer) => layer.name === layerName);
|
2021-05-12 14:30:12 +02:00
|
|
|
}
|
|
|
|
|
2021-05-18 15:41:16 +02:00
|
|
|
public findPhaserLayer(layerName: string): TilemapLayer | undefined {
|
2021-05-28 12:13:10 +02:00
|
|
|
return this.phaserLayers.find((layer) => layer.layer.name === layerName);
|
2021-05-18 15:41:16 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 17:09:10 +02:00
|
|
|
public addTerrain(terrain : Phaser.Tilemaps.Tileset): void {
|
|
|
|
for (const phaserLayer of this.phaserLayers) {
|
|
|
|
phaserLayer.tileset.push(terrain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-30 15:44:22 +02:00
|
|
|
}
|