-
Notifications
You must be signed in to change notification settings - Fork 1
/
Controls.ts
75 lines (65 loc) · 2.42 KB
/
Controls.ts
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
/**
* Created by MSI on 06.05.2017.
*/
/**
* Created by MSI on 12.03.2017.
*/
export class Controls {
gp: any;
private buttonStates: Object = {};
public onBtnPress: Function;
public onBtnRelease: Function;
public onLeft: Function;
public onRight: Function;
public onUp: Function;
public onDown: Function;
private prevX: number;
private prevY: number;
buttonPressed(b: any): boolean {
if (typeof(b) == "object") {
return b.pressed;
}
return b == 1.0;
}
onJoystickButtonPress(btn: number, state: boolean) {
if (state) {
if (this.onBtnPress) this.onBtnPress(btn);
if (this.onBtnRelease) this.onBtnRelease(btn)
}
}
update(): void{
if (this.gp) {
if (this.gp.axes[0] != this.prevX && this.gp.axes[0] > .99 && this.onRight) this.onRight();
if (this.gp.axes[0] != this.prevX && this.gp.axes[0] < -.99 && this.onLeft) this.onLeft();
if (this.gp.axes[1] != this.prevY && this.gp.axes[1] > .99 && this.onUp) this.onUp();
if (this.gp.axes[1] != this.prevY && this.gp.axes[1] < -.99 && this.onDown) this.onDown();
this.prevX = this.gp.axes[0];
this.prevY = this.gp.axes[1];
for (let x = 0; x < this.gp.buttons.length; ++x) {
let newBtnState = this.buttonPressed(this.gp.buttons[x]);
if (newBtnState != this.buttonStates[x]) {
this.onJoystickButtonPress(x, newBtnState);
}
this.buttonStates[x] = newBtnState;
}
}
}
constructor () {
let nav: any = navigator;
let gamepads = navigator.getGamepads ? navigator.getGamepads() : (nav.webkitGetGamepads ? nav.webkitGetGamepads : []);
this.gp = gamepads[0];
window.addEventListener("gamepaddisconnected", (e) => {
this.gp = null;
}, false);
window.addEventListener("gamepadconnected", (e: any) => {
if (e.gamepad) {
this.gp = navigator.getGamepads()[e.gamepad.index];
console.log("Gamepad connected at index %d: %s. %d buttons, %d axes.",
this.gp.index, this.gp.id,
this.gp.buttons.length, this.gp.axes.length);
} else {
this.gp = null;
}
});
}
}