From: kharhamel Date: Mon, 27 Apr 2020 13:03:05 +0000 (+0200) Subject: first step in simplification: remove the concept of room in the front end X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=ee4a59578a92549990bee31bff47340792db6bba;p=libreadventure.git first step in simplification: remove the concept of room in the front end --- diff --git a/front/src/Connexion.ts b/front/src/Connexion.ts index 0d9f998..d3c4b87 100644 --- a/front/src/Connexion.ts +++ b/front/src/Connexion.ts @@ -1,8 +1,8 @@ -import {GameManagerInterface} from "./Phaser/Game/GameManager"; +import {GameManager} from "./Phaser/Game/GameManager"; const SocketIo = require('socket.io-client'); import Axios from "axios"; -import {API_URL} from "./Enum/EnvironmentVariable"; +import {API_URL, ROOM} from "./Enum/EnvironmentVariable"; class Message { userId: string; @@ -107,7 +107,7 @@ export interface ConnexionInterface { startedRoom : string; createConnexion() : Promise; joinARoom(roomId : string) : void; - sharePosition(roomId : string, x : number, y : number, direction : string) : void; + sharePosition(x : number, y : number, direction : string) : void; positionOfAllUser() : void; } export class Connexion implements ConnexionInterface{ @@ -117,9 +117,9 @@ export class Connexion implements ConnexionInterface{ userId: string; startedRoom : string; - GameManager: GameManagerInterface; + GameManager: GameManager; - constructor(email : string, GameManager: GameManagerInterface) { + constructor(email : string, GameManager: GameManager) { this.email = email; this.GameManager = GameManager; } @@ -141,7 +141,7 @@ export class Connexion implements ConnexionInterface{ this.joinARoom(this.startedRoom); //share your first position - this.sharePosition(this.startedRoom, 0, 0); + this.sharePosition(0, 0); this.positionOfAllUser(); @@ -166,16 +166,15 @@ export class Connexion implements ConnexionInterface{ /** * - * @param roomId * @param x * @param y * @param direction */ - sharePosition(roomId : string, x : number, y : number, direction : string = "none") : void{ + sharePosition(x : number, y : number, direction : string = "none") : void{ if(!this.socket){ return; } - let messageUserPosition = new MessageUserPosition(this.userId, roomId, new Point(x, y, direction)); + let messageUserPosition = new MessageUserPosition(this.userId, ROOM[0], new Point(x, y, direction)); this.socket.emit('user-position', messageUserPosition.toString()); } diff --git a/front/src/Phaser/Game/GameManager.ts b/front/src/Phaser/Game/GameManager.ts index e18bbdd..916840f 100644 --- a/front/src/Phaser/Game/GameManager.ts +++ b/front/src/Phaser/Game/GameManager.ts @@ -1,4 +1,4 @@ -import {GameSceneInterface, GameScene} from "./GameScene"; +import {GameScene} from "./GameScene"; import {ROOM} from "../../Enum/EnvironmentVariable" import {Connexion, ConnexionInterface, ListMessageUserPositionInterface} from "../../Connexion"; @@ -9,46 +9,32 @@ export enum StatusGameManagerEnum { export let ConnexionInstance : ConnexionInterface; -export interface GameManagerInterface { - GameScenes: Array; - status : number; - createCurrentPlayer() : void; - shareUserPosition(ListMessageUserPosition : ListMessageUserPositionInterface): void; -} -export class GameManager implements GameManagerInterface { - GameScenes: Array = []; +export class GameManager { status: number; private ConnexionInstance: Connexion; + private currentGameScene: GameScene; constructor() { this.status = StatusGameManagerEnum.IN_PROGRESS; - this.configureGame(); } connect(email:string) { this.ConnexionInstance = new Connexion(email, this); + ConnexionInstance = this.ConnexionInstance; return this.ConnexionInstance.createConnexion() } - - /** - * permit to config rooms - */ - configureGame() { - ROOM.forEach((roomId) => { - let newGame = new GameScene(roomId, this); - this.GameScenes.push((newGame as GameSceneInterface)); - }); + + setCurrentGameScene(gameScene: GameScene) { + this.currentGameScene = gameScene; } + /** * Permit to create player in started room - * @param RoomId - * @param UserId */ createCurrentPlayer(): void { //Get started room send by the backend - let game: GameSceneInterface = this.GameScenes.find((Game: GameSceneInterface) => Game.RoomId === this.ConnexionInstance.startedRoom); - game.createCurrentPlayer(this.ConnexionInstance.userId); + this.currentGameScene.createCurrentPlayer(this.ConnexionInstance.userId); this.status = StatusGameManagerEnum.CURRENT_USER_CREATED; } @@ -61,11 +47,7 @@ export class GameManager implements GameManagerInterface { return; } try { - let Game: GameSceneInterface = this.GameScenes.find((Game: GameSceneInterface) => Game.RoomId === ListMessageUserPosition.roomId); - if (!Game) { - return; - } - Game.shareUserPosition(ListMessageUserPosition.listUsersPosition) + this.currentGameScene.shareUserPosition(ListMessageUserPosition.listUsersPosition) } catch (e) { console.error(e); } diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 494508e..e36fe80 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -1,7 +1,7 @@ -import {GameManagerInterface, StatusGameManagerEnum} from "./GameManager"; +import {GameManager, gameManager, StatusGameManagerEnum} from "./GameManager"; import {MessageUserPositionInterface} from "../../Connexion"; import {CurrentGamerInterface, GamerInterface, Player} from "../Player/Player"; -import {DEBUG_MODE, RESOLUTION, ZOOM_LEVEL} from "../../Enum/EnvironmentVariable"; +import {DEBUG_MODE, RESOLUTION, ROOM, ZOOM_LEVEL} from "../../Enum/EnvironmentVariable"; import Tile = Phaser.Tilemaps.Tile; import {ITiledMap, ITiledTileSet} from "../Map/ITiledMap"; import {cypressAsserter} from "../../Cypress/CypressAsserter"; @@ -13,14 +13,12 @@ export enum Textures { } export interface GameSceneInterface extends Phaser.Scene { - RoomId : string; Map: Phaser.Tilemaps.Tilemap; createCurrentPlayer(UserId : string) : void; shareUserPosition(UsersPosition : Array): void; } export class GameScene extends Phaser.Scene implements GameSceneInterface{ - GameManager : GameManagerInterface; - RoomId : string; + GameManager : GameManager; Terrains : Array; CurrentPlayer: CurrentGamerInterface; MapPlayers : Phaser.Physics.Arcade.Group; @@ -32,17 +30,17 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface{ startY = (window.innerHeight / 2) / RESOLUTION; - constructor(RoomId : string, GameManager : GameManagerInterface) { + constructor() { super({ key: "GameScene" }); - this.RoomId = RoomId; - this.GameManager = GameManager; + this.GameManager = gameManager; this.Terrains = []; } //hook preload scene preload(): void { + this.GameManager.setCurrentGameScene(this); cypressAsserter.preloadStarted(); let mapUrl = 'maps/map.json'; this.load.on('filecomplete-tilemapJSON-'+Textures.Map, (key: string, type: string, data: any) => { diff --git a/front/src/Phaser/Player/Player.ts b/front/src/Phaser/Player/Player.ts index e99fa42..a779872 100644 --- a/front/src/Phaser/Player/Player.ts +++ b/front/src/Phaser/Player/Player.ts @@ -95,7 +95,7 @@ export class Player extends PlayableCaracter implements CurrentGamerInterface, G private sharePosition(direction: string) { if (ConnexionInstance) { - ConnexionInstance.sharePosition((this.scene as GameSceneInterface).RoomId, this.x, this.y, direction); + ConnexionInstance.sharePosition(this.x, this.y, direction); } } diff --git a/front/src/index.ts b/front/src/index.ts index c55a5d0..d4f048c 100644 --- a/front/src/index.ts +++ b/front/src/index.ts @@ -1,16 +1,16 @@ import 'phaser'; import GameConfig = Phaser.Types.Core.GameConfig; -import {gameManager, GameManager} from "./Phaser/Game/GameManager"; import {DEBUG_MODE, RESOLUTION} from "./Enum/EnvironmentVariable"; import {cypressAsserter} from "./Cypress/CypressAsserter"; import {LogincScene} from "./Phaser/Login/LogincScene"; +import {GameScene} from "./Phaser/Game/GameScene"; const config: GameConfig = { title: "Office game", width: window.innerWidth / RESOLUTION, height: window.innerHeight / RESOLUTION, parent: "game", - scene: [LogincScene, ...gameManager.GameScenes as any], + scene: [LogincScene, GameScene], zoom: RESOLUTION, physics: { default: "arcade",