partey_workadventure/back/src/Model/Websocket/MessageUserPosition.ts

45 lines
1.1 KiB
TypeScript
Raw Normal View History

import {Message} from "./Message";
import {PointInterface} from "./PointInterface";
export class Point implements PointInterface{
x: number;
y: number;
2020-04-07 21:02:23 +02:00
direction: string;
2020-04-07 21:02:23 +02:00
constructor(x : number, y : number, direction : string = "none") {
if(x === null || y === null){
2020-04-04 17:56:43 +02:00
throw Error("position x and y cannot be null");
}
this.x = x;
this.y = y;
2020-04-07 21:02:23 +02:00
this.direction = direction;
}
toJson(){
return {
x : this.x,
2020-04-07 21:02:23 +02:00
y: this.y,
direction: this.direction
}
}
}
export class MessageUserPosition extends Message{
position: PointInterface;
constructor(message: string) {
super(message);
let data = JSON.parse(message);
2020-04-07 21:02:23 +02:00
this.position = new Point(data.position.x, data.position.y, data.position.direction);
}
toString() {
return JSON.stringify(
Object.assign(
super.toJson(),
{
position: this.position.toJson()
})
);
}
}