2020-04-11 16:46:28 +02:00
|
|
|
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,
|
2020-04-11 18:17:36 +02:00
|
|
|
Interact,
|
2020-04-12 19:35:51 +02:00
|
|
|
Shout,
|
2020-04-11 16:46:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//we cannot the map structure so we have to create a replacment
|
|
|
|
export class ActiveEventList {
|
2020-04-13 13:42:21 +02:00
|
|
|
private KeysCode : any;
|
2020-04-11 16:46:28 +02:00
|
|
|
constructor() {
|
2020-04-13 13:42:21 +02:00
|
|
|
this.KeysCode = {};
|
2020-04-11 16:46:28 +02:00
|
|
|
}
|
|
|
|
get(event: UserInputEvent): boolean {
|
2020-04-13 13:42:21 +02:00
|
|
|
return this.KeysCode[event] || false;
|
2020-04-11 16:46:28 +02:00
|
|
|
}
|
|
|
|
set(event: UserInputEvent, value: boolean): boolean {
|
2020-04-13 13:42:21 +02:00
|
|
|
return this.KeysCode[event] = true;
|
2020-04-11 16:46:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//this class is responsible for catching user inputs and listing all active user actions at every game tick events.
|
|
|
|
export class UserInputManager {
|
2020-04-13 13:42:21 +02:00
|
|
|
private KeysCode: UserInputManagerDatum[] = [
|
2020-04-11 16:46:28 +02:00
|
|
|
{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},
|
2020-04-11 18:17:36 +02:00
|
|
|
|
|
|
|
{keyCode: Phaser.Input.Keyboard.KeyCodes.E, event: UserInputEvent.Interact, keyInstance: null},
|
2020-04-12 19:35:51 +02:00
|
|
|
{keyCode: Phaser.Input.Keyboard.KeyCodes.F, event: UserInputEvent.Shout, keyInstance: null},
|
2020-04-11 16:46:28 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
constructor(Scene : GameSceneInterface) {
|
2020-04-13 13:42:21 +02:00
|
|
|
this.KeysCode.forEach(d => {
|
2020-04-11 16:46:28 +02:00
|
|
|
d.keyInstance = Scene.input.keyboard.addKey(d.keyCode);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getEventListForGameTick(): ActiveEventList {
|
|
|
|
let eventsMap = new ActiveEventList();
|
2020-04-13 13:42:21 +02:00
|
|
|
this.KeysCode.forEach(d => {
|
2020-04-11 16:46:28 +02:00
|
|
|
if (d. keyInstance.isDown) {
|
|
|
|
eventsMap.set(d.event, true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return eventsMap;
|
|
|
|
}
|
|
|
|
}
|