2020-04-07 20:41:35 +02:00
|
|
|
import {MapManagerInterface} from "../Game/MapManager";
|
|
|
|
import {getPlayerAnimations, playAnimation, PlayerAnimationNames} from "./Animation";
|
|
|
|
import {Connexion} from "../../Connexion";
|
|
|
|
import {GameSceneInterface} from "../Game/GameScene";
|
|
|
|
import {ConnexionInstance} from "../Game/GameManager";
|
2020-04-07 19:23:21 +02:00
|
|
|
|
|
|
|
export class Player extends Phaser.GameObjects.Sprite{
|
|
|
|
MapManager : MapManagerInterface;
|
|
|
|
PlayerValue : string;
|
2020-04-07 20:41:35 +02:00
|
|
|
Connexion: Connexion;
|
2020-04-07 19:23:21 +02:00
|
|
|
|
|
|
|
constructor(
|
2020-04-07 20:41:35 +02:00
|
|
|
Scene : GameSceneInterface,
|
2020-04-07 19:23:21 +02:00
|
|
|
x : number,
|
|
|
|
y : number,
|
|
|
|
MapManager: MapManagerInterface,
|
|
|
|
PlayerValue : string = "player"
|
|
|
|
) {
|
|
|
|
super(Scene, x, y, PlayerValue);
|
|
|
|
this.PlayerValue = PlayerValue;
|
|
|
|
Scene.add.existing(this);
|
|
|
|
this.MapManager = MapManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
initAnimation(){
|
|
|
|
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
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
move(){
|
|
|
|
//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)){
|
|
|
|
if(!this.CanToMoveUp()){
|
|
|
|
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)){
|
|
|
|
if(!this.CanToMoveLeft()){
|
|
|
|
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)){
|
|
|
|
if(!this.CanToMoveDown()){
|
|
|
|
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)){
|
|
|
|
if(!this.CanToMoveRight()){
|
|
|
|
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-07 20:41:35 +02:00
|
|
|
}else{
|
2020-04-07 21:02:23 +02:00
|
|
|
this.sharePosition(direction);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private CanToMoveUp(){
|
|
|
|
return this.y > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private CanToMoveLeft(){
|
|
|
|
return this.x > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private CanToMoveDown(){
|
|
|
|
return this.MapManager.Map.heightInPixels > this.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
private CanToMoveRight(){
|
|
|
|
return this.MapManager.Map.widthInPixels > this.x;
|
|
|
|
}
|
|
|
|
}
|