This repository has been archived by the owner on Mar 3, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (70 loc) · 2.32 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const typeMap = require('./mappings/typemap');
const GamepadderUtils = require('./utils/utils');
//Gamepadder Class
class Gamepadder {
//generate a new gamepad object
constructor({ index, id, buttons, axes, mapping, connected }) {
//set default parameters for controller
this.id = index;
this.name = id;
this.buttons = buttons;
this.axes = axes;
this.mappingType = mapping;
this.connected = connected;
this.previousButtons = {};
this.buttonPresses = {};
//get the specific controller type and it's button and stick mapping
const { name, map: { buttonMap, stickMap } } = this.setType();
//store display name
this.displayName = name;
//set default options for this controller
this.options = {
sensitivity: {
x: 5,
y: 5
},
deadzone: {
L: 0.25,
R: 0.25
},
buttonMap,
stickMap
};
}
//get the type of controller based on the gamepad id, number of buttons, and number of axes
setType() {
const test = GamepadderUtils.parseControllerID(this.name);
//use generic controller map by default
let type = typeMap[0];
typeMap.some((gamePadType) => {
//check for matching vendor and product ids
const vendorAndProductMatch = (gamePadType.vendor === test.vendor && gamePadType.product === test.product);
//check for matching name if no vendor and product ids are set
const noVendorNameMatch = (!test.vendor && !test.product && test.defaultName && gamePadType.name === test.defaultName);
if(vendorAndProductMatch || noVendorMatch) {
type = gamePadType;
return true;
}
});
//return the controller information
return type;
}
//check the current gamepad object for button presses and register those presses on our controller object
checkForButtonPress(gamepad) {
if(gamepad && gamepad.buttons.length) {
this.previousButtons = this.buttonPresses;
this.buttonPresses = {};
this.buttons.forEach((button, index) => {
const buttonName = this.options.buttonMap[index];
const pressed = gamepad.buttons[index].value > 0.5;
this.buttonPresses[buttonName] = pressed
});
}
return this;
}
}
//make the gamepadder class and its utils available
module.exports = {
Gamepadder,
GamepadderUtils
};