Refactor to include connexion
This commit is contained in:
parent
5d463d097a
commit
bac1e804ad
@ -1,3 +1,5 @@
|
|||||||
|
import {GameManagerInterface} from "./Phaser/Game/GameManager";
|
||||||
|
|
||||||
const SocketIo = require('socket.io-client');
|
const SocketIo = require('socket.io-client');
|
||||||
import Axios from "axios";
|
import Axios from "axios";
|
||||||
import {API_URL} from "./Enum/EnvironmentVariable";
|
import {API_URL} from "./Enum/EnvironmentVariable";
|
||||||
@ -70,8 +72,11 @@ export class Connexion {
|
|||||||
email : string;
|
email : string;
|
||||||
startedRoom : string;
|
startedRoom : string;
|
||||||
|
|
||||||
constructor(email : string) {
|
GameManager: GameManagerInterface;
|
||||||
|
|
||||||
|
constructor(email : string, GameManager: GameManagerInterface) {
|
||||||
this.email = email;
|
this.email = email;
|
||||||
|
this.GameManager = GameManager;
|
||||||
Axios.post(`${API_URL}/login`, {email: email})
|
Axios.post(`${API_URL}/login`, {email: email})
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
this.token = res.data.token;
|
this.token = res.data.token;
|
||||||
@ -87,9 +92,8 @@ export class Connexion {
|
|||||||
this.joinARoom(this.startedRoom);
|
this.joinARoom(this.startedRoom);
|
||||||
|
|
||||||
//share your first position
|
//share your first position
|
||||||
this.sharePosition(0, 0);
|
this.sharePosition(this.startedRoom, 0, 0);
|
||||||
|
|
||||||
//create listen event to get all data user shared by the back
|
|
||||||
this.positionOfAllUser();
|
this.positionOfAllUser();
|
||||||
|
|
||||||
this.errorMessage();
|
this.errorMessage();
|
||||||
@ -114,8 +118,8 @@ export class Connexion {
|
|||||||
* @param x
|
* @param x
|
||||||
* @param y
|
* @param y
|
||||||
*/
|
*/
|
||||||
sharePosition(x : number, y : number){
|
sharePosition(roomId : string, x : number, y : number){
|
||||||
let messageUserPosition = new MessageUserPosition(this.email, this.startedRoom, new Point(x, y));
|
let messageUserPosition = new MessageUserPosition(this.email, roomId, new Point(x, y));
|
||||||
this.socket.emit('user-position', messageUserPosition.toString());
|
this.socket.emit('user-position', messageUserPosition.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,8 +139,10 @@ export class Connexion {
|
|||||||
**/
|
**/
|
||||||
positionOfAllUser(){
|
positionOfAllUser(){
|
||||||
this.socket.on("user-position", (message : string) => {
|
this.socket.on("user-position", (message : string) => {
|
||||||
//TODO show all user in map
|
let data = JSON.parse(message);
|
||||||
console.info("user-position", message);
|
data.forEach((UserPositions : any) => {
|
||||||
|
this.GameManager.sharedUserPosition(UserPositions);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
const API_URL = process.env.API_URL || "http://api.workadventure.localhost";
|
const API_URL = process.env.API_URL || "http://api.workadventure.localhost";
|
||||||
|
const ROOM = [process.env.ROOM || "THECODINGMACHINE"];
|
||||||
const RESOLUTION = 2;
|
const RESOLUTION = 2;
|
||||||
|
|
||||||
export {
|
export {
|
||||||
API_URL,
|
API_URL,
|
||||||
RESOLUTION
|
RESOLUTION,
|
||||||
|
ROOM
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
import {RESOLUTION} from "../Enum/EnvironmentVariable";
|
import {RESOLUTION} from "../../Enum/EnvironmentVariable";
|
||||||
import {Player} from "./Player";
|
import {Player} from "../Player/Player";
|
||||||
import {MapManagerInterface} from "./MapManager";
|
import {MapManagerInterface} from "./MapManager";
|
||||||
|
|
||||||
export interface CameraManagerInterface {
|
export interface CameraManagerInterface {
|
31
front/src/Phaser/Game/GameManager.ts
Normal file
31
front/src/Phaser/Game/GameManager.ts
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import {GameSceneInterface, GameScene} from "./GameScene";
|
||||||
|
import {ROOM} from "../../Enum/EnvironmentVariable"
|
||||||
|
import {Connexion} from "../../Connexion";
|
||||||
|
|
||||||
|
export let ConnexionInstance : Connexion;
|
||||||
|
|
||||||
|
export interface GameManagerInterface {
|
||||||
|
GameScenes: Array<GameSceneInterface>;
|
||||||
|
|
||||||
|
sharedUserPosition(UserPositions: any): void;
|
||||||
|
}
|
||||||
|
export class GameManager implements GameManagerInterface {
|
||||||
|
GameScenes: Array<GameSceneInterface> = [];
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.configureGame();
|
||||||
|
ConnexionInstance = new Connexion("test@gmail.com", this);
|
||||||
|
}
|
||||||
|
|
||||||
|
configureGame() {
|
||||||
|
ROOM.forEach((roomId) => {
|
||||||
|
let newGame = new GameScene(roomId, this);
|
||||||
|
this.GameScenes.push(newGame);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
sharedUserPosition(UserPositions: any) {
|
||||||
|
let Game: GameSceneInterface = this.GameScenes.find((Game: GameSceneInterface) => Game.RoomId === UserPositions.roomId);
|
||||||
|
Game.sharedUserPosition(UserPositions)
|
||||||
|
}
|
||||||
|
}
|
@ -1,12 +1,19 @@
|
|||||||
import {MapManagerInterface, MapManager} from "./MapManager";
|
import {MapManagerInterface, MapManager} from "./MapManager";
|
||||||
|
import {GameManagerInterface} from "./GameManager";
|
||||||
|
|
||||||
export class GameScene extends Phaser.Scene {
|
export interface GameSceneInterface extends Phaser.Scene {
|
||||||
|
RoomId : string;
|
||||||
|
sharedUserPosition(data : []): void;
|
||||||
|
}
|
||||||
|
export class GameScene extends Phaser.Scene implements GameSceneInterface{
|
||||||
private MapManager : MapManagerInterface;
|
private MapManager : MapManagerInterface;
|
||||||
|
RoomId : string;
|
||||||
|
|
||||||
constructor() {
|
constructor(RoomId : string, GameManager : GameManagerInterface) {
|
||||||
super({
|
super({
|
||||||
key: "GameScene"
|
key: "GameScene"
|
||||||
});
|
});
|
||||||
|
this.RoomId = RoomId;
|
||||||
}
|
}
|
||||||
|
|
||||||
//hook preload scene
|
//hook preload scene
|
||||||
@ -32,4 +39,9 @@ export class GameScene extends Phaser.Scene {
|
|||||||
update(dt: number): void {
|
update(dt: number): void {
|
||||||
this.MapManager.update();
|
this.MapManager.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sharedUserPosition(data: []): void {
|
||||||
|
//TODO share position of all user
|
||||||
|
//console.log("sharedUserPosition", data);
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,6 +1,7 @@
|
|||||||
import {CameraManager, CameraManagerInterface} from "./CameraManager";
|
import {CameraManager, CameraManagerInterface} from "./CameraManager";
|
||||||
import {RESOLUTION} from "../Enum/EnvironmentVariable";
|
import {RESOLUTION} from "../../Enum/EnvironmentVariable";
|
||||||
import {Player} from "./Player";
|
import {Player} from "../Player/Player";
|
||||||
|
import {GameScene, GameSceneInterface} from "./GameScene";
|
||||||
|
|
||||||
export interface MapManagerInterface {
|
export interface MapManagerInterface {
|
||||||
keyZ: Phaser.Input.Keyboard.Key;
|
keyZ: Phaser.Input.Keyboard.Key;
|
||||||
@ -16,6 +17,7 @@ export interface MapManagerInterface {
|
|||||||
Map: Phaser.Tilemaps.Tilemap;
|
Map: Phaser.Tilemaps.Tilemap;
|
||||||
Terrain: Phaser.Tilemaps.Tileset;
|
Terrain: Phaser.Tilemaps.Tileset;
|
||||||
Camera: CameraManagerInterface;
|
Camera: CameraManagerInterface;
|
||||||
|
Scene: GameSceneInterface;
|
||||||
update(): void;
|
update(): void;
|
||||||
}
|
}
|
||||||
export class MapManager implements MapManagerInterface{
|
export class MapManager implements MapManagerInterface{
|
||||||
@ -32,12 +34,12 @@ export class MapManager implements MapManagerInterface{
|
|||||||
Terrain : Phaser.Tilemaps.Tileset;
|
Terrain : Phaser.Tilemaps.Tileset;
|
||||||
Camera: CameraManagerInterface;
|
Camera: CameraManagerInterface;
|
||||||
CurrentPlayer: Player;
|
CurrentPlayer: Player;
|
||||||
Scene: Phaser.Scene;
|
Scene: GameSceneInterface;
|
||||||
Map: Phaser.Tilemaps.Tilemap;
|
Map: Phaser.Tilemaps.Tilemap;
|
||||||
startX = (window.innerWidth / 2) / RESOLUTION;
|
startX = (window.innerWidth / 2) / RESOLUTION;
|
||||||
startY = (window.innerHeight / 2) / RESOLUTION;
|
startY = (window.innerHeight / 2) / RESOLUTION;
|
||||||
|
|
||||||
constructor(scene: Phaser.Scene){
|
constructor(scene: GameSceneInterface){
|
||||||
this.Scene = scene;
|
this.Scene = scene;
|
||||||
|
|
||||||
//initalise map
|
//initalise map
|
@ -1,12 +1,16 @@
|
|||||||
import {MapManagerInterface} from "./MapManager";
|
import {MapManagerInterface} from "../Game/MapManager";
|
||||||
import {getPlayerAnimations, playAnimation, PlayerAnimationNames} from "./Player/Animation";
|
import {getPlayerAnimations, playAnimation, PlayerAnimationNames} from "./Animation";
|
||||||
|
import {Connexion} from "../../Connexion";
|
||||||
|
import {GameSceneInterface} from "../Game/GameScene";
|
||||||
|
import {ConnexionInstance} from "../Game/GameManager";
|
||||||
|
|
||||||
export class Player extends Phaser.GameObjects.Sprite{
|
export class Player extends Phaser.GameObjects.Sprite{
|
||||||
MapManager : MapManagerInterface;
|
MapManager : MapManagerInterface;
|
||||||
PlayerValue : string;
|
PlayerValue : string;
|
||||||
|
Connexion: Connexion;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
Scene : Phaser.Scene,
|
Scene : GameSceneInterface,
|
||||||
x : number,
|
x : number,
|
||||||
y : number,
|
y : number,
|
||||||
MapManager: MapManagerInterface,
|
MapManager: MapManagerInterface,
|
||||||
@ -69,6 +73,14 @@ export class Player extends Phaser.GameObjects.Sprite{
|
|||||||
}
|
}
|
||||||
if(!haveMove){
|
if(!haveMove){
|
||||||
playAnimation(this, PlayerAnimationNames.None);
|
playAnimation(this, PlayerAnimationNames.None);
|
||||||
|
}else{
|
||||||
|
this.sharePosition();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private sharePosition(){
|
||||||
|
if(ConnexionInstance) {
|
||||||
|
ConnexionInstance.sharePosition((this.scene as GameSceneInterface).RoomId, this.x, this.y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,15 +1,16 @@
|
|||||||
import 'phaser';
|
import 'phaser';
|
||||||
import GameConfig = Phaser.Types.Core.GameConfig;
|
import GameConfig = Phaser.Types.Core.GameConfig;
|
||||||
import {GameScene} from "./Phaser/GameScene";
|
import {GameManager} from "./Phaser/Game/GameManager";
|
||||||
import {Connexion} from "./Connexion";
|
|
||||||
import {RESOLUTION} from "./Enum/EnvironmentVariable";
|
import {RESOLUTION} from "./Enum/EnvironmentVariable";
|
||||||
|
|
||||||
|
let gameManager = new GameManager();
|
||||||
|
|
||||||
const config: GameConfig = {
|
const config: GameConfig = {
|
||||||
title: "Office game",
|
title: "Office game",
|
||||||
width: window.innerWidth / RESOLUTION,
|
width: window.innerWidth / RESOLUTION,
|
||||||
height: window.innerHeight / RESOLUTION,
|
height: window.innerHeight / RESOLUTION,
|
||||||
parent: "game",
|
parent: "game",
|
||||||
scene: [GameScene],
|
scene: gameManager.GameScenes,
|
||||||
zoom: RESOLUTION,
|
zoom: RESOLUTION,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -18,5 +19,3 @@ let game = new Phaser.Game(config);
|
|||||||
window.addEventListener('resize', function (event) {
|
window.addEventListener('resize', function (event) {
|
||||||
game.scale.resize(window.innerWidth / RESOLUTION, window.innerHeight / RESOLUTION);
|
game.scale.resize(window.innerWidth / RESOLUTION, window.innerHeight / RESOLUTION);
|
||||||
});
|
});
|
||||||
|
|
||||||
const connexion = new Connexion("test@gmail.com");
|
|
||||||
|
Loading…
Reference in New Issue
Block a user