2020-04-07 20:41:35 +02:00
|
|
|
import {GameSceneInterface, GameScene} from "./GameScene";
|
|
|
|
import {ROOM} from "../../Enum/EnvironmentVariable"
|
2020-04-12 13:57:00 +02:00
|
|
|
import {Connexion, ConnexionInterface, ListMessageUserPositionInterface} from "../../Connexion";
|
2020-04-26 19:59:51 +02:00
|
|
|
import {SimplePeerInterface, SimplePeer} from "../../WebRtc/SimplePeer";
|
2020-04-29 00:01:37 +02:00
|
|
|
import {LogincScene} from "../Login/LogincScene";
|
2020-04-10 12:54:05 +02:00
|
|
|
|
|
|
|
export enum StatusGameManagerEnum {
|
|
|
|
IN_PROGRESS = 1,
|
|
|
|
CURRENT_USER_CREATED = 2
|
|
|
|
}
|
2020-04-07 20:41:35 +02:00
|
|
|
|
2020-04-12 13:57:00 +02:00
|
|
|
export let ConnexionInstance : ConnexionInterface;
|
2020-04-07 20:41:35 +02:00
|
|
|
|
|
|
|
export interface GameManagerInterface {
|
|
|
|
GameScenes: Array<GameSceneInterface>;
|
2020-04-10 12:54:05 +02:00
|
|
|
status : number;
|
2020-04-26 19:59:51 +02:00
|
|
|
SimplePeer: SimplePeerInterface;
|
2020-04-10 12:54:05 +02:00
|
|
|
createCurrentPlayer() : void;
|
|
|
|
shareUserPosition(ListMessageUserPosition : ListMessageUserPositionInterface): void;
|
2020-04-29 00:01:37 +02:00
|
|
|
connect(email : string) : Promise<any>;
|
2020-04-07 20:41:35 +02:00
|
|
|
}
|
|
|
|
export class GameManager implements GameManagerInterface {
|
|
|
|
GameScenes: Array<GameSceneInterface> = [];
|
2020-04-10 12:54:05 +02:00
|
|
|
status: number;
|
2020-04-26 19:59:51 +02:00
|
|
|
SimplePeer : SimplePeerInterface;
|
2020-04-07 20:41:35 +02:00
|
|
|
|
|
|
|
constructor() {
|
2020-04-10 12:54:05 +02:00
|
|
|
this.status = StatusGameManagerEnum.IN_PROGRESS;
|
2020-04-26 17:54:56 +02:00
|
|
|
this.configureGame();
|
2020-04-07 20:41:35 +02:00
|
|
|
}
|
|
|
|
|
2020-04-29 00:01:37 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param email
|
|
|
|
*/
|
|
|
|
connect(email : string) : Promise<any> {
|
2020-04-28 20:50:51 +02:00
|
|
|
ConnexionInstance = new Connexion(email, this);
|
2020-04-29 00:01:37 +02:00
|
|
|
return ConnexionInstance.createConnexion().then(() => {
|
|
|
|
this.SimplePeer = new SimplePeer(ConnexionInstance);
|
|
|
|
});
|
2020-04-10 12:54:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* permit to config rooms
|
|
|
|
*/
|
2020-04-07 20:41:35 +02:00
|
|
|
configureGame() {
|
2020-04-29 00:01:37 +02:00
|
|
|
//create login scene
|
|
|
|
let LoginScene = new LogincScene();
|
|
|
|
this.GameScenes.push(LoginScene)
|
|
|
|
|
|
|
|
//create scene
|
2020-04-07 20:41:35 +02:00
|
|
|
ROOM.forEach((roomId) => {
|
|
|
|
let newGame = new GameScene(roomId, this);
|
2020-04-10 12:54:05 +02:00
|
|
|
this.GameScenes.push((newGame as GameSceneInterface));
|
2020-04-07 20:41:35 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
/**
|
2020-04-29 00:01:37 +02:00
|
|
|
*
|
2020-04-10 12:54:05 +02:00
|
|
|
*/
|
|
|
|
createCurrentPlayer(): void {
|
2020-04-13 13:42:21 +02:00
|
|
|
//Get started room send by the backend
|
2020-04-10 12:54:05 +02:00
|
|
|
let game: GameSceneInterface = this.GameScenes.find((Game: GameSceneInterface) => Game.RoomId === ConnexionInstance.startedRoom);
|
2020-04-29 00:01:37 +02:00
|
|
|
if(!game){
|
|
|
|
return;
|
|
|
|
}
|
2020-04-10 12:54:05 +02:00
|
|
|
game.createCurrentPlayer(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 {
|
|
|
|
let Game: GameSceneInterface = this.GameScenes.find((Game: GameSceneInterface) => Game.RoomId === ListMessageUserPosition.roomId);
|
|
|
|
if (!Game) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Game.shareUserPosition(ListMessageUserPosition.listUsersPosition)
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2020-04-07 20:41:35 +02:00
|
|
|
}
|
2020-04-26 17:54:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const gameManager = new GameManager();
|