partey_workadventure/front/src/Phaser/Player/Player.ts

134 lines
4.6 KiB
TypeScript
Raw Normal View History

import { PlayerAnimationDirections } from "./Animation";
import type { GameScene } from "../Game/GameScene";
import { ActiveEventList, UserInputEvent, UserInputManager } from "../UserInput/UserInputManager";
import { Character } from "../Entity/Character";
import type { RemotePlayer } from "../Entity/RemotePlayer";
import { get } from "svelte/store";
import { userMovingStore } from "../../Stores/GameStore";
import {
followStateStore,
followRoleStore,
followUsersStore,
followRoles,
followStates,
} from "../../Stores/FollowStore";
export const hasMovedEventName = "hasMoved";
2021-05-10 17:10:41 +02:00
export const requestEmoteEventName = "requestEmote";
2020-04-12 13:57:00 +02:00
2021-05-10 17:10:41 +02:00
export class Player extends Character {
constructor(
Scene: GameScene,
2020-04-13 15:34:09 +02:00
x: number,
y: number,
name: string,
texturesPromise: Promise<string[]>,
direction: PlayerAnimationDirections,
2020-07-27 22:36:07 +02:00
moving: boolean,
2021-04-02 21:21:11 +02:00
private userInputManager: UserInputManager,
companion: string | null,
companionTexturePromise?: Promise<string>
) {
super(Scene, x, y, texturesPromise, name, direction, moving, 1, true, companion, companionTexturePromise);
//the current player model should be push away by other players to prevent conflict
this.getBody().setImmovable(false);
2020-05-26 17:43:25 +02:00
}
private inputStep(activeEvents: ActiveEventList, x: number, y: number) {
// Process input events
if (activeEvents.get(UserInputEvent.MoveUp)) {
y = y - 1;
2020-04-30 19:36:28 +02:00
} else if (activeEvents.get(UserInputEvent.MoveDown)) {
y = y + 1;
}
if (activeEvents.get(UserInputEvent.MoveLeft)) {
x = x - 1;
2020-04-30 19:36:28 +02:00
} else if (activeEvents.get(UserInputEvent.MoveRight)) {
x = x + 1;
}
// Compute movement deltas
const speedMultiplier = activeEvents.get(UserInputEvent.SpeedUp) ? 25 : 9;
const moveAmount = speedMultiplier * 20;
x = x * moveAmount;
y = y * moveAmount;
// 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;
}
}
2021-04-02 17:14:34 +02:00
// Send movement events
const emit = () => this.emit(hasMovedEventName, { moving, direction, x: this.x, y: this.y });
if (moving) {
2020-04-30 19:36:28 +02:00
this.move(x, y);
emit();
} else if (get(userMovingStore)) {
this.stop();
emit();
2020-04-07 20:41:35 +02:00
}
// Update state
userMovingStore.set(moving);
}
private computeFollowMovement(): number[] {
// Find followed WOKA and abort following if we lost it
2021-12-15 14:48:45 +01:00
const player = this.scene.MapPlayersByKey.get(get(followUsersStore)[0]);
if (!player) {
2021-12-15 14:48:45 +01:00
this.scene.connection?.emitFollowAbort();
followStateStore.set(followStates.off);
return [0, 0];
}
// Compute movement direction
const xDistance = player.x - this.x;
const yDistance = player.y - this.y;
const distance = Math.pow(xDistance, 2) + Math.pow(yDistance, 2);
if (distance < 2000) {
return [0, 0];
}
const xMovement = xDistance / Math.sqrt(distance);
const yMovement = yDistance / Math.sqrt(distance);
return [xMovement, yMovement];
}
public enableFollowing() {
followStateStore.set(followStates.active);
}
public moveUser(delta: number): void {
const activeEvents = this.userInputManager.getEventListForGameTick();
const state = get(followStateStore);
const role = get(followRoleStore);
if (activeEvents.get(UserInputEvent.Interact)) {
if (state === followStates.off && this.scene.groups.size > 0) {
followStateStore.set(followStates.requesting);
followRoleStore.set(followRoles.leader);
} else if (state === followStates.active) {
followStateStore.set(followStates.ending);
}
}
let x = 0;
let y = 0;
if ((state === followStates.active || state === followStates.ending) && role === followRoles.follower) {
[x, y] = this.computeFollowMovement();
}
this.inputStep(activeEvents, x, y);
}
}