forked from mtcliatt/Gravity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
312 lines (260 loc) · 8.71 KB
/
index.html
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Gravity - Matt, Chris, Ben</title>
<link rel="stylesheet" type="text/css" href="./css/main.css">
</head>
<body>
<script src="./js/three.min.js"></script>
<script src="./js/gPointerLockControls.js"></script>
<script src="./js/gravity.js"></script>
<script src="./js/ob.js"></script>
<script src="./js/level.js"></script>
<div id="menu">
<div id="info">
<p>
GRAVITY -
<span id="info-direction">DOWN</span>
<span> </span>
ACTIVE -
<span id="info-active">FALSE</span>
<span> </span>
FACE -
<span id="info-face">BACK</span>
</p>
</div>
</div>
<div id="blocker">
<div id="instructions">
<span style="font-size:40px">Click to play</span>
<br /> (W, A, S, D = Move, ENTER = Gravity
<br /> MOUSE = Look around, R = Reset Level)
</div>
</div>
<div id="congrats">
<div id="congrats-inner">
<span style="font-size:60px">You've won!</span>
<br />
<span style="font-size:32px">Press enter to play again..</span>
</div>
</div>
<div id="levelBanner">
<div id="levelBanner-inner">
<span style="font-size:60px">On to next level!</span>
</div>
</div>
<script>
var altCam, camera, scene, renderer;
var controls, altControls;
var onAlt = false;
var blocker = document.getElementById('blocker');
var instructions = document.getElementById('instructions');
var level = 0, won = false;
var havePointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;
var element = document.body;
var pointerlockchange = function(event) {
if (document.pointerLockElement === element || document.mozPointerLockElement === element || document.webkitPointerLockElement === element) {
controlsEnabled = true;
controls.enabled = true;
blocker.style.display = 'none';
prevTime = performance.now();
} else {
controls.enabled = false;
blocker.style.display = '-webkit-box';
blocker.style.display = '-moz-box';
blocker.style.display = 'box';
instructions.style.display = '';
}
};
var pointerlockerror = function(event) {
instructions.style.display = '';
};
document.addEventListener('keyup', function(event) {
if (event.keyCode == 32) {
//onAlt = !onAlt;
//controlsEnabled = !controlsEnabled;
}
if (event.keyCode == 13) {
if ( won ) {
won = false;
document.getElementById('congrats').style.visibility = 'hidden';
setup(0);
}
}
if (event.keyCode == 82) {
setup(level.level);
}
});
// Hook pointer lock state change events
document.addEventListener('pointerlockchange', pointerlockchange, false);
document.addEventListener('mozpointerlockchange', pointerlockchange, false);
document.addEventListener('webkitpointerlockchange', pointerlockchange, false);
document.addEventListener('pointerlockerror', pointerlockerror, false);
document.addEventListener('mozpointerlockerror', pointerlockerror, false);
document.addEventListener('webkitpointerlockerror', pointerlockerror, false);
instructions.addEventListener('click', function(event) {
instructions.style.display = 'none';
// Ask the browser to lock the pointer
element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock;
if (/Firefox/i.test(navigator.userAgent)) {
var fullscreenchange = function(event) {
if (document.fullscreenElement === element || document.mozFullscreenElement === element || document.mozFullScreenElement === element) {
document.removeEventListener('fullscreenchange', fullscreenchange);
document.removeEventListener('mozfullscreenchange', fullscreenchange);
element.requestPointerLock();
}
};
document.addEventListener('fullscreenchange', fullscreenchange, false);
document.addEventListener('mozfullscreenchange', fullscreenchange, false);
element.requestFullscreen = element.requestFullscreen || element.mozRequestFullscreen || element.mozRequestFullScreen || element.webkitRequestFullscreen;
element.requestFullscreen();
} else {
element.requestPointerLock();
}
animate();
}, false);
// here is where we really begin
init();
var controlsEnabled = false;
var moveForward = false;
var moveBackward = false;
var moveLeft = false;
var moveRight = false;
var canJump = false;
var prevTime = 0;
var velocity = new THREE.Vector3();
var level;
function init() {
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1000);
altCam = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1000);
altCam.position.set(0, 0, -100);
//altControls = new THREE.OrbitControls(altCam);
scene = new THREE.Scene();
altCam.lookAt(scene.position);
scene.fog = new THREE.Fog(0xffffff, 0, 750);
var light = new THREE.HemisphereLight(0xeeeeff, 0x777788, 0.75);
light.position.set(0.5, 1, 0.75);
scene.add(light);
level = new Level(scene, camera, 0);
controls = level.getControls();
controls.addGravityListener(function(event) {
document.getElementById('info-direction').innerHTML = event.gravity.name;
document.getElementById('info-active').innerHTML =
(event.active) ? 'TRUE' : 'FALSE';
});
level.addEndListener(function() {
document.getElementById('levelBanner').style.visibility = 'visible';
console.log('Showing level thing');
setTimeout(function() {
document.getElementById('levelBanner').style.visibility = 'hidden';
}, 3000);
setup(level.level + 1);
});
controls.addDirectionListener(function(event) {
var raycaster = new THREE.Raycaster(controls.getObject().position,
event.direction);
var intersects = raycaster.intersectObjects([level.boundingBox]);
if (intersects.length) {
var faceIndex = intersects[0].faceIndex;
var face;
switch (faceIndex) {
case 10:
case 11:
face = 'BACK';
break;
case 0:
case 1:
face = 'RIGHT';
break;
case 8:
case 9:
face = 'FRONT';
break;
case 2:
case 3:
face = 'LEFT';
break;
case 6:
case 7:
face = 'DOWN';
break;
case 4:
case 5:
face = 'UP';
break;
}
document.getElementById('info-face').innerHTML = face;
}
});
renderer = new THREE.WebGLRenderer();
renderer.setClearColor(0xffffff);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
//
window.addEventListener('resize', onWindowResize, false);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
altCam.aspect = window.innerWidth / window.innerHeight;
altCam.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate(now) {
requestAnimationFrame(animate);
/*
if (onAlt) {
var time = performance.now();
var delta = ( time - prevTime ) / 1000;
altControls.update(delta);
level.update();
prevTime = time;
renderer.render( scene, altCam );
} else {*/
if (controlsEnabled && !won) {
var time = performance.now();
var delta = (time - prevTime) / 1000;
controls.update(delta, scene.children);
level.update();
prevTime = time;
renderer.render(scene, camera);
}
}
function setup(at) {
if(at >= objsPlatforms.length) {
return win();
}
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1000 );
scene = new THREE.Scene();
scene.fog = new THREE.Fog( 0xffffff, 0, 750 );
var light = new THREE.HemisphereLight( 0xeeeeff, 0x777788, 0.75 );
light.position.set( 0.5, 1, 0.75 );
scene.add( light );
level = new Level(scene, camera, at);
controls.dispose();
controls.enabled = false;
controls = level.getControls();
controls.enabled = true;
controls.addGravityListener(function(event) {
document.getElementById('info-direction').innerHTML = event.gravity.name;
document.getElementById('info-active').innerHTML =
(event.active) ? 'TRUE' : 'FALSE';
});
level.addEndListener( function() {
document.getElementById('levelBanner').style.visibility = 'visible';
console.log('Showing level thing');
setTimeout(function() {
document.getElementById('levelBanner').style.visibility = 'hidden';
}, 3000);
setup(at + 1);
});
}
function win() {
won = true;
document.getElementById('congrats').style.visibility = 'visible';
}
</script>
</body>
</html>