-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.js
More file actions
155 lines (129 loc) · 3.86 KB
/
Copy pathplayer.js
File metadata and controls
155 lines (129 loc) · 3.86 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var movingIn = [false,false,false,false];
var speedIn = [0,0,0,0];
//If someone starts playing with the keyboard, disable tilt controls
var anyKeyPressed = false;
function playerInitiatedPause() {
gamePausedOverlay.style.display = "block";
gamePaused = !gamePaused;
camera.position.z = cameraZ;
}
document.addEventListener("keydown", function(event) {
anyKeyPressed = true;
switch (event.key) {
case 'w':
case 'ArrowUp':
movingIn[0] = true;
break;
case 'a':
case 'ArrowLeft':
movingIn[1] = true;
break;
case 's':
case 'ArrowDown':
movingIn[2] = true;
break;
case 'd':
case 'ArrowRight':
movingIn[3] = true;
break;
case 'c':
cameraposition = (cameraposition + 1) % 2;
if (!cameraposition) {camera.setpositiontwo()}
else {camera.setpositionone()}
break;
case 'Escape':
playerInitiatedPause()
}
});
document.addEventListener("keyup", function(event) {
anyKeyPressed = true;
switch (event.key) {
case 'w':
case 'ArrowUp':
movingIn[0] = false;
break;
case 'a':
case 'ArrowLeft':
movingIn[1] = false;
break;
case 's':
case 'ArrowDown':
movingIn[2] = false;
break;
case 'd':
case 'ArrowRight':
movingIn[3] = false;
break;
}
});
document.addEventListener("click", function() {
playerInitiatedPause();
});
var player = new THREE.Mesh(new THREE.CubeGeometry(SIZE*2,SIZE*2,SIZE*2), new THREE.MeshNormalMaterial());
function calculateSpeed(currentSpeed, increasing) {
currentSpeed = currentSpeed || 0.5;
newSpeed = Math.min(increasing ? currentSpeed * 1.2 : currentSpeed / 1.03, 5);
return Math.max(newSpeed,0.5);
}
function movePlayer() {
speedIn = movingIn.map((increasing, index) => calculateSpeed(speedIn[index],increasing));
player.position.x += currentLevel.speedMultiplier * (speedIn[3] - speedIn[1]) / 5;
player.position.y += currentLevel.speedMultiplier * (speedIn[0] - speedIn[2]) / 5;
}
//Allow tilting the device to control the player
function accelerometer(e) {
if (!anyKeyPressed) {
if (!localStorage.getItem("hasPlayedBefore")) {
alert("This game is best played with portrait orientation lock on, but with the device in the landscape orientation.");
localStorage.setItem("hasPlayedBefore",true);
}
//x,y,z from the accelerometer's reference point, not mine
var y = e.beta || e.y || e.acceleration.y;
var z = e.gamma || e.z || e.acceleration.z;
if (y < -25) {
y = -25;
}
if (y > 25) {
y = 25;
}
if (z < -25) {
z = -25;
}
if (z > 25) {
z = 25;
}
z /= 25;
y /= 25;
if (z < 0) {
movingIn[3] = 0;
movingIn[1] = Math.abs(z);
} else {
movingIn[1] = 0;
movingIn[3] = Math.abs(z);
}
if (y < 0) {
movingIn[2] = 0;
movingIn[0] = Math.abs(y);
} else {
movingIn[0] = 0;
movingIn[2] = Math.abs(y);
}
}
}
function getAccel(){
DeviceMotionEvent.requestPermission().then(response => {
if (response == 'granted') {
window.addEventListener("deviceorientation", accelerometer);
window.addEventListener("MozOrientation", accelerometer);
window.addEventListener("devicemotion", accelerometer);
}
});
}
try {
getAccel();
} catch {
//Should might work for Android devices
if (!!window.DeviceMotionEvent) {
window.addEventListener("devicemotion", accelerometer);
}
}