-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
40 lines (33 loc) · 864 Bytes
/
controller.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
'use strict';
const fs = require('fs');
const path = require('path');
const ini = require('ini');
const jsnes = require('jsnes');
class Controller {
constructor(nes, num) {
const confFile = ini.parse(
fs.readFileSync(path.join(__dirname, './keybindings.conf'), 'utf8'));
this.mapping = confFile[`Player${num}`];
this.nes = nes;
this.num = num;
}
keyDown(codeStr) {
for (const key in this.mapping) {
if (this.mapping[key] === codeStr) {
this.nes.buttonDown(
this.num,
jsnes.Controller[`BUTTON_${key.toUpperCase()}`]);
}
}
}
keyUp(codeStr) {
for (const key in this.mapping) {
if (this.mapping[key] === codeStr) {
this.nes.buttonUp(
this.num,
jsnes.Controller[`BUTTON_${key.toUpperCase()}`]);
}
}
}
}
module.exports = Controller;