diff --git a/front/src/Phaser/Game/MapManager.ts b/front/src/Phaser/Game/MapManager.ts index 8b3d9231..3f8521d4 100644 --- a/front/src/Phaser/Game/MapManager.ts +++ b/front/src/Phaser/Game/MapManager.ts @@ -2,35 +2,17 @@ import {CameraManager, CameraManagerInterface} from "./CameraManager"; import {RESOLUTION} from "../../Enum/EnvironmentVariable"; import {Player} from "../Player/Player"; import {GameScene, GameSceneInterface} from "./GameScene"; +import {UserInputManager} from "../UserInput/UserInputManager"; 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; + userInputManager: UserInputManager; 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; @@ -38,6 +20,7 @@ export class MapManager implements MapManagerInterface{ Map: Phaser.Tilemaps.Tilemap; startX = (window.innerWidth / 2) / RESOLUTION; startY = (window.innerHeight / 2) / RESOLUTION; + userInputManager: UserInputManager; constructor(scene: GameSceneInterface){ this.Scene = scene; @@ -49,11 +32,9 @@ export class MapManager implements MapManagerInterface{ this.Map.createStaticLayer("Calque 1", [this.Terrain], 0, 0); this.Map.createStaticLayer("Calque 2", [this.Terrain], 0, 0); - //initialise keyboard - this.initKeyBoard(); - //initialise camera this.Camera = new CameraManager(this.Scene, this.Scene.cameras.main, this); + this.userInputManager = new UserInputManager(this.Scene); //initialise player this.CurrentPlayer = new Player( this.Scene, @@ -65,22 +46,9 @@ export class MapManager implements MapManagerInterface{ this.CurrentPlayer.initAnimation(); } - - 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(); + let activeEvents = this.userInputManager.getEventListForGameTick(); + + this.CurrentPlayer.move(activeEvents); } } \ No newline at end of file diff --git a/front/src/Phaser/Player/Player.ts b/front/src/Phaser/Player/Player.ts index 563843fd..11b3d4f6 100644 --- a/front/src/Phaser/Player/Player.ts +++ b/front/src/Phaser/Player/Player.ts @@ -3,6 +3,7 @@ import {getPlayerAnimations, playAnimation, PlayerAnimationNames} from "./Animat import {GameSceneInterface} from "../Game/GameScene"; import {ConnexionInstance} from "../Game/GameManager"; import {CameraManagerInterface} from "../Game/CameraManager"; +import {ActiveEventList, UserInputEvent} from "../UserInput/UserInputManager"; export class Player extends Phaser.GameObjects.Sprite{ MapManager : MapManagerInterface; @@ -36,13 +37,13 @@ export class Player extends Phaser.GameObjects.Sprite{ }) } - move(){ + move(activeEvents: ActiveEventList){ //if user client on shift, camera and player speed - let speedMultiplier = this.MapManager.keyShift.isDown ? 5 : 1; + let speedMultiplier = activeEvents.get(UserInputEvent.SpeedUp) ? 5 : 1; let haveMove = false; let direction = null; - if((this.MapManager.keyZ.isDown || this.MapManager.keyUp.isDown)){ + if(activeEvents.get(UserInputEvent.MoveUp)){ if(!this.CanMoveUp()){ return; } @@ -51,7 +52,7 @@ export class Player extends Phaser.GameObjects.Sprite{ haveMove = true; direction = PlayerAnimationNames.WalkUp; } - if((this.MapManager.keyQ.isDown || this.MapManager.keyLeft.isDown)){ + if(activeEvents.get(UserInputEvent.MoveLeft)){ if(!this.CanMoveLeft()){ return; } @@ -60,7 +61,7 @@ export class Player extends Phaser.GameObjects.Sprite{ haveMove = true; direction = PlayerAnimationNames.WalkLeft; } - if((this.MapManager.keyS.isDown || this.MapManager.keyDown.isDown)){ + if(activeEvents.get(UserInputEvent.MoveDown)){ if(!this.CanMoveDown()){ return; } @@ -69,7 +70,7 @@ export class Player extends Phaser.GameObjects.Sprite{ haveMove = true; direction = PlayerAnimationNames.WalkDown; } - if((this.MapManager.keyD.isDown || this.MapManager.keyRight.isDown)){ + if(activeEvents.get(UserInputEvent.MoveRight)){ if(!this.CanMoveRight()){ return; } diff --git a/front/src/Phaser/UserInput/UserInputManager.ts b/front/src/Phaser/UserInput/UserInputManager.ts new file mode 100644 index 00000000..c362bd95 --- /dev/null +++ b/front/src/Phaser/UserInput/UserInputManager.ts @@ -0,0 +1,63 @@ +import Map = Phaser.Structs.Map; +import {GameSceneInterface} from "../Game/GameScene"; + +interface UserInputManagerDatum { + keyCode: number; + keyInstance: Phaser.Input.Keyboard.Key; + event: UserInputEvent +} + +export enum UserInputEvent { + MoveLeft = 1, + MoveUp, + MoveRight, + MoveDown, + SpeedUp, +} + +//we cannot the map structure so we have to create a replacment +export class ActiveEventList { + private data:any; + constructor() { + this.data = {}; + } + get(event: UserInputEvent): boolean { + return this.data[event] || false; + } + set(event: UserInputEvent, value: boolean): boolean { + return this.data[event] = true; + } +} + +//this class is responsible for catching user inputs and listing all active user actions at every game tick events. +export class UserInputManager { + private data: UserInputManagerDatum[] = [ + {keyCode: Phaser.Input.Keyboard.KeyCodes.Z, event: UserInputEvent.MoveUp, keyInstance: null}, + {keyCode: Phaser.Input.Keyboard.KeyCodes.Q, event: UserInputEvent.MoveLeft, keyInstance: null}, + {keyCode: Phaser.Input.Keyboard.KeyCodes.S, event: UserInputEvent.MoveDown, keyInstance: null}, + {keyCode: Phaser.Input.Keyboard.KeyCodes.D, event: UserInputEvent.MoveRight, keyInstance: null}, + + {keyCode: Phaser.Input.Keyboard.KeyCodes.UP, event: UserInputEvent.MoveUp, keyInstance: null}, + {keyCode: Phaser.Input.Keyboard.KeyCodes.LEFT, event: UserInputEvent.MoveLeft, keyInstance: null}, + {keyCode: Phaser.Input.Keyboard.KeyCodes.DOWN, event: UserInputEvent.MoveDown, keyInstance: null}, + {keyCode: Phaser.Input.Keyboard.KeyCodes.RIGHT, event: UserInputEvent.MoveRight, keyInstance: null}, + + {keyCode: Phaser.Input.Keyboard.KeyCodes.SHIFT, event: UserInputEvent.SpeedUp, keyInstance: null}, + ]; + + constructor(Scene : GameSceneInterface) { + this.data.forEach(d => { + d.keyInstance = Scene.input.keyboard.addKey(d.keyCode); + }); + } + + getEventListForGameTick(): ActiveEventList { + let eventsMap = new ActiveEventList(); + this.data.forEach(d => { + if (d. keyInstance.isDown) { + eventsMap.set(d.event, true); + } + }); + return eventsMap; + } +} \ No newline at end of file