From b565080312f02ee51ba22f7f84327a18b130ad77 Mon Sep 17 00:00:00 2001 From: Hanusiak Piotr Date: Wed, 9 Feb 2022 10:52:44 +0100 Subject: [PATCH] parse x and y position from moveTo param --- front/src/Phaser/Game/GameScene.ts | 14 ++++++++++---- front/src/Utils/StringUtils.ts | 12 ++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 front/src/Utils/StringUtils.ts diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 44992d85..8d13cb7b 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -92,6 +92,7 @@ import { MapStore } from "../../Stores/Utils/MapStore"; import { followUsersColorStore } from "../../Stores/FollowStore"; import { GameSceneUserInputHandler } from "../UserInput/GameSceneUserInputHandler"; import { locale } from "../../i18n/i18n-svelte"; +import { StringUtils } from "../../Utils/StringUtils"; export interface GameSceneInitInterface { initPosition: PointInterface | null; reconnecting: boolean; @@ -1632,12 +1633,17 @@ ${escapedMessage} const moveToParam = urlManager.getHashParameter("moveTo"); if (moveToParam) { try { - const destinationObject = this.gameMap.getObjectWithName(moveToParam); let endPos; - if (destinationObject) { - endPos = this.gameMap.getTileIndexAt(destinationObject.x, destinationObject.y); + const posFromParam = StringUtils.parsePointFromParam(moveToParam); + if (posFromParam) { + endPos = this.gameMap.getTileIndexAt(posFromParam.x, posFromParam.y); } else { - endPos = this.gameMap.getRandomPositionFromLayer(moveToParam); + const destinationObject = this.gameMap.getObjectWithName(moveToParam); + if (destinationObject) { + endPos = this.gameMap.getTileIndexAt(destinationObject.x, destinationObject.y); + } else { + endPos = this.gameMap.getRandomPositionFromLayer(moveToParam); + } } this.pathfindingManager .findPath(this.gameMap.getTileIndexAt(this.CurrentPlayer.x, this.CurrentPlayer.y), endPos) diff --git a/front/src/Utils/StringUtils.ts b/front/src/Utils/StringUtils.ts new file mode 100644 index 00000000..1f168c15 --- /dev/null +++ b/front/src/Utils/StringUtils.ts @@ -0,0 +1,12 @@ +export class StringUtils { + public static parsePointFromParam(param: string, separator: string = ","): { x: number; y: number } | undefined { + const values = param.split(separator).map((val) => parseInt(val)); + if (values.length !== 2) { + return; + } + if (isNaN(values[0]) || isNaN(values[1])) { + return; + } + return { x: values[0], y: values[1] }; + } +}