partey_workadventure/front/src/Phaser/Game/GameManager.ts

83 lines
2.3 KiB
TypeScript
Raw Normal View History

import {GameScene} from "./GameScene";
2020-04-07 20:41:35 +02:00
import {ROOM} from "../../Enum/EnvironmentVariable"
2020-04-12 13:57:00 +02:00
import {Connexion, ConnexionInterface, ListMessageUserPositionInterface} from "../../Connexion";
import {SimplePeerInterface, SimplePeer} from "../../WebRtc/SimplePeer";
import {LogincScene} from "../Login/LogincScene";
export enum StatusGameManagerEnum {
IN_PROGRESS = 1,
CURRENT_USER_CREATED = 2
}
2020-04-07 20:41:35 +02:00
export interface HasMovedEvent {
direction: string;
x: number;
y: number;
frame: string;
}
2020-04-07 20:41:35 +02:00
export class GameManager {
status: number;
private ConnexionInstance: Connexion;
private currentGameScene: GameScene;
private playerName: string;
SimplePeer : SimplePeerInterface;
private frameUserSelected: string;
2020-04-07 20:41:35 +02:00
constructor() {
this.status = StatusGameManagerEnum.IN_PROGRESS;
2020-04-07 20:41:35 +02:00
}
connect(name: string, frameUserSelected : string) {
this.playerName = name;
this.frameUserSelected = frameUserSelected;
this.ConnexionInstance = new Connexion(name, this);
return this.ConnexionInstance.createConnexion(frameUserSelected).then(() => {
this.SimplePeer = new SimplePeer(this.ConnexionInstance);
});
}
setCurrentGameScene(gameScene: GameScene) {
this.currentGameScene = gameScene;
2020-04-07 20:41:35 +02:00
}
/**
* Permit to create player in started room
*/
createCurrentPlayer(): void {
//Get started room send by the backend
this.currentGameScene.createCurrentPlayer(this.ConnexionInstance.userId);
this.status = StatusGameManagerEnum.CURRENT_USER_CREATED;
}
/**
* Share position in game
* @param ListMessageUserPosition
*/
shareUserPosition(ListMessageUserPosition: ListMessageUserPositionInterface): void {
if (this.status === StatusGameManagerEnum.IN_PROGRESS) {
return;
}
try {
this.currentGameScene.shareUserPosition(ListMessageUserPosition.listUsersPosition)
} catch (e) {
console.error(e);
}
2020-04-07 20:41:35 +02:00
}
getPlayerName(): string {
return this.playerName;
}
2020-05-03 15:51:16 +02:00
getFrameSelected(): string {
return this.frameUserSelected;
}
pushPlayerPosition(event: HasMovedEvent) {
this.ConnexionInstance.sharePosition(event.x, event.y, event.frame, event.direction);
}
}
export const gameManager = new GameManager();