2020-04-07 20:41:35 +02:00
|
|
|
import {getPlayerAnimations, playAnimation, PlayerAnimationNames} from "./Animation";
|
2020-04-12 16:12:08 +02:00
|
|
|
import {GameSceneInterface, Textures} from "../Game/GameScene";
|
2020-04-07 20:41:35 +02:00
|
|
|
import {ConnexionInstance} from "../Game/GameManager";
|
2020-04-07 22:38:53 +02:00
|
|
|
import {CameraManagerInterface} from "../Game/CameraManager";
|
2020-04-10 12:54:05 +02:00
|
|
|
import {MessageUserPositionInterface} from "../../Connexion";
|
2020-04-13 13:42:21 +02:00
|
|
|
import {ActiveEventList, UserInputEvent, UserInputManager} from "../UserInput/UserInputManager";
|
2020-04-12 16:12:08 +02:00
|
|
|
import {PlayableCaracter} from "../Entity/PlayableCaracter";
|
2020-04-07 19:23:21 +02:00
|
|
|
|
2020-04-13 13:42:21 +02:00
|
|
|
export interface CurrentGamerInterface extends PlayableCaracter{
|
2020-04-12 13:57:00 +02:00
|
|
|
userId : string;
|
|
|
|
PlayerValue : string;
|
|
|
|
CameraManager: CameraManagerInterface;
|
|
|
|
initAnimation() : void;
|
2020-04-13 13:42:21 +02:00
|
|
|
moveUser() : void;
|
|
|
|
say(text : string) : void;
|
2020-04-12 13:57:00 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 13:42:21 +02:00
|
|
|
export interface GamerInterface extends PlayableCaracter{
|
2020-04-12 13:57:00 +02:00
|
|
|
userId : string;
|
|
|
|
PlayerValue : string;
|
|
|
|
CameraManager: CameraManagerInterface;
|
|
|
|
initAnimation() : void;
|
|
|
|
updatePosition(MessageUserPosition : MessageUserPositionInterface) : void;
|
2020-04-13 13:42:21 +02:00
|
|
|
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;
|
|
|
|
PlayerValue: string;
|
2020-04-07 22:38:53 +02:00
|
|
|
CameraManager: CameraManagerInterface;
|
2020-04-13 13:42:21 +02:00
|
|
|
userInputManager: UserInputManager;
|
2020-04-07 19:23:21 +02:00
|
|
|
|
|
|
|
constructor(
|
2020-04-10 12:54:05 +02:00
|
|
|
userId: string,
|
2020-04-13 15:34:09 +02:00
|
|
|
Scene: GameSceneInterface,
|
|
|
|
x: number,
|
|
|
|
y: number,
|
2020-04-07 22:38:53 +02:00
|
|
|
CameraManager: CameraManagerInterface,
|
2020-04-13 15:34:09 +02:00
|
|
|
PlayerValue: string = Textures.Player
|
2020-04-07 19:23:21 +02:00
|
|
|
) {
|
2020-04-13 13:42:21 +02:00
|
|
|
super(Scene, x, y, PlayerValue, 1);
|
|
|
|
|
|
|
|
//create input to move
|
|
|
|
this.userInputManager = new UserInputManager(Scene);
|
|
|
|
|
|
|
|
//set data
|
2020-04-10 12:54:05 +02:00
|
|
|
this.userId = userId;
|
2020-04-07 19:23:21 +02:00
|
|
|
this.PlayerValue = PlayerValue;
|
2020-04-07 22:38:53 +02:00
|
|
|
this.CameraManager = CameraManager;
|
2020-04-13 13:42:21 +02:00
|
|
|
|
|
|
|
//the current player model should be push away by other players to prevent conflict
|
|
|
|
this.setImmovable(false);
|
|
|
|
//edit the hitbox to better match the caracter model
|
|
|
|
this.setSize(32, 32);
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 15:34:09 +02:00
|
|
|
initAnimation(): void {
|
2020-04-13 13:42:21 +02:00
|
|
|
getPlayerAnimations().forEach(d => {
|
2020-04-07 19:23:21 +02:00
|
|
|
this.scene.anims.create({
|
|
|
|
key: d.key,
|
2020-04-13 13:42:21 +02:00
|
|
|
frames: this.scene.anims.generateFrameNumbers(d.frameModel, {start: d.frameStart, end: d.frameEnd}),
|
2020-04-07 19:23:21 +02:00
|
|
|
frameRate: d.frameRate,
|
|
|
|
repeat: d.repeat
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-13 15:34:09 +02:00
|
|
|
moveUser(): void {
|
2020-04-07 19:23:21 +02:00
|
|
|
//if user client on shift, camera and player speed
|
|
|
|
let haveMove = false;
|
2020-04-07 21:02:23 +02:00
|
|
|
let direction = null;
|
2020-04-07 19:23:21 +02:00
|
|
|
|
2020-04-13 13:42:21 +02:00
|
|
|
let activeEvents = this.userInputManager.getEventListForGameTick();
|
|
|
|
let speedMultiplier = activeEvents.get(UserInputEvent.SpeedUp) ? 500 : 100;
|
|
|
|
|
|
|
|
if (activeEvents.get(UserInputEvent.MoveUp)) {
|
|
|
|
this.move(0, -speedMultiplier);
|
2020-04-07 19:23:21 +02:00
|
|
|
haveMove = true;
|
2020-04-07 21:02:23 +02:00
|
|
|
direction = PlayerAnimationNames.WalkUp;
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
2020-04-13 13:42:21 +02:00
|
|
|
if (activeEvents.get(UserInputEvent.MoveLeft)) {
|
|
|
|
this.move(-speedMultiplier, 0);
|
2020-04-07 19:23:21 +02:00
|
|
|
haveMove = true;
|
2020-04-07 21:02:23 +02:00
|
|
|
direction = PlayerAnimationNames.WalkLeft;
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
2020-04-13 13:42:21 +02:00
|
|
|
if (activeEvents.get(UserInputEvent.MoveDown)) {
|
|
|
|
this.move(0, speedMultiplier);
|
2020-04-07 19:23:21 +02:00
|
|
|
haveMove = true;
|
2020-04-07 21:02:23 +02:00
|
|
|
direction = PlayerAnimationNames.WalkDown;
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
2020-04-13 13:42:21 +02:00
|
|
|
if (activeEvents.get(UserInputEvent.MoveRight)) {
|
|
|
|
this.move(speedMultiplier, 0);
|
2020-04-07 19:23:21 +02:00
|
|
|
haveMove = true;
|
2020-04-07 21:02:23 +02:00
|
|
|
direction = PlayerAnimationNames.WalkRight;
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
2020-04-13 13:42:21 +02:00
|
|
|
if (!haveMove) {
|
2020-04-10 12:54:05 +02:00
|
|
|
direction = PlayerAnimationNames.None;
|
2020-04-13 13:42:21 +02:00
|
|
|
this.move(0, 0)
|
2020-04-07 20:41:35 +02:00
|
|
|
}
|
2020-04-10 12:54:05 +02:00
|
|
|
this.sharePosition(direction);
|
2020-04-07 22:38:53 +02:00
|
|
|
this.CameraManager.moveCamera(this);
|
2020-04-07 20:41:35 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 15:34:09 +02:00
|
|
|
private sharePosition(direction: string) {
|
|
|
|
if (ConnexionInstance) {
|
2020-04-07 21:02:23 +02:00
|
|
|
ConnexionInstance.sharePosition((this.scene as GameSceneInterface).RoomId, this.x, this.y, direction);
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 15:34:09 +02:00
|
|
|
updatePosition(MessageUserPosition: MessageUserPositionInterface) {
|
2020-04-10 12:54:05 +02:00
|
|
|
playAnimation(this, MessageUserPosition.position.direction);
|
|
|
|
this.setX(MessageUserPosition.position.x);
|
|
|
|
this.setY(MessageUserPosition.position.y);
|
|
|
|
}
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|