Process input events in follow mode as well

This commit is contained in:
PizZaKatZe 2021-12-15 00:10:28 +01:00
parent 290e5131e9
commit 1ab8165951

View File

@ -36,107 +36,73 @@ export class Player extends Character {
this.getBody().setImmovable(false); this.getBody().setImmovable(false);
} }
private inputStep(activeEvents: ActiveEventList) { private inputStep(activeEvents: ActiveEventList, x: number, y: number) {
//if user client on shift, camera and player speed // Process input events
let direction = this.lastDirection;
let moving = false;
const speedMultiplier = activeEvents.get(UserInputEvent.SpeedUp) ? 25 : 9;
const moveAmount = speedMultiplier * 20;
let x = 0;
let y = 0;
if (activeEvents.get(UserInputEvent.MoveUp)) { if (activeEvents.get(UserInputEvent.MoveUp)) {
y = -moveAmount; y = y - 1;
direction = PlayerAnimationDirections.Up;
moving = true;
} else if (activeEvents.get(UserInputEvent.MoveDown)) { } else if (activeEvents.get(UserInputEvent.MoveDown)) {
y = moveAmount; y = y + 1;
direction = PlayerAnimationDirections.Down;
moving = true;
} }
if (activeEvents.get(UserInputEvent.MoveLeft)) { if (activeEvents.get(UserInputEvent.MoveLeft)) {
x = -moveAmount; x = x - 1;
direction = PlayerAnimationDirections.Left;
moving = true;
} else if (activeEvents.get(UserInputEvent.MoveRight)) { } else if (activeEvents.get(UserInputEvent.MoveRight)) {
x = moveAmount; x = x + 1;
direction = PlayerAnimationDirections.Right;
moving = true;
} }
moving = moving || activeEvents.get(UserInputEvent.JoystickMove); // Compute movement deltas
const speedMultiplier = activeEvents.get(UserInputEvent.SpeedUp) ? 25 : 9;
const moveAmount = speedMultiplier * 20;
x = x * moveAmount;
y = y * moveAmount;
if (x !== 0 || y !== 0) { // Compute moving state
const joystickMovement = activeEvents.get(UserInputEvent.JoystickMove);
const moving = x !== 0 || y !== 0 || joystickMovement;
// Compute direction
let direction = this.lastDirection;
if (moving && !joystickMovement) {
if (Math.abs(x) > Math.abs(y)) {
direction = x < 0 ? PlayerAnimationDirections.Left : PlayerAnimationDirections.Right;
} else {
direction = y < 0 ? PlayerAnimationDirections.Up : PlayerAnimationDirections.Down;
}
}
// Send movement events
const emit = () => this.emit(hasMovedEventName, { moving, direction, x: this.x, y: this.y });
if (moving) {
this.move(x, y); this.move(x, y);
this.emit(hasMovedEventName, { moving, direction, x: this.x, y: this.y, oldX: x, oldY: y }); emit();
} else if (get(userMovingStore) && moving) { } else if (get(userMovingStore)) {
// slow joystick movement
this.move(0, 0);
this.emit(hasMovedEventName, {
moving,
direction: direction,
x: this.x,
y: this.y,
oldX: x,
oldY: y,
});
} else if (get(userMovingStore) && !moving) {
this.stop(); this.stop();
this.emit(hasMovedEventName, { emit();
moving,
direction: direction,
x: this.x,
y: this.y,
oldX: x,
oldY: y,
});
} }
// Update state
userMovingStore.set(moving); userMovingStore.set(moving);
} }
private followStep(delta: number) { private computeFollowMovement(): number[] {
// Find followed WOKA and abort following if we lost it
const player = this.scene.findPlayer((p) => p.PlayerValue === get(followUsersStore)[0]); const player = this.scene.findPlayer((p) => p.PlayerValue === get(followUsersStore)[0]);
if (!player) { if (!player) {
this.scene.connection?.emitFollowAbort(get(followUsersStore)[0], this.PlayerValue); this.scene.connection?.emitFollowAbort(get(followUsersStore)[0], this.PlayerValue);
followStateStore.set(followStates.off); followStateStore.set(followStates.off);
return; return [0, 0];
} }
const xDist = player.x - this.x; // Compute movement direction
const yDist = player.y - this.y; const xDistance = player.x - this.x;
const distance = Math.pow(xDist, 2) + Math.pow(yDist, 2); const yDistance = player.y - this.y;
const distance = Math.pow(xDistance, 2) + Math.pow(yDistance, 2);
let moving = false;
let direction = this.lastDirection;
if (distance < 2000) { if (distance < 2000) {
this.stop(); return [0, 0];
} else {
moving = true;
const moveAmount = 9 * 20;
const xDir = xDist / Math.sqrt(distance);
const yDir = yDist / Math.sqrt(distance);
this.move(xDir * moveAmount, yDir * moveAmount);
if (Math.abs(xDist) > Math.abs(yDist)) {
direction = xDist < 0 ? PlayerAnimationDirections.Left : PlayerAnimationDirections.Right;
} else {
direction = yDist < 0 ? PlayerAnimationDirections.Up : PlayerAnimationDirections.Down;
} }
} const xMovement = xDistance / Math.sqrt(distance);
const yMovement = yDistance / Math.sqrt(distance);
this.emit(hasMovedEventName, { return [xMovement, yMovement];
moving: moving,
direction: direction,
x: this.x,
y: this.y,
});
userMovingStore.set(moving);
} }
public enableFollowing() { public enableFollowing() {
@ -157,10 +123,11 @@ export class Player extends Character {
} }
} }
if ((state !== followStates.active && state !== followStates.ending) || role !== followRoles.follower) { let x = 0;
this.inputStep(activeEvents); let y = 0;
} else { if ((state === followStates.active || state === followStates.ending) && role === followRoles.follower) {
this.followStep(delta); [x, y] = this.computeFollowMovement();
} }
this.inputStep(activeEvents, x, y);
} }
} }