-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInputManager.js
91 lines (82 loc) · 2.08 KB
/
InputManager.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
var InputManager = function() {};
InputManager.prototype.initialize = function(functions) {
this.characters = {
'leftArrow': false,
'upArrow': false,
'rightArrow': false,
'downArrow': false,
'escape': false
};
this.engineFunctions = functions;
this.bindShit();
return this;
};
/**
* Get which keys are being pressed
*
* @return Object this.characters: The character object, which represents which keys are being pressed
*/
InputManager.prototype.getPressedKeys = function() {
return this.characters;
};
/**
* Convenience method to clear the pressed characters.
*/
InputManager.prototype.clear = function() {
for (var character in this.characters) {
if (this.characters.hasOwnProperty(character)) {
this.characters[character] = false;
}
}
}
InputManager.prototype.bindShit = function() {
var ret = true;
$('body, canvas').bind('keydown', function(e) {
// ignore spacebar presses, so the keypress handler below can pick this event up
if (e.which == 32) { return true; }
// chrome handles escape strangely
if (e.which == 27) {
this.engineFunctions.pause();
}
if (e.which == 37) {
this.characters['leftArrow'] = true;
ret = false;
}
if (e.which == 38) {
this.characters['upArrow'] = true;
ret = false;
}
if (e.which == 39) {
this.characters['rightArrow'] = true;
ret = false;
}
if (e.which == 40) {
this.characters['downArrow'] = true;
ret = false;
}
return ret;
}.bind(this)).bind('keyup', function(e) {
if (e.which == 37) {
this.characters['leftArrow'] = false;
ret = false;
}
if (e.which == 38) {
this.characters['upArrow'] = false;
ret = false;
}
if (e.which == 39) {
this.characters['rightArrow'] = false;
ret = false;
}
if (e.which == 40) {
this.characters['downArrow'] = false;
ret = false;
}
return ret;
}.bind(this)).bind('keypress', function(e) {
if (e.which == 32) {
this.engineFunctions.fire();
}
return false;
}.bind(this));
};