-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputEngineBasic
70 lines (49 loc) · 2.02 KB
/
InputEngineBasic
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
InputEngineClass = Class.extend({
bindings: {},
actions: {},
mouse: {
x: 0,
y: 0
},
//-----------------------------
setup: function () {
//move key bindings
gInputEngine.bind(87, 'look-up');
gInputEngine.bind(65, 'look-left');
gInputEngine.bind(83, 'look-down');
gInputEngine.bind(68, 'look-right');
//fire key bindings
// gInputEngine.bind(81,'powerup-use'); //Q
// gInputEngine.bind(69,'fire-superWeapon'); //E
gInputEngine.bind(32,'fire-prmary'); //Space
// gInputEngine.bind(16,'fire-missile'); //Shift Ascii code=0 key code=16
//event listeners
document.getElementById('spaceCanvas').addEventListener('mousemove', gInputEngine.onMouseMove);
document.getElementById('spaceCanvas').addEventListener('keydown', gInputEngine.onKeyDown);
document.getElementById('spaceCanvas').addEventListener('keyup', gInputEngine.onKeyUp);
},
//-----------------------------
onMouseMove: function (event) {
gInputEngine.mouse.x = event.clientX;
gInputEngine.mouse.y = event.clientY;
},
//-----------------------------
onKeyDown: function (event) {
var action = gInputEngine.bindings[event.keyID];
if (action) {
gInputEngine.actions[action] = true;
}
},
//-----------------------------
onKeyUp: function (event) {
var action = gInputEngine.bindings[event.keyID];
if (action) {
gInputEngine.actions[action] = false;
}
},
//-----------------------------
bind: function (key, action) {
gInputEngine.bindings[key] = action;
}
});
gInputEngine = new InputEngineClass();