Refactor to include connexion

This commit is contained in:
gparant 2020-04-07 20:41:35 +02:00
parent 5d463d097a
commit bac1e804ad
8 changed files with 88 additions and 24 deletions

View file

@ -0,0 +1,95 @@
import {RESOLUTION} from "../../Enum/EnvironmentVariable";
import {Player} from "../Player/Player";
import {MapManagerInterface} from "./MapManager";
export interface CameraManagerInterface {
CurrentPlayer : Player;
MapManager : MapManagerInterface;
moveCamera() : void;
}
export class CameraManager implements CameraManagerInterface{
Scene : Phaser.Scene;
Camera : Phaser.Cameras.Scene2D.Camera;
CurrentPlayer : Player;
MapManager : MapManagerInterface;
constructor(
Scene: Phaser.Scene,
Camera : Phaser.Cameras.Scene2D.Camera,
MapManager: MapManagerInterface,
CurrentPlayer: Player
) {
this.Scene = Scene;
this.MapManager = MapManager;
this.Camera = Camera;
this.CurrentPlayer = CurrentPlayer;
}
/**
*
* @param x
* @param y
* @param speedMultiplier
*/
private moveCameraPosition(x:number, y:number, speedMultiplier: number): void {
this.Camera.scrollX += speedMultiplier * 2 * x;
this.Camera.scrollY += speedMultiplier * 2 * y;
}
/**
*
*/
moveCamera(): void {
//center of camera
let startX = ((window.innerWidth / 2) / RESOLUTION);
let startY = ((window.innerHeight / 2) / RESOLUTION);
//if user client on shift, camera and player speed
let speedMultiplier = this.MapManager.keyShift.isDown ? 5 : 1;
if (this.MapManager.keyZ.isDown || this.MapManager.keyUp.isDown) {
if (!this.CanToMoveUp()) {
this.Camera.scrollY = 0;
}else if (this.CurrentPlayer.y < (this.MapManager.Map.widthInPixels - startY)) {
this.moveCameraPosition(0, -1, speedMultiplier);
}
}
if (this.MapManager.keyQ.isDown || this.MapManager.keyLeft.isDown) {
if (!this.CanToMoveLeft()) {
this.Camera.scrollX = 0;
}else if (this.CurrentPlayer.x < (this.MapManager.Map.heightInPixels - startX)) {
this.moveCameraPosition(-1, 0, speedMultiplier);
}
}
if (this.MapManager.keyS.isDown || this.MapManager.keyDown.isDown) {
if (!this.CanToMoveDown()) {
this.Camera.scrollY = (this.MapManager.Map.heightInPixels - (window.innerHeight / RESOLUTION));
} else if (this.CurrentPlayer.y > startY) {
this.moveCameraPosition(0, 1, speedMultiplier);
}
}
if (this.MapManager.keyD.isDown || this.MapManager.keyRight.isDown) {
if (!this.CanToMoveRight()) {
this.Camera.scrollX = (this.MapManager.Map.widthInPixels - (window.innerWidth / RESOLUTION));
} else if (this.CurrentPlayer.x > startX) {
this.moveCameraPosition(1, 0, speedMultiplier);
}
}
}
private CanToMoveUp(){
return this.Camera.scrollY > 0;
}
private CanToMoveLeft(){
return this.Camera.scrollX > 0;
}
private CanToMoveDown(){
return this.MapManager.Map.heightInPixels > (this.Camera.scrollY + (window.innerHeight / RESOLUTION))
}
private CanToMoveRight(){
return this.MapManager.Map.widthInPixels > (this.Camera.scrollX + (window.innerWidth / RESOLUTION))
}
}

View 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)
}
}

View file

@ -0,0 +1,47 @@
import {MapManagerInterface, MapManager} from "./MapManager";
import {GameManagerInterface} from "./GameManager";
export interface GameSceneInterface extends Phaser.Scene {
RoomId : string;
sharedUserPosition(data : []): void;
}
export class GameScene extends Phaser.Scene implements GameSceneInterface{
private MapManager : MapManagerInterface;
RoomId : string;
constructor(RoomId : string, GameManager : GameManagerInterface) {
super({
key: "GameScene"
});
this.RoomId = RoomId;
}
//hook preload scene
preload(): void {
this.load.image('tiles', 'maps/tiles.png');
this.load.tilemapTiledJSON('map', 'maps/map2.json');
this.load.spritesheet('player',
'resources/characters/pipoya/Male 01-1.png',
{ frameWidth: 32, frameHeight: 32 }
);
}
//hook initialisation
init(){};
//hook create scene
create(): void {
//create map manager
this.MapManager = new MapManager(this);
}
//hook update
update(dt: number): void {
this.MapManager.update();
}
sharedUserPosition(data: []): void {
//TODO share position of all user
//console.log("sharedUserPosition", data);
}
}

View file

@ -0,0 +1,87 @@
import {CameraManager, CameraManagerInterface} from "./CameraManager";
import {RESOLUTION} from "../../Enum/EnvironmentVariable";
import {Player} from "../Player/Player";
import {GameScene, GameSceneInterface} from "./GameScene";
export interface MapManagerInterface {
keyZ: Phaser.Input.Keyboard.Key;
keyQ: Phaser.Input.Keyboard.Key;
keyS: Phaser.Input.Keyboard.Key;
keyD: Phaser.Input.Keyboard.Key;
keyRight: Phaser.Input.Keyboard.Key;
keyLeft: Phaser.Input.Keyboard.Key;
keyUp: Phaser.Input.Keyboard.Key;
keyDown: Phaser.Input.Keyboard.Key;
keyShift: Phaser.Input.Keyboard.Key;
Map: Phaser.Tilemaps.Tilemap;
Terrain: Phaser.Tilemaps.Tileset;
Camera: CameraManagerInterface;
Scene: GameSceneInterface;
update(): void;
}
export class MapManager implements MapManagerInterface{
keyZ: Phaser.Input.Keyboard.Key;
keyQ: Phaser.Input.Keyboard.Key;
keyS: Phaser.Input.Keyboard.Key;
keyD: Phaser.Input.Keyboard.Key;
keyRight: Phaser.Input.Keyboard.Key;
keyLeft: Phaser.Input.Keyboard.Key;
keyUp: Phaser.Input.Keyboard.Key;
keyDown: Phaser.Input.Keyboard.Key;
keyShift: Phaser.Input.Keyboard.Key;
Terrain : Phaser.Tilemaps.Tileset;
Camera: CameraManagerInterface;
CurrentPlayer: Player;
Scene: GameSceneInterface;
Map: Phaser.Tilemaps.Tilemap;
startX = (window.innerWidth / 2) / RESOLUTION;
startY = (window.innerHeight / 2) / RESOLUTION;
constructor(scene: GameSceneInterface){
this.Scene = scene;
//initalise map
this.Map = this.Scene.add.tilemap("map");
this.Terrain = this.Map.addTilesetImage("tiles", "tiles");
this.Map.createStaticLayer("tiles", "tiles");
this.Map.createStaticLayer("Calque 1", [this.Terrain], 0, 0);
this.Map.createStaticLayer("Calque 2", [this.Terrain], 0, 0);
//initialise keyboard
this.initKeyBoard();
//initialise player
this.CurrentPlayer = new Player(
this.Scene,
this.startX,
this.startY,
this
);
this.CurrentPlayer.initAnimation();
//initialise camera
this.Camera = new CameraManager(this.Scene, this.Scene.cameras.main, this, this.CurrentPlayer);
}
initKeyBoard() {
this.keyShift = this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SHIFT);
this.keyZ = this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Z);
this.keyQ = this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Q);
this.keyS = this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S);
this.keyD = this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D);
this.keyUp = this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.UP);
this.keyLeft = this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.LEFT);
this.keyDown = this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.DOWN);
this.keyRight = this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.RIGHT);
}
update() : void {
this.CurrentPlayer.move();
this.Camera.moveCamera()
}
}