-
Notifications
You must be signed in to change notification settings - Fork 57
/
lodeRunner.gamepad.js
255 lines (223 loc) · 7.33 KB
/
lodeRunner.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
var MAX_CONTROLLER = 4;
var gamepadReady = 0;
var gamepadPollingId = null;
var gamepadLastId = -1; //for send to server only
var requestAnimationFrame = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame;
var cancelRequestAnimationFrame = window.cancelRequestAnimationFrame ||
window.webkitCancelRequestAnimationFrame ||
window.mozCancelRequestAnimationFrame;
function gamepadEnable()
{
if(!gamepadSupport()) return;
window.addEventListener("gamepadconnected",gamepadConnectFun, false);
window.addEventListener("gamepaddisconnected", gamepadDisconnectFun, false);
gamepadPollingId = setInterval(gamepadCheck, 500);
}
function gamepadSupport()
{
return ("getGamepads" in navigator) || ("webkitGetGamepads" in navigator);
}
function gamepadConnectFun(e)
{
//console.log("Gamepad connected at index %d: %s. %d buttons, %d axes.",
// e.gamepad.index, e.gamepad.id,
// e.gamepad.buttons.length, e.gamepad.axes.length);
gamepadReady = 1;
if(gamepadPollingId) {
clearInterval(gamepadPollingId);
gamepadPollingId = null;
}
requestAnimationFrame(gamepadRequestState);
}
function gamepadDisconnectFun(e)
{
console.log("disconnection game port#%d", e.gamepad.index );
gamepadReady = 0;
if(!gamepadPollingId) gamepadPollingId = setInterval(gamepadCheck, 500);
}
function gamepadCheck()
{
gamepad = gamepadGetInstance();
for(var id=0; id < MAX_CONTROLLER; id ++) {
if(gamepad[id]) {
if(!gamepadReady) {
gamepadConnectTrigger(gamepad[id]);
}
}
}
}
function gamepadGetInstance()
{
return ("getGamepads" in navigator) ?
navigator.getGamepads() :
(("webkitGetGamepads" in navigator) ? navigator.webkitGetGamepads() : []);
}
//=================================
// Generate gamepadconnected event
//=================================
function gamepadConnectTrigger(gamepad)
{
var event = document.createEvent('Event');
event.initEvent("gamepadconnected", true, true); //can bubble, and is cancellable
event.gamepad = gamepad;
dispatchEvent(event);
}
function gamepadDisable()
{
if(!gamepadSupport()) return;
window.removeEventListener("gamepadconnected",gamepadConnectFun, false);
window.removeEventListener("gamepaddisconnected", gamepadDisconnectFun, false);
if(gamepadPollingId) {
clearInterval(gamepadPollingId);
gamepadPollingId = null;
}
gamepadReady = 0;
}
function getLastGamepadInfo()
{
var gamepadInfo = null;
if(gamepadLastId >= 0 && gamepadLastId < MAX_CONTROLLER ) {
var gamepad = gamepadGetInstance()[gamepadLastId];
if(gamepad) {
gamepadInfo =gamepad.index + "." + gamepad.id +
(gamepad.mapping != ""?("." +gamepad.mapping):"");
}
}
return gamepadInfo;
}
//===========================================================================
var keyCodeMapping = [
//[[keyCode1, ctrl, shift], [keyCode2, ctrl, shift]
[[KEYCODE_UP, 0, 0], [KEYCODE_UP, 0, 0]], //up
[[KEYCODE_DOWN, 0, 0], [KEYCODE_DOWN, 0, 0]], //down
[[KEYCODE_LEFT, 0, 0], [KEYCODE_LEFT, 0, 0]], //left
[[KEYCODE_RIGHT, 0, 0], [KEYCODE_RIGHT, 0, 0]], //right
[[KEYCODE_Z, 0, 0], [KEYCODE_ENTER, 0, 0]], //dig-left
[[KEYCODE_X, 0, 0], [KEYCODE_ENTER, 0, 0]], //dig-right
[[KEYCODE_ESC, 0, 0], [KEYCODE_ENTER, 0, 0]], //pause
[[KEYCODE_ENTER, 0, 0], [KEYCODE_ENTER, 0, 0]], //enter
[[KEYCODE_A, 1, 0], [KEYCODE_ENTER, 0, 0]] //game abort
];
var gameButtonMapping = [
//index[0] buttonMethod: 0: don't repeat, 1: repeat, 2: combine button
[ 1, ['B', 12],['A', 1, "<", -0.6],['B', 16]], //UP
[ 1, ['B', 13],['A', 1, ">", 0.6],['B', 17]], //DOWN
[ 1, ['B', 14],['A', 0, "<", -0.6],['B', 18]], //LEFT
[ 1, ['B', 15],['A', 0, ">", 0.6],['B', 19]], //RIGHT
[ 1, ['B', 2],['B', 3]], //DIG-LEFT
[ 1, ['B', 1]], //DIG-RIGHT
[ 0, ['B', 8],['B', 11]], //PAUSE (ESC)
[ 0, ['B', 9]], //Enter (Accept)
[ 2, ['B', 4],['B', 5], ['B', 6],['B', 7]] //GAME ABORT (CTRL-A)
];
var lastButtonState = [
[ 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0]
];
function gamepadRequestState()
{
var i, j;
var gamepad, curButtonState;
var buttonMethod;
for(var id=0; id < MAX_CONTROLLER; id ++) {
gamepad = gamepadGetInstance()[id];
curButtonState = [];
if(gamepad) {
for(i = 0; i < gameButtonMapping.length; i++) {
curButtonState[i] = 0;
buttonMethod = gameButtonMapping[i][0];
for(j = 1; j < gameButtonMapping[i].length && !curButtonState[i]; j++) {
var curButtonMapping = gameButtonMapping[i][j];
switch(curButtonMapping[0]) {
case 'A': //axes
var axesId = curButtonMapping[1];
if(gamepad.axes.length > axesId) {
var axesCmpSign = curButtonMapping[2];
var axesCmpValue = curButtonMapping[3];
var axesValue = Math.trunc(gamepad.axes[axesId]*100)/100;
if(typeof axesValue != 'undefined' ) {
if( (axesCmpSign == ">" && axesValue > axesCmpValue && axesValue <= 1.05)
||(axesCmpSign == "<" && axesValue < axesCmpValue && axesValue >= -1.05) )
{
//console.log("axes#%d = %f", axesId, axesValue );
curButtonState[i] = 1;
}
}
}
break;
case 'B': //button
var buttonId = curButtonMapping[1];
var button = gamepad.buttons[buttonId];
if(buttonPressed(button))
curButtonState[i] = 1;
if(buttonMethod == 2) { //combine mode
j++;
if(curButtonState[i]) {
curButtonMapping = gameButtonMapping[i][j];
buttonId = curButtonMapping[1];
button = gamepad.buttons[buttonId];
if(buttonPressed(button)) {
curButtonState[i] = 1;
} else {
curButtonState[i] = 0;
}
}
}
break;
}
}
switch(buttonMethod) {
case 1: //repeat
if(document.onkeydown == handleKeyDown || document.onkeydown == anyKeyDown) {
if(curButtonState[i])sendKeyDown(keyCodeMapping[i], id);
else if(lastButtonState[id][i]) sendKeyUp(keyCodeMapping[i], id);
break;
}
case 0: //don't repeat
case 2:
if(lastButtonState[id][i] != curButtonState[i]) {
if(curButtonState[i])sendKeyDown(keyCodeMapping[i], id);
else if(lastButtonState[id][i]) sendKeyUp(keyCodeMapping[i], id);
}
break;
}
}
lastButtonState[id] = curButtonState;
}
}
if(gamepadReady) requestAnimationFrame(gamepadRequestState);
}
function buttonPressed(button)
{
var pressed = button == 1.0;
if (typeof(button) == "object") {
pressed = button.pressed;
}
return pressed;
}
function gamepadClearId()
{
gamepadLastId = -1;
}
function sendKeyUp(twinKey, id)
{
var key = (inputNameState)? twinKey[1]:twinKey[0];
if(key[0] == null) return;
var event = {keyCode: key[0], ctrlKey: key[1], shiftKey: key[2]};
//console.log("sendKeyUp = %d",keyCode );
if(typeof document.onkeydown == "function" ) document.onkeyup(event);
gamepadLastId = id;
}
function sendKeyDown(twinKey, id)
{
var key = (inputNameState)? twinKey[1]:twinKey[0];
if(key[0] == null) return;
var event = {keyCode: key[0], ctrlKey: key[1], shiftKey: key[2]};
//console.log("sendKeyDown = %d",keyCode );
if(typeof document.onkeydown == "function" ) document.onkeydown(event);
gamepadLastId = id;
}