-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathControls.js
68 lines (68 loc) · 2.73 KB
/
Controls.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
/**
* Created by MSI on 06.05.2017.
*/
define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Created by MSI on 12.03.2017.
*/
var Controls = /** @class */ (function () {
function Controls() {
var _this = this;
this.buttonStates = {};
var nav = navigator;
var gamepads = navigator.getGamepads ? navigator.getGamepads() : (nav.webkitGetGamepads ? nav.webkitGetGamepads : []);
this.gp = gamepads[0];
window.addEventListener("gamepaddisconnected", function (e) {
_this.gp = null;
}, false);
window.addEventListener("gamepadconnected", function (e) {
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;
}
});
}
Controls.prototype.buttonPressed = function (b) {
if (typeof (b) == "object") {
return b.pressed;
}
return b == 1.0;
};
Controls.prototype.onJoystickButtonPress = function (btn, state) {
if (state) {
if (this.onBtnPress)
this.onBtnPress(btn);
if (this.onBtnRelease)
this.onBtnRelease(btn);
}
};
Controls.prototype.update = function () {
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 (var x = 0; x < this.gp.buttons.length; ++x) {
var newBtnState = this.buttonPressed(this.gp.buttons[x]);
if (newBtnState != this.buttonStates[x]) {
this.onJoystickButtonPress(x, newBtnState);
}
this.buttonStates[x] = newBtnState;
}
}
};
return Controls;
}());
exports.Controls = Controls;
});