/
keys.js
106 lines (91 loc) · 2.15 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var Keys = function(game) {
var self = this
this.chr = 0
this.pressed = 0
this.left = 0
this.up = 0
this.right = 0
this.down = 0
this.shoot = 0
this.shootKey = [32,18,16,190,191,90,122,88,120]
this.game = game
document.onkeydown = function(e) {
e.preventDefault()
self.checkkeydown(e)
return false
}
document.onkeyup = function(e) {
e.preventDefault()
self.checkkeyup(e)
return false
}
}
Keys.prototype = {
checkkeydown: function(e) {
if (typeof e.which === 'undefined' || typeof e.charCode === 'undefined') this.chr = e.keyCode // IE, Opera
else if (typeof e.which !== 'undefined' && typeof e.charCode !== 'undefined') this.chr = e.which // All others
switch (this.chr) {
case 37:
case 74:
this.right = 0
this.left = 1
break
case 38:
case 73:
this.down = 0
this.up = 2
break
case 39:
case 76:
this.left = 0
this.right = 4
break
case 40:
case 75:
this.up = 0
this.down = 8
break
default:
if ( ~this.shootKey.indexOf(this.chr) ) {
this.shoot = 1
}
break
}
this.pressed = this.left
+ this.up
+ this.right
+ this.down
}
, checkkeyup: function(e) {
if (typeof e.which === 'undefined' || typeof e.charCode === 'undefined') this.chr = e.keyCode // IE, Opera
else if (typeof e.which !== 'undefined' && typeof e.charCode !== 'undefined') this.chr = e.which // All others
switch (this.chr) {
case 37:
case 74:
this.left = 0
break
case 38:
case 73:
this.up = 0
break
case 39:
case 76:
this.right = 0
break
case 40:
case 75:
this.down = 0
break
default:
if ( ~this.shootKey.indexOf(this.chr) ) {
this.shoot = 0
}
break
}
this.pressed = this.left
+ this.up
+ this.right
+ this.down
}
}
module.exports = Keys