Skip to content

Commit

Permalink
Add multi controller support!
Browse files Browse the repository at this point in the history
  • Loading branch information
vaneenige committed Jul 24, 2018
1 parent f56c6fa commit 8c1cf91
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
const m = ['b', 'a', 'y', 'x', 'l', 'r'];

class Unswitch {
constructor(s) {
this.a = 8;
this.b = {};
this.c = {};
for (let i = 0; i < m.length; i += 1) {
this.b[m[i]] = { p: false };
}
Object.assign(this.c, s);
function Unswitch(s) {
this.a = 8;
this.b = {};
this.c = {};
for (let i = 0; i < m.length; i += 1) {
this.b[m[i]] = { p: false };
}

update() {
const pad = navigator.getGamepads()[0];
if (pad === null) return;
const { buttons, axes } = pad;
for (let i = 0; i < m.length; i += 1) {
const button = m[i];
if (this.b[button].p !== buttons[i].pressed && this.c[button]) {
this.b[button].p = buttons[i].pressed;
this.c[button](this.b[button].p);
Object.assign(this.c, s);
this.update = () => {
const gamepads = navigator.getGamepads();
for (let i = 0; i < Object.keys(gamepads).length; i += 1) {
if (gamepads[i].id && gamepads[i].id.indexOf(this.c.side) !== -1) {
const pad = gamepads[i];
const { buttons, axes } = pad;
for (let j = 0; j < m.length; j += 1) {
const button = m[j];
if (this.b[button].p !== buttons[j].pressed && this.c[button]) {
this.b[button].p = buttons[j].pressed;
this.c[button](this.b[button].p);
}
}
if (this.c.axes) {
const position = Math.round(axes[9] / (2 / 7) + 3.5);
if (position !== this.a) {
this.c.axes(position);
this.a = position;
}
}
break;
}
}
if (this.c.axes) {
const position = Math.round(axes[9] / (2 / 7) + 3.5);
if (position !== this.a) {
this.c.axes(position);
this.a = position;
}
}
}
};
}

export default Unswitch;

0 comments on commit 8c1cf91

Please sign in to comment.