-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjoystick.js
70 lines (56 loc) · 1.29 KB
/
joystick.js
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
//
// joystick wrapper for browser gamepad
//
// Copyright 2018, John Clark
//
// Released under the GNU General Public License
// https://www.gnu.org/licenses/gpl.html
//
const ps4 = {
button0: 2,
button1: 0,
button2: 1,
axis0: 0,
axis1: 1,
axis2: 2,
axis3: 3
};
export class Joystick
{
constructor() {
this.gamepad = undefined;
}
get button0() {
return this.get_button_pressed("button0");
};
get button1() {
return this.get_button_pressed("button1");
};
get button2() {
return this.get_button_pressed("button2");
};
get axis0() {
return this.get_axis_value("axis0");
};
get axis1() {
return this.get_axis_value("axis1");
};
get axis2() {
return this.get_axis_value("axis2");
};
get axis3() {
return this.get_axis_value("axis3");
};
get_button_pressed(btn) {
if(!this.gamepad) return false;
return this.gamepad.buttons[ps4[btn]].pressed;
}
get_axis_value(axis) {
if(!this.gamepad) return 128;
let val = Math.floor((this.gamepad.axes[ps4[axis]] * 128) + 128);
if(val < 0) val = 0;
if(val > 255) val = 255;
if((val > 122) && (val < 133)) val = 128;
return val;
}
}