Repository navigation
Expand file tree
/
Copy pathgamepad.js
More file actions
637 lines (565 loc) · 22.4 KB
/
Copy pathgamepad.js
File metadata and controls
637 lines (565 loc) · 22.4 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
/*
* Copyright 2026 Mark Longo
* Gamepad & Controller Support for Dungeon Stomp Three.js
*/
import * as THREE from 'three';
export const GP_BUTTONS = Object.freeze({
A: 0, // Bottom face button (Xbox A, PS Cross, Switch B) - Jump / Confirm
B: 1, // Right face button (Xbox B, PS Circle, Switch A) - Crouch / Cancel / Back
X: 2, // Left face button (Xbox X, PS Square, Switch Y) - Interact / Open Door / Chest
Y: 3, // Top face button (Xbox Y, PS Triangle, Switch X) - Next Weapon / Toggle
LB: 4, // Left bumper / shoulder (L1) - Prev Weapon / Fly Down
RB: 5, // Right bumper / shoulder (R1) - Next Weapon / Fly Up
LT: 6, // Left trigger (L2) - Sprint (analog) / Fly Down
RT: 7, // Right trigger (R2) - Primary Attack / Cast Spell (analog)
BACK: 8, // Back / View / Select - God Mode Cheat / Options
START: 9, // Start / Menu / Options - Pause / Resume Menu
L3: 10, // Left stick press - Sprint toggle
R3: 11, // Right stick press - Center camera pitch / Fly mode
DPAD_UP: 12, // D-pad Up - Toggle Music / Menu Up
DPAD_DOWN: 13, // D-pad Down - Toggle Normal Maps / Menu Down
DPAD_LEFT: 14, // D-pad Left - Prev Weapon / Menu Left
DPAD_RIGHT: 15 // D-pad Right - Next Weapon / Menu Right
});
export const GP_AXES = Object.freeze({
LEFT_X: 0,
LEFT_Y: 1,
RIGHT_X: 2,
RIGHT_Y: 3
});
// Tuned controller configuration
const DEFAULT_LOOK_SENSITIVITY = 2.8;
const DEFAULT_DEADZONE = 0.15;
const DEFAULT_LOOK_EXPONENT = 1.8;
const MAX_PITCH = Math.PI * 0.47; // ~85 degrees up or down
const _euler = new THREE.Euler(0, 0, 0, 'YXZ');
let lookSensitivity = DEFAULT_LOOK_SENSITIVITY;
let invertY = false;
let vibrationEnabled = true;
// Active gamepad state
let activeGamepadIndex = -1;
let lastActiveGamepadName = '';
const prevButtons = new Map(); // buttonIndex -> boolean
let l3SprintToggled = false;
// Menu navigation state
let menuNavTimer = 0;
let menuNavLastDir = 0; // -1 = up, 1 = down
let menuHorizTimer = 0;
let menuHorizLastDir = 0; // -1 = left, 1 = right
const MENU_REPEAT_DELAY = 0.32;
const MENU_REPEAT_INTERVAL = 0.14;
let currentFocusElement = null;
// Helpers to get connected gamepads
export function getConnectedGamepads() {
if (!navigator.getGamepads) return [];
try {
const raw = navigator.getGamepads();
const list = [];
for (let i = 0; i < raw.length; i++) {
if (raw[i] && raw[i].connected) {
list.push(raw[i]);
}
}
return list;
} catch {
return [];
}
}
export function getPrimaryGamepad() {
const gamepads = getConnectedGamepads();
if (gamepads.length === 0) return null;
if (activeGamepadIndex !== -1) {
const found = gamepads.find(g => g.index === activeGamepadIndex);
if (found) return found;
}
// Default to the first connected gamepad
return gamepads[0];
}
export function formatGamepadName(rawId) {
if (!rawId) return 'Controller';
const lower = rawId.toLowerCase();
if (lower.includes('xbox') || lower.includes('xinput')) {
return 'Xbox Controller';
}
if (lower.includes('dualsense') || lower.includes('ps5')) {
return 'PlayStation DualSense';
}
if (lower.includes('dualshock') || lower.includes('wireless controller') || lower.includes('054c')) {
return 'PlayStation Controller';
}
if (lower.includes('pro controller') || lower.includes('switch') || lower.includes('joy-con')) {
return 'Nintendo Switch Controller';
}
// Clean up generic vendor IDs like "(STANDARD GAMEPAD Vendor: 045e Product: 028e)"
const clean = rawId.split('(')[0].trim();
return clean || 'Gamepad';
}
function showControllerToast(message) {
let toast = document.getElementById('controller-toast');
if (!toast) {
toast = document.createElement('div');
toast.id = 'controller-toast';
document.body.appendChild(toast);
}
toast.textContent = message;
toast.classList.add('show');
clearTimeout(toast.__timer);
toast.__timer = setTimeout(() => {
toast.classList.remove('show');
}, 3200);
}
function updateGamepadDisplay() {
const el = document.getElementById('gamepad-display');
const gp = getPrimaryGamepad();
if (!el) return;
if (gp) {
const name = formatGamepadName(gp.id);
el.textContent = name;
el.style.color = '#00ff88';
} else {
el.textContent = 'None';
el.style.color = '#64748b';
}
}
// Analog deadzone and response curve calculation
export function applyStickDeadzoneAndCurve(rawX, rawY, deadzone = DEFAULT_DEADZONE, exponent = DEFAULT_LOOK_EXPONENT) {
const mag = Math.hypot(rawX, rawY);
if (mag <= deadzone) {
return { x: 0, y: 0, mag: 0 };
}
const normalizedMag = Math.min(1.0, (mag - deadzone) / (1.0 - deadzone));
const curvedMag = Math.pow(normalizedMag, exponent);
const scale = curvedMag / mag;
return {
x: rawX * scale,
y: rawY * scale,
mag: curvedMag
};
}
function isButtonPressed(gp, index, threshold = 0.5) {
if (!gp || !gp.buttons || !gp.buttons[index]) return false;
const btn = gp.buttons[index];
if (typeof btn === 'number') return btn > threshold;
return Boolean(btn.pressed || btn.value > threshold);
}
function getButtonValue(gp, index) {
if (!gp || !gp.buttons || !gp.buttons[index]) return 0;
const btn = gp.buttons[index];
if (typeof btn === 'number') return btn;
return typeof btn.value === 'number' ? btn.value : (btn.pressed ? 1.0 : 0.0);
}
// Haptic feedback / Rumble
export function triggerGamepadVibration(duration = 100, weakMagnitude = 0.5, strongMagnitude = 0.5) {
if (!vibrationEnabled) return;
const gamepads = getConnectedGamepads();
for (const gp of gamepads) {
if (!gp || !gp.connected) continue;
const actuator = gp.vibrationActuator || (gp.hapticActuators && gp.hapticActuators[0]);
if (actuator) {
if (typeof actuator.playEffect === 'function') {
actuator.playEffect('dual-rumble', {
startDelay: 0,
duration: Math.max(10, Math.min(2000, duration)),
weakMagnitude: Math.max(0, Math.min(1, weakMagnitude)),
strongMagnitude: Math.max(0, Math.min(1, strongMagnitude))
}).catch(() => {});
} else if (typeof actuator.pulse === 'function') {
actuator.pulse(Math.max(0, Math.min(1, (weakMagnitude + strongMagnitude) / 2)), duration).catch(() => {});
}
}
}
}
// Camera look logic for Three.js camera
export function applyGamepadLook(camera, delta, lookX, lookY) {
if (!camera || (Math.abs(lookX) < 1e-4 && Math.abs(lookY) < 1e-4)) return;
_euler.setFromQuaternion(camera.quaternion);
// Yaw: lookX > 0 (pushing right) turns view right (decreasing Euler Y)
_euler.y -= lookX * lookSensitivity * delta;
// Pitch: lookY > 0 (pushing down) tilts view down
const pitchDelta = (invertY ? lookY : -lookY) * lookSensitivity * delta;
_euler.x += pitchDelta;
// Clamp pitch between -MAX_PITCH and +MAX_PITCH to avoid inverted camera
_euler.x = Math.max(-MAX_PITCH, Math.min(MAX_PITCH, _euler.x));
camera.quaternion.setFromEuler(_euler);
}
export function recenterCameraPitch(camera) {
if (!camera) return;
_euler.setFromQuaternion(camera.quaternion);
_euler.x = 0;
camera.quaternion.setFromEuler(_euler);
}
// Settings getters & setters
export function setGamepadSensitivity(val) {
if (Number.isFinite(val) && val > 0.1 && val <= 10.0) {
lookSensitivity = val;
}
}
export function getGamepadSensitivity() {
return lookSensitivity;
}
export function setGamepadInvertY(val) {
invertY = Boolean(val);
}
export function getGamepadInvertY() {
return invertY;
}
export function setGamepadVibrationEnabled(val) {
vibrationEnabled = Boolean(val);
}
export function isGamepadVibrationEnabled() {
return vibrationEnabled;
}
// Menu Navigation via Controller
function getActiveMenuView() {
const gen = document.getElementById('menu-view-generator');
if (gen && gen.style.display !== 'none') return gen;
const opt = document.getElementById('menu-view-options');
if (opt && opt.style.display !== 'none') return opt;
return document.getElementById('menu-view-main');
}
function getFocusableElements(container) {
if (!container) return [];
const elements = Array.from(
container.querySelectorAll('button:not(:disabled), input:not(:disabled), select:not(:disabled)')
);
return elements.filter(el => el.offsetParent !== null && !el.disabled);
}
export function clearMenuFocus() {
if (currentFocusElement) {
currentFocusElement.classList.remove('gamepad-focused');
const parentLabel = currentFocusElement.closest('.menu-checkbox-label');
if (parentLabel) parentLabel.classList.remove('gamepad-focused');
currentFocusElement = null;
}
}
function setMenuFocus(el, playSoundFn) {
clearMenuFocus();
if (!el) return;
currentFocusElement = el;
el.classList.add('gamepad-focused');
const parentLabel = el.closest('.menu-checkbox-label');
if (parentLabel) parentLabel.classList.add('gamepad-focused');
try {
el.focus({ preventScroll: true });
} catch {}
if (playSoundFn) playSoundFn('buttonselect111.wav', 0.35);
}
export function updateGamepadMenuNavigation(delta, gp, playSoundFn) {
if (!gp) return;
const activeView = getActiveMenuView();
if (!activeView || activeView.style.display === 'none') {
clearMenuFocus();
return;
}
const items = getFocusableElements(activeView);
if (items.length === 0) {
clearMenuFocus();
return;
}
let currentIndex = currentFocusElement ? items.indexOf(currentFocusElement) : -1;
if (currentIndex === -1) {
// Prioritize Resume button if present and enabled
const resumeBtn = activeView.querySelector('#menu-btn-resume:not(:disabled)');
if (resumeBtn && items.includes(resumeBtn)) {
currentIndex = items.indexOf(resumeBtn);
} else {
currentIndex = 0;
}
setMenuFocus(items[currentIndex], null);
}
// Read Left Stick and D-Pad for navigation
const stickY = gp.axes && gp.axes[1] ? gp.axes[1] : 0;
const stickX = gp.axes && gp.axes[0] ? gp.axes[0] : 0;
const dpadUp = isButtonPressed(gp, GP_BUTTONS.DPAD_UP);
const dpadDown = isButtonPressed(gp, GP_BUTTONS.DPAD_DOWN);
const dpadLeft = isButtonPressed(gp, GP_BUTTONS.DPAD_LEFT);
const dpadRight = isButtonPressed(gp, GP_BUTTONS.DPAD_RIGHT);
// Vertical navigation (Up / Down)
let vertDir = 0;
if (dpadUp || stickY < -0.45) vertDir = -1;
else if (dpadDown || stickY > 0.45) vertDir = 1;
if (vertDir !== 0) {
if (menuNavLastDir !== vertDir) {
menuNavLastDir = vertDir;
menuNavTimer = MENU_REPEAT_DELAY;
currentIndex = (currentIndex + vertDir + items.length) % items.length;
setMenuFocus(items[currentIndex], playSoundFn);
} else {
menuNavTimer -= delta;
if (menuNavTimer <= 0) {
menuNavTimer = MENU_REPEAT_INTERVAL;
currentIndex = (currentIndex + vertDir + items.length) % items.length;
setMenuFocus(items[currentIndex], playSoundFn);
}
}
} else {
menuNavLastDir = 0;
menuNavTimer = 0;
}
// Horizontal adjustment (Left / Right for sliders and checkboxes)
let horizDir = 0;
if (dpadLeft || stickX < -0.45) horizDir = -1;
else if (dpadRight || stickX > 0.45) horizDir = 1;
if (horizDir !== 0) {
if (menuHorizLastDir !== horizDir) {
menuHorizLastDir = horizDir;
menuHorizTimer = MENU_REPEAT_DELAY;
adjustFocusedItem(currentFocusElement, horizDir, playSoundFn);
} else {
menuHorizTimer -= delta;
if (menuHorizTimer <= 0) {
menuHorizTimer = MENU_REPEAT_INTERVAL;
adjustFocusedItem(currentFocusElement, horizDir, playSoundFn);
}
}
} else {
menuHorizLastDir = 0;
menuHorizTimer = 0;
}
// A Button (Select / Confirm)
const justA = isButtonJustPressed(gp, GP_BUTTONS.A);
if (justA && currentFocusElement) {
triggerGamepadVibration(60, 0.3, 0.1);
if (currentFocusElement.type === 'checkbox') {
currentFocusElement.checked = !currentFocusElement.checked;
currentFocusElement.dispatchEvent(new Event('change', { bubbles: true }));
if (playSoundFn) playSoundFn('buttonselect111.wav', 0.4);
} else if (currentFocusElement.tagName === 'BUTTON') {
currentFocusElement.click();
} else {
currentFocusElement.click();
}
}
// B Button (Back / Cancel / Resume)
const justB = isButtonJustPressed(gp, GP_BUTTONS.B);
if (justB) {
triggerGamepadVibration(50, 0.2, 0.1);
const backBtn = activeView.querySelector('button[id$="-back"]');
if (backBtn) {
backBtn.click();
} else {
const resumeBtn = document.getElementById('menu-btn-resume');
if (resumeBtn && !resumeBtn.disabled) {
resumeBtn.click();
}
}
}
// Start / Menu Button (Resume from main menu)
const justStart = isButtonJustPressed(gp, GP_BUTTONS.START);
if (justStart) {
const resumeBtn = document.getElementById('menu-btn-resume');
if (resumeBtn && !resumeBtn.disabled) {
triggerGamepadVibration(50, 0.2, 0.1);
resumeBtn.click();
}
}
}
function adjustFocusedItem(el, dir, playSoundFn) {
if (!el) return;
if (el.type === 'range') {
const min = parseFloat(el.min) || 0;
const max = parseFloat(el.max) || 100;
const step = parseFloat(el.step) || 1;
let val = parseFloat(el.value) || 0;
val = Math.max(min, Math.min(max, val + dir * step));
// Avoid precision floating artifacts
const decimals = (el.step && el.step.includes('.')) ? el.step.split('.')[1].length : 0;
el.value = decimals > 0 ? val.toFixed(decimals) : String(Math.round(val));
el.dispatchEvent(new Event('input', { bubbles: true }));
el.dispatchEvent(new Event('change', { bubbles: true }));
if (playSoundFn) playSoundFn('buttonselect111.wav', 0.25);
} else if (el.type === 'checkbox') {
el.checked = !el.checked;
el.dispatchEvent(new Event('change', { bubbles: true }));
if (playSoundFn) playSoundFn('buttonselect111.wav', 0.35);
}
}
function isButtonJustPressed(gp, buttonIndex) {
const isDown = isButtonPressed(gp, buttonIndex);
const wasDown = prevButtons.get(buttonIndex) || false;
return isDown && !wasDown;
}
// Main Gamepad update loop (called each frame)
export function pollGamepad(delta, isPaused, isFlyMode, playSoundFn) {
const gp = getPrimaryGamepad();
// Check for auto-switching to newly active gamepad
const allGps = getConnectedGamepads();
if (allGps.length > 0) {
for (const candidate of allGps) {
// Check if user is touching buttons or sticks on another controller
let active = false;
for (let i = 0; i < candidate.buttons.length; i++) {
if (isButtonPressed(candidate, i)) { active = true; break; }
}
if (!active && candidate.axes) {
for (let a = 0; a < candidate.axes.length; a++) {
if (Math.abs(candidate.axes[a]) > 0.35) { active = true; break; }
}
}
if (active && candidate.index !== activeGamepadIndex) {
activeGamepadIndex = candidate.index;
const newName = formatGamepadName(candidate.id);
if (newName !== lastActiveGamepadName) {
lastActiveGamepadName = newName;
showControllerToast(`🎮 ${newName} Active`);
}
updateGamepadDisplay();
break;
}
}
}
if (gp) {
const currentName = formatGamepadName(gp.id);
if (currentName !== lastActiveGamepadName) {
lastActiveGamepadName = currentName;
updateGamepadDisplay();
showControllerToast(`🎮 ${currentName} Connected`);
}
}
if (!gp) {
prevButtons.clear();
updateGamepadDisplay();
return {
connected: false,
moveX: 0,
moveY: 0,
lookX: 0,
lookY: 0,
hasLook: false,
sprint: false,
jumpJustPressed: false,
flyUp: false,
flyDown: false,
attack: false,
interact: false,
cycleWeapon: 0,
toggleFlyMode: false,
toggleGodMode: false,
toggleMusic: false,
toggleNormalMaps: false,
pauseToggle: false,
recenterPitch: false
};
}
// In menu / paused: delegate to menu navigation
if (isPaused) {
updateGamepadMenuNavigation(delta, gp, playSoundFn);
// Record buttons for edge detection
for (let i = 0; i < 16; i++) {
prevButtons.set(i, isButtonPressed(gp, i));
}
return { connected: true, inMenu: true };
}
// Clear menu focus when entering gameplay
clearMenuFocus();
// 1. Left Stick (Movement) - Analog with 360 degree radial deadzone
const rawLeftX = gp.axes && gp.axes[GP_AXES.LEFT_X] !== undefined ? gp.axes[GP_AXES.LEFT_X] : 0;
const rawLeftY = gp.axes && gp.axes[GP_AXES.LEFT_Y] !== undefined ? gp.axes[GP_AXES.LEFT_Y] : 0;
const leftStick = applyStickDeadzoneAndCurve(rawLeftX, rawLeftY, DEFAULT_DEADZONE, 1.1);
// 2. Right Stick (Camera Look / Aim) - Non-linear response curve for precise aiming & fast spins
const rawRightX = gp.axes && gp.axes[GP_AXES.RIGHT_X] !== undefined ? gp.axes[GP_AXES.RIGHT_X] : 0;
const rawRightY = gp.axes && gp.axes[GP_AXES.RIGHT_Y] !== undefined ? gp.axes[GP_AXES.RIGHT_Y] : 0;
const rightStick = applyStickDeadzoneAndCurve(rawRightX, rawRightY, 0.12, DEFAULT_LOOK_EXPONENT);
// 3. Triggers
const ltValue = getButtonValue(gp, GP_BUTTONS.LT);
const rtValue = getButtonValue(gp, GP_BUTTONS.RT);
// Sprint: Hold LT (> 0.2) or click L3 (toggle)
const justL3 = isButtonJustPressed(gp, GP_BUTTONS.L3);
if (justL3) {
l3SprintToggled = !l3SprintToggled;
triggerGamepadVibration(40, 0.2, 0.05);
}
// Cancel L3 sprint toggle if player stops moving
if (l3SprintToggled && leftStick.mag < 0.05) {
l3SprintToggled = false;
}
const isSprinting = ltValue > 0.25 || l3SprintToggled;
// Jump: A button (just pressed)
const justA = isButtonJustPressed(gp, GP_BUTTONS.A);
if (justA && !isFlyMode) {
triggerGamepadVibration(40, 0.2, 0.05);
}
// Fly Mode controls
const flyUp = isFlyMode && (isButtonPressed(gp, GP_BUTTONS.A) || isButtonPressed(gp, GP_BUTTONS.RB));
const flyDown = isFlyMode && (isButtonPressed(gp, GP_BUTTONS.B) || isButtonPressed(gp, GP_BUTTONS.LB) || ltValue > 0.25);
// Primary Attack / Cast: RT (analog threshold > 0.4)
const isRTDown = rtValue > 0.4 || isButtonPressed(gp, GP_BUTTONS.RT);
const wasRTDown = prevButtons.get(GP_BUTTONS.RT) || false;
const justAttack = isRTDown && !wasRTDown;
// Interact (Open Door / Chest): X button (just pressed)
const justInteract = isButtonJustPressed(gp, GP_BUTTONS.X);
// Cycle Weapon: RB or D-Pad Right = Next (+1); LB or D-Pad Left = Prev (-1); Y = Next (+1)
let cycleWeapon = 0;
if (isButtonJustPressed(gp, GP_BUTTONS.RB) || isButtonJustPressed(gp, GP_BUTTONS.DPAD_RIGHT) || isButtonJustPressed(gp, GP_BUTTONS.Y)) {
cycleWeapon = 1;
triggerGamepadVibration(45, 0.25, 0.05);
} else if (isButtonJustPressed(gp, GP_BUTTONS.LB) || isButtonJustPressed(gp, GP_BUTTONS.DPAD_LEFT)) {
cycleWeapon = -1;
triggerGamepadVibration(45, 0.25, 0.05);
}
// Toggle Fly Mode: R3 click + held LT or B in cheat mode
const justR3 = isButtonJustPressed(gp, GP_BUTTONS.R3);
// D-Pad Up: Toggle Music
const justDpadUp = isButtonJustPressed(gp, GP_BUTTONS.DPAD_UP);
// D-Pad Down: Toggle Normal Maps
const justDpadDown = isButtonJustPressed(gp, GP_BUTTONS.DPAD_DOWN);
// Back / View Button: God Mode toggle
const justBack = isButtonJustPressed(gp, GP_BUTTONS.BACK);
// Start / Menu Button: Pause
const justStart = isButtonJustPressed(gp, GP_BUTTONS.START);
// Record button states for next frame edge detection
for (let i = 0; i < 16; i++) {
prevButtons.set(i, isButtonPressed(gp, i));
}
prevButtons.set(GP_BUTTONS.RT, isRTDown);
return {
connected: true,
inMenu: false,
// Movement analog values (-1 to +1)
moveX: leftStick.x,
moveY: leftStick.y,
moveMag: leftStick.mag,
// Camera look analog values (-1 to +1)
lookX: rightStick.x,
lookY: rightStick.y,
hasLook: rightStick.mag > 0,
// Action triggers
sprint: isSprinting,
jumpJustPressed: justA,
flyUp,
flyDown,
attack: justAttack,
interact: justInteract,
cycleWeapon,
toggleFlyMode: false, // Handled separately or with R3
recenterPitch: justR3,
toggleGodMode: justBack,
toggleMusic: justDpadUp,
toggleNormalMaps: justDpadDown,
pauseToggle: justStart
};
}
// Lifecycle listeners
export function initGamepadSystem() {
window.addEventListener('gamepadconnected', (e) => {
const gp = e.gamepad;
activeGamepadIndex = gp.index;
const name = formatGamepadName(gp.id);
lastActiveGamepadName = name;
showControllerToast(`🎮 ${name} Connected`);
updateGamepadDisplay();
triggerGamepadVibration(80, 0.4, 0.2);
});
window.addEventListener('gamepaddisconnected', (e) => {
const gp = e.gamepad;
if (activeGamepadIndex === gp.index) {
activeGamepadIndex = -1;
lastActiveGamepadName = '';
}
showControllerToast(`🎮 Controller Disconnected`);
updateGamepadDisplay();
prevButtons.clear();
});
// Initial check
setTimeout(updateGamepadDisplay, 100);
}