optional following path movement speed
This commit is contained in:
parent
9b94705177
commit
c023c4f8fd
@ -4,7 +4,7 @@ export const isMovePlayerToEventConfig = new tg.IsInterface()
|
|||||||
.withProperties({
|
.withProperties({
|
||||||
x: tg.isNumber,
|
x: tg.isNumber,
|
||||||
y: tg.isNumber,
|
y: tg.isNumber,
|
||||||
speed: tg.isNumber,
|
speed: tg.isOptional(tg.isNumber),
|
||||||
})
|
})
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
|
@ -84,7 +84,7 @@ export class WorkadventurePlayerCommands extends IframeApiContribution<Workadven
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async moveTo(x: number, y: number, speed: number): Promise<{ x: number; y: number }> {
|
public async moveTo(x: number, y: number, speed?: number): Promise<{ x: number; y: number }> {
|
||||||
return await queryWorkadventure({
|
return await queryWorkadventure({
|
||||||
type: "movePlayerTo",
|
type: "movePlayerTo",
|
||||||
data: { x, y, speed },
|
data: { x, y, speed },
|
||||||
|
@ -1467,8 +1467,7 @@ ${escapedMessage}
|
|||||||
const startTile = this.getGameMap().getTileIndexAt(this.CurrentPlayer.x, this.CurrentPlayer.y);
|
const startTile = this.getGameMap().getTileIndexAt(this.CurrentPlayer.x, this.CurrentPlayer.y);
|
||||||
const path = await this.getPathfindingManager().findPath(startTile, index, true, true);
|
const path = await this.getPathfindingManager().findPath(startTile, index, true, true);
|
||||||
path.shift();
|
path.shift();
|
||||||
return this.CurrentPlayer.setPathToFollow(path);
|
return this.CurrentPlayer.setPathToFollow(path, message.speed);
|
||||||
// return position;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 pathWalkingSpeed?: number;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
Scene: GameScene,
|
Scene: GameScene,
|
||||||
@ -70,15 +71,20 @@ export class Player extends Character {
|
|||||||
this.scene.connection?.emitFollowConfirmation();
|
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
|
// take collider offset into consideraton
|
||||||
this.pathToFollow = this.adjustPathToFollowToColliderBounds(path);
|
this.pathToFollow = this.adjustPathToFollowToColliderBounds(path);
|
||||||
|
this.pathWalkingSpeed = speed;
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y });
|
this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y });
|
||||||
this.followingPathPromiseResolve = resolve;
|
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 }[] {
|
private adjustPathToFollowToColliderBounds(path: { x: number; y: number }[]): { x: number; y: number }[] {
|
||||||
return path.map((step) => {
|
return path.map((step) => {
|
||||||
return { x: step.x, y: step.y - this.getBody().offset.y };
|
return { x: step.x, y: step.y - this.getBody().offset.y };
|
||||||
@ -101,8 +107,8 @@ export class Player extends Character {
|
|||||||
|
|
||||||
// Compute movement deltas
|
// Compute movement deltas
|
||||||
const followMode = get(followStateStore) !== "off";
|
const followMode = get(followStateStore) !== "off";
|
||||||
const speedup = activeEvents.get(UserInputEvent.SpeedUp) && !followMode ? 25 : 9;
|
const speed = this.deduceSpeed(activeEvents.get(UserInputEvent.SpeedUp), followMode);
|
||||||
const moveAmount = speedup * 20;
|
const moveAmount = speed * 20;
|
||||||
x = x * moveAmount;
|
x = x * moveAmount;
|
||||||
y = y * moveAmount;
|
y = y * moveAmount;
|
||||||
|
|
||||||
@ -156,6 +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.pathToFollow = undefined;
|
||||||
|
this.pathWalkingSpeed = undefined;
|
||||||
this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y });
|
this.followingPathPromiseResolve?.call(this, { x: this.x, y: this.y });
|
||||||
}
|
}
|
||||||
if (!this.pathToFollow) {
|
if (!this.pathToFollow) {
|
||||||
|
@ -8,10 +8,25 @@
|
|||||||
WA.onInit().then(() => {
|
WA.onInit().then(() => {
|
||||||
console.log('After WA init');
|
console.log('After WA init');
|
||||||
const movePlayerButton = document.getElementById('movePlayerButton');
|
const movePlayerButton = document.getElementById('movePlayerButton');
|
||||||
|
const randomChainedMovementButton = document.getElementById('randomChainedMovementButton');
|
||||||
const xField = document.getElementById('x');
|
const xField = document.getElementById('x');
|
||||||
const yField = document.getElementById('y');
|
const yField = document.getElementById('y');
|
||||||
const speedField = document.getElementById('speed');
|
const speedField = document.getElementById('speed');
|
||||||
|
|
||||||
|
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 () => {
|
movePlayerButton.addEventListener('click', async () => {
|
||||||
const position = await WA.player.moveTo(
|
const position = await WA.player.moveTo(
|
||||||
parseInt(xField.value),
|
parseInt(xField.value),
|
||||||
@ -30,6 +45,7 @@ Y: <input type="text" id="y" value="655" /><br/>
|
|||||||
Speed: <input type="text" id="speed" value="20" /><br/>
|
Speed: <input type="text" id="speed" value="20" /><br/>
|
||||||
|
|
||||||
<button id="movePlayerButton">Move Player</button>
|
<button id="movePlayerButton">Move Player</button>
|
||||||
|
<button id="randomChainedMovementButton">Do random chained movement</button>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
Reference in New Issue
Block a user