diff --git a/front/src/Phaser/Player/Player.ts b/front/src/Phaser/Player/Player.ts index 44b4868982..a5096ebb28 100644 --- a/front/src/Phaser/Player/Player.ts +++ b/front/src/Phaser/Player/Player.ts @@ -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)) { @@ -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) { diff --git a/front/src/Phaser/UserInput/UserInputManager.ts b/front/src/Phaser/UserInput/UserInputManager.ts index d0b77b9cff..c8dd026f7c 100644 --- a/front/src/Phaser/UserInput/UserInputManager.ts +++ b/front/src/Phaser/UserInput/UserInputManager.ts @@ -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 } @@ -17,7 +17,6 @@ export enum UserInputEvent { SpeedUp, Interact, Shout, - JoystickMove, } //we cannot use a map structure so we have to create a replacment @@ -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) }, @@ -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 }, ]; } @@ -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);