Merge pull request #1743 from thecodingmachine/move-to-from-hash-parameter
Move to from hash parameter
This commit is contained in:
commit
ef2f8b141f
8 changed files with 384 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);
|
||||
}
|
||||
|
|
|
@ -560,6 +560,12 @@ export class GameScene extends DirtyScene {
|
|||
.catch((e) => console.error(e));
|
||||
}
|
||||
|
||||
this.pathfindingManager = new PathfindingManager(
|
||||
this,
|
||||
this.gameMap.getCollisionsGrid(),
|
||||
this.gameMap.getTileDimensions()
|
||||
);
|
||||
|
||||
//notify game manager can to create currentUser in map
|
||||
this.createCurrentPlayer();
|
||||
this.removeAllRemotePlayers(); //cleanup the list of remote players in case the scene was rebooted
|
||||
|
@ -570,11 +576,6 @@ export class GameScene extends DirtyScene {
|
|||
waScaleManager
|
||||
);
|
||||
|
||||
this.pathfindingManager = new PathfindingManager(
|
||||
this,
|
||||
this.gameMap.getCollisionsGrid(),
|
||||
this.gameMap.getTileDimensions()
|
||||
);
|
||||
biggestAvailableAreaStore.recompute();
|
||||
this.cameraManager.startFollowPlayer(this.CurrentPlayer);
|
||||
|
||||
|
@ -1726,6 +1727,22 @@ ${escapedMessage}
|
|||
this.connection?.emitEmoteEvent(emoteKey);
|
||||
analyticsClient.launchEmote(emoteKey);
|
||||
});
|
||||
const moveToParam = urlManager.getHashParameter("moveTo");
|
||||
if (moveToParam) {
|
||||
try {
|
||||
const endPos = this.gameMap.getRandomPositionFromLayer(moveToParam);
|
||||
this.pathfindingManager
|
||||
.findPath(this.gameMap.getTileIndexAt(this.CurrentPlayer.x, this.CurrentPlayer.y), endPos)
|
||||
.then((path) => {
|
||||
if (path && path.length > 0) {
|
||||
this.CurrentPlayer.setPathToFollow(path).catch((reason) => console.warn(reason));
|
||||
}
|
||||
})
|
||||
.catch((reason) => console.warn(reason));
|
||||
} catch (err) {
|
||||
console.warn(`Cannot proceed with moveTo command:\n\t-> ${err}`);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof TextureError) {
|
||||
gameManager.leaveGame(SelectCharacterSceneName, new SelectCharacterScene());
|
||||
|
|
|
@ -45,8 +45,28 @@ class UrlManager {
|
|||
}
|
||||
|
||||
public getStartLayerNameFromUrl(): string | null {
|
||||
const hash = window.location.hash;
|
||||
return hash.length > 1 ? hash.substring(1) : null;
|
||||
const parameters = this.getHashParameters();
|
||||
for (const key in parameters) {
|
||||
if (parameters[key] === undefined) {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public getHashParameter(name: string): string | undefined {
|
||||
return this.getHashParameters()[name];
|
||||
}
|
||||
|
||||
private getHashParameters(): Record<string, string> {
|
||||
return window.location.hash
|
||||
.substring(1)
|
||||
.split("&")
|
||||
.reduce((res: Record<string, string>, item: string) => {
|
||||
const parts = item.split("=");
|
||||
res[parts[0]] = parts[1];
|
||||
return res;
|
||||
}, {});
|
||||
}
|
||||
|
||||
pushStartLayerNameToUrl(startLayerName: string): void {
|
||||
|
|
|
@ -31,4 +31,8 @@ export class MathUtils {
|
|||
const distance = Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2);
|
||||
return squared ? Math.sqrt(distance) : distance;
|
||||
}
|
||||
|
||||
public static randomFromArray<T>(array: T[]): T {
|
||||
return array[Math.floor(Math.random() * array.length)];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue