reject path following path promise if interrupted

This commit is contained in:
Hanusiak Piotr 2022-01-19 14:16:05 +01:00
parent c023c4f8fd
commit 8f40e7a33c
2 changed files with 28 additions and 18 deletions

View File

@ -13,6 +13,7 @@ export const requestEmoteEventName = "requestEmote";
export class Player extends Character { export class Player extends Character {
private pathToFollow?: { x: number; y: number }[]; private pathToFollow?: { x: number; y: number }[];
private followingPathPromiseResolve?: (position: { x: number; y: number }) => void; private followingPathPromiseResolve?: (position: { x: number; y: number }) => void;
private followingPathPromiseReject?: (position: { x: number; y: number }) => void;
private pathWalkingSpeed?: number; private pathWalkingSpeed?: number;
constructor( constructor(
@ -45,8 +46,7 @@ export class Player extends Character {
} }
if (this.pathToFollow && activeUserInputEvents.anyExcept(UserInputEvent.SpeedUp)) { if (this.pathToFollow && activeUserInputEvents.anyExcept(UserInputEvent.SpeedUp)) {
this.pathToFollow = undefined; this.finishFollowingPath();
this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y });
} }
let x = 0; let x = 0;
@ -75,9 +75,10 @@ export class Player extends Character {
// take collider offset into consideraton // take collider offset into consideraton
this.pathToFollow = this.adjustPathToFollowToColliderBounds(path); this.pathToFollow = this.adjustPathToFollowToColliderBounds(path);
this.pathWalkingSpeed = speed; this.pathWalkingSpeed = speed;
return new Promise((resolve) => { return new Promise((resolve, reject) => {
this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y }); this.followingPathPromiseReject?.call(this, { x: this.x, y: this.y });
this.followingPathPromiseResolve = resolve; this.followingPathPromiseResolve = resolve;
this.followingPathPromiseReject = reject;
}); });
} }
@ -161,9 +162,7 @@ export class Player extends Character {
private computeFollowPathMovement(): number[] { private computeFollowPathMovement(): number[] {
if (this.pathToFollow?.length === 0) { if (this.pathToFollow?.length === 0) {
this.pathToFollow = undefined; this.finishFollowingPath();
this.pathWalkingSpeed = undefined;
this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y });
} }
if (!this.pathToFollow) { if (!this.pathToFollow) {
return [0, 0]; return [0, 0];
@ -180,6 +179,13 @@ export class Player extends Character {
return this.getMovementDirection(xDistance, yDistance, distance); return this.getMovementDirection(xDistance, yDistance, distance);
} }
private finishFollowingPath(cancelled: boolean = false): void {
this.pathToFollow = undefined;
this.pathWalkingSpeed = undefined;
const func = cancelled ? this.followingPathPromiseReject : this.followingPathPromiseResolve;
func?.call(this, { x: this.x, y: this.y });
}
private getMovementDirection(xDistance: number, yDistance: number, distance: number): [number, number] { private getMovementDirection(xDistance: number, yDistance: number, distance: number): [number, number] {
return [xDistance / Math.sqrt(distance), yDistance / Math.sqrt(distance)]; return [xDistance / Math.sqrt(distance), yDistance / Math.sqrt(distance)];
} }

View File

@ -14,17 +14,21 @@
const speedField = document.getElementById('speed'); const speedField = document.getElementById('speed');
randomChainedMovementButton.addEventListener('click', async () => { randomChainedMovementButton.addEventListener('click', async () => {
let pos; try {
pos = await WA.player.moveTo(100, 100, 10); let pos;
console.log(pos); pos = await WA.player.moveTo(100, 100, 10);
pos = await WA.player.moveTo(500, 100, 10); console.log(pos);
console.log(pos); pos = await WA.player.moveTo(500, 100, 10);
pos = await WA.player.moveTo(500, 500, 10); console.log(pos);
console.log(pos); pos = await WA.player.moveTo(500, 500, 10);
pos = await WA.player.moveTo(100, 500, 10); console.log(pos);
console.log(pos); pos = await WA.player.moveTo(100, 500, 10);
pos = await WA.player.moveTo(100, 100, 10); console.log(pos);
console.log(pos); pos = await WA.player.moveTo(100, 100, 10);
console.log(pos);
} catch (err) {
console.log('movement was stopped forcefully');
}
}); });
movePlayerButton.addEventListener('click', async () => { movePlayerButton.addEventListener('click', async () => {