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();
|
||||
|
|
|
@ -418,15 +418,7 @@ export class GameScene extends ResizableScene implements CenterListener {
|
|||
context.strokeStyle = '#ffffff';
|
||||
context.stroke();
|
||||
this.circleTexture.refresh();
|
||||
|
||||
// Let's alter browser history
|
||||
const url = new URL(this.MapUrlFile);
|
||||
let path = '/_/'+this.instance+'/'+url.host+url.pathname;
|
||||
if (this.startLayerName) {
|
||||
path += '#'+this.startLayerName;
|
||||
}
|
||||
window.history.pushState({}, 'WorkAdventure', path);
|
||||
|
||||
|
||||
// Let's pause the scene if the connection is not established yet
|
||||
if (this.connection === undefined) {
|
||||
// Let's wait 0.5 seconds before printing the "connecting" screen to avoid blinking
|
||||
|
@ -686,6 +678,7 @@ export class GameScene extends ResizableScene implements CenterListener {
|
|||
* @param tileWidth
|
||||
* @param tileHeight
|
||||
*/
|
||||
//todo: push that into the gameManager
|
||||
private loadNextGame(layer: ITiledMapLayer, mapWidth: number, tileWidth: number, tileHeight: number){
|
||||
const exitSceneUrl = this.getExitSceneUrl(layer);
|
||||
if (exitSceneUrl === undefined) {
|
||||
|
@ -698,7 +691,8 @@ export class GameScene extends ResizableScene implements CenterListener {
|
|||
|
||||
// TODO: eventually compute a relative URL
|
||||
const absoluteExitSceneUrl = new URL(exitSceneUrl, this.MapUrlFile).href;
|
||||
const exitSceneKey = gameManager.loadMap(absoluteExitSceneUrl, this.scene, instance);
|
||||
gameManager.loadMap(absoluteExitSceneUrl, instance);
|
||||
const exitSceneKey = instance;
|
||||
|
||||
const tiles : number[] = layer.data as number[];
|
||||
for (let key=0; key < tiles.length; key++) {
|
||||
|
@ -785,14 +779,6 @@ export class GameScene extends ResizableScene implements CenterListener {
|
|||
});
|
||||
}
|
||||
|
||||
createCollisionObject(){
|
||||
/*this.Objects.forEach((Object : Phaser.Physics.Arcade.Sprite) => {
|
||||
this.physics.add.collider(this.CurrentPlayer, Object, (object1, object2) => {
|
||||
this.CurrentPlayer.say("Collision with object : " + (object2 as Phaser.Physics.Arcade.Sprite).texture.key)
|
||||
});
|
||||
})*/
|
||||
}
|
||||
|
||||
createCurrentPlayer(){
|
||||
//initialise player
|
||||
//TODO create animation moving between exit and start
|
||||
|
@ -809,7 +795,6 @@ export class GameScene extends ResizableScene implements CenterListener {
|
|||
|
||||
//create collision
|
||||
this.createCollisionWithPlayer();
|
||||
this.createCollisionObject();
|
||||
}
|
||||
|
||||
pushPlayerPosition(event: HasMovedEvent) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue