/
controls.js
74 lines (61 loc) · 1.98 KB
/
controls.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
var controls={
//keyboard handler
keyList: [],
touchl: { },
touchr: { },
isRight: function() {
return controls.touchr.down||
(controls.touchr.startUp+200>gS.gameTime)||
controls.isKeyDown(68)||
controls.isKeyDown(39);
},
isLeft: function() {
return controls.touchl.down||
(controls.touchl.startUp+200>gS.gameTime)||
controls.isKeyDown(65)||
controls.isKeyDown(37);
},
keyEvent: function(keyCode,keyDown) {
if (keyDown)
if ((keyCode==32)||(keyCode==87)||(keyCode==38)) controls.jumpTrigger=1;
controls.keyList[keyCode]=keyDown;
},
isKeyDown: function(keyCode) {
return controls.keyList[keyCode];
},
touch: function(tl,down) {
controls.isTouch=1;
for (var i=0;i<tl.length;i+=1) {
var t=tl[i];
var side=(t.clientX<controls.width/2)?controls.touchl:controls.touchr;
if (down) {
if ((gS.gameTime-side.startUp)<200) controls.jumpTrigger=1;
side.start=gS.gameTime;
side.down=1;
} else {
if ((gS.gameTime-side.start)<200) controls.jumpTrigger=1;
side.start=0;
side.startUp=gS.gameTime;
side.down=0;
}
}
}
}
function bindControls(el) {
controls.touchl.start=controls.touchl.startUp=-1000;
controls.touchl.down=0;
controls.touchr.start=controls.touchr.startUp=-1000;
controls.touchr.down=0;
el.onkeydown=function(evt) { controls.keyEvent(evt.keyCode,1); evt.preventDefault(); return false; };
el.onkeyup=function(evt) { controls.keyEvent(evt.keyCode,0); evt.preventDefault(); return false; };
el.ontouchstart=function(evt) { evt.preventDefault(); controls.touch(evt.changedTouches,1); return false; }
el.ontouchend=function(evt) { evt.preventDefault(); controls.touch(evt.changedTouches,0); return false; }
controls.width=window.innerWidth;
el.focus();
//repress the context menu
window.oncontextmenu = function(event) {
event.preventDefault();
event.stopPropagation();
return false;
};
}