-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimePilot.Controller.Gamepad.js
93 lines (83 loc) · 3.12 KB
/
TimePilot.Controller.Gamepad.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* global define */
define("TimePilot.Controller.Gamepad", [
"engine/helpers"
], function (
helpers
) {
var Gamepad = function (controllerInterface) {
this._controllerInterface = controllerInterface;
this.connect();
};
Gamepad.prototype = {
/**
* Is the fire button pressed on the Gamepad.
* @type {Boolean}
*/
_isFireButtonPressed: false,
/**
* Is the pause button pressed on the Gamepad.
* @type {Boolean}
*/
_isPauseButtonPressed: false,
/**
* Is the restart button pressed on the Gamepad.
* @type {Boolean}
*/
_isRestartButtonPressed: false,
/**
* Looping to check for gamepad data.
* @method _gameLoop
*/
_gameLoop: function () {
var gamepads = navigator.getGamepads ? navigator.getGamepads() : (
navigator.webkitGetGamepads ? navigator.webkitGetGamepads : []
);
for (var playerIndex = 0; playerIndex < gamepads.length; playerIndex++) {
var gamepad = gamepads[playerIndex];
if (gamepad) {
if (gamepad.buttons[0].pressed && !this._isFireButtonPressed) {
this._isFireButtonPressed = true;
this._controllerInterface.startShooting();
} else if (!gamepad.buttons[0].pressed && this._isFireButtonPressed) {
this._isFireButtonPressed = false;
this._controllerInterface.stopShooting();
}
if (gamepad.buttons[9].pressed && !this._isPauseButtonPressed) {
this._isPauseButtonPressed = true;
this._controllerInterface.togglePause();
} else if (!gamepad.buttons[9].pressed) {
this._isPauseButtonPressed = false;
}
if (gamepad.buttons[8].pressed && !this._isRestartButtonPressed) {
this._isRestartButtonPressed = true;
this._controllerInterface.restart();
} else if (!gamepad.buttons[8].pressed) {
this._isRestartButtonPressed = false;
}
if (gamepad.axes[0] || gamepad.axes[1]) {
var heading = helpers.findHeading({
posX: -(gamepad.axes[0]),
posY: -(gamepad.axes[1])
});
this._controllerInterface.rotateToHeading(heading);
}
}
}
window.requestAnimationFrame(this._gameLoop.bind(this));
},
/**
* Connecting the Gamepad controller interface
* @method connect
*/
connect: function () {
this._gameLoop();
},
/**
* Disconnecting the Gamepad controller interface.
* @method disconnect
*/
disconnect: function () {
}
};
return Gamepad;
});