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

106 lines
3.7 KiB
TypeScript
Raw Normal View History

2020-04-07 20:41:35 +02:00
import {getPlayerAnimations, playAnimation, PlayerAnimationNames} from "./Animation";
import {GameSceneInterface, Textures} from "../Game/GameScene";
import {MessageUserPositionInterface} from "../../Connexion";
import {ActiveEventList, UserInputEvent, UserInputManager} from "../UserInput/UserInputManager";
import {PlayableCaracter} from "../Entity/PlayableCaracter";
export const hasMovedEventName = "hasMoved";
export interface CurrentGamerInterface extends PlayableCaracter{
2020-04-12 13:57:00 +02:00
initAnimation() : void;
moveUser(delta: number) : void;
say(text : string) : void;
2020-04-12 13:57:00 +02:00
}
export interface GamerInterface extends PlayableCaracter{
2020-04-12 13:57:00 +02:00
userId : string;
initAnimation() : void;
updatePosition(MessageUserPosition : MessageUserPositionInterface) : void;
say(text : string) : void;
2020-04-12 13:57:00 +02:00
}
2020-04-13 15:34:09 +02:00
export class Player extends PlayableCaracter implements CurrentGamerInterface, GamerInterface {
userId: string;
userInputManager: UserInputManager;
previousMove: string;
constructor(
userId: string,
2020-04-13 15:34:09 +02:00
Scene: GameSceneInterface,
x: number,
y: number,
name: string,
PlayerTexture: string = Textures.Player
) {
super(Scene, x, y, PlayerTexture, name, 1);
//create input to move
this.userInputManager = new UserInputManager(Scene);
//set data
this.userId = userId;
//the current player model should be push away by other players to prevent conflict
this.setImmovable(false);
}
2020-04-13 15:34:09 +02:00
initAnimation(): void {
getPlayerAnimations(this.PlayerTexture).forEach(d => {
this.scene.anims.create({
key: d.key,
frames: this.scene.anims.generateFrameNumbers(d.frameModel, {start: d.frameStart, end: d.frameEnd}),
frameRate: d.frameRate,
repeat: d.repeat
});
})
}
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 activeEvents = this.userInputManager.getEventListForGameTick();
let speedMultiplier = activeEvents.get(UserInputEvent.SpeedUp) ? 25 : 9;
let moveAmount = speedMultiplier * 20;
2020-04-30 19:36:28 +02:00
let x = 0;
let y = 0;
if (activeEvents.get(UserInputEvent.MoveUp)) {
2020-04-30 19:36:28 +02:00
y = - moveAmount;
direction = `${this.PlayerTexture}-${PlayerAnimationNames.WalkUp}`;
2020-04-30 19:36:28 +02:00
} else if (activeEvents.get(UserInputEvent.MoveDown)) {
y = moveAmount;
direction = `${this.PlayerTexture}-${PlayerAnimationNames.WalkDown}`;
}
if (activeEvents.get(UserInputEvent.MoveLeft)) {
2020-04-30 19:36:28 +02:00
x = -moveAmount;
direction = `${this.PlayerTexture}-${PlayerAnimationNames.WalkLeft}`;
2020-04-30 19:36:28 +02:00
} else if (activeEvents.get(UserInputEvent.MoveRight)) {
x = moveAmount;
direction = `${this.PlayerTexture}-${PlayerAnimationNames.WalkRight}`;
}
2020-04-30 19:36:28 +02:00
if (x !== 0 || y !== 0) {
this.move(x, y);
this.emit(hasMovedEventName, {direction, x: this.x, y: this.y});
2020-04-30 19:36:28 +02:00
} else {
if (this.previousMove !== PlayerAnimationNames.None) {
direction = PlayerAnimationNames.None;
this.stop();
this.emit(hasMovedEventName, {direction, x: this.x, y: this.y});
}
2020-04-07 20:41:35 +02:00
}
if (direction !== null) {
this.previousMove = direction;
2020-04-07 20:41:35 +02:00
}
}
//todo: put this method into the NonPlayer class instead
2020-04-13 15:34:09 +02:00
updatePosition(MessageUserPosition: MessageUserPositionInterface) {
playAnimation(this, MessageUserPosition.position.direction);
this.setX(MessageUserPosition.position.x);
this.setY(MessageUserPosition.position.y);
this.setDepth(MessageUserPosition.position.y);
}
}