workadventure/front/src/Phaser/Game/GameManager.ts
David Négrier 2448fef53a Adding a notion of instances per mapAdding a notion of instances to room
The URL signature becomes:

https://workadventu.re/_/[instance]/[path_to_map.json]

This allows us to create many instances of the same map (and therefore to create several different worlds for different people)

An exit on a map can target another "instance" by passing the "exitInstance" property.
2020-05-23 17:45:49 +02:00

194 lines
5.4 KiB
TypeScript

import {GameScene} from "./GameScene";
import {
Connexion,
GroupCreatedUpdatedMessageInterface,
ListMessageUserPositionInterface,
MessageUserJoined,
MessageUserMovedInterface,
MessageUserPositionInterface,
Point,
PointInterface
} from "../../Connexion";
import {SimplePeerInterface, SimplePeer} from "../../WebRtc/SimplePeer";
import {AddPlayerInterface} from "./AddPlayerInterface";
import {ReconnectingSceneName} from "../Reconnecting/ReconnectingScene";
export enum StatusGameManagerEnum {
IN_PROGRESS = 1,
CURRENT_USER_CREATED = 2
}
export interface HasMovedEvent {
direction: string;
moving: boolean;
x: number;
y: number;
}
export interface MapObject {
key: string,
url: string
}
export class GameManager {
status: number;
private ConnexionInstance: Connexion;
private currentGameScene: GameScene;
private playerName: string;
SimplePeer : SimplePeerInterface;
private characterUserSelected: string;
constructor() {
this.status = StatusGameManagerEnum.IN_PROGRESS;
}
connect(name: string, characterUserSelected : string) {
this.playerName = name;
this.characterUserSelected = characterUserSelected;
this.ConnexionInstance = new Connexion(this);
return this.ConnexionInstance.createConnexion(name, characterUserSelected).then((data : any) => {
this.SimplePeer = new SimplePeer(this.ConnexionInstance);
return data;
}).catch((err) => {
throw err;
});
}
loadMaps(){
return this.ConnexionInstance.loadMaps().then((data) => {
return data;
}).catch((err) => {
throw err;
});
}
setCurrentGameScene(gameScene: GameScene) {
this.currentGameScene = gameScene;
}
/**
* Permit to create player in started room
*/
createCurrentPlayer(): void {
//Get started room send by the backend
this.currentGameScene.createCurrentPlayer();
this.status = StatusGameManagerEnum.CURRENT_USER_CREATED;
}
joinRoom(sceneKey: string, startX: number, startY: number, direction: string, moving: boolean){
this.ConnexionInstance.joinARoom(sceneKey, startX, startY, direction, moving);
}
onUserJoins(message: MessageUserJoined): void {
let userMessage: AddPlayerInterface = {
userId: message.userId,
character: message.character,
name: message.name,
position: message.position
}
this.currentGameScene.addPlayer(userMessage);
}
onUserMoved(message: MessageUserMovedInterface): void {
this.currentGameScene.updatePlayerPosition(message);
}
onUserLeft(userId: string): void {
this.currentGameScene.removePlayer(userId);
}
/**
* Share position in game
* @param ListMessageUserPosition
* @deprecated
*/
shareUserPosition(ListMessageUserPosition: ListMessageUserPositionInterface): void {
if (this.status === StatusGameManagerEnum.IN_PROGRESS) {
return;
}
try {
this.currentGameScene.shareUserPosition(ListMessageUserPosition.listUsersPosition)
} catch (e) {
console.error(e);
}
}
initUsersPosition(usersPosition: MessageUserPositionInterface[]): void {
// Shall we wait for room to be loaded?
/*if (this.status === StatusGameManagerEnum.IN_PROGRESS) {
return;
}*/
try {
this.currentGameScene.initUsersPosition(usersPosition)
} 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;
}
getPlayerId(): string {
return this.ConnexionInstance.userId;
}
getCharacterSelected(): string {
return this.characterUserSelected;
}
pushPlayerPosition(event: HasMovedEvent) {
this.ConnexionInstance.sharePosition(event.x, event.y, event.direction, event.moving);
}
loadMap(mapUrl: string, scene: Phaser.Scenes.ScenePlugin, instance: string): string {
let sceneKey = GameScene.getMapKeyByUrl(mapUrl);
let gameIndex = scene.getIndex(sceneKey);
let game : Phaser.Scene = null;
if(gameIndex === -1){
game = GameScene.createFromUrl(mapUrl, instance);
scene.add(sceneKey, game, false);
}
return sceneKey;
}
private oldSceneKey : string;
switchToDisconnectedScene(): void {
this.oldSceneKey = this.currentGameScene.scene.key;
this.currentGameScene.scene.start(ReconnectingSceneName);
}
reconnectToGameScene(lastPositionShared: PointInterface) {
this.currentGameScene.scene.start(this.oldSceneKey, { initPosition: lastPositionShared });
}
}
export const gameManager = new GameManager();