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

112 lines
3.6 KiB
TypeScript
Raw Normal View History

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";
import {CameraManagerInterface} from "../Game/CameraManager";
import {ActiveEventList, UserInputEvent} from "../UserInput/UserInputManager";
export class Player extends Phaser.GameObjects.Sprite{
MapManager : MapManagerInterface;
PlayerValue : string;
CameraManager: CameraManagerInterface;
constructor(
2020-04-07 20:41:35 +02:00
Scene : GameSceneInterface,
x : number,
y : number,
CameraManager: CameraManagerInterface,
MapManager: MapManagerInterface,
PlayerValue : string = "player"
) {
super(Scene, x, y, PlayerValue);
this.PlayerValue = PlayerValue;
Scene.add.existing(this);
this.MapManager = MapManager;
this.CameraManager = CameraManager;
}
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(activeEvents: ActiveEventList){
//if user client on shift, camera and player speed
let speedMultiplier = activeEvents.get(UserInputEvent.SpeedUp) ? 5 : 1;
let haveMove = false;
2020-04-07 21:02:23 +02:00
let direction = null;
if(activeEvents.get(UserInputEvent.MoveUp)){
if(!this.CanMoveUp()){
return;
}
playAnimation(this, PlayerAnimationNames.WalkUp);
this.setY(this.y - (2 * speedMultiplier));
haveMove = true;
2020-04-07 21:02:23 +02:00
direction = PlayerAnimationNames.WalkUp;
}
if(activeEvents.get(UserInputEvent.MoveLeft)){
if(!this.CanMoveLeft()){
return;
}
playAnimation(this, PlayerAnimationNames.WalkLeft);
this.setX(this.x - (2 * speedMultiplier));
haveMove = true;
2020-04-07 21:02:23 +02:00
direction = PlayerAnimationNames.WalkLeft;
}
if(activeEvents.get(UserInputEvent.MoveDown)){
if(!this.CanMoveDown()){
return;
}
playAnimation(this, PlayerAnimationNames.WalkDown);
this.setY(this.y + (2 * speedMultiplier));
haveMove = true;
2020-04-07 21:02:23 +02:00
direction = PlayerAnimationNames.WalkDown;
}
if(activeEvents.get(UserInputEvent.MoveRight)){
if(!this.CanMoveRight()){
return;
}
playAnimation(this, PlayerAnimationNames.WalkRight);
this.setX(this.x + (2 * speedMultiplier));
haveMove = true;
2020-04-07 21:02:23 +02:00
direction = PlayerAnimationNames.WalkRight;
}
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
}
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);
}
}
private CanMoveUp(){
return this.y > 0;
}
private CanMoveLeft(){
return this.x > 0;
}
private CanMoveDown(){
return this.MapManager.Map.heightInPixels > this.y;
}
private CanMoveRight(){
return this.MapManager.Map.widthInPixels > this.x;
}
}