Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Better joystick handling #8

Open
wants to merge 1 commit into
base: mobile_support
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions front/src/Phaser/Player/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class Player extends Character implements CurrentGamerInterface {
let x = 0;
let y = 0;
if (activeEvents.get(UserInputEvent.MoveUp)) {
y = -moveAmount;
y = - moveAmount;
direction = PlayerAnimationNames.WalkUp;
moving = true;
} else if (activeEvents.get(UserInputEvent.MoveDown)) {
Expand All @@ -59,17 +59,15 @@ export class Player extends Character implements CurrentGamerInterface {
direction = PlayerAnimationNames.WalkRight;
moving = true;
}
moving = moving || activeEvents.get(UserInputEvent.JoystickMove);
if (x !== 0 || y !== 0) {
this.move(x, y);
this.emit(hasMovedEventName, {moving, direction, x: this.x, y: this.y});
} else if (this.wasMoving && moving) {
// slow joystick movement
this.move(0, 0);
this.emit(hasMovedEventName, {moving, direction: this.previousDirection, x: this.x, y: this.y});
} else if (this.wasMoving && !moving) {
this.stop();
this.emit(hasMovedEventName, {moving, direction: this.previousDirection, x: this.x, y: this.y});
} else {
if (this.wasMoving) {
//direction = PlayerAnimationNames.None;
this.stop();
this.emit(hasMovedEventName, {moving, direction: this.previousDirection, x: this.x, y: this.y});
}
}

if (direction !== null) {
Expand Down
69 changes: 9 additions & 60 deletions front/src/Phaser/UserInput/UserInputManager.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Direction, IVirtualJoystick } from "../../types";
import { CursorKey, 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;
keyInstance: Phaser.Input.Keyboard.Key | CursorKey;
event: UserInputEvent
}

Expand All @@ -17,7 +17,6 @@ export enum UserInputEvent {
SpeedUp,
Interact,
Shout,
JoystickMove,
}

//we cannot use a map structure so we have to create a replacment
Expand All @@ -30,54 +29,22 @@ export class ActiveEventList {
set(event: UserInputEvent, value: boolean): void {
this.eventMap.set(event, value);
}
forEach(callback: (value: boolean, key: UserInputEvent) => void): void {
this.eventMap.forEach(callback);
}
any(): boolean {
return Array.from(this.eventMap.values()).reduce((accu, curr) => accu || curr, false);
}
}

//this class is responsible for catching user inputs and listing all active user actions at every game tick events.
export class UserInputManager {
private KeysCode!: UserInputManagerDatum[];
private Scene: GameScene;

private joystick : IVirtualJoystick;
private joystickEvents = new ActiveEventList();
private joystickForceThreshold = 60;
private joystickForceAccuX = 0;
private joystickForceAccuY = 0;

constructor(Scene: GameScene, virtualJoystick: IVirtualJoystick) {
this.Scene = Scene;
this.initKeyBoardEvent();
this.joystick = virtualJoystick;
this.joystick.on("update", () => {
this.joystickForceAccuX = this.joystick.forceX ? this.joystickForceAccuX : 0;
this.joystickForceAccuY = this.joystick.forceY ? this.joystickForceAccuY : 0;
const cursorKeys = this.joystick.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;
}
}
});
this.initKeyBoardEvent();
}

initKeyBoardEvent(){
const joystickKeys = this.joystick.createCursorKeys();
this.KeysCode = [
{event: UserInputEvent.MoveUp, keyInstance: this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Z, false) },
{event: UserInputEvent.MoveUp, keyInstance: this.Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.W, false) },
Expand All @@ -96,6 +63,11 @@ export class UserInputManager {
{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) },

{event: UserInputEvent.MoveUp, keyInstance: joystickKeys.up },
{event: UserInputEvent.MoveLeft, keyInstance: joystickKeys.left },
{event: UserInputEvent.MoveDown, keyInstance: joystickKeys.down },
{event: UserInputEvent.MoveRight, keyInstance: joystickKeys.right },
];
}

Expand All @@ -109,29 +81,6 @@ export class UserInputManager {

getEventListForGameTick(): ActiveEventList {
const eventsMap = new ActiveEventList();
this.joystickEvents.forEach((value, key) => {
if (value) {
switch (key) {
case UserInputEvent.MoveUp:
case UserInputEvent.MoveDown:
this.joystickForceAccuY += this.joystick.forceY;
if (Math.abs(this.joystickForceAccuY) > this.joystickForceThreshold) {
eventsMap.set(key, value);
this.joystickForceAccuY = 0;
}
break;
case UserInputEvent.MoveLeft:
case UserInputEvent.MoveRight:
this.joystickForceAccuX += this.joystick.forceX;
if (Math.abs(this.joystickForceAccuX) > this.joystickForceThreshold) {
eventsMap.set(key, value);
this.joystickForceAccuX = 0;
}
break;
}
}
});
eventsMap.set(UserInputEvent.JoystickMove, this.joystickEvents.any());
this.KeysCode.forEach(d => {
if (d.keyInstance.isDown) {
eventsMap.set(d.event, true);
Expand Down