Merge branch 'master' of github.com:thecodingmachine/workadventure

This commit is contained in:
David Négrier 2020-04-06 17:59:06 +02:00
commit cf7125301a
25 changed files with 897 additions and 28 deletions

148
front/src/Connexion.ts Normal file
View file

@ -0,0 +1,148 @@
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 {
userId: string;
roomId: string;
constructor(userId : string, roomId : string) {
this.userId = userId;
this.roomId = roomId;
}
toJson() {
return {
userId: this.userId,
roomId: this.roomId,
}
}
}
export class Point implements PointInterface{
x: number;
y: number;
constructor(x : number, y : number) {
if(x === null || y === null){
throw Error("position x and y cannot be null");
}
this.x = x;
this.y = y;
}
toJson(){
return {
x : this.x,
y: this.y
}
}
}
export class MessageUserPosition extends Message{
position: PointInterface;
constructor(userId : string, roomId : string, point : Point) {
super(userId, roomId);
this.position = point;
}
toString() {
return JSON.stringify(
Object.assign(
super.toJson(),
{
position: this.position.toJson()
})
);
}
}
export class Connexion {
socket : any;
token : string;
email : string;
startedRoom : string;
constructor(email : string) {
this.email = email;
Axios.post(`${API_URL}/login`, {email: email})
.then((res) => {
this.token = res.data.token;
this.startedRoom = res.data.roomId;
this.socket = SocketIo(`${API_URL}`, {
query: {
token: this.token
}
});
//join the room
this.joinARoom(this.startedRoom);
//share your first position
this.sharePosition(0, 0);
//create listen event to get all data user shared by the back
this.positionOfAllUser();
this.errorMessage();
})
.catch((err) => {
console.error(err);
throw err;
});
}
/**
* Permit to join a room
* @param roomId
*/
joinARoom(roomId : string){
let messageUserPosition = new MessageUserPosition(this.email, this.startedRoom, new Point(0, 0));
this.socket.emit('join-room', messageUserPosition.toString());
}
/**
* Permit to share your position in map
* @param x
* @param y
*/
sharePosition(x : number, y : number){
let messageUserPosition = new MessageUserPosition(this.email, this.startedRoom, new Point(x, y));
this.socket.emit('user-position', messageUserPosition.toString());
}
/**
* The data sent is an array with information for each user :
* [
* {
* userId: <string>,
* roomId: <string>,
* position: {
* x : <number>,
* y : <number>
* }
* },
* ...
* ]
**/
positionOfAllUser(){
this.socket.on("user-position", (message : string) => {
//TODO show all user in map
console.info("user-position", message);
});
}
errorMessage(){
this.socket.on('message-error', (message : string) => {
console.error("message-error", message);
})
}
}

View file

@ -0,0 +1,5 @@
const API_URL = process.env.API_URL || "http://api.workadventure.localhost";
export {
API_URL
}

View file

@ -1,5 +1,14 @@
export class GameScene extends Phaser.Scene {
private keyZ: Phaser.Input.Keyboard.Key;
private keyQ: Phaser.Input.Keyboard.Key;
private keyS: Phaser.Input.Keyboard.Key;
private keyD: Phaser.Input.Keyboard.Key;
private keyRight: Phaser.Input.Keyboard.Key;
private keyLeft: Phaser.Input.Keyboard.Key;
private keyUp: Phaser.Input.Keyboard.Key;
private keyDown: Phaser.Input.Keyboard.Key;
private keyShift: Phaser.Input.Keyboard.Key;
constructor() {
super({
@ -17,6 +26,22 @@ export class GameScene extends Phaser.Scene {
}
init(): void {
this.keyShift = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SHIFT);
this.keyZ = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Z);
this.keyQ = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Q);
this.keyS = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S);
this.keyD = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D);
this.keyUp = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.UP);
this.keyLeft = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.LEFT);
this.keyDown = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.DOWN);
this.keyRight = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.RIGHT);
}
private moveCamera(x:number, y:number, speedMultiplier: number): void {
this.cameras.main.scrollX += speedMultiplier * 2 * x;
this.cameras.main.scrollY += speedMultiplier * 2 * y;
}
create(): void {
@ -66,9 +91,25 @@ export class GameScene extends Phaser.Scene {
private angle: number = 0;
update(dt: number): void {
this.cameras.main.scrollX = Math.floor(300 + 300 * Math.cos(this.angle));
let speedMultiplier = this.keyShift.isDown ? 5 : 1;
if (this.keyZ.isDown || this.keyUp.isDown) {
this.moveCamera(0, -1, speedMultiplier);
}
if (this.keyQ.isDown || this.keyLeft.isDown) {
this.moveCamera(-1, 0, speedMultiplier);
}
if (this.keyS.isDown || this.keyDown.isDown) {
this.moveCamera(0, 1, speedMultiplier);
}
if (this.keyD.isDown || this.keyRight.isDown) {
this.moveCamera(1, 0, speedMultiplier);
}
/*this.cameras.main.scrollX = Math.floor(300 + 300 * Math.cos(this.angle));
this.cameras.main.scrollY = Math.floor(300 + 300 * Math.sin(this.angle));
this.angle = dt * 0.0001;
this.angle = dt * 0.0001;*/
}
}

View file

@ -1,6 +1,7 @@
import 'phaser';
import GameConfig = Phaser.Types.Core.GameConfig;
import {GameScene} from "./GameScene";
import {Connexion} from "./Connexion";
const resolution = 2;
@ -18,3 +19,5 @@ let game = new Phaser.Game(config);
window.addEventListener('resize', function (event) {
game.scale.resize(window.innerWidth / resolution, window.innerHeight / resolution);
});
const connexion = new Connexion("test@gmail.com");