-
Notifications
You must be signed in to change notification settings - Fork 4
/
userInput.js
344 lines (279 loc) · 9.84 KB
/
userInput.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
var userInput = {
mouseDown: false,
lastMouseX: null,
lastMouseY: null,
rotationMatrix: mat4.nidentity(),
// a queue of objects. each object can have 'dir' or 'reldir' (relative direction) and 'skippedCount' - the number of iterations it was skipped
currentlyPressedDirs: [],
touchX: null,
touchY: null,
HANDLERS_MENU: 1, HANDLERS_3D: 2,
handlersSet: 0 // which set of event handlers is connected at the moment
}
function handleMouseDown(event) {
userInput.mouseDown = true;
userInput.lastMouseX = event.clientX;
userInput.lastMouseY = event.clientY;
}
function handleMouseUp(event) {
userInput.mouseDown = false;
}
function handleMouseMove(event) {
if (!userInput.mouseDown) {
return;
}
var newX = event.clientX;
var newY = event.clientY;
if (!gameConf.debug)
return;
var deltaX = newX - userInput.lastMouseX
var deltaY = newY - userInput.lastMouseY;
var newRot = mat4.nidentity(); //createRotationMatrix(deltaX / 2, [0, 1, 0]);
mat4.rotateY(newRot, radFromDeg(deltaX / 2));
mat4.rotateX(newRot, radFromDeg(deltaY / 2)); // = // newRotationMatrix.x(createRotationMatrix(deltaY / 2, [1, 0, 0]));
// userInput.rotationMatrix = newRotationMatrix.x(userInput.rotationMatrix);
mat4.multiply(newRot, userInput.rotationMatrix, userInput.rotationMatrix);
userInput.lastMouseX = newX
userInput.lastMouseY = newY;
}
function resetUserRotation() {
mat4.identity(userInput.rotationMatrix);
}
// https://developer.mozilla.org/En/DOM/Event/UIEvent/KeyEvent
// from http://stackoverflow.com/questions/1465374/javascript-event-keycode-constants
if (typeof KeyEvent == "undefined") {
var KeyEvent = {
DOM_VK_RETURN: 13,
DOM_VK_ENTER: 14,
DOM_VK_PAUSE: 19,
DOM_VK_ESCAPE: 27,
DOM_VK_SPACE: 32,
DOM_VK_LEFT: 37,
DOM_VK_UP: 38,
DOM_VK_RIGHT: 39,
DOM_VK_DOWN: 40,
DOM_VK_0: 48,
DOM_VK_1: 49,
DOM_VK_2: 50,
DOM_VK_3: 51,
DOM_VK_4: 52,
DOM_VK_5: 53,
DOM_VK_6: 54,
DOM_VK_7: 55,
DOM_VK_8: 56,
DOM_VK_9: 57,
DOM_VK_A: 65,
DOM_VK_B: 66,
DOM_VK_C: 67,
DOM_VK_D: 68,
DOM_VK_E: 69,
DOM_VK_F: 70,
DOM_VK_G: 71,
DOM_VK_H: 72,
DOM_VK_I: 73,
DOM_VK_J: 74,
DOM_VK_K: 75,
DOM_VK_L: 76,
DOM_VK_M: 77,
DOM_VK_N: 78,
DOM_VK_O: 79,
DOM_VK_P: 80,
DOM_VK_Q: 81,
DOM_VK_R: 82,
DOM_VK_S: 83,
DOM_VK_T: 84,
DOM_VK_U: 85,
DOM_VK_V: 86,
DOM_VK_W: 87,
DOM_VK_X: 88,
DOM_VK_Y: 89,
DOM_VK_Z: 90,
DOM_VK_F1: 112,
DOM_VK_F2: 113,
DOM_VK_F3: 114,
DOM_VK_F4: 115,
DOM_VK_F5: 116,
DOM_VK_F6: 117,
DOM_VK_F7: 118,
DOM_VK_F8: 119,
DOM_VK_F9: 120,
DOM_VK_F10: 121,
DOM_VK_F11: 122,
DOM_VK_F12: 123
};
}
KeyEvent._DOM_VK_OPEN_SQUARE = 219; // open square brackets for relative directions
KeyEvent._DOM_VK_CLOSE_SQUARE = 221;
var keysFuncs = {}
keysFuncs[KeyEvent.DOM_VK_LEFT] = Directions.Left;
keysFuncs[KeyEvent.DOM_VK_RIGHT] = Directions.Right;
keysFuncs[KeyEvent.DOM_VK_UP] = Directions.Up;
keysFuncs[KeyEvent.DOM_VK_DOWN] = Directions.Down;
keysFuncs[KeyEvent.DOM_VK_W] = Directions.Up;
keysFuncs[KeyEvent.DOM_VK_A] = Directions.Left;
keysFuncs[KeyEvent.DOM_VK_S] = Directions.Down;
keysFuncs[KeyEvent.DOM_VK_D] = Directions.Right;
function handleKeyDown(event) {
var ret = true;
// directions control
if (keysFuncs[event.keyCode] !== undefined)
{
// skippedCount is the number of samples the key was not consumed in
userInput.currentlyPressedDirs.push( { dir:keysFuncs[event.keyCode], skippedCount:0 } );
ret = false;
}
else if (event.keyCode === KeyEvent._DOM_VK_OPEN_SQUARE) { // left
userInput.currentlyPressedDirs.push( { reldir:Directions.Left, skippedCount:0 } );
ret = false;
}
else if (event.keyCode === KeyEvent._DOM_VK_CLOSE_SQUARE) { // right
userInput.currentlyPressedDirs.push( { reldir:Directions.Right, skippedCount:0 } );
ret = false;
}
// other keys or "any key"
if (event.keyCode === KeyEvent.DOM_VK_P || event.keyCode === KeyEvent.DOM_VK_PAUSE) {
game.setPaused(!game.isPaused());
ret = false;
}
else if (event.keyCode === KeyEvent.DOM_VK_M) {
game.enableSound(!game.soundEnabled);
ret = false;
}
else if (event.keyCode === KeyEvent.DOM_VK_ESCAPE) {
game.setPaused(true);
c2d.verbosePauseScreen();
ret = false;
}
else if (!gameConf.debug && game.isPaused()) { // if in operational mode, any other key takes us out of manual mode
game.setPaused(false);
ret = false;
}
// consume space, not to scroll down
if (event.keyCode === KeyEvent.DOM_VK_SPACE)
ret = false;
if (gameConf.debug) {
if (event.keyCode === KeyEvent.DOM_VK_E) {
startExplosion(userPlayer, 1);
}
else if (event.keyCode === KeyEvent.DOM_VK_U) {
++game.userLivesCount;
world.staticDraw.lives.startOneUp();
}
else if (event.keyCode === KeyEvent.DOM_VK_B) {
addBonus();
}
}
return ret;
}
function handleKeyUp(event) {
// userInput.currentlyPressedKeys[event.keyCode] = 0;
}
function sampleKeys(player)
{
for(var k in userInput.currentlyPressedDirs)
{
var inRec = userInput.currentlyPressedDirs[k]; // TBD-this is BAD, still takes memory
if (inRec === undefined)
continue;
//writeDebug("SAMP-FOUND " + userInput.currentlyPressedDirs.length)
if (inRec.dir)
moveDir = viewControl.translateDir(inRec.dir, player);
else // reldir Directions.getRelativeDir(player.eDir, dr);
moveDir = viewControl.translateRelativeDir(Directions.getRelativeDir(player.eDir, inRec.reldir, player));
writeDebug("SAMP-DIR " + moveDir.name)
// its need to not be the opposite of where the player is currently heading
// since the selected direction might not have updated the current direction yet
if (moveDir !== null && moveDir !== player.eDir.opposite)
{
player.selDir = moveDir;
if (game.isPaused() && gameConf.debug) { // debugging step-by-step
writeDebug("DO")
movePlayer(userPlayer);
userPlayer.curStepFrac = 1;
}
userInput.currentlyPressedDirs[k] = undefined; // delete inRec
}
else {
++inRec.skippedCount;
// if we didn't consume it after 4 samples, discard it.
// this means that the backwards key was pressed well before the turn key
if (inRec.skippedCount > 4)
userInput.currentlyPressedDirs[k] = undefined;
}
}
}
var touchX = null, touchY = null
function handleTouchStart(evt) {
evt.preventDefault();
// writeDebug("TOUCH U " + userInput.touchX)
//writeDebug("TOUCH STARTI " + evt.touches[0].clientX + " " + evt.touches[0].clientY)
touchX = evt.touches[0].clientX;
touchY = evt.touches[0].clientY;
// writeDebug("TOUCH STARTX " + touchX + " " + touchY)
return false
};
function handleTouchMove(evt) {
evt.preventDefault();
//writeDebug("TOUCH MOVE")
if (!touchX || !touchY) {
return false;
}
var xUp = evt.touches[0].clientX;
var yUp = evt.touches[0].clientY;
var xDiff = touchX - xUp;
var yDiff = touchY - yUp;
var axDiff = Math.abs(xDiff);
var ayDiff = Math.abs(yDiff);
writeDebug("TOUCH " + xDiff + " " + yDiff)
var d = null
if (axDiff > ayDiff) {
if (axDiff > 10) {
if (xDiff > 0) {
d = Directions.Left;
}
else {
d = Directions.Right;
}
}
}
else {
if (ayDiff > 10) {
if ( yDiff > 0 ) {
d = Directions.Up;
}
else {
d = Directions.Down;
}
}
}
if (d !== null) {
userInput.currentlyPressedDirs.push( { dir:d, skippedCount:0 } );
touchX = null;
touchX = null;
game.setPaused(false);
}
return false
};
function handleTouchEnd(evt) {
evt.preventDefault();
//writeDebug("TOUCH END")
touchX = null;
touchX = null;
return false
}
// set up input handlers for 3d game
function setupGameInput(canvas, canvas2d)
{
canvas.onmousedown = handleMouseDown;
canvas2d.onmousedown = handleMouseDown;
document.onmouseup = handleMouseUp;
document.onmousemove = handleMouseMove;
// disable scrolling in the page using keyboard
document.onkeydown = handleKeyDown;
document.onkeyup = handleKeyUp;
document.body.addEventListener("touchstart", handleTouchStart, false);
document.body.addEventListener("touchmove", handleTouchMove, false);
document.body.addEventListener("touchend", handleTouchEnd, false);
document.body.addEventListener("touchcancel", handleTouchEnd, false);
userInput.handlersSet = userInput.HANDLERS_3D;
}