Multi players on the map

- Fix share user position
 - Fix initialise map
 - Create function to add user on the map with back end data
This commit is contained in:
gparant 2020-04-10 12:54:05 +02:00
parent 6bec8b3703
commit d257b2b944
10 changed files with 253 additions and 48 deletions

View file

@ -4,13 +4,7 @@ const SocketIo = require('socket.io-client');
import Axios from "axios";
import {API_URL} from "./Enum/EnvironmentVariable";
export interface PointInterface {
x: number;
y: number;
toJson() : object;
}
export class Message {
class Message {
userId: string;
roomId: string;
@ -27,7 +21,14 @@ export class Message {
}
}
export class Point implements PointInterface{
export interface PointInterface {
x: number;
y: number;
direction : string;
toJson() : object;
}
class Point implements PointInterface{
x: number;
y: number;
direction : string;
@ -50,7 +51,12 @@ export class Point implements PointInterface{
}
}
export class MessageUserPosition extends Message{
export interface MessageUserPositionInterface {
userId: string;
roomId: string;
position: PointInterface;
}
class MessageUserPosition extends Message implements MessageUserPositionInterface{
position: PointInterface;
constructor(userId : string, roomId : string, point : Point) {
@ -69,10 +75,36 @@ export class MessageUserPosition extends Message{
}
}
export interface ListMessageUserPositionInterface {
roomId : string;
listUsersPosition: Array<MessageUserPosition>;
}
class ListMessageUserPosition{
roomId : string;
listUsersPosition: Array<MessageUserPosition>;
constructor(roomId : string, data : any) {
this.roomId = roomId;
this.listUsersPosition = new Array<MessageUserPosition>();
data.forEach((userPosition: any) => {
this.listUsersPosition.push(new MessageUserPosition(
userPosition.userId,
userPosition.roomId,
new Point(
userPosition.position.x,
userPosition.position.y,
userPosition.position.direction
)
));
});
}
}
export class Connexion {
socket : any;
token : string;
email : string;
userId: string;
startedRoom : string;
GameManager: GameManagerInterface;
@ -80,10 +112,14 @@ export class Connexion {
constructor(email : string, GameManager: GameManagerInterface) {
this.email = email;
this.GameManager = GameManager;
Axios.post(`${API_URL}/login`, {email: email})
}
createConnexion(){
return Axios.post(`${API_URL}/login`, {email: this.email})
.then((res) => {
this.token = res.data.token;
this.startedRoom = res.data.roomId;
this.userId = res.data.userId;
this.socket = SocketIo(`${API_URL}`, {
query: {
@ -100,6 +136,11 @@ export class Connexion {
this.positionOfAllUser();
this.errorMessage();
return{
userId: this.userId,
roomId: this.startedRoom
}
})
.catch((err) => {
console.error(err);
@ -112,7 +153,7 @@ export class Connexion {
* @param roomId
*/
joinARoom(roomId : string){
let messageUserPosition = new MessageUserPosition(this.email, this.startedRoom, new Point(0, 0));
let messageUserPosition = new MessageUserPosition(this.userId, this.startedRoom, new Point(0, 0));
this.socket.emit('join-room', messageUserPosition.toString());
}
@ -127,7 +168,7 @@ export class Connexion {
if(!this.socket){
return;
}
let messageUserPosition = new MessageUserPosition(this.email, roomId, new Point(x, y, direction));
let messageUserPosition = new MessageUserPosition(this.userId, roomId, new Point(x, y, direction));
this.socket.emit('user-position', messageUserPosition.toString());
}
@ -146,11 +187,12 @@ export class Connexion {
* ...
* ]
**/
positionOfAllUser(){
this.socket.on("user-position", (message : string) => {
let data = JSON.parse(message);
data.forEach((UserPositions : any) => {
this.GameManager.sharedUserPosition(UserPositions);
positionOfAllUser() {
this.socket.on("user-position", (message: string) => {
let dataList = JSON.parse(message);
dataList.forEach((UserPositions: any) => {
let listMessageUserPosition = new ListMessageUserPosition(UserPositions[0], UserPositions[1]);
this.GameManager.shareUserPosition(listMessageUserPosition);
});
});
}