-
Notifications
You must be signed in to change notification settings - Fork 0
/
Joypad.js
33 lines (25 loc) · 819 Bytes
/
Joypad.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
/**
* TODO
* Maybe add an implementation for a real hardware joystick, like: https://www.play-zone.ch/en/ps2-daumen-joystick-mit-select-button-breakout-board.html
*/
const J5 = require('johnny-five');
class Joypad {
constructor({ wsConnection, pins, player }) {
this.wsConnection = wsConnection;
this.pins = pins;
this.player = player;
this.connectPins();
}
connectPins() {
const buttons = ['up', 'bottom', 'left', 'right', 'fire'];
buttons.forEach((b) => {
new J5.Button({ pin: this.pins[b] })
.on('down', this.sendInput.bind(this, b, 'down'))
.on('up', this.sendInput.bind(this, b, 'up'));
});
}
sendInput(command, type) {
this.wsConnection.send(JSON.stringify({ player: this.player, command, type }));
}
};
module.exports.Joypad = Joypad;