2020-05-19 19:11:12 +02:00
|
|
|
import {GameScene} from "./GameScene";
|
2020-05-08 00:35:36 +02:00
|
|
|
import {
|
2020-05-24 23:14:12 +02:00
|
|
|
Connection,
|
2020-05-08 00:35:36 +02:00
|
|
|
GroupCreatedUpdatedMessageInterface,
|
2020-05-23 15:43:26 +02:00
|
|
|
ListMessageUserPositionInterface,
|
|
|
|
MessageUserJoined,
|
|
|
|
MessageUserMovedInterface,
|
|
|
|
MessageUserPositionInterface,
|
|
|
|
Point,
|
|
|
|
PointInterface
|
2020-05-24 23:14:12 +02:00
|
|
|
} from "../../Connection";
|
2020-06-03 22:32:43 +02:00
|
|
|
import {SimplePeer} from "../../WebRtc/SimplePeer";
|
2020-05-19 19:11:12 +02:00
|
|
|
import {AddPlayerInterface} from "./AddPlayerInterface";
|
2020-06-10 15:43:22 +02:00
|
|
|
import {ReconnectingScene, ReconnectingSceneName} from "../Reconnecting/ReconnectingScene";
|
|
|
|
import ScenePlugin = Phaser.Scenes.ScenePlugin;
|
|
|
|
import {Scene} from "phaser";
|
2020-04-10 12:54:05 +02:00
|
|
|
|
2020-05-24 23:33:56 +02:00
|
|
|
/*export enum StatusGameManagerEnum {
|
2020-04-10 12:54:05 +02:00
|
|
|
IN_PROGRESS = 1,
|
|
|
|
CURRENT_USER_CREATED = 2
|
2020-05-24 23:33:56 +02:00
|
|
|
}*/
|
2020-04-07 20:41:35 +02:00
|
|
|
|
2020-05-02 16:54:52 +02:00
|
|
|
export interface HasMovedEvent {
|
|
|
|
direction: string;
|
2020-05-22 22:59:43 +02:00
|
|
|
moving: boolean;
|
2020-05-02 16:54:52 +02:00
|
|
|
x: number;
|
|
|
|
y: number;
|
|
|
|
}
|
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-05-24 23:33:56 +02:00
|
|
|
//status: number;
|
2020-05-24 23:14:12 +02:00
|
|
|
private ConnectionInstance: Connection;
|
2020-06-10 15:43:22 +02:00
|
|
|
private currentGameScene: GameScene|null;
|
2020-05-03 15:29:40 +02:00
|
|
|
private playerName: string;
|
2020-06-03 22:32:43 +02:00
|
|
|
SimplePeer : SimplePeer;
|
2020-05-08 15:18:22 +02:00
|
|
|
private characterUserSelected: string;
|
2020-04-07 20:41:35 +02:00
|
|
|
|
|
|
|
constructor() {
|
2020-05-24 23:33:56 +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-24 23:14:12 +02:00
|
|
|
this.ConnectionInstance = new Connection(this);
|
|
|
|
return this.ConnectionInstance.createConnection(name, characterUserSelected).then((data : any) => {
|
|
|
|
this.SimplePeer = new SimplePeer(this.ConnectionInstance);
|
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-24 22:53:10 +02:00
|
|
|
loadStartMap(){
|
2020-05-24 23:14:12 +02:00
|
|
|
return this.ConnectionInstance.loadStartMap().then((data) => {
|
2020-05-09 21:28:50 +02:00
|
|
|
return data;
|
2020-05-09 19:41:21 +02:00
|
|
|
}).catch((err) => {
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-19 19:11:12 +02:00
|
|
|
setCurrentGameScene(gameScene: GameScene) {
|
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
|
|
|
|
*/
|
2020-05-24 23:33:56 +02:00
|
|
|
/*createCurrentPlayer(): void {
|
2020-04-13 13:42:21 +02:00
|
|
|
//Get started room send by the backend
|
2020-05-14 23:19:48 +02:00
|
|
|
this.currentGameScene.createCurrentPlayer();
|
2020-05-24 23:33:56 +02:00
|
|
|
//this.status = StatusGameManagerEnum.CURRENT_USER_CREATED;
|
|
|
|
}*/
|
2020-04-10 12:54:05 +02:00
|
|
|
|
2020-05-22 22:59:43 +02:00
|
|
|
joinRoom(sceneKey: string, startX: number, startY: number, direction: string, moving: boolean){
|
2020-05-24 23:14:12 +02:00
|
|
|
this.ConnectionInstance.joinARoom(sceneKey, startX, startY, direction, moving);
|
2020-05-10 13:58:32 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 19:11:12 +02:00
|
|
|
onUserJoins(message: MessageUserJoined): void {
|
|
|
|
let userMessage: AddPlayerInterface = {
|
|
|
|
userId: message.userId,
|
|
|
|
character: message.character,
|
|
|
|
name: message.name,
|
2020-05-22 22:59:43 +02:00
|
|
|
position: message.position
|
2020-05-19 19:11:12 +02:00
|
|
|
}
|
2020-06-10 15:43:22 +02:00
|
|
|
this.getCurrentGameScene().addPlayer(userMessage);
|
2020-05-19 19:11:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onUserMoved(message: MessageUserMovedInterface): void {
|
2020-06-10 15:43:22 +02:00
|
|
|
this.getCurrentGameScene().updatePlayerPosition(message);
|
2020-05-19 19:11:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onUserLeft(userId: string): void {
|
2020-06-10 15:43:22 +02:00
|
|
|
this.getCurrentGameScene().removePlayer(userId);
|
2020-05-19 19:11:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
initUsersPosition(usersPosition: MessageUserPositionInterface[]): void {
|
|
|
|
// Shall we wait for room to be loaded?
|
|
|
|
/*if (this.status === StatusGameManagerEnum.IN_PROGRESS) {
|
|
|
|
return;
|
|
|
|
}*/
|
|
|
|
try {
|
2020-06-10 15:43:22 +02:00
|
|
|
this.getCurrentGameScene().initUsersPosition(usersPosition)
|
2020-05-19 19:11:12 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 00:35:36 +02:00
|
|
|
/**
|
|
|
|
* Share group position in game
|
|
|
|
*/
|
|
|
|
shareGroupPosition(groupPositionMessage: GroupCreatedUpdatedMessageInterface): void {
|
2020-05-24 23:33:56 +02:00
|
|
|
/*if (this.status === StatusGameManagerEnum.IN_PROGRESS) {
|
2020-05-08 00:35:36 +02:00
|
|
|
return;
|
2020-05-24 23:33:56 +02:00
|
|
|
}*/
|
2020-05-08 00:35:36 +02:00
|
|
|
try {
|
2020-06-10 15:43:22 +02:00
|
|
|
this.getCurrentGameScene().shareGroupPosition(groupPositionMessage)
|
2020-05-08 00:35:36 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteGroup(groupId: string): void {
|
2020-05-24 23:33:56 +02:00
|
|
|
/*if (this.status === StatusGameManagerEnum.IN_PROGRESS) {
|
2020-05-08 00:35:36 +02:00
|
|
|
return;
|
2020-05-24 23:33:56 +02:00
|
|
|
}*/
|
2020-05-08 00:35:36 +02:00
|
|
|
try {
|
2020-06-10 15:43:22 +02:00
|
|
|
this.getCurrentGameScene().deleteGroup(groupId)
|
2020-05-08 00:35:36 +02:00
|
|
|
} 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-06-04 18:54:34 +02:00
|
|
|
getPlayerId(): string|null {
|
2020-05-24 23:14:12 +02:00
|
|
|
return this.ConnectionInstance.userId;
|
2020-05-14 23:19:48 +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-24 23:14:12 +02:00
|
|
|
this.ConnectionInstance.sharePosition(event.x, event.y, event.direction, event.moving);
|
2020-05-02 16:54:52 +02:00
|
|
|
}
|
2020-05-11 23:26:40 +02:00
|
|
|
|
2020-05-23 17:27:49 +02:00
|
|
|
loadMap(mapUrl: string, scene: Phaser.Scenes.ScenePlugin, instance: string): string {
|
|
|
|
let sceneKey = GameScene.getMapKeyByUrl(mapUrl);
|
2020-05-11 23:26:40 +02:00
|
|
|
|
|
|
|
let gameIndex = scene.getIndex(sceneKey);
|
|
|
|
if(gameIndex === -1){
|
2020-06-04 18:11:07 +02:00
|
|
|
let game : Phaser.Scene = GameScene.createFromUrl(mapUrl, instance);
|
2020-05-11 23:26:40 +02:00
|
|
|
scene.add(sceneKey, game, false);
|
|
|
|
}
|
|
|
|
return sceneKey;
|
|
|
|
}
|
2020-05-23 15:43:26 +02:00
|
|
|
|
|
|
|
private oldSceneKey : string;
|
2020-06-10 15:43:22 +02:00
|
|
|
private oldMapUrlFile : string;
|
|
|
|
private oldInstance : string;
|
|
|
|
private scenePlugin: ScenePlugin;
|
2020-06-11 09:37:33 +02:00
|
|
|
private reconnectScene: Scene|null;
|
2020-05-23 15:43:26 +02:00
|
|
|
switchToDisconnectedScene(): void {
|
2020-06-10 15:43:22 +02:00
|
|
|
if (this.currentGameScene === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log('Switching to disconnected scene');
|
2020-05-23 15:43:26 +02:00
|
|
|
this.oldSceneKey = this.currentGameScene.scene.key;
|
2020-06-10 15:43:22 +02:00
|
|
|
this.oldMapUrlFile = this.currentGameScene.MapUrlFile;
|
|
|
|
this.oldInstance = this.currentGameScene.instance;
|
2020-05-23 15:43:26 +02:00
|
|
|
this.currentGameScene.scene.start(ReconnectingSceneName);
|
2020-06-10 15:43:22 +02:00
|
|
|
this.reconnectScene = this.currentGameScene.scene.get(ReconnectingSceneName);
|
|
|
|
// Let's completely delete an purge the disconnected scene. We will start again from 0.
|
|
|
|
this.currentGameScene.scene.remove(this.oldSceneKey);
|
|
|
|
this.scenePlugin = this.currentGameScene.scene;
|
|
|
|
this.currentGameScene = null;
|
2020-05-23 15:43:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
reconnectToGameScene(lastPositionShared: PointInterface) {
|
2020-06-11 09:37:33 +02:00
|
|
|
if (this.reconnectScene === null && this.currentGameScene) {
|
|
|
|
// In case we are asked to reconnect even if switchToDisconnectedScene was not triggered (can happen when a laptop goes to sleep)
|
|
|
|
this.switchToDisconnectedScene();
|
|
|
|
}
|
2020-06-10 15:43:22 +02:00
|
|
|
const game : Phaser.Scene = GameScene.createFromUrl(this.oldMapUrlFile, this.oldInstance);
|
2020-06-11 09:37:33 +02:00
|
|
|
this.reconnectScene?.scene.add(this.oldSceneKey, game, true, { initPosition: lastPositionShared });
|
2020-06-10 15:43:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private getCurrentGameScene(): GameScene {
|
|
|
|
if (this.currentGameScene === null) {
|
|
|
|
throw new Error('No current game scene enabled');
|
|
|
|
}
|
|
|
|
return this.currentGameScene;
|
2020-05-23 15:43:26 +02:00
|
|
|
}
|
2020-04-26 17:54:56 +02:00
|
|
|
}
|
|
|
|
|
2020-05-03 15:29:40 +02:00
|
|
|
export const gameManager = new GameManager();
|