2020-10-08 18:51:24 +02:00
|
|
|
import {GameScene, GameSceneInitInterface} from "./GameScene";
|
2020-05-08 00:35:36 +02:00
|
|
|
import {
|
2020-06-22 18:42:54 +02:00
|
|
|
StartMapInterface
|
2020-09-25 18:29:22 +02:00
|
|
|
} from "../../Connexion/ConnexionModels";
|
2020-06-22 11:58:07 +02:00
|
|
|
import Axios from "axios";
|
|
|
|
import {API_URL} from "../../Enum/EnvironmentVariable";
|
2020-09-28 15:02:37 +02:00
|
|
|
import {connectionManager} from "../../Connexion/ConnectionManager";
|
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-10-08 18:51:24 +02:00
|
|
|
export interface loadMapResponseInterface {
|
|
|
|
key: string,
|
|
|
|
startLayerName: string;
|
|
|
|
}
|
|
|
|
|
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-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-08 18:51:24 +02:00
|
|
|
/**
|
|
|
|
* 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);
|
2020-05-11 23:26:40 +02:00
|
|
|
|
2020-06-09 23:13:26 +02:00
|
|
|
const gameIndex = scene.getIndex(sceneKey);
|
2020-05-11 23:26:40 +02:00
|
|
|
if(gameIndex === -1){
|
2020-06-09 23:13:26 +02:00
|
|
|
const game : Phaser.Scene = GameScene.createFromUrl(mapUrl, instance);
|
2020-05-11 23:26:40 +02:00
|
|
|
scene.add(sceneKey, game, false);
|
|
|
|
}
|
|
|
|
return sceneKey;
|
|
|
|
}
|
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-04-26 17:54:56 +02:00
|
|
|
}
|
|
|
|
|
2020-05-03 15:29:40 +02:00
|
|
|
export const gameManager = new GameManager();
|