2020-05-09 19:41:21 +02:00
|
|
|
import {GameScene, GameSceneInterface} from "./GameScene";
|
2020-05-08 00:35:36 +02:00
|
|
|
import {
|
|
|
|
Connexion,
|
|
|
|
GroupCreatedUpdatedMessageInterface,
|
|
|
|
ListMessageUserPositionInterface
|
|
|
|
} from "../../Connexion";
|
2020-04-26 19:59:51 +02:00
|
|
|
import {SimplePeerInterface, SimplePeer} from "../../WebRtc/SimplePeer";
|
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-05-02 16:54:52 +02:00
|
|
|
export interface HasMovedEvent {
|
|
|
|
direction: string;
|
|
|
|
x: number;
|
|
|
|
y: number;
|
2020-05-08 15:18:22 +02:00
|
|
|
character: string;
|
2020-05-02 16:54:52 +02:00
|
|
|
}
|
2020-04-07 20:41:35 +02:00
|
|
|
|
2020-05-09 21:28:50 +02:00
|
|
|
export interface MapObject {
|
|
|
|
key: string,
|
|
|
|
url: string
|
|
|
|
}
|
|
|
|
|
2020-04-27 15:03:05 +02:00
|
|
|
export class GameManager {
|
2020-04-10 12:54:05 +02:00
|
|
|
status: number;
|
2020-04-26 17:54:56 +02:00
|
|
|
private ConnexionInstance: Connexion;
|
2020-05-09 19:41:21 +02:00
|
|
|
private currentGameScene: GameSceneInterface;
|
2020-05-03 15:29:40 +02:00
|
|
|
private playerName: string;
|
2020-04-26 19:59:51 +02:00
|
|
|
SimplePeer : SimplePeerInterface;
|
2020-05-08 15:18:22 +02:00
|
|
|
private characterUserSelected: string;
|
2020-05-09 21:28:50 +02:00
|
|
|
Maps: Array<MapObject>;
|
2020-04-07 20:41:35 +02:00
|
|
|
|
|
|
|
constructor() {
|
2020-04-10 12:54:05 +02:00
|
|
|
this.status = StatusGameManagerEnum.IN_PROGRESS;
|
2020-04-07 20:41:35 +02:00
|
|
|
}
|
2020-05-03 15:29:40 +02:00
|
|
|
|
2020-05-08 15:18:22 +02:00
|
|
|
connect(name: string, characterUserSelected : string) {
|
2020-05-03 15:29:40 +02:00
|
|
|
this.playerName = name;
|
2020-05-08 15:18:22 +02:00
|
|
|
this.characterUserSelected = characterUserSelected;
|
2020-05-03 15:29:40 +02:00
|
|
|
this.ConnexionInstance = new Connexion(name, this);
|
2020-05-09 19:41:21 +02:00
|
|
|
return this.ConnexionInstance.createConnexion(characterUserSelected).then((data : any) => {
|
2020-05-02 16:54:52 +02:00
|
|
|
this.SimplePeer = new SimplePeer(this.ConnexionInstance);
|
2020-05-09 19:41:21 +02:00
|
|
|
return data;
|
|
|
|
}).catch((err) => {
|
|
|
|
throw err;
|
2020-04-29 00:01:37 +02:00
|
|
|
});
|
2020-04-10 12:54:05 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 19:41:21 +02:00
|
|
|
loadMaps(){
|
2020-05-09 21:28:50 +02:00
|
|
|
return this.ConnexionInstance.loadMaps().then((data) => {
|
|
|
|
this.Maps = data.maps;
|
|
|
|
return data;
|
2020-05-09 19:41:21 +02:00
|
|
|
}).catch((err) => {
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setCurrentGameScene(gameScene: GameSceneInterface) {
|
2020-04-27 15:03:05 +02:00
|
|
|
this.currentGameScene = gameScene;
|
2020-04-07 20:41:35 +02:00
|
|
|
}
|
|
|
|
|
2020-04-27 15:03:05 +02:00
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
/**
|
|
|
|
* Permit to create player in started room
|
|
|
|
*/
|
|
|
|
createCurrentPlayer(): void {
|
2020-04-13 13:42:21 +02:00
|
|
|
//Get started room send by the backend
|
2020-04-27 15:03:05 +02:00
|
|
|
this.currentGameScene.createCurrentPlayer(this.ConnexionInstance.userId);
|
2020-04-10 12:54:05 +02:00
|
|
|
this.status = StatusGameManagerEnum.CURRENT_USER_CREATED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Share position in game
|
|
|
|
* @param ListMessageUserPosition
|
|
|
|
*/
|
|
|
|
shareUserPosition(ListMessageUserPosition: ListMessageUserPositionInterface): void {
|
|
|
|
if (this.status === StatusGameManagerEnum.IN_PROGRESS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2020-04-27 15:03:05 +02:00
|
|
|
this.currentGameScene.shareUserPosition(ListMessageUserPosition.listUsersPosition)
|
2020-04-10 12:54:05 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2020-04-07 20:41:35 +02:00
|
|
|
}
|
2020-05-03 15:29:40 +02:00
|
|
|
|
2020-05-08 00:35:36 +02:00
|
|
|
/**
|
|
|
|
* Share group position in game
|
|
|
|
*/
|
|
|
|
shareGroupPosition(groupPositionMessage: GroupCreatedUpdatedMessageInterface): void {
|
|
|
|
if (this.status === StatusGameManagerEnum.IN_PROGRESS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
this.currentGameScene.shareGroupPosition(groupPositionMessage)
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteGroup(groupId: string): void {
|
|
|
|
if (this.status === StatusGameManagerEnum.IN_PROGRESS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
this.currentGameScene.deleteGroup(groupId)
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 15:29:40 +02:00
|
|
|
getPlayerName(): string {
|
|
|
|
return this.playerName;
|
|
|
|
}
|
2020-05-03 15:51:16 +02:00
|
|
|
|
2020-05-08 15:18:22 +02:00
|
|
|
getCharacterSelected(): string {
|
|
|
|
return this.characterUserSelected;
|
2020-05-06 01:50:01 +02:00
|
|
|
}
|
|
|
|
|
2020-05-02 16:54:52 +02:00
|
|
|
pushPlayerPosition(event: HasMovedEvent) {
|
2020-05-08 15:18:22 +02:00
|
|
|
this.ConnexionInstance.sharePosition(event.x, event.y, event.character, event.direction);
|
2020-05-02 16:54:52 +02:00
|
|
|
}
|
2020-04-26 17:54:56 +02:00
|
|
|
}
|
|
|
|
|
2020-05-03 15:29:40 +02:00
|
|
|
export const gameManager = new GameManager();
|