diff --git a/front/src/Api/Events/MovePlayerToEvent.ts b/front/src/Api/Events/MovePlayerToEvent.ts index cdb0830c..462e2f43 100644 --- a/front/src/Api/Events/MovePlayerToEvent.ts +++ b/front/src/Api/Events/MovePlayerToEvent.ts @@ -4,7 +4,7 @@ export const isMovePlayerToEventConfig = new tg.IsInterface() .withProperties({ x: tg.isNumber, y: tg.isNumber, - speed: tg.isNumber, + speed: tg.isOptional(tg.isNumber), }) .get(); diff --git a/front/src/Api/iframe/player.ts b/front/src/Api/iframe/player.ts index 59687a5e..f2014f55 100644 --- a/front/src/Api/iframe/player.ts +++ b/front/src/Api/iframe/player.ts @@ -84,7 +84,7 @@ export class WorkadventurePlayerCommands extends IframeApiContribution { + public async moveTo(x: number, y: number, speed?: number): Promise<{ x: number; y: number }> { return await queryWorkadventure({ type: "movePlayerTo", data: { x, y, speed }, diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 5b61ebc5..a92e65be 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -1467,8 +1467,7 @@ ${escapedMessage} const startTile = this.getGameMap().getTileIndexAt(this.CurrentPlayer.x, this.CurrentPlayer.y); const path = await this.getPathfindingManager().findPath(startTile, index, true, true); path.shift(); - return this.CurrentPlayer.setPathToFollow(path); - // return position; + return this.CurrentPlayer.setPathToFollow(path, message.speed); }); } diff --git a/front/src/Phaser/Player/Player.ts b/front/src/Phaser/Player/Player.ts index 60e5cdcf..ae661e54 100644 --- a/front/src/Phaser/Player/Player.ts +++ b/front/src/Phaser/Player/Player.ts @@ -13,6 +13,7 @@ export const requestEmoteEventName = "requestEmote"; export class Player extends Character { private pathToFollow?: { x: number; y: number }[]; private followingPathPromiseResolve?: (position: { x: number; y: number }) => void; + private pathWalkingSpeed?: number; constructor( Scene: GameScene, @@ -70,15 +71,20 @@ export class Player extends Character { this.scene.connection?.emitFollowConfirmation(); } - public async setPathToFollow(path: { x: number; y: number }[]): Promise<{ x: number; y: number }> { + public async setPathToFollow(path: { x: number; y: number }[], speed?: number): Promise<{ x: number; y: number }> { // take collider offset into consideraton this.pathToFollow = this.adjustPathToFollowToColliderBounds(path); + this.pathWalkingSpeed = speed; return new Promise((resolve) => { this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y }); this.followingPathPromiseResolve = resolve; }); } + private deduceSpeed(speedUp: boolean, followMode: boolean): number { + return this.pathWalkingSpeed ? this.pathWalkingSpeed : speedUp && !followMode ? 25 : 9; + } + private adjustPathToFollowToColliderBounds(path: { x: number; y: number }[]): { x: number; y: number }[] { return path.map((step) => { return { x: step.x, y: step.y - this.getBody().offset.y }; @@ -101,8 +107,8 @@ export class Player extends Character { // Compute movement deltas const followMode = get(followStateStore) !== "off"; - const speedup = activeEvents.get(UserInputEvent.SpeedUp) && !followMode ? 25 : 9; - const moveAmount = speedup * 20; + const speed = this.deduceSpeed(activeEvents.get(UserInputEvent.SpeedUp), followMode); + const moveAmount = speed * 20; x = x * moveAmount; y = y * moveAmount; @@ -156,6 +162,7 @@ export class Player extends Character { private computeFollowPathMovement(): number[] { if (this.pathToFollow?.length === 0) { this.pathToFollow = undefined; + this.pathWalkingSpeed = undefined; this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y }); } if (!this.pathToFollow) { diff --git a/maps/tests/MovePlayer/script.php b/maps/tests/MovePlayer/script.php index 9d68065f..30d250a0 100644 --- a/maps/tests/MovePlayer/script.php +++ b/maps/tests/MovePlayer/script.php @@ -8,11 +8,26 @@ WA.onInit().then(() => { console.log('After WA init'); const movePlayerButton = document.getElementById('movePlayerButton'); + const randomChainedMovementButton = document.getElementById('randomChainedMovementButton'); const xField = document.getElementById('x'); const yField = document.getElementById('y'); const speedField = document.getElementById('speed'); - movePlayerButton.addEventListener('click', async () => { + randomChainedMovementButton.addEventListener('click', async () => { + let pos; + pos = await WA.player.moveTo(100, 100, 10); + console.log(pos); + pos = await WA.player.moveTo(500, 100, 10); + console.log(pos); + pos = await WA.player.moveTo(500, 500, 10); + console.log(pos); + pos = await WA.player.moveTo(100, 500, 10); + console.log(pos); + pos = await WA.player.moveTo(100, 100, 10); + console.log(pos); + }); + + movePlayerButton.addEventListener('click', async () => { const position = await WA.player.moveTo( parseInt(xField.value), parseInt(yField.value), @@ -30,6 +45,7 @@ Y:
Speed:
+