27c6034661
- Create position and check if user is in position to switch in the next scene. - When scene is load, we load all scene in the layer of name "exit". - Layer "exit" of map.json have a parametter "exitSceneKey" to identify next scene. - Add layer "start", the player could start in the scene on the object present in the layer of name "start".
128 lines
3.4 KiB
TypeScript
128 lines
3.4 KiB
TypeScript
import {GameScene, GameSceneInterface} from "./GameScene";
|
|
import {
|
|
Connexion,
|
|
GroupCreatedUpdatedMessageInterface,
|
|
ListMessageUserPositionInterface
|
|
} from "../../Connexion";
|
|
import {SimplePeerInterface, SimplePeer} from "../../WebRtc/SimplePeer";
|
|
|
|
export enum StatusGameManagerEnum {
|
|
IN_PROGRESS = 1,
|
|
CURRENT_USER_CREATED = 2
|
|
}
|
|
|
|
export interface HasMovedEvent {
|
|
direction: string;
|
|
x: number;
|
|
y: number;
|
|
character: string;
|
|
}
|
|
|
|
export interface MapObject {
|
|
key: string,
|
|
url: string
|
|
}
|
|
|
|
export class GameManager {
|
|
status: number;
|
|
private ConnexionInstance: Connexion;
|
|
private currentGameScene: GameSceneInterface;
|
|
private playerName: string;
|
|
SimplePeer : SimplePeerInterface;
|
|
private characterUserSelected: string;
|
|
Maps: Array<MapObject>;
|
|
|
|
constructor() {
|
|
this.status = StatusGameManagerEnum.IN_PROGRESS;
|
|
}
|
|
|
|
connect(name: string, characterUserSelected : string) {
|
|
this.playerName = name;
|
|
this.characterUserSelected = characterUserSelected;
|
|
this.ConnexionInstance = new Connexion(name, this);
|
|
return this.ConnexionInstance.createConnexion(characterUserSelected).then((data : any) => {
|
|
this.SimplePeer = new SimplePeer(this.ConnexionInstance);
|
|
return data;
|
|
}).catch((err) => {
|
|
throw err;
|
|
});
|
|
}
|
|
|
|
loadMaps(){
|
|
return this.ConnexionInstance.loadMaps().then((data) => {
|
|
this.Maps = data.maps;
|
|
return data;
|
|
}).catch((err) => {
|
|
throw err;
|
|
});
|
|
}
|
|
|
|
setCurrentGameScene(gameScene: GameSceneInterface) {
|
|
this.currentGameScene = gameScene;
|
|
}
|
|
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
}
|
|
|
|
getPlayerName(): string {
|
|
return this.playerName;
|
|
}
|
|
|
|
getCharacterSelected(): string {
|
|
return this.characterUserSelected;
|
|
}
|
|
|
|
pushPlayerPosition(event: HasMovedEvent) {
|
|
this.ConnexionInstance.sharePosition(event.x, event.y, event.character, event.direction);
|
|
}
|
|
}
|
|
|
|
export const gameManager = new GameManager();
|