-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontrols.js
116 lines (109 loc) · 2.86 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
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
107
108
109
110
111
112
113
114
115
116
// Move camera
function mv(p) {
new TWEEN.Tween(group.position).to(p, 500).start();
}
function onKeyDown(e) {
//console.log(e.keyCode);
switch(e.keyCode) {
case 37: mv({x:0.5}); break; // l
case 39: mv({x:-0.5}); break; // r
case 38: mv({y:-0.5}); break; // u
case 40: mv({y:0.5}); break; // d
};
buttonPressed();
}
function checkControls() {
var btns = 0;
if(navigator.webkitGetGamepads) {
var gp = navigator.webkitGetGamepads()[0];
if(gp.buttons[0] == 1) {
btns |= 1;
} else if(gp.buttons[1] == 1) {
btns |= 2;
} else if(gp.buttons[2] == 1) {
btns |= 4;
} else if(gp.buttons[3] == 1) {
btns |= 8;
}
} else if (navigator.getGamepads && navigator.getGamepads()[0]) {
var gp = navigator.getGamepads()[0];
if(gp.buttons[0].value > 0 || gp.buttons[0].pressed == true) {
btns |= 1;
} else if(gp.buttons[1].value > 0 || gp.buttons[1].pressed == true) {
btns |= 2;
} else if(gp.buttons[2].value > 0 || gp.buttons[2].pressed == true) {
btns |= 4;
} else if(gp.buttons[3].value > 0 || gp.buttons[3].pressed == true) {
btns |= 8;
}
} else {
// move based on orientation
btns = lastGamepadBtns;
var v = new THREE.Vector3( 0, 0, -1 );
v.applyQuaternion( camera.quaternion );
var threshMax = 0.2;
var threshMin = 0.1;
if (btns&1) {
if (v.y<threshMin) btns &= ~1;
} else {
if (v.y>threshMax) btns |= 1;
}
if (btns&2) {
if (v.x>-threshMin) btns &= ~2;
} else {
if (v.x<-threshMax) btns |= 2;
}
if (btns&4) {
if (v.x<threshMin) btns &= ~4;
} else {
if (v.x>threshMax) btns |= 4;
}
if (btns&8) {
if (v.y>-threshMin) btns &= ~8;
} else {
if (v.y<-threshMax) btns |= 8;
}
}
var newBtn = btns & ~lastGamepadBtns;
if (newBtn&1) mv({y:0.5});
if (newBtn&2) mv({x:-0.5});
if (newBtn&4) mv({x:0.5});
if (newBtn&8) mv({y:-0.5});
if (newBtn) buttonPressed();
lastGamepadBtns = btns;
}
function onClick(e) {
if (e.pageX > window.innerWidth/2)
mv({x:0.5});
else
mv({x:-0.5});
if (e.pageY > window.innerHeight/2)
mv({y:-0.5});
else
mv({y:0.5});
buttonPressed();
}
function setOrientationControls(e) {
if (!e.alpha) {
return;
}
if (USE_GYRO) {
controls = new THREE.DeviceOrientationControls(camera, true);
controls.connect();
controls.update();
window.removeEventListener('deviceorientation', setOrientationControls, true);
}
}
function buttonPressed() {
// restart game?
if (!gameRunning) {
resetGame();
newGame();
}
}
// ---------------------------------------------------------------------------
function setupControls() {
window.addEventListener( 'keydown', onKeyDown, false );
window.addEventListener( 'deviceorientation', setOrientationControls, true);
window.addEventListener('click', onClick, false);
}