-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgamepad.js
More file actions
67 lines (58 loc) · 1.39 KB
/
gamepad.js
File metadata and controls
67 lines (58 loc) · 1.39 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
var gpIndex = -1;
var JOY_AXIS = 0;
var browser = navigator.userAgent.toLowerCase();
if(browser.indexOf('firefox') > -1) {
JOY_AXIS = 1;
}
setInterval (function() {
detectGamepad();
}, 1000);
function detectGamepad() {
if(gpIndex > -1)
return;
var pads = navigator.getGamepads();
for(var i in pads) {
if(pads[i] && pads[i].connected && pads[i].timestamp > 0) {
gpIndex = i;
window.requestAnimationFrame(checkGamepad);
break;
}
}
}
detectGamepad();
var resetPause = true;
var resetReset = true;
var resetCharge = true;
function checkGamepad(timestamp) {
var gp = navigator.getGamepads()[gpIndex];
var analogueLR = gp.axes[JOY_AXIS];
try {
if(analogueLR < -0.5)
Controls.handleLeft();
else if(analogueLR > 0.5)
Controls.handleRight();
else
Controls.handleStop();
if(gp.buttons[0].value === 1 || gp.buttons[2].value === 1) {
Controls.handleFire();
}
if(gp.buttons[1].value === 1 || gp.buttons[3].value === 1) {
if(resetCharge) {
Controls.handleCharge();
resetCharge = false;
}
}
else
resetCharge = true;
if(gp.buttons[9].value === 1) {
if(resetPause) {
Controls.handlePause();
resetPause = false;
}
}
else
resetPause = true;
}
catch(e) { console.log(e); }
window.requestAnimationFrame(checkGamepad);
}