Integrate virtual joystick
This commit is contained in:
parent
e75fb9a6a9
commit
c3230bc2b3
10 changed files with 170 additions and 16 deletions
|
@ -1,4 +1,8 @@
|
|||
import { Direction, IVirtualJoystick } from "../../types";
|
||||
import {GameScene} from "../Game/GameScene";
|
||||
const {
|
||||
default: VirtualJoystick,
|
||||
} = require("phaser3-rex-plugins/plugins/virtualjoystick.js");
|
||||
|
||||
interface UserInputManagerDatum {
|
||||
keyInstance: Phaser.Input.Keyboard.Key;
|
||||
|
@ -25,6 +29,9 @@ export class ActiveEventList {
|
|||
set(event: UserInputEvent, value: boolean): void {
|
||||
this.KeysCode.set(event, value);
|
||||
}
|
||||
forEach(callback: (value: boolean, key: UserInputEvent) => void): void {
|
||||
this.KeysCode.forEach(callback);
|
||||
}
|
||||
}
|
||||
|
||||
//this class is responsible for catching user inputs and listing all active user actions at every game tick events.
|
||||
|
@ -32,10 +39,32 @@ export class UserInputManager {
|
|||
private KeysCode!: UserInputManagerDatum[];
|
||||
private Scene: GameScene;
|
||||
private isInputDisabled : boolean;
|
||||
constructor(Scene : GameScene) {
|
||||
private joystickEvents = new ActiveEventList();
|
||||
|
||||
constructor(Scene: GameScene, virtualJoystick: IVirtualJoystick) {
|
||||
this.Scene = Scene;
|
||||
this.initKeyBoardEvent();
|
||||
this.isInputDisabled = false;
|
||||
this.initKeyBoardEvent();
|
||||
virtualJoystick.on("update", () => {
|
||||
const cursorKeys = virtualJoystick.createCursorKeys();
|
||||
for (const name in cursorKeys) {
|
||||
const key = cursorKeys[name as Direction];
|
||||
switch (name) {
|
||||
case "up":
|
||||
this.joystickEvents.set(UserInputEvent.MoveUp, key.isDown);
|
||||
break;
|
||||
case "left":
|
||||
this.joystickEvents.set(UserInputEvent.MoveLeft, key.isDown);
|
||||
break;
|
||||
case "down":
|
||||
this.joystickEvents.set(UserInputEvent.MoveDown, key.isDown);
|
||||
break;
|
||||
case "right":
|
||||
this.joystickEvents.set(UserInputEvent.MoveRight, key.isDown);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
initKeyBoardEvent(){
|
||||
|
@ -78,11 +107,15 @@ export class UserInputManager {
|
|||
if (this.isInputDisabled) {
|
||||
return eventsMap;
|
||||
}
|
||||
this.joystickEvents.forEach((value, key) => {
|
||||
if (value) {
|
||||
eventsMap.set(key, value);
|
||||
}
|
||||
});
|
||||
this.KeysCode.forEach(d => {
|
||||
if (d. keyInstance.isDown) {
|
||||
eventsMap.set(d.event, true);
|
||||
}
|
||||
|
||||
});
|
||||
return eventsMap;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue