-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.js
89 lines (62 loc) · 1.93 KB
/
keys.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
;(function() {
'use strict';
var t;
function KeyListener(grid,timer) {
t=this;
t.grid=grid;
t.timer=grid.timer;
t.inititialiseKeys();
}
KeyListener.prototype = {
// the grid the events happen to
grid:{},
// the timer triggering intervals
timer:{},
/**
* inititialiseKeys() - adds an event listener for keypresses
*/
inititialiseKeys:function()
{
window.addEventListener('keydown',t.keyListener);
},
/**
* keyListener() - responds to the key presses for game play
*/
keyListener:function(keyEvent)
{
var cells=t.grid.cells;
var inPlayPiece=t.grid.pieces[1];
switch (keyEvent.keyCode)
{
case 37:
keyEvent.preventDefault();
inPlayPiece.movePiece(cells,'left',t.timer);
break;
case 39:
keyEvent.preventDefault();
inPlayPiece.movePiece(cells,'right',t.timer);
break;
case 40:
keyEvent.preventDefault();
inPlayPiece.movePiece(cells,'down',t.timer);
break;
case 32:
keyEvent.preventDefault();
inPlayPiece.rotate(cells);
break;
case 49: // 1 key for pausing
keyEvent.preventDefault();
if (true === t.timer.running)
{
t.timer.pauseTimer();
}
else
{
t.timer.startTimer();
}
break;
}
}
};
window.KeyListener = KeyListener;
}());