From: gparant Date: Tue, 7 Apr 2020 19:02:23 +0000 (+0200) Subject: Fix Message send to add direction X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=67c3eaa7f48533e32c5d46e348a66a4aaa08eb09;p=libreadventure.git Fix Message send to add direction --- diff --git a/back/src/Model/Websocket/MessageUserPosition.ts b/back/src/Model/Websocket/MessageUserPosition.ts index 493f145..117c99c 100644 --- a/back/src/Model/Websocket/MessageUserPosition.ts +++ b/back/src/Model/Websocket/MessageUserPosition.ts @@ -4,19 +4,22 @@ import {PointInterface} from "./PointInterface"; export class Point implements PointInterface{ x: number; y: number; + direction: string; - constructor(x : number, y : number) { + constructor(x : number, y : number, direction : string = "none") { if(x === null || y === null){ throw Error("position x and y cannot be null"); } this.x = x; this.y = y; + this.direction = direction; } toJson(){ return { x : this.x, - y: this.y + y: this.y, + direction: this.direction } } } @@ -27,7 +30,7 @@ export class MessageUserPosition extends Message{ constructor(message: string) { super(message); let data = JSON.parse(message); - this.position = new Point(data.position.x, data.position.y); + this.position = new Point(data.position.x, data.position.y, data.position.direction); } toString() { diff --git a/back/src/Model/Websocket/PointInterface.ts b/back/src/Model/Websocket/PointInterface.ts index 7f2ab39..7b464a5 100644 --- a/back/src/Model/Websocket/PointInterface.ts +++ b/back/src/Model/Websocket/PointInterface.ts @@ -1,5 +1,6 @@ export interface PointInterface { x: number; y: number; + direction: string; toJson() : object; } \ No newline at end of file diff --git a/front/src/Connexion.ts b/front/src/Connexion.ts index 18bcd6d..9c6bde0 100644 --- a/front/src/Connexion.ts +++ b/front/src/Connexion.ts @@ -30,19 +30,22 @@ export class Message { export class Point implements PointInterface{ x: number; y: number; + direction : string; - constructor(x : number, y : number) { + constructor(x : number, y : number, direction : string = "none") { if(x === null || y === null){ throw Error("position x and y cannot be null"); } this.x = x; this.y = y; + this.direction = direction; } toJson(){ return { x : this.x, - y: this.y + y: this.y, + direction: this.direction } } } @@ -114,15 +117,17 @@ export class Connexion { } /** - * Permit to share your position in map + * + * @param roomId * @param x * @param y + * @param direction */ - sharePosition(roomId : string, x : number, y : number){ + sharePosition(roomId : string, x : number, y : number, direction : string = "none"){ if(!this.socket){ return; } - let messageUserPosition = new MessageUserPosition(this.email, roomId, new Point(x, y)); + let messageUserPosition = new MessageUserPosition(this.email, roomId, new Point(x, y, direction)); this.socket.emit('user-position', messageUserPosition.toString()); } diff --git a/front/src/Phaser/Player/Player.ts b/front/src/Phaser/Player/Player.ts index ebfc19b..f391104 100644 --- a/front/src/Phaser/Player/Player.ts +++ b/front/src/Phaser/Player/Player.ts @@ -38,6 +38,7 @@ export class Player extends Phaser.GameObjects.Sprite{ //if user client on shift, camera and player speed let speedMultiplier = this.MapManager.keyShift.isDown ? 5 : 1; let haveMove = false; + let direction = null; if((this.MapManager.keyZ.isDown || this.MapManager.keyUp.isDown)){ if(!this.CanToMoveUp()){ @@ -46,6 +47,7 @@ export class Player extends Phaser.GameObjects.Sprite{ playAnimation(this, PlayerAnimationNames.WalkUp); this.setY(this.y - (2 * speedMultiplier)); haveMove = true; + direction = PlayerAnimationNames.WalkUp; } if((this.MapManager.keyQ.isDown || this.MapManager.keyLeft.isDown)){ if(!this.CanToMoveLeft()){ @@ -54,6 +56,7 @@ export class Player extends Phaser.GameObjects.Sprite{ playAnimation(this, PlayerAnimationNames.WalkLeft); this.setX(this.x - (2 * speedMultiplier)); haveMove = true; + direction = PlayerAnimationNames.WalkLeft; } if((this.MapManager.keyS.isDown || this.MapManager.keyDown.isDown)){ if(!this.CanToMoveDown()){ @@ -62,6 +65,7 @@ export class Player extends Phaser.GameObjects.Sprite{ playAnimation(this, PlayerAnimationNames.WalkDown); this.setY(this.y + (2 * speedMultiplier)); haveMove = true; + direction = PlayerAnimationNames.WalkDown; } if((this.MapManager.keyD.isDown || this.MapManager.keyRight.isDown)){ if(!this.CanToMoveRight()){ @@ -70,17 +74,18 @@ export class Player extends Phaser.GameObjects.Sprite{ playAnimation(this, PlayerAnimationNames.WalkRight); this.setX(this.x + (2 * speedMultiplier)); haveMove = true; + direction = PlayerAnimationNames.WalkRight; } if(!haveMove){ playAnimation(this, PlayerAnimationNames.None); }else{ - this.sharePosition(); + this.sharePosition(direction); } } - private sharePosition(){ + private sharePosition(direction : string){ if(ConnexionInstance) { - ConnexionInstance.sharePosition((this.scene as GameSceneInterface).RoomId, this.x, this.y); + ConnexionInstance.sharePosition((this.scene as GameSceneInterface).RoomId, this.x, this.y, direction); } }