-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinput.js
More file actions
766 lines (656 loc) · 26.6 KB
/
input.js
File metadata and controls
766 lines (656 loc) · 26.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
export class InputManager {
constructor(player, gameController) {
this.player = player;
this.gameController = gameController;
this.keys = {
left: false,
right: false,
jump: false,
slide: false
};
this.enabled = true;
// Store event listener references for proper cleanup
this.eventListeners = {
keydown: (event) => this.onKeyDown(event),
keyup: (event) => this.onKeyUp(event),
resize: () => this.onWindowResize(),
documentClick: null
};
this.setupEventListeners();
}
setupEventListeners() {
document.addEventListener('keydown', this.eventListeners.keydown);
document.addEventListener('keyup', this.eventListeners.keyup);
window.addEventListener('resize', this.eventListeners.resize, false);
// Add universal restart support
this.setupUniversalRestart();
}
onKeyDown(event) {
// Don't process any input if disabled
if (!this.enabled) return;
// Don't interfere with form inputs
if (event.target && (event.target.tagName === 'INPUT' || event.target.tagName === 'TEXTAREA')) {
return;
}
// Check global flag for player registration
if (window.playerRegistrationActive) {
return;
}
// Check if player registration form is visible
const registrationForm = document.getElementById('player-registration');
if (registrationForm && registrationForm.style.display !== 'none' && registrationForm.style.opacity !== '0') {
return;
}
// Only process game controls if game is active
if (!this.gameController.isGameActive()) return;
// Prevent default behavior for game keys only during gameplay
if (['ArrowLeft', 'ArrowRight', 'ArrowUp', 'Space'].includes(event.code)) {
event.preventDefault();
}
switch (event.code) {
case 'ArrowLeft':
case 'KeyA':
if (!this.keys.left) {
this.keys.left = true;
this.player.moveLeft();
}
break;
case 'ArrowRight':
case 'KeyD':
if (!this.keys.right) {
this.keys.right = true;
this.player.moveRight();
}
break;
case 'Space':
case 'ArrowUp':
case 'KeyW':
if (!this.keys.jump) {
// Prevent jumping while flying
if (this.gameController.powerUpManager && this.gameController.powerUpManager.getFlyingStatus()) {
break;
}
this.keys.jump = true;
this.player.jump();
}
break;
case 'ArrowDown':
case 'KeyS':
if (!this.keys.slide) {
// Prevent sliding while flying
if (this.gameController.powerUpManager && this.gameController.powerUpManager.getFlyingStatus()) {
break;
}
this.keys.slide = true;
this.player.slide();
}
break;
}
}
onKeyUp(event) {
// Don't process any input if disabled
if (!this.enabled) return;
// Don't interfere with form inputs
if (event.target && (event.target.tagName === 'INPUT' || event.target.tagName === 'TEXTAREA')) {
return;
}
switch (event.code) {
case 'ArrowLeft':
case 'KeyA':
this.keys.left = false;
break;
case 'ArrowRight':
case 'KeyD':
this.keys.right = false;
break;
case 'Space':
case 'ArrowUp':
case 'KeyW':
this.keys.jump = false;
break;
case 'ArrowDown':
case 'KeyS':
this.keys.slide = false;
break;
}
}
onWindowResize() {
this.gameController.handleWindowResize();
}
// Touch/Mobile support (optional)
setupMobileControls() {
// Create virtual buttons for mobile
this.createMobileUI();
// Add fallback for production builds - retry if controls don't appear
setTimeout(() => {
if (!document.querySelector('.mobile-gamepad-container')) {
this.createMobileUI();
}
}, 1000);
}
createMobileUI() {
// Prevent creating duplicate mobile UI
if (document.querySelector('.mobile-swipe-container')) {
return;
}
// Enhanced mobile detection for production builds
const isMobile = /Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ||
'ontouchstart' in window ||
navigator.maxTouchPoints > 0 ||
window.innerWidth <= 768;
// More flexible desktop detection - don't block mobile UI if uncertain
const isDesktop = window.innerWidth > 1024 &&
!('ontouchstart' in window) &&
navigator.maxTouchPoints === 0;
// Only skip mobile UI if we're certain it's desktop
if (!isMobile && isDesktop) return;
// Create main swipe container
const swipeContainer = document.createElement('div');
swipeContainer.className = 'mobile-swipe-container';
Object.assign(swipeContainer.style, {
position: 'fixed',
top: '0',
left: '0',
right: '0',
bottom: '0',
zIndex: '999',
pointerEvents: 'auto',
background: 'transparent'
});
// Mobile-specific debouncing (separate from desktop keyboard state)
this.mobileButtons = {
left: false,
right: false,
jump: false,
slide: false
};
// Setup swipe gesture detection
this.setupSwipeGestures(swipeContainer);
document.body.appendChild(swipeContainer);
// Add mobile-specific CSS only once
this.addMobileSwipeStyles();
// Add body class to prevent pull-to-refresh when mobile controls are active
document.body.classList.add('mobile-swipe-active');
// Monitor game state to manage scroll prevention
this.setupGameStateMonitoring();
}
setupGameStateMonitoring() {
// Flag to control monitoring loop
this.isMonitoringGameState = true;
// Monitor game state to enable/disable scroll prevention
const monitorGameState = () => {
if (!this.isMonitoringGameState) {
return; // Stop monitoring if flag is false
}
if (this.gameController && this.gameController.isGameActive) {
if (this.gameController.isGameActive()) {
// Game is active - ensure scroll prevention is enabled
document.body.classList.add('mobile-swipe-active');
} else {
// Game is inactive - allow normal scrolling
document.body.classList.remove('mobile-swipe-active');
}
}
// Continue monitoring only if still needed
if (this.isMonitoringGameState) {
this.gameStateMonitorId = requestAnimationFrame(monitorGameState);
}
};
// Start monitoring
this.gameStateMonitorId = requestAnimationFrame(monitorGameState);
}
setupSwipeGestures(container) {
// Swipe detection variables
let fingerDownPos = { x: 0, y: 0 };
let fingerUpPos = { x: 0, y: 0 };
const SWIPE_THRESHOLD = 50; // Minimum distance for swipe detection
const TOP_EDGE_THRESHOLD = 50; // Prevent swipes starting near top edge
const detectSwipeAfterRelease = false; // Detect during movement for responsiveness
// Helper functions for swipe detection
const getVerticalMoveValue = () => {
return Math.abs(fingerDownPos.y - fingerUpPos.y);
};
const getHorizontalMoveValue = () => {
return Math.abs(fingerDownPos.x - fingerUpPos.x);
};
const checkSwipe = () => {
const verticalMove = getVerticalMoveValue();
const horizontalMove = getHorizontalMoveValue();
// Check for vertical swipes (up/down)
if (verticalMove > SWIPE_THRESHOLD && verticalMove > horizontalMove) {
// Prevent swipe down if it started near the top edge (to avoid browser refresh)
const isSwipeDown = fingerDownPos.y - fingerUpPos.y > 0;
const startedNearTop = fingerUpPos.y < TOP_EDGE_THRESHOLD;
if (isSwipeDown && startedNearTop) {
// Ignore swipe down that started near top edge
return;
}
if (isSwipeDown) {
// Swipe down - Slide
this.onSwipeDown();
} else {
// Swipe up - Jump
this.onSwipeUp();
}
fingerUpPos = { ...fingerDownPos };
}
// Check for horizontal swipes (left/right)
else if (horizontalMove > SWIPE_THRESHOLD && horizontalMove > verticalMove) {
if (fingerDownPos.x - fingerUpPos.x > 0) {
// Swipe right
this.onSwipeRight();
} else {
// Swipe left
this.onSwipeLeft();
}
fingerUpPos = { ...fingerDownPos };
}
};
// Store touch event handlers for cleanup
this.eventListeners.touchstart = (e) => {
const touch = e.touches[0];
fingerUpPos = { x: touch.clientX, y: touch.clientY };
fingerDownPos = { x: touch.clientX, y: touch.clientY };
// Prevent default behavior to avoid browser gestures
e.preventDefault();
};
this.eventListeners.touchmove = (e) => {
if (!detectSwipeAfterRelease) {
const touch = e.touches[0];
fingerDownPos = { x: touch.clientX, y: touch.clientY };
checkSwipe();
}
// Prevent default to stop browser pull-to-refresh and scrolling
e.preventDefault();
};
this.eventListeners.touchend = (e) => {
if (detectSwipeAfterRelease) {
checkSwipe();
}
// Prevent default to avoid any delayed browser gestures
e.preventDefault();
};
// Store mouse event handlers for cleanup
let isMouseDown = false;
this.eventListeners.mousedown = (e) => {
isMouseDown = true;
fingerUpPos = { x: e.clientX, y: e.clientY };
fingerDownPos = { x: e.clientX, y: e.clientY };
};
this.eventListeners.mousemove = (e) => {
if (isMouseDown && !detectSwipeAfterRelease) {
fingerDownPos = { x: e.clientX, y: e.clientY };
checkSwipe();
}
};
this.eventListeners.mouseup = (e) => {
if (isMouseDown) {
isMouseDown = false;
if (detectSwipeAfterRelease) {
checkSwipe();
}
}
};
// Add event listeners to container
container.addEventListener('touchstart', this.eventListeners.touchstart, { passive: false });
container.addEventListener('touchmove', this.eventListeners.touchmove, { passive: false });
container.addEventListener('touchend', this.eventListeners.touchend, { passive: false });
container.addEventListener('mousedown', this.eventListeners.mousedown);
container.addEventListener('mousemove', this.eventListeners.mousemove);
container.addEventListener('mouseup', this.eventListeners.mouseup);
// Store container reference for cleanup
this.swipeContainer = container;
}
// Swipe callback functions
onSwipeUp() {
if (this.gameController.isGameActive()) {
// Prevent jumping while flying
if (this.gameController.powerUpManager && this.gameController.powerUpManager.getFlyingStatus()) {
return;
}
// Allow jump if:
// 1. Not currently debounced, OR
// 2. Player can double jump and is already jumping (for wind power)
const canJump = !this.mobileButtons.jump ||
(this.player.canDoubleJump && this.player.isJumping && !this.player.hasDoubleJumped);
if (canJump) {
this.mobileButtons.jump = true;
this.player.jump();
// Haptic feedback
if (navigator.vibrate) {
navigator.vibrate(50);
}
setTimeout(() => {
this.mobileButtons.jump = false;
}, 300);
}
}
}
onSwipeDown() {
if (!this.mobileButtons.slide && this.gameController.isGameActive()) {
// Prevent sliding while flying
if (this.gameController.powerUpManager && this.gameController.powerUpManager.getFlyingStatus()) {
return;
}
this.mobileButtons.slide = true;
this.player.slide();
// Haptic feedback
if (navigator.vibrate) {
navigator.vibrate(50);
}
setTimeout(() => {
this.mobileButtons.slide = false;
}, 300);
}
}
onSwipeLeft() {
if (!this.mobileButtons.left && this.gameController.isGameActive()) {
this.mobileButtons.left = true;
this.player.moveLeft();
// Haptic feedback
if (navigator.vibrate) {
navigator.vibrate(30);
}
setTimeout(() => {
this.mobileButtons.left = false;
}, 200);
}
}
onSwipeRight() {
if (!this.mobileButtons.right && this.gameController.isGameActive()) {
this.mobileButtons.right = true;
this.player.moveRight();
// Haptic feedback
if (navigator.vibrate) {
navigator.vibrate(30);
}
setTimeout(() => {
this.mobileButtons.right = false;
}, 200);
}
}
addMobileSwipeStyles() {
// Add CSS only for mobile swipe controls (never on desktop)
if (!document.querySelector('#mobile-swipe-styles')) {
const style = document.createElement('style');
style.id = 'mobile-swipe-styles';
style.textContent = `
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.mobile-swipe-container {
touch-action: none;
overscroll-behavior: none;
-webkit-overscroll-behavior: none;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Additional body styles to prevent pull-to-refresh */
body.mobile-swipe-active {
touch-action: none;
overscroll-behavior: none;
-webkit-overscroll-behavior: none;
overflow: hidden;
position: fixed;
width: 100%;
height: 100%;
}
/* Responsive design for mobile swipe container */
@media (max-width: 480px) {
.mobile-swipe-container {
touch-action: none;
overscroll-behavior: none;
}
}
@media (max-width: 320px) {
.mobile-swipe-container {
touch-action: none;
overscroll-behavior: none;
}
}
/* Prevent scrolling on mobile when swiping */
.mobile-swipe-active {
overflow: hidden !important;
position: fixed !important;
width: 100% !important;
height: 100% !important;
}
`;
document.head.appendChild(style);
}
}
setupUniversalRestart() {
// Store the document click listener reference for cleanup
this.eventListeners.documentClick = (event) => {
// Don't handle clicks during player registration
if (window.playerRegistrationActive) {
return;
}
if (!this.gameController.isGameActive()) {
// Check if clicking on interactive elements that should NOT restart the game
const isInteractiveElement = this.isInteractiveElement(event.target);
// Only restart if clicking on non-interactive areas
const gameOverElement = this.gameController.uiManager?.gameOverElement;
const canvas = this.gameController.renderer?.domElement;
if (!isInteractiveElement && canvas && event.target === canvas) {
// Only allow restart when clicking directly on the canvas (background)
event.preventDefault();
this.gameController.restartGame();
}
}
};
// Touch/click restart - but exclude interactive elements
document.addEventListener('click', this.eventListeners.documentClick);
// Remove touch restart for mobile - rely only on restart button
// This prevents conflicts with tab interactions on mobile
// Gamepad restart support
this.setupGamepadRestart();
}
isInteractiveElement(element) {
if (!element) return false;
// Check if the element or any parent is an interactive element
const interactiveSelectors = [
'button',
'input',
'select',
'textarea',
'a',
'[role="button"]',
'[role="tab"]',
'[tabindex]',
'.tab-header',
'.view-btn',
'.primary-button',
'.restart-button',
'.search-input',
'.filter-option',
'.leaderboard-entry'
];
// Check if current element matches
for (const selector of interactiveSelectors) {
if (element.matches && element.matches(selector)) {
return true;
}
}
// Check if any parent element matches
let parent = element.parentElement;
while (parent && parent !== document.body) {
for (const selector of interactiveSelectors) {
if (parent.matches && parent.matches(selector)) {
return true;
}
}
parent = parent.parentElement;
}
return false;
}
setupGamepadRestart() {
// Store original gameOver method for cleanup
if (this.gameController.gameOver && !this.originalGameOver) {
this.originalGameOver = this.gameController.gameOver.bind(this.gameController);
}
// Check for gamepad restart every frame when game is over
const checkGamepadRestart = () => {
if (!this.gameController.isGameActive()) {
const gamepads = navigator.getGamepads();
for (let i = 0; i < gamepads.length; i++) {
const gamepad = gamepads[i];
if (gamepad) {
// Check for common restart buttons: A, Start, Select, or any face button
if (gamepad.buttons[0]?.pressed || // A button (Xbox) / X button (PS)
gamepad.buttons[1]?.pressed || // B button (Xbox) / Circle button (PS)
gamepad.buttons[2]?.pressed || // X button (Xbox) / Square button (PS)
gamepad.buttons[3]?.pressed || // Y button (Xbox) / Triangle button (PS)
gamepad.buttons[9]?.pressed || // Start button
gamepad.buttons[8]?.pressed) { // Select/Back button
this.gameController.restartGame();
return;
}
}
}
}
// Continue checking if game is still over
if (!this.gameController.isGameActive()) {
this.gamepadRestartId = requestAnimationFrame(checkGamepadRestart);
}
};
// Override gameOver method to start gamepad checking
if (this.originalGameOver) {
this.gameController.gameOver = () => {
this.originalGameOver();
setTimeout(() => {
this.gamepadRestartId = requestAnimationFrame(checkGamepadRestart);
}, 100); // Small delay to avoid immediate restart
};
}
}
// Get current input state
getInputState() {
return { ...this.keys };
}
// Reset input state
reset() {
this.keys = {
left: false,
right: false,
jump: false,
slide: false
};
}
// Enable/disable input processing
enable() {
this.enabled = true;
}
disable() {
this.enabled = false;
// Reset any active keys when disabling
this.reset();
}
// Cleanup method to remove all event listeners and prevent accumulation
destroy() {
// Remove document and window event listeners
if (this.eventListeners.keydown) {
document.removeEventListener('keydown', this.eventListeners.keydown);
}
if (this.eventListeners.keyup) {
document.removeEventListener('keyup', this.eventListeners.keyup);
}
if (this.eventListeners.resize) {
window.removeEventListener('resize', this.eventListeners.resize, false);
}
if (this.eventListeners.documentClick) {
document.removeEventListener('click', this.eventListeners.documentClick);
}
// Remove swipe gesture event listeners from container
if (this.swipeContainer) {
if (this.eventListeners.touchstart) {
this.swipeContainer.removeEventListener('touchstart', this.eventListeners.touchstart);
}
if (this.eventListeners.touchmove) {
this.swipeContainer.removeEventListener('touchmove', this.eventListeners.touchmove);
}
if (this.eventListeners.touchend) {
this.swipeContainer.removeEventListener('touchend', this.eventListeners.touchend);
}
if (this.eventListeners.mousedown) {
this.swipeContainer.removeEventListener('mousedown', this.eventListeners.mousedown);
}
if (this.eventListeners.mousemove) {
this.swipeContainer.removeEventListener('mousemove', this.eventListeners.mousemove);
}
if (this.eventListeners.mouseup) {
this.swipeContainer.removeEventListener('mouseup', this.eventListeners.mouseup);
}
}
// Remove mobile UI elements
const mobileContainer = document.querySelector('.mobile-swipe-container');
if (mobileContainer) {
mobileContainer.remove();
}
// Remove mobile styles
const mobileStyles = document.querySelector('#mobile-swipe-styles');
if (mobileStyles) {
mobileStyles.remove();
}
// Clear event listener references
this.eventListeners = {
keydown: null,
keyup: null,
resize: null,
documentClick: null,
touchstart: null,
touchmove: null,
touchend: null,
mousedown: null,
mousemove: null,
mouseup: null
};
// Clear container reference
this.swipeContainer = null;
// Reset key states
this.keys = {
left: false,
right: false,
jump: false
};
// Reset mobile button states if they exist
if (this.mobileButtons) {
this.mobileButtons = {
left: false,
right: false,
jump: false
};
}
// Stop game state monitoring
this.isMonitoringGameState = false;
if (this.gameStateMonitorId) {
cancelAnimationFrame(this.gameStateMonitorId);
this.gameStateMonitorId = null;
}
// Stop gamepad restart monitoring
if (this.gamepadRestartId) {
cancelAnimationFrame(this.gamepadRestartId);
this.gamepadRestartId = null;
}
// Restore original gameOver method
if (this.originalGameOver && this.gameController) {
this.gameController.gameOver = this.originalGameOver;
this.originalGameOver = null;
}
// Remove mobile swipe body class if it exists
document.body.classList.remove('mobile-swipe-active');
// Disable input processing
this.enabled = false;
}
}