Fix Message send to add direction

This commit is contained in:
gparant 2020-04-07 21:02:23 +02:00
parent aba3322188
commit 67c3eaa7f4
4 changed files with 25 additions and 11 deletions

View File

@ -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() {

View File

@ -1,5 +1,6 @@
export interface PointInterface {
x: number;
y: number;
direction: string;
toJson() : object;
}

View File

@ -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());
}

View File

@ -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);
}
}