-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
300 lines (257 loc) · 7.76 KB
/
main.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
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
//defining global vars
var width = window.innerWidth;
var height = window.innerHeight;
var aspect = width/height;
var unitsize = 250;
var wallheight = unitsize;
var movespeed = 100;
var lookspeed = 0.075;
var score = 1000;
var t= THREE, scene, cam, renderer, controls, clock, projector, model, skin;
var runAnim = true,
mouse = {x:0, y:0};
var runBoost, lastRunBoost = false;
//Time handling
var time = 3000;
var userFinishTime;
var finished = false;
// map is x index (1-9), y index (0-9)
// 0 is ok location, 1 is wall, 2 is innerwall
var map = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1,],
[1, 1, 0, 2, 0, 0, 0, 1, 1, 1,],
[1, 1, 0, 0, 0, 2, 0, 1, 1, 1,],
[1, 1, 0, 2, 0, 2, 0, 1, 1, 1,],
[1, 1, 0, 2, 0, 2, 0, 1, 1, 1,],
[1, 1, 0, 2, 0, 2, 0, 1, 1, 1,],
[1, 1, 0, 2, 0, 0, 0, 0, 0, 1,],
[1, 0, 0, 2, 0, 2, 0, 0, 0, 1,],
[1, 0, 0, 2, 0, 0, 2, 0, 0, 1,],
[1, 2, 2, 0, 2, 2, 0, 0, 1, 1,],
[1, 1, 1, 0, 0, 0, 0, 1, 1, 1,],
[1, 1, 1, 0, 0, 1, 0, 0, 1, 1,],
[1, 1, 1, 1, 1, 1, 0, 0, 1, 1,],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1,],
], mapW = map.length, mapH = map[0].length;
$(document).ready(function(){
$('body').append('<div id="intro">JUNGLERUN</div>');
$('#intro').append('<div id="portalpic">FIND THE PORTAL</div><img src="images/portal.png">');
$('#intro').append('<div id="speedboostpic">LOOK FOR SPEEDBOOSTS</div><img src="images/speedboost.png">');
$('#intro').css({height: height}).on('click', function(e){
e.preventDefault();
$(this).fadeOut();
startGame();
});
});
function startGame(){
init();
animate();
}
//initialize game
function init() {
clock = new t.Clock(); //timer for rendering smooth animation
projector = new t.Projector(); //2d ray
scene = new t.Scene(); //world
scene.fog = new t.FogExp2(0xD6F1FF, 0.0010); //adds fog
//camera
cam = new t.PerspectiveCamera(60, aspect, 1, 10000); // FOV,
cam.position.y = unitsize * 0.2;
scene.add(cam);
//camera controls
controls = new t.FirstPersonControls(cam);
controls.movementSpeed = movespeed;
controls.lookSpeed = lookspeed;
controls.lookVertical = false;
controls.noFly = true;
//world objects
setupScene();
renderer = new t.WebGLRenderer();
renderer.setSize(width, height);
renderer.domElement.style.backgroundColor = '#d11d1d';
document.body.appendChild(renderer.domElement);
//Track Mouse position
document.addEventListener('mousemove', onDocumentMouseMove, false);
//display
$('body').append('<div id="hud">Score: <span id="score">'+score+'</span></div>');
$('body').append('<div id="timer">Time: <span id="times">'+time+'</span></div>');
//win condition
$('body').append('<div id="win"></div>');
$('#win').css({width: width, height: height});
//Countdown timer
}
function countdownTimer(){
if(finished == false){
time--;
$('#times').remove();
$('#timer').append('<span id="times">'+time+'</span></div>');
if(time == 0){
console.log('over!!')
clearInterval();
removeTimer();
}
}else{
handleEnd();
}
}
function removeTimer(){
userFinishTime = time;
handleEnd();
}
function animate() {
if(runAnim){
requestAnimationFrame(animate);
}
render();
}
function refreshScore(){
$('#score').remove()
$('#hud').append('<span id="score">'+score+'</span></div>');
}
function getSpeedBoost(){
$('body').append('<div id="speedBoostMsg">SPEED BOOST GET</div>');
$('body').append('<div id="addPoints">+200</div>');
$('#addPoints').delay(1000).fadeOut();
$('#speedBoostMsg').delay(2000).fadeOut();
removeSpeedBoost();
}
function removeSpeedBoost(){
$('#addPoints').remove;
$('#speedBoostMsg').remove;
}
function render() {
var delta = clock.getDelta(), speed = delta;
controls.update(delta);
//create timer
speedcube.rotation.x += 0.1;
speedcube.rotation.y += 0.1;
//win condition
if(distance(cam.position.x, cam.position.z, endportal.position.x, endportal.position.z) < 100){
userFinishTime = time;
finished = true;
runAnim = false;
handleWin();
}else{
countdownTimer();
}
//delay for speed pickup
if(!lastRunBoost){
if(distance(cam.position.x, cam.position.z, speedcube.position.x, speedcube.position.z) < 15){
movespeed = movespeed + 300;
controls.movementSpeed = movespeed;
lastRunBoost = true;
score += 200;
refreshScore();
getSpeedBoost();
}
speedcube.material.wireframe = false;
}else{
speedcube.material.wireframe = true;
}
renderer.render(scene, cam);
}
function setupScene(){
var units = mapW;
var floor = new t.Mesh(
new t.CubeGeometry(units * unitsize, 10, units * unitsize),
new t.MeshLambertMaterial({map: t.ImageUtils.loadTexture('images/lava-floor.jpg')})
);
scene.add(floor);
var cube = new t.CubeGeometry(unitsize, wallheight, unitsize);
var materials = [
new t.MeshLambertMaterial({map: t.ImageUtils.loadTexture('images/jungle.jpg')}),
new t.MeshLambertMaterial({map: t.ImageUtils.loadTexture('images/wall-2.jpg')}),
];
for(var i=0; i< mapW; i++){
for(var j=0, m=map[i].length; j<m; j++){
if(map[i][j]){
var wall = new t.Mesh(cube, materials[map[i][j]-1]);
wall.position.x = (i-units/2) * unitsize;
wall.position.y = wallheight/2;
wall.position.z = (j-units/2) * unitsize;
scene.add(wall);
}
}
}
//speed cube
speedcube = new t.Mesh(
new t.CubeGeometry(20, 20, 20),
new t.MeshBasicMaterial({map: t.ImageUtils.loadTexture('images/speed.png')})
);
speedcube.position.set(-unitsize-15, 35, -unitsize-15);
scene.add(speedcube);
//end portal
endportal = new t.Mesh(
new t.CubeGeometry(100, 100, 100),
new t.MeshBasicMaterial({map: t.ImageUtils.loadTexture('images/door.gif')})
);
endportal.position.set(80, 50, -1500);
scene.add(endportal);
//lights
var directionalLight1 = new t.DirectionalLight( 0xF7EFBE, 0.7);
directionalLight1.position.set(0.5, 1, 0.5);
scene.add(directionalLight1);
var directionalLight2 = new t.DirectionalLight( 0xF7EFBE, 0.5);
directionalLight2.position.set(-0.5, -1, -0.5);
scene.add(directionalLight2);
}
function distance(x1, y1, x2, y2) {
//slope
return Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
function getMapSector(v) {
var x = Math.floor((v.x + unitsize / 2) / unitsize + mapW/2);
var z = Math.floor((v.z + unitsize / 2) / unitsize + mapW/2);
return {x: x, z: z};
}
function checkWallCollision(v) {
var c = getMapSector(v);
return map[c.x][c.z] > 0;
}
function onDocumentMouseMove(e){
e.preventDefault();
mouse.x = (e.clientX/ width) * 2 -1;
mouse.y = -(e.clientY/ height) * 2 + 1;
}
function handleWin(){
score = score + time;
$('canvas').remove();
$('#hud').remove();
$('#timer').remove();
$('body').append('<div id="credits"><h2>Your Score: ' + score/2 + '</h2></div>');
setTimeout(function(){
$('#credits').append('<button id="nextLevel" onClick="newLevel()">NEXT LEVEL</button>');
}, 3000)
}
function handleEnd(){
$('canvas').remove();
$('#hud').remove();
$('#timer').remove();
$('body').append('<div id="credits"><h2>Total Score:' + score + '</h2></div>');
$('#credits').append('<h2>Game Over</h2>');
}
//window resize
function newLevel(){
finished = false;
runAnim = true;
location.reload();
}
$(window).resize(function(){
width = window.innerWidth;
height = window.innerHeight;
aspect = width/height;
if(cam){
cam.aspect = aspect;
cam.updateProjectionMatrix();
}
if(renderer){
renderer.setSize(width, height);
}
$('#intro, #win').css({width: width, height: height});
});
//removes moving when window isn in focus
$(window).focus(function() {
if (controls) controls.freeze = false;
});
$(window).blur(function() {
if (controls) controls.freeze = true;
});