Refactor to include connexion
authorgparant <g.parant@thecodingmachine.com>
Tue, 7 Apr 2020 18:41:35 +0000 (20:41 +0200)
committergparant <g.parant@thecodingmachine.com>
Tue, 7 Apr 2020 18:41:35 +0000 (20:41 +0200)
front/src/Connexion.ts
front/src/Enum/EnvironmentVariable.ts
front/src/Phaser/Game/CameraManager.ts [moved from front/src/Phaser/CameraManager.ts with 96% similarity]
front/src/Phaser/Game/GameManager.ts [new file with mode: 0644]
front/src/Phaser/Game/GameScene.ts [moved from front/src/Phaser/GameScene.ts with 59% similarity]
front/src/Phaser/Game/MapManager.ts [moved from front/src/Phaser/MapManager.ts with 91% similarity]
front/src/Phaser/Player/Player.ts [moved from front/src/Phaser/Player.ts with 83% similarity]
front/src/index.ts

index 4f094152a95127b333b73b1bb95e538eefd49231..b27be366eff6404563c31ff8142b338c5648a0f7 100644 (file)
@@ -1,3 +1,5 @@
+import {GameManagerInterface} from "./Phaser/Game/GameManager";
+
 const SocketIo = require('socket.io-client');
 import Axios from "axios";
 import {API_URL} from "./Enum/EnvironmentVariable";
@@ -70,8 +72,11 @@ export class Connexion {
     email : string;
     startedRoom : string;
 
-    constructor(email : string) {
+    GameManager: GameManagerInterface;
+
+    constructor(email : string, GameManager: GameManagerInterface) {
         this.email = email;
+        this.GameManager = GameManager;
         Axios.post(`${API_URL}/login`, {email: email})
             .then((res) => {
                 this.token = res.data.token;
@@ -87,9 +92,8 @@ export class Connexion {
                 this.joinARoom(this.startedRoom);
 
                 //share your first position
-                this.sharePosition(0, 0);
+                this.sharePosition(this.startedRoom, 0, 0);
 
-                //create listen event to get all data user shared by the back
                 this.positionOfAllUser();
 
                 this.errorMessage();
@@ -114,8 +118,8 @@ export class Connexion {
      * @param x
      * @param y
      */
-    sharePosition(x : number, y : number){
-        let messageUserPosition = new MessageUserPosition(this.email, this.startedRoom, new Point(x, y));
+    sharePosition(roomId : string, x : number, y : number){
+        let messageUserPosition = new MessageUserPosition(this.email, roomId, new Point(x, y));
         this.socket.emit('user-position', messageUserPosition.toString());
     }
 
@@ -135,8 +139,10 @@ export class Connexion {
      **/
     positionOfAllUser(){
         this.socket.on("user-position", (message : string) => {
-            //TODO show all user in map
-            console.info("user-position", message);
+            let data = JSON.parse(message);
+            data.forEach((UserPositions : any) => {
+                this.GameManager.sharedUserPosition(UserPositions);
+            });
         });
     }
 
index 71934e6c887ef4998892900225a5fe3dd871acc6..3cf52b85dc5e19d378ff6bba2377f303dda7cfb8 100644 (file)
@@ -1,7 +1,9 @@
 const API_URL = process.env.API_URL || "http://api.workadventure.localhost";
+const ROOM = [process.env.ROOM || "THECODINGMACHINE"];
 const RESOLUTION = 2;
 
 export {
     API_URL,
-    RESOLUTION
+    RESOLUTION,
+    ROOM
 }
\ No newline at end of file
similarity index 96%
rename from front/src/Phaser/CameraManager.ts
rename to front/src/Phaser/Game/CameraManager.ts
index 528e06ca0a98a33859209652bf529b01baff2d78..829bedf48a990017846a65068b85524820977c58 100644 (file)
@@ -1,5 +1,5 @@
-import {RESOLUTION} from "../Enum/EnvironmentVariable";
-import {Player} from "./Player";
+import {RESOLUTION} from "../../Enum/EnvironmentVariable";
+import {Player} from "../Player/Player";
 import {MapManagerInterface} from "./MapManager";
 
 export interface CameraManagerInterface {
diff --git a/front/src/Phaser/Game/GameManager.ts b/front/src/Phaser/Game/GameManager.ts
new file mode 100644 (file)
index 0000000..ff9f5b5
--- /dev/null
@@ -0,0 +1,31 @@
+import {GameSceneInterface, GameScene} from "./GameScene";
+import {ROOM} from "../../Enum/EnvironmentVariable"
+import {Connexion} from "../../Connexion";
+
+export let ConnexionInstance : Connexion;
+
+export interface GameManagerInterface {
+    GameScenes: Array<GameSceneInterface>;
+
+    sharedUserPosition(UserPositions: any): void;
+}
+export class GameManager implements GameManagerInterface {
+    GameScenes: Array<GameSceneInterface> = [];
+
+    constructor() {
+        this.configureGame();
+        ConnexionInstance = new Connexion("test@gmail.com", this);
+    }
+
+    configureGame() {
+        ROOM.forEach((roomId) => {
+            let newGame = new GameScene(roomId, this);
+            this.GameScenes.push(newGame);
+        });
+    }
+
+    sharedUserPosition(UserPositions: any) {
+        let Game: GameSceneInterface = this.GameScenes.find((Game: GameSceneInterface) => Game.RoomId === UserPositions.roomId);
+        Game.sharedUserPosition(UserPositions)
+    }
+}
\ No newline at end of file
similarity index 59%
rename from front/src/Phaser/GameScene.ts
rename to front/src/Phaser/Game/GameScene.ts
index 4f2eb9cc7d22e79bbd5474dfd8a6e096fb3de33a..947da7bd024209812e651017e2e30e7b6aa98328 100644 (file)
@@ -1,12 +1,19 @@
 import {MapManagerInterface, MapManager} from "./MapManager";
+import {GameManagerInterface} from "./GameManager";
 
-export class GameScene extends Phaser.Scene {
+export interface GameSceneInterface extends Phaser.Scene {
+    RoomId : string;
+    sharedUserPosition(data : []): void;
+}
+export class GameScene extends Phaser.Scene implements GameSceneInterface{
     private MapManager : MapManagerInterface;
+    RoomId : string;
 
-    constructor() {
+    constructor(RoomId : string, GameManager : GameManagerInterface) {
         super({
             key: "GameScene"
         });
+        this.RoomId = RoomId;
     }
 
     //hook preload scene
@@ -32,4 +39,9 @@ export class GameScene extends Phaser.Scene {
     update(dt: number): void {
         this.MapManager.update();
     }
+
+    sharedUserPosition(data: []): void {
+        //TODO share position of all user
+        //console.log("sharedUserPosition", data);
+    }
 }
similarity index 91%
rename from front/src/Phaser/MapManager.ts
rename to front/src/Phaser/Game/MapManager.ts
index ecd86210ec80fd6693e1bfad1eb952d030608bd8..f12a32d1e12d9f3bd5459e0ff231b4dc5db457b0 100644 (file)
@@ -1,6 +1,7 @@
 import {CameraManager, CameraManagerInterface} from "./CameraManager";
-import {RESOLUTION} from "../Enum/EnvironmentVariable";
-import {Player} from "./Player";
+import {RESOLUTION} from "../../Enum/EnvironmentVariable";
+import {Player} from "../Player/Player";
+import {GameScene, GameSceneInterface} from "./GameScene";
 
 export interface MapManagerInterface {
     keyZ: Phaser.Input.Keyboard.Key;
@@ -16,6 +17,7 @@ export interface MapManagerInterface {
     Map: Phaser.Tilemaps.Tilemap;
     Terrain: Phaser.Tilemaps.Tileset;
     Camera: CameraManagerInterface;
+    Scene: GameSceneInterface;
     update(): void;
 }
 export class MapManager implements MapManagerInterface{
@@ -32,12 +34,12 @@ export class MapManager implements MapManagerInterface{
     Terrain : Phaser.Tilemaps.Tileset;
     Camera: CameraManagerInterface;
     CurrentPlayer: Player;
-    Scene: Phaser.Scene;
+    Scene: GameSceneInterface;
     Map: Phaser.Tilemaps.Tilemap;
     startX = (window.innerWidth / 2) / RESOLUTION;
     startY = (window.innerHeight / 2) / RESOLUTION;
 
-    constructor(scene: Phaser.Scene){
+    constructor(scene: GameSceneInterface){
         this.Scene = scene;
 
         //initalise map
similarity index 83%
rename from front/src/Phaser/Player.ts
rename to front/src/Phaser/Player/Player.ts
index f76778a472f27cf549b377c272411dda8723fcec..ebfc19b0cfc57fad8f93bbe2ef601b1ed187223e 100644 (file)
@@ -1,12 +1,16 @@
-import {MapManagerInterface} from "./MapManager";
-import {getPlayerAnimations, playAnimation, PlayerAnimationNames} from "./Player/Animation";
+import {MapManagerInterface} from "../Game/MapManager";
+import {getPlayerAnimations, playAnimation, PlayerAnimationNames} from "./Animation";
+import {Connexion} from "../../Connexion";
+import {GameSceneInterface} from "../Game/GameScene";
+import {ConnexionInstance} from "../Game/GameManager";
 
 export class Player extends Phaser.GameObjects.Sprite{
     MapManager : MapManagerInterface;
     PlayerValue : string;
+    Connexion: Connexion;
 
     constructor(
-        Scene : Phaser.Scene,
+        Scene : GameSceneInterface,
         x : number,
         y : number,
         MapManager: MapManagerInterface,
@@ -69,6 +73,14 @@ export class Player extends Phaser.GameObjects.Sprite{
         }
         if(!haveMove){
             playAnimation(this, PlayerAnimationNames.None);
+        }else{
+            this.sharePosition();
+        }
+    }
+
+    private sharePosition(){
+        if(ConnexionInstance) {
+            ConnexionInstance.sharePosition((this.scene as GameSceneInterface).RoomId, this.x, this.y);
         }
     }
 
index f0f5107dfa43e38f1d3e0195468aa87843e5b297..650fd52cde7bea72555521c3222f48834d12c8da 100644 (file)
@@ -1,15 +1,16 @@
 import 'phaser';
 import GameConfig = Phaser.Types.Core.GameConfig;
-import {GameScene} from "./Phaser/GameScene";
-import {Connexion} from "./Connexion";
+import {GameManager} from "./Phaser/Game/GameManager";
 import {RESOLUTION} from "./Enum/EnvironmentVariable";
 
+let gameManager = new GameManager();
+
 const config: GameConfig = {
     title: "Office game",
     width: window.innerWidth / RESOLUTION,
     height: window.innerHeight / RESOLUTION,
     parent: "game",
-    scene: [GameScene],
+    scene: gameManager.GameScenes,
     zoom: RESOLUTION,
 };
 
@@ -18,5 +19,3 @@ let game = new Phaser.Game(config);
 window.addEventListener('resize', function (event) {
     game.scale.resize(window.innerWidth / RESOLUTION, window.innerHeight / RESOLUTION);
 });
-
-const connexion = new Connexion("test@gmail.com");