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;
Map: Phaser.Tilemaps.Tilemap;
startX = (window.innerWidth / 2) / RESOLUTION;
startY = (window.innerHeight / 2) / RESOLUTION;
+ userInputManager: UserInputManager;
constructor(scene: GameSceneInterface){
this.Scene = scene;
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,
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
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;
})
}
- 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;
}
haveMove = true;
direction = PlayerAnimationNames.WalkUp;
}
- if((this.MapManager.keyQ.isDown || this.MapManager.keyLeft.isDown)){
+ if(activeEvents.get(UserInputEvent.MoveLeft)){
if(!this.CanMoveLeft()){
return;
}
haveMove = true;
direction = PlayerAnimationNames.WalkLeft;
}
- if((this.MapManager.keyS.isDown || this.MapManager.keyDown.isDown)){
+ if(activeEvents.get(UserInputEvent.MoveDown)){
if(!this.CanMoveDown()){
return;
}
haveMove = true;
direction = PlayerAnimationNames.WalkDown;
}
- if((this.MapManager.keyD.isDown || this.MapManager.keyRight.isDown)){
+ if(activeEvents.get(UserInputEvent.MoveRight)){
if(!this.CanMoveRight()){
return;
}
--- /dev/null
+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