2020-04-07 20:41:35 +02:00
|
|
|
import {MapManagerInterface} from "../Game/MapManager";
|
|
|
|
import {getPlayerAnimations, playAnimation, PlayerAnimationNames} from "./Animation";
|
|
|
|
import {GameSceneInterface} from "../Game/GameScene";
|
|
|
|
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-07 19:23:21 +02:00
|
|
|
|
2020-04-12 13:57:00 +02:00
|
|
|
export interface CurrentGamerInterface{
|
|
|
|
userId : string;
|
|
|
|
MapManager : MapManagerInterface;
|
|
|
|
PlayerValue : string;
|
|
|
|
CameraManager: CameraManagerInterface;
|
|
|
|
initAnimation() : void;
|
|
|
|
move() : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface GamerInterface{
|
|
|
|
userId : string;
|
|
|
|
MapManager : MapManagerInterface;
|
|
|
|
PlayerValue : string;
|
|
|
|
CameraManager: CameraManagerInterface;
|
|
|
|
initAnimation() : void;
|
|
|
|
updatePosition(MessageUserPosition : MessageUserPositionInterface) : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Player extends Phaser.GameObjects.Sprite implements CurrentGamerInterface, GamerInterface{
|
2020-04-10 12:54:05 +02:00
|
|
|
userId : string;
|
2020-04-07 19:23:21 +02:00
|
|
|
MapManager : MapManagerInterface;
|
|
|
|
PlayerValue : string;
|
2020-04-07 22:38:53 +02:00
|
|
|
CameraManager: CameraManagerInterface;
|
2020-04-07 19:23:21 +02:00
|
|
|
|
|
|
|
constructor(
|
2020-04-10 12:54:05 +02:00
|
|
|
userId: string,
|
2020-04-07 20:41:35 +02:00
|
|
|
Scene : GameSceneInterface,
|
2020-04-07 19:23:21 +02:00
|
|
|
x : number,
|
|
|
|
y : number,
|
2020-04-07 22:38:53 +02:00
|
|
|
CameraManager: CameraManagerInterface,
|
2020-04-07 19:23:21 +02:00
|
|
|
MapManager: MapManagerInterface,
|
|
|
|
PlayerValue : string = "player"
|
|
|
|
) {
|
|
|
|
super(Scene, x, y, PlayerValue);
|
2020-04-10 12:54:05 +02:00
|
|
|
this.userId = userId;
|
2020-04-07 19:23:21 +02:00
|
|
|
this.PlayerValue = PlayerValue;
|
|
|
|
Scene.add.existing(this);
|
|
|
|
this.MapManager = MapManager;
|
2020-04-07 22:38:53 +02:00
|
|
|
this.CameraManager = CameraManager;
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
|
|
|
|
2020-04-12 13:57:00 +02:00
|
|
|
initAnimation() : void{
|
2020-04-07 19:23:21 +02:00
|
|
|
getPlayerAnimations(this.PlayerValue).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
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:57:00 +02:00
|
|
|
move() : void{
|
2020-04-07 19:23:21 +02:00
|
|
|
//if user client on shift, camera and player speed
|
|
|
|
let speedMultiplier = this.MapManager.keyShift.isDown ? 5 : 1;
|
|
|
|
let haveMove = false;
|
2020-04-07 21:02:23 +02:00
|
|
|
let direction = null;
|
2020-04-07 19:23:21 +02:00
|
|
|
|
|
|
|
if((this.MapManager.keyZ.isDown || this.MapManager.keyUp.isDown)){
|
2020-04-07 22:38:53 +02:00
|
|
|
if(!this.CanMoveUp()){
|
2020-04-07 19:23:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
playAnimation(this, PlayerAnimationNames.WalkUp);
|
|
|
|
this.setY(this.y - (2 * speedMultiplier));
|
|
|
|
haveMove = true;
|
2020-04-07 21:02:23 +02:00
|
|
|
direction = PlayerAnimationNames.WalkUp;
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
|
|
|
if((this.MapManager.keyQ.isDown || this.MapManager.keyLeft.isDown)){
|
2020-04-07 22:38:53 +02:00
|
|
|
if(!this.CanMoveLeft()){
|
2020-04-07 19:23:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
playAnimation(this, PlayerAnimationNames.WalkLeft);
|
|
|
|
this.setX(this.x - (2 * speedMultiplier));
|
|
|
|
haveMove = true;
|
2020-04-07 21:02:23 +02:00
|
|
|
direction = PlayerAnimationNames.WalkLeft;
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
|
|
|
if((this.MapManager.keyS.isDown || this.MapManager.keyDown.isDown)){
|
2020-04-07 22:38:53 +02:00
|
|
|
if(!this.CanMoveDown()){
|
2020-04-07 19:23:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
playAnimation(this, PlayerAnimationNames.WalkDown);
|
|
|
|
this.setY(this.y + (2 * speedMultiplier));
|
|
|
|
haveMove = true;
|
2020-04-07 21:02:23 +02:00
|
|
|
direction = PlayerAnimationNames.WalkDown;
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
|
|
|
if((this.MapManager.keyD.isDown || this.MapManager.keyRight.isDown)){
|
2020-04-07 22:38:53 +02:00
|
|
|
if(!this.CanMoveRight()){
|
2020-04-07 19:23:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
playAnimation(this, PlayerAnimationNames.WalkRight);
|
|
|
|
this.setX(this.x + (2 * speedMultiplier));
|
|
|
|
haveMove = true;
|
2020-04-07 21:02:23 +02:00
|
|
|
direction = PlayerAnimationNames.WalkRight;
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
|
|
|
if(!haveMove){
|
|
|
|
playAnimation(this, PlayerAnimationNames.None);
|
2020-04-10 12:54:05 +02:00
|
|
|
direction = PlayerAnimationNames.None;
|
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-07 21:02:23 +02:00
|
|
|
private sharePosition(direction : string){
|
2020-04-07 20:41:35 +02:00
|
|
|
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-07 22:38:53 +02:00
|
|
|
private CanMoveUp(){
|
2020-04-07 19:23:21 +02:00
|
|
|
return this.y > 0;
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:38:53 +02:00
|
|
|
private CanMoveLeft(){
|
2020-04-07 19:23:21 +02:00
|
|
|
return this.x > 0;
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:38:53 +02:00
|
|
|
private CanMoveDown(){
|
2020-04-07 19:23:21 +02:00
|
|
|
return this.MapManager.Map.heightInPixels > this.y;
|
|
|
|
}
|
|
|
|
|
2020-04-07 22:38:53 +02:00
|
|
|
private CanMoveRight(){
|
2020-04-07 19:23:21 +02:00
|
|
|
return this.MapManager.Map.widthInPixels > this.x;
|
|
|
|
}
|
2020-04-10 12:54:05 +02:00
|
|
|
|
|
|
|
updatePosition(MessageUserPosition : MessageUserPositionInterface){
|
|
|
|
playAnimation(this, MessageUserPosition.position.direction);
|
|
|
|
this.setX(MessageUserPosition.position.x);
|
|
|
|
this.setY(MessageUserPosition.position.y);
|
|
|
|
}
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|