workadventure/front/src/Phaser/Game/GameManager.ts
David Négrier 02e6b50b16 Adding the display of a circle around the group
This PR adds the display of a circle around groups. This is useful to view where you need to go to speak to someone but also to debug.

Note: implementation is suboptimal, relying on a "graphics" object that is known to be slow. In the future, we need to use a circle as a sprite instead.
2020-05-08 00:35:36 +02:00

113 lines
3.0 KiB
TypeScript

import {GameScene} from "./GameScene";
import {ROOM} from "../../Enum/EnvironmentVariable"
import {
Connexion,
ConnexionInterface,
GroupCreatedUpdatedMessageInterface,
ListMessageUserPositionInterface
} from "../../Connexion";
import {SimplePeerInterface, SimplePeer} from "../../WebRtc/SimplePeer";
import {LogincScene} from "../Login/LogincScene";
export enum StatusGameManagerEnum {
IN_PROGRESS = 1,
CURRENT_USER_CREATED = 2
}
export interface HasMovedEvent {
direction: string;
x: number;
y: number;
frame: string;
}
export class GameManager {
status: number;
private ConnexionInstance: Connexion;
private currentGameScene: GameScene;
private playerName: string;
SimplePeer : SimplePeerInterface;
private frameUserSelected: string;
constructor() {
this.status = StatusGameManagerEnum.IN_PROGRESS;
}
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;
}
/**
* 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;
}
getFrameSelected(): string {
return this.frameUserSelected;
}
pushPlayerPosition(event: HasMovedEvent) {
this.ConnexionInstance.sharePosition(event.x, event.y, event.frame, event.direction);
}
}
export const gameManager = new GameManager();