2020-10-12 16:23:07 +02:00
|
|
|
import {GameScene} from "./GameScene";
|
2020-09-28 15:02:37 +02:00
|
|
|
import {connectionManager} from "../../Connexion/ConnectionManager";
|
2020-10-12 16:23:07 +02:00
|
|
|
import {Room} from "../../Connexion/Room";
|
2020-10-12 17:42:37 +02:00
|
|
|
import {FourOFourSceneName} from "../Reconnecting/FourOFourScene";
|
2020-04-10 12:54:05 +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-04-27 15:03:05 +02:00
|
|
|
export class GameManager {
|
2020-08-07 23:39:06 +02:00
|
|
|
private playerName!: string;
|
|
|
|
private characterLayers!: string[];
|
2020-10-12 16:23:07 +02:00
|
|
|
private startRoom!:Room;
|
|
|
|
private sceneManager!: Phaser.Scenes.SceneManager;
|
2020-10-12 17:42:37 +02:00
|
|
|
|
2020-10-12 16:23:07 +02:00
|
|
|
public async init(sceneManager: Phaser.Scenes.SceneManager) {
|
|
|
|
this.sceneManager = sceneManager;
|
2020-10-12 17:42:37 +02:00
|
|
|
try {
|
|
|
|
this.startRoom = await connectionManager.initGameConnexion();
|
|
|
|
} catch (e) {
|
|
|
|
this.sceneManager.start(FourOFourSceneName, {
|
|
|
|
url: window.location.pathname.toString()
|
|
|
|
});
|
|
|
|
}
|
2020-10-12 16:23:07 +02:00
|
|
|
this.loadMap(this.startRoom.url, this.startRoom.ID);
|
|
|
|
}
|
2020-04-07 20:41:35 +02:00
|
|
|
|
2020-07-28 15:53:44 +02:00
|
|
|
public setPlayerName(name: string): void {
|
2020-05-03 15:29:40 +02:00
|
|
|
this.playerName = name;
|
2020-07-28 15:53:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public setCharacterUserSelected(characterUserSelected : string): void {
|
|
|
|
this.characterLayers = [characterUserSelected];
|
|
|
|
}
|
|
|
|
|
|
|
|
public setCharacterLayers(layers: string[]) {
|
|
|
|
this.characterLayers = layers;
|
2020-04-10 12:54:05 +02:00
|
|
|
}
|
|
|
|
|
2020-05-03 15:29:40 +02:00
|
|
|
getPlayerName(): string {
|
|
|
|
return this.playerName;
|
|
|
|
}
|
2020-05-03 15:51:16 +02:00
|
|
|
|
2020-07-28 17:43:33 +02:00
|
|
|
getCharacterSelected(): string[] {
|
|
|
|
return this.characterLayers;
|
2020-05-06 01:50:01 +02:00
|
|
|
}
|
2020-10-12 17:42:37 +02:00
|
|
|
|
|
|
|
|
2020-10-12 16:23:07 +02:00
|
|
|
public loadMap(mapUrl: string, roomID: string): void {
|
|
|
|
console.log('Loading map '+roomID+' at url '+mapUrl);
|
|
|
|
const gameIndex = this.sceneManager.getIndex(roomID);
|
2020-05-11 23:26:40 +02:00
|
|
|
if(gameIndex === -1){
|
2020-10-12 16:23:07 +02:00
|
|
|
const game : Phaser.Scene = GameScene.createFromUrl(mapUrl, roomID);
|
|
|
|
this.sceneManager.add(roomID, game, false);
|
2020-05-11 23:26:40 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-08 18:51:24 +02:00
|
|
|
|
|
|
|
public getMapKeyByUrl(mapUrlStart: string) : string {
|
|
|
|
// FIXME: the key should be computed from the full URL of the map.
|
|
|
|
const startPos = mapUrlStart.indexOf('://')+3;
|
|
|
|
const endPos = mapUrlStart.indexOf(".json");
|
|
|
|
return mapUrlStart.substring(startPos, endPos);
|
|
|
|
}
|
2020-10-12 17:42:37 +02:00
|
|
|
|
2020-10-12 16:23:07 +02:00
|
|
|
public async goToStartingMap() {
|
|
|
|
this.sceneManager.start(this.startRoom.ID, {startLayerName: 'global'});
|
|
|
|
}
|
2020-04-26 17:54:56 +02:00
|
|
|
}
|
|
|
|
|
2020-05-03 15:29:40 +02:00
|
|
|
export const gameManager = new GameManager();
|