workadventure/front/src/Phaser/Player/Player.ts

79 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-04-07 20:41:35 +02:00
import {getPlayerAnimations, playAnimation, PlayerAnimationNames} from "./Animation";
import {ActiveEventList, UserInputEvent, UserInputManager} from "../UserInput/UserInputManager";
import {PlayableCaracter} from "../Entity/PlayableCaracter";
2020-04-19 20:45:34 +02:00
import {gameManager} from "../../Connexion/GameManager";
import {Textures} from "../Game/GameScene";
export interface CurrentGamerInterface extends PlayableCaracter{
2020-04-12 13:57:00 +02:00
userId : string;
PlayerValue : string;
initAnimation() : void;
moveUser() : void;
say(text : string) : void;
2020-04-12 13:57:00 +02:00
}
2020-04-19 20:45:34 +02:00
export class Player extends PlayableCaracter implements CurrentGamerInterface {
2020-04-13 15:34:09 +02:00
userId: string;
PlayerValue: string;
userInputManager: UserInputManager;
2020-04-19 18:39:59 +02:00
private email: string;
constructor(
userId: string,
2020-04-19 18:39:59 +02:00
email: string,
2020-04-19 20:45:34 +02:00
Scene: Phaser.Scene,
2020-04-13 15:34:09 +02:00
x: number,
y: number,
PlayerValue: string = Textures.Player
) {
super(Scene, x, y, PlayerValue, 1);
//create input to move
this.userInputManager = new UserInputManager(Scene);
//set data
this.userId = userId;
2020-04-19 18:39:59 +02:00
this.email = email;
this.PlayerValue = PlayerValue;
//the current player model should be push away by other players to prevent conflict
this.setImmovable(false);
2020-04-19 18:39:59 +02:00
this.say("My email is "+this.email)
}
2020-04-13 15:34:09 +02:00
moveUser(): void {
//if user client on shift, camera and player speed
let haveMove = false;
2020-04-07 21:02:23 +02:00
let direction = null;
let activeEvents = this.userInputManager.getEventListForGameTick();
let speedMultiplier = activeEvents.get(UserInputEvent.SpeedUp) ? 500 : 100;
if (activeEvents.get(UserInputEvent.MoveUp)) {
this.move(0, -speedMultiplier);
haveMove = true;
2020-04-07 21:02:23 +02:00
direction = PlayerAnimationNames.WalkUp;
}
if (activeEvents.get(UserInputEvent.MoveLeft)) {
this.move(-speedMultiplier, 0);
haveMove = true;
2020-04-07 21:02:23 +02:00
direction = PlayerAnimationNames.WalkLeft;
}
if (activeEvents.get(UserInputEvent.MoveDown)) {
this.move(0, speedMultiplier);
haveMove = true;
2020-04-07 21:02:23 +02:00
direction = PlayerAnimationNames.WalkDown;
}
if (activeEvents.get(UserInputEvent.MoveRight)) {
this.move(speedMultiplier, 0);
haveMove = true;
2020-04-07 21:02:23 +02:00
direction = PlayerAnimationNames.WalkRight;
}
if (!haveMove) {
direction = PlayerAnimationNames.None;
2020-04-13 15:41:11 +02:00
this.stop();
2020-04-07 20:41:35 +02:00
}
2020-04-19 20:45:34 +02:00
gameManager.updateConnectedUserPosition(this.x, this.y);
}
}