workadventure/front/src/Phaser/Game/GameManager.ts

60 lines
1.7 KiB
TypeScript
Raw Normal View History

import {GameScene} from "./GameScene";
2020-09-28 15:02:37 +02:00
import {connectionManager} from "../../Connexion/ConnectionManager";
import {Room} from "../../Connexion/Room";
export interface HasMovedEvent {
direction: string;
moving: boolean;
x: number;
y: number;
}
2020-04-07 20:41:35 +02:00
export class GameManager {
private playerName!: string;
private characterLayers!: string[];
private startRoom!:Room;
public async init(scenePlugin: Phaser.Scenes.ScenePlugin) {
this.startRoom = await connectionManager.initGameConnexion();
2020-10-13 17:08:24 +02:00
await this.loadMap(this.startRoom, scenePlugin);
}
2020-04-07 20:41:35 +02:00
2020-07-28 15:53:44 +02:00
public setPlayerName(name: string): void {
this.playerName = name;
2020-07-28 15:53:44 +02:00
}
public setCharacterLayers(layers: string[]): void {
2020-07-28 15:53:44 +02:00
this.characterLayers = layers;
}
getPlayerName(): string {
return this.playerName;
}
2020-05-03 15:51:16 +02:00
getCharacterSelected(): string[] {
return this.characterLayers;
}
2020-10-13 17:08:24 +02:00
public async loadMap(room: Room, scenePlugin: Phaser.Scenes.ScenePlugin): Promise<void> {
const roomID = room.id;
const mapUrl = await room.getMapUrl();
console.log('Loading map '+roomID+' at url '+mapUrl);
2020-10-13 17:08:24 +02:00
const gameIndex = scenePlugin.getIndex(mapUrl);
if(gameIndex === -1){
2020-10-13 18:44:50 +02:00
const game : Phaser.Scene = GameScene.createFromUrl(room, mapUrl);
console.log('Adding scene '+mapUrl);
scenePlugin.add(mapUrl, game, false);
}
}
2020-10-08 18:51:24 +02:00
public async goToStartingMap(scenePlugin: Phaser.Scenes.ScenePlugin) {
const url = await this.startRoom.getMapUrl();
console.log('Starting scene '+url);
scenePlugin.start(url);
}
}
export const gameManager = new GameManager();