rewrote the authorisation flow: give more responsability to gameManager and less to gameScene
This commit is contained in:
parent
032facb75f
commit
02c193a262
14 changed files with 244 additions and 170 deletions
|
@ -1,10 +1,6 @@
|
|||
import {GameScene, GameSceneInitInterface} from "./GameScene";
|
||||
import {
|
||||
StartMapInterface
|
||||
} from "../../Connexion/ConnexionModels";
|
||||
import Axios from "axios";
|
||||
import {API_URL} from "../../Enum/EnvironmentVariable";
|
||||
import {GameScene} from "./GameScene";
|
||||
import {connectionManager} from "../../Connexion/ConnectionManager";
|
||||
import {Room} from "../../Connexion/Room";
|
||||
|
||||
export interface HasMovedEvent {
|
||||
direction: string;
|
||||
|
@ -13,14 +9,17 @@ export interface HasMovedEvent {
|
|||
y: number;
|
||||
}
|
||||
|
||||
export interface loadMapResponseInterface {
|
||||
key: string,
|
||||
startLayerName: string;
|
||||
}
|
||||
|
||||
export class GameManager {
|
||||
private playerName!: string;
|
||||
private characterLayers!: string[];
|
||||
private startRoom!:Room;
|
||||
private sceneManager!: Phaser.Scenes.SceneManager;
|
||||
|
||||
public async init(sceneManager: Phaser.Scenes.SceneManager) {
|
||||
this.sceneManager = sceneManager;
|
||||
this.startRoom = await connectionManager.initGameConnexion();
|
||||
this.loadMap(this.startRoom.url, this.startRoom.ID);
|
||||
}
|
||||
|
||||
public setPlayerName(name: string): void {
|
||||
this.playerName = name;
|
||||
|
@ -41,55 +40,15 @@ export class GameManager {
|
|||
getCharacterSelected(): string[] {
|
||||
return this.characterLayers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the map URL and the instance from the current URL
|
||||
*/
|
||||
private findMapUrl(): [string, string]|null {
|
||||
const path = window.location.pathname;
|
||||
if (!path.startsWith('/_/')) {
|
||||
return null;
|
||||
}
|
||||
const instanceAndMap = path.substr(3);
|
||||
const firstSlash = instanceAndMap.indexOf('/');
|
||||
if (firstSlash === -1) {
|
||||
return null;
|
||||
}
|
||||
const instance = instanceAndMap.substr(0, firstSlash);
|
||||
return [window.location.protocol+'//'+instanceAndMap.substr(firstSlash+1), instance];
|
||||
}
|
||||
|
||||
public loadStartingMap(scene: Phaser.Scenes.ScenePlugin): Promise<loadMapResponseInterface> {
|
||||
// Do we have a start URL in the address bar? If so, let's redirect to this address
|
||||
const instanceAndMapUrl = this.findMapUrl();
|
||||
if (instanceAndMapUrl !== null) {
|
||||
const [mapUrl, instance] = instanceAndMapUrl;
|
||||
const key = gameManager.loadMap(mapUrl, scene, instance);
|
||||
const startLayerName = window.location.hash ? window.location.hash.substr(1) : '';
|
||||
return Promise.resolve({key, startLayerName});
|
||||
|
||||
} else {
|
||||
// If we do not have a map address in the URL, let's ask the server for a start map.
|
||||
return connectionManager.getMapUrlStart().then((mapUrlStart: string) => {
|
||||
const key = gameManager.loadMap(window.location.protocol + "//" + mapUrlStart, scene, 'global');
|
||||
return {key, startLayerName: ''}
|
||||
}).catch((err) => {
|
||||
console.error(err);
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public loadMap(mapUrl: string, scene: Phaser.Scenes.ScenePlugin, instance: string): string {
|
||||
const sceneKey = this.getMapKeyByUrl(mapUrl);
|
||||
|
||||
const gameIndex = scene.getIndex(sceneKey);
|
||||
|
||||
public loadMap(mapUrl: string, roomID: string): void {
|
||||
console.log('Loading map '+roomID+' at url '+mapUrl);
|
||||
const gameIndex = this.sceneManager.getIndex(roomID);
|
||||
if(gameIndex === -1){
|
||||
const game : Phaser.Scene = GameScene.createFromUrl(mapUrl, instance);
|
||||
scene.add(sceneKey, game, false);
|
||||
const game : Phaser.Scene = GameScene.createFromUrl(mapUrl, roomID);
|
||||
this.sceneManager.add(roomID, game, false);
|
||||
}
|
||||
return sceneKey;
|
||||
}
|
||||
|
||||
public getMapKeyByUrl(mapUrlStart: string) : string {
|
||||
|
@ -98,6 +57,10 @@ export class GameManager {
|
|||
const endPos = mapUrlStart.indexOf(".json");
|
||||
return mapUrlStart.substring(startPos, endPos);
|
||||
}
|
||||
|
||||
public async goToStartingMap() {
|
||||
this.sceneManager.start(this.startRoom.ID, {startLayerName: 'global'});
|
||||
}
|
||||
}
|
||||
|
||||
export const gameManager = new GameManager();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue