partey_workadventure/front/src/Phaser/UserInput/UserInputManager.ts

90 lines
3.7 KiB
TypeScript
Raw Normal View History

import {GameScene} from "../Game/GameScene";
interface UserInputManagerDatum {
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,
}
//we cannot the map structure so we have to create a replacment
export class ActiveEventList {
2020-06-10 12:15:25 +02:00
private KeysCode : Map<UserInputEvent, boolean> = new Map<UserInputEvent, boolean>();
get(event: UserInputEvent): boolean {
2020-06-10 12:15:25 +02:00
return this.KeysCode.get(event) || false;
}
2020-06-10 12:15:25 +02:00
set(event: UserInputEvent, value: boolean): void {
this.KeysCode.set(event, value);
}
}
//this class is responsible for catching user inputs and listing all active user actions at every game tick events.
export class UserInputManager {
2020-09-21 00:34:25 +02:00
private KeysCode!: UserInputManagerDatum[];
private Scene: GameScene;
2020-06-04 18:11:07 +02:00
constructor(Scene : GameScene) {
2020-09-21 00:34:25 +02:00
this.Scene = Scene;
this.initKeyBoardEvent();
}
2020-09-21 00:34:25 +02:00
initKeyBoardEvent(){
2020-06-04 18:11:07 +02:00
this.KeysCode = [
{event: UserInputEvent.MoveUp, keyInstance: this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Z, false) },
2020-11-09 12:24:45 +01:00
{event: UserInputEvent.MoveUp, keyInstance: this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.W, false) },
{event: UserInputEvent.MoveLeft, keyInstance: this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Q, false) },
2020-11-09 12:24:45 +01:00
{event: UserInputEvent.MoveLeft, keyInstance: this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.A, false) },
{event: UserInputEvent.MoveDown, keyInstance: this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S, false) },
{event: UserInputEvent.MoveRight, keyInstance: this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D, false) },
{event: UserInputEvent.MoveUp, keyInstance: this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.UP, false) },
{event: UserInputEvent.MoveLeft, keyInstance: this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.LEFT, false) },
{event: UserInputEvent.MoveDown, keyInstance: this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.DOWN, false) },
{event: UserInputEvent.MoveRight, keyInstance: this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.RIGHT, false) },
{event: UserInputEvent.SpeedUp, keyInstance: this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SHIFT, false) },
2020-06-04 18:11:07 +02:00
{event: UserInputEvent.Interact, keyInstance: this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.E, false) },
{event: UserInputEvent.Interact, keyInstance: this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE, false) },
{event: UserInputEvent.Shout, keyInstance: this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.F, false) },
2020-06-04 18:11:07 +02:00
];
}
2020-09-21 00:34:25 +02:00
clearAllInputKeyboard(){
this.Scene.input.keyboard.removeAllKeys();
}
getEventListForGameTick(): ActiveEventList {
2020-06-09 23:13:26 +02:00
const eventsMap = new ActiveEventList();
this.KeysCode.forEach(d => {
if (d. keyInstance.isDown) {
eventsMap.set(d.event, true);
}
});
return eventsMap;
}
spaceEvent(callback : Function){
this.Scene.input.keyboard.on('keyup-SPACE', (event: Event) => {
callback();
return event;
});
}
2020-10-31 14:04:55 +01:00
addSpaceEventListner(callback : Function){
this.Scene.input.keyboard.addListener('keyup-SPACE', callback);
}
removeSpaceEventListner(callback : Function){
this.Scene.input.keyboard.removeListener('keyup-SPACE', callback);
}
}