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

128 lines
4.3 KiB
TypeScript
Raw Normal View History

import {PlayerAnimationDirections} from "./Animation";
import type {GameScene} from "../Game/GameScene";
2020-09-25 18:29:22 +02:00
import {UserInputEvent, UserInputManager} from "../UserInput/UserInputManager";
2020-06-04 18:54:34 +02:00
import {Character} from "../Entity/Character";
2021-05-10 17:10:41 +02:00
import {RadialMenu, RadialMenuClickEvent, RadialMenuItem} from "../Components/RadialMenu";
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 {
private previousDirection: string = PlayerAnimationDirections.Down;
private wasMoving: boolean = false;
2021-05-10 17:10:41 +02:00
private emoteMenu: RadialMenu|null = null;
2021-05-19 18:08:53 +02:00
private updateListener: () => void;
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>
) {
2021-05-10 17:10:41 +02:00
super(Scene, x, y, texturesPromise, name, direction, moving, 1, companion, companionTexturePromise);
//the current player model should be push away by other players to prevent conflict
this.getBody().setImmovable(false);
2021-05-19 18:08:53 +02:00
this.updateListener = () => {
if (this.emoteMenu) {
this.emoteMenu.x = this.x;
this.emoteMenu.y = this.y;
}
};
this.scene.events.addListener('postupdate', this.updateListener);
2020-05-26 17:43:25 +02:00
}
moveUser(delta: number): void {
//if user client on shift, camera and player speed
2020-04-07 21:02:23 +02:00
let direction = null;
let moving = false;
2020-06-09 23:13:26 +02:00
const activeEvents = this.userInputManager.getEventListForGameTick();
const speedMultiplier = activeEvents.get(UserInputEvent.SpeedUp) ? 25 : 9;
const moveAmount = speedMultiplier * 20;
2020-04-30 19:36:28 +02:00
let x = 0;
let y = 0;
if (activeEvents.get(UserInputEvent.MoveUp)) {
y = -moveAmount;
direction = PlayerAnimationDirections.Up;
moving = true;
2020-04-30 19:36:28 +02:00
} else if (activeEvents.get(UserInputEvent.MoveDown)) {
y = moveAmount;
direction = PlayerAnimationDirections.Down;
moving = true;
}
if (activeEvents.get(UserInputEvent.MoveLeft)) {
2020-04-30 19:36:28 +02:00
x = -moveAmount;
direction = PlayerAnimationDirections.Left;
moving = true;
2020-04-30 19:36:28 +02:00
} else if (activeEvents.get(UserInputEvent.MoveRight)) {
x = moveAmount;
direction = PlayerAnimationDirections.Right;
moving = true;
}
moving = moving || activeEvents.get(UserInputEvent.JoystickMove);
2021-04-02 17:14:34 +02:00
2020-04-30 19:36:28 +02:00
if (x !== 0 || y !== 0) {
this.move(x, y);
this.emit(hasMovedEventName, {moving, direction, x: this.x, y: this.y});
} else if (this.wasMoving && moving) {
// slow joystick movement
this.move(0, 0);
this.emit(hasMovedEventName, {moving, direction: this.previousDirection, x: this.x, y: this.y});
} else if (this.wasMoving && !moving) {
this.stop();
this.emit(hasMovedEventName, {moving, direction: this.previousDirection, x: this.x, y: this.y});
2020-04-07 20:41:35 +02:00
}
if (direction !== null) {
this.previousDirection = direction;
2020-04-07 20:41:35 +02:00
}
this.wasMoving = moving;
}
public isMoving(): boolean {
return this.wasMoving;
}
2021-05-10 17:10:41 +02:00
openOrCloseEmoteMenu(emotes:RadialMenuItem[]) {
if(this.emoteMenu) {
this.closeEmoteMenu();
} else {
this.openEmoteMenu(emotes);
}
}
isClickable(): boolean {
return true;
}
openEmoteMenu(emotes:RadialMenuItem[]): void {
this.cancelPreviousEmote();
2021-05-19 18:08:53 +02:00
this.emoteMenu = new RadialMenu(this.scene, this.x, this.y, emotes)
2021-05-10 17:10:41 +02:00
this.emoteMenu.on(RadialMenuClickEvent, (item: RadialMenuItem) => {
this.closeEmoteMenu();
this.emit(requestEmoteEventName, item.name);
this.playEmote(item.name);
2021-05-19 18:08:53 +02:00
});
2021-05-10 17:10:41 +02:00
}
closeEmoteMenu(): void {
if (!this.emoteMenu) return;
this.emoteMenu.destroy();
this.emoteMenu = null;
}
2021-05-19 18:08:53 +02:00
destroy() {
this.scene.events.removeListener('postupdate', this.updateListener);
super.destroy();
}
}