used the vent system to remove the dependency of Player on Connexion
authorkharhamel <oognic@gmail.com>
Sat, 2 May 2020 14:54:52 +0000 (16:54 +0200)
committerkharhamel <oognic@gmail.com>
Sat, 2 May 2020 14:56:36 +0000 (16:56 +0200)
front/src/Phaser/Game/GameManager.ts
front/src/Phaser/Game/GameScene.ts
front/src/Phaser/Player/Player.ts

index 1877b29a003c8fbf948ce8ee6a887306a48f9b7a..2dd9fdf19c6c7638b6f445bdb3cb5c72e178a74f 100644 (file)
@@ -9,7 +9,11 @@ export enum StatusGameManagerEnum {
     CURRENT_USER_CREATED = 2
 }
 
-export let ConnexionInstance : ConnexionInterface;
+export interface HasMovedEvent {
+    direction: string;
+    x: number;
+    y: number;
+}
 
 export class GameManager {
     status: number;
@@ -23,9 +27,8 @@ export class GameManager {
     
     connect(email:string) {
         this.ConnexionInstance = new Connexion(email, this);
-        ConnexionInstance = this.ConnexionInstance;
         return this.ConnexionInstance.createConnexion().then(() => {
-            this.SimplePeer = new SimplePeer(ConnexionInstance);
+            this.SimplePeer = new SimplePeer(this.ConnexionInstance);
         });
     }
 
@@ -57,6 +60,10 @@ export class GameManager {
             console.error(e);
         }
     }
+
+    pushPlayerPosition(event: HasMovedEvent) {
+        this.ConnexionInstance.sharePosition(event.x, event.y, event.direction);
+    }
 }
 
 export const gameManager = new GameManager();
\ No newline at end of file
index f7fbd7c3c554fdded9da38a6018f681515d5f567..8764e95908b7dfc08fba6b9547a46da84817774b 100644 (file)
@@ -1,6 +1,6 @@
-import {GameManager, gameManager, StatusGameManagerEnum} from "./GameManager";
+import {GameManager, gameManager, HasMovedEvent, StatusGameManagerEnum} from "./GameManager";
 import {MessageUserPositionInterface} from "../../Connexion";
-import {CurrentGamerInterface, GamerInterface, Player} from "../Player/Player";
+import {CurrentGamerInterface, GamerInterface, hasMovedEventName, Player} from "../Player/Player";
 import {DEBUG_MODE, RESOLUTION, ROOM, ZOOM_LEVEL} from "../../Enum/EnvironmentVariable";
 import Tile = Phaser.Tilemaps.Tile;
 import {ITiledMap, ITiledTileSet} from "../Map/ITiledMap";
@@ -168,6 +168,11 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface{
         //create collision
         this.createCollisionWithPlayer();
         this.createCollisionObject();
+        this.CurrentPlayer.on(hasMovedEventName, this.pushPlayerPosition.bind(this))
+    }
+    
+    pushPlayerPosition(event: HasMovedEvent) {
+        this.GameManager.pushPlayerPosition(event);
     }
 
     EventToClickOnTile(){
index 60326a2721898c104651f4c88fc7a788220cb916..4f5e790a950246c997e6524c046ca911bc6885f2 100644 (file)
@@ -1,10 +1,11 @@
 import {getPlayerAnimations, playAnimation, PlayerAnimationNames} from "./Animation";
 import {GameSceneInterface, Textures} from "../Game/GameScene";
-import {ConnexionInstance} from "../Game/GameManager";
 import {MessageUserPositionInterface} from "../../Connexion";
 import {ActiveEventList, UserInputEvent, UserInputManager} from "../UserInput/UserInputManager";
 import {PlayableCaracter} from "../Entity/PlayableCaracter";
 
+
+export const hasMovedEventName = "hasMoved";
 export interface CurrentGamerInterface extends PlayableCaracter{
     userId : string;
     PlayerValue : string;
@@ -88,15 +89,11 @@ export class Player extends PlayableCaracter implements CurrentGamerInterface, G
             direction = PlayerAnimationNames.None;
             this.stop();
         }
-        this.sharePosition(direction);
-    }
-
-    private sharePosition(direction: string) {
-        if (ConnexionInstance) {
-            ConnexionInstance.sharePosition(this.x, this.y, direction);
-        }
+        
+        this.emit(hasMovedEventName, {direction, x: this.x, y: this.y});
     }
 
+    //todo: put this method into the NonPlayer class instead
     updatePosition(MessageUserPosition: MessageUserPositionInterface) {
         playAnimation(this, MessageUserPosition.position.direction);
         this.setX(MessageUserPosition.position.x);