Skip to content

Commit 75fe95c

Browse files
Merge pull request #18 from MakingSpiderSense/bugfix/movement-speed-modifier/118-show-speed-lines-on-mobile
Add touch event listeners for mobile movement tracking in speed modifier component
2 parents 43236ea + f292227 commit 75fe95c

1 file changed

Lines changed: 53 additions & 1 deletion

File tree

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

Lines changed: 53 additions & 1 deletion
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
*
@@ -233,6 +260,30 @@ const movementSpeedModifierComponent = {
233260
this.applySpeedMultiplier();
234261
},
235262

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

293345
/**

0 commit comments

Comments
 (0)