Skip to content

Commit 483817e

Browse files
Merge pull request #20 from MakingSpiderSense/bugfix/movement-speed-modifier/118-show-speed-lines-on-mobile
Add touch event listeners for mobile movement tracking in speed modifier component (again)
2 parents 2072f2b + 0481858 commit 483817e

1 file changed

Lines changed: 55 additions & 2 deletions

File tree

src/components/movement-speed-modifier/movement-speed-modifier.js

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,21 @@ const movementSpeedModifierComponent = {
3232
this.currentAppliedMultiplier = 1; // Set to 1 initially as the boost needs to be triggered
3333
this.axisX = 0;
3434
this.axisY = 0;
35+
this.touchCount = 0;
3536
this.timeSinceLastLinePattern = 0;
3637
// Bind handlers to this component instance, and store the references so listeners can be removed later
3738
this.onThumbstickDown = this.onThumbstickDown.bind(this);
3839
this.onAxisMove = this.onAxisMove.bind(this);
3940
this.onKeyDown = this.onKeyDown.bind(this);
4041
this.onKeyUp = this.onKeyUp.bind(this);
4142
this.onWindowBlur = this.onWindowBlur.bind(this);
43+
this.onTouchStart = this.onTouchStart.bind(this);
44+
this.onTouchEnd = this.onTouchEnd.bind(this);
4245
// Set up input listeners
4346
this.leftControllerEl = this.data.leftController;
4447
this.addLeftControllerListeners();
4548
this.addKeyboardListeners();
49+
this.addTouchListeners();
4650
// Create the speed line container and line elements
4751
this.lineContainer = null;
4852
this.lineElements = [];
@@ -97,6 +101,7 @@ const movementSpeedModifierComponent = {
97101
remove: function () {
98102
this.removeLeftControllerListeners();
99103
this.removeKeyboardListeners();
104+
this.removeTouchListeners();
100105
this.resetSpeeds();
101106
this.removeSpeedLines();
102107
},
@@ -145,6 +150,28 @@ const movementSpeedModifierComponent = {
145150
window.removeEventListener("blur", this.onWindowBlur);
146151
},
147152

153+
/**
154+
* Add touch listeners
155+
*
156+
* Adds touch listeners to track the number of fingers on the screen for mobile movement.
157+
*/
158+
addTouchListeners: function () {
159+
window.addEventListener("touchstart", this.onTouchStart);
160+
window.addEventListener("touchend", this.onTouchEnd);
161+
window.addEventListener("touchcancel", this.onTouchEnd);
162+
},
163+
164+
/**
165+
* Remove touch listeners
166+
*
167+
* Removes the touch listeners used for mobile movement tracking.
168+
*/
169+
removeTouchListeners: function () {
170+
window.removeEventListener("touchstart", this.onTouchStart);
171+
window.removeEventListener("touchend", this.onTouchEnd);
172+
window.removeEventListener("touchcancel", this.onTouchEnd);
173+
},
174+
148175
/**
149176
* Thumbstick down handler
150177
*
@@ -227,12 +254,37 @@ const movementSpeedModifierComponent = {
227254
* Clears held keyboard sprint state if the window loses focus.
228255
*/
229256
onWindowBlur: function () {
230-
if (!this.keyboardShiftActive && !this.keyboardForwardActive) return;
257+
if (!this.keyboardShiftActive && !this.keyboardForwardActive && this.touchCount === 0) return;
231258
this.keyboardShiftActive = false;
232259
this.keyboardForwardActive = false;
260+
this.touchCount = 0;
233261
this.applySpeedMultiplier();
234262
},
235263

264+
/**
265+
* Touch start handler
266+
*
267+
* Tracks the number of fingers on the screen to detect forward or backward movement on mobile.
268+
*
269+
* @param {TouchEvent} event The touch event.
270+
*/
271+
onTouchStart: function (event) {
272+
this.touchCount = event.touches.length;
273+
this.syncSpeedLinesVisibility();
274+
},
275+
276+
/**
277+
* Touch end handler
278+
*
279+
* Tracks the number of fingers on the screen to detect when movement stops on mobile.
280+
*
281+
* @param {TouchEvent} event The touch event.
282+
*/
283+
onTouchEnd: function (event) {
284+
this.touchCount = event.touches.length;
285+
this.syncSpeedLinesVisibility();
286+
},
287+
236288
/**
237289
* Is joystick forward
238290
*
@@ -287,7 +339,8 @@ const movementSpeedModifierComponent = {
287339
const keyboardMovingForward = this.keyboardForwardActive;
288340
const joystickMovingForward = this.isJoystickForward();
289341
const armSwingMovingForward = armSwingMovement && armSwingMovement.moving && !armSwingMovement.reverseHeld;
290-
return keyboardMovingForward || joystickMovingForward || armSwingMovingForward;
342+
const touchMovingForward = this.touchCount === 1; // 1 finger = forward, 2 = backward
343+
return keyboardMovingForward || joystickMovingForward || armSwingMovingForward || touchMovingForward;
291344
},
292345

293346
/**

0 commit comments

Comments
 (0)