moveTo parameter working if layer is found
This commit is contained in:
parent
cb0c5beecf
commit
e87c0a07a4
4 changed files with 57 additions and 8 deletions
|
@ -1,4 +1,10 @@
|
|||
import type { ITiledMap, ITiledMapLayer, ITiledMapObject, ITiledMapProperty } from "../Map/ITiledMap";
|
||||
import type {
|
||||
ITiledMap,
|
||||
ITiledMapLayer,
|
||||
ITiledMapObject,
|
||||
ITiledMapProperty,
|
||||
ITiledMapTileLayer,
|
||||
} from "../Map/ITiledMap";
|
||||
import { flattenGroupLayersMap } from "../Map/LayersFlattener";
|
||||
import TilemapLayer = Phaser.Tilemaps.TilemapLayer;
|
||||
import { DEPTH_OVERLAY_INDEX } from "./DepthIndexes";
|
||||
|
@ -291,6 +297,31 @@ export class GameMap {
|
|||
}
|
||||
}
|
||||
|
||||
public getRandomPositionFromLayer(layerName: string): { x: number; y: number } {
|
||||
const layer = this.findLayer(layerName) as ITiledMapTileLayer;
|
||||
if (!layer) {
|
||||
throw new Error(`No layer "${layerName}" was found`);
|
||||
}
|
||||
const tiles = layer.data;
|
||||
if (!tiles) {
|
||||
throw new Error(`No tiles in "${layerName}" were found`);
|
||||
}
|
||||
if (typeof tiles === "string") {
|
||||
throw new Error("The content of a JSON map must be filled as a JSON array, not as a string");
|
||||
}
|
||||
const possiblePositions: { x: number; y: number }[] = [];
|
||||
tiles.forEach((objectKey: number, key: number) => {
|
||||
if (objectKey === 0) {
|
||||
return;
|
||||
}
|
||||
possiblePositions.push({ x: key % layer.width, y: Math.floor(key / layer.width) });
|
||||
});
|
||||
if (possiblePositions.length > 0) {
|
||||
return MathUtils.randomFromArray(possiblePositions);
|
||||
}
|
||||
throw new Error("No possible position found");
|
||||
}
|
||||
|
||||
private getLayersByKey(key: number): Array<ITiledMapLayer> {
|
||||
return this.flatLayers.filter((flatLayer) => flatLayer.type === "tilelayer" && flatLayer.data[key] !== 0);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue