-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
332 lines (284 loc) · 9.41 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
var scene, camera, renderer;
var floor, sun, sky, player; // global variables
// Variables related to games
var game = {
finished: false,
points: 0,
speed: 1,
spawnRate: 1,
input: { left: false, right: false }
};
var newTime = new Date().getTime();
var oldTime = new Date().getTime();
// Arrays to hold model objects
var obstacleTypes = [];
var obstacles = [];
var coins = [];
var Colors = {
red: 0xf25346,
white: 0xd8d0d1,
brown: 0x59332e,
pink: 0xF5986E,
brownDark: 0x23190f,
blue: 0x68c3c0,
gold: 0xffdf00,
sunsetOrange: 0xfe5b35
};
init();
function init() {
scene = new THREE.Scene();
// Initialize camera
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 20000);
camera.position.z = 14;
camera.position.y = 4;
var ambientLight = new THREE.AmbientLight(Colors.white);
scene.add(ambientLight);
// Add sun
var geomSun = new THREE.TetrahedronGeometry(8, 2);
var matSun = new THREE.MeshPhongMaterial({
color: Colors.red,
shininess: 0,
specular: 0xffffff,
shading: THREE.FlatShading
});
sun = new THREE.Mesh(geomSun, matSun);
sun.position.set(9, 20, -10);
sun.scale.set(0.25, 0.25, 0.25);
scene.add(sun);
// Set Sun as the source of directional light and intensity
// By Eucludian distance
var light = new THREE.DirectionalLight(Colors.sunsetOrange,
sun.position.distanceTo(new THREE.Vector3(0, 0, 0)));
light.position.set(sun.position);
scene.add(light);
// Loader to load up the textruers
var loader = new THREE.TextureLoader();
var grassTexture = loader.load('src/images/grasslight-small.jpg', function(texture) {
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.offset.set(0, 0);
texture.repeat.set(1, 512);
});
floor = new THREE.Mesh(
new THREE.PlaneGeometry(100, 100000, 8, 8),
new THREE.MeshLambertMaterial({
map: grassTexture
}));
floor.rotation.x -= Math.PI / 2;
floor.position.y -= 1
scene.add(floor);
var skyTexture = loader.load('src/images/sky.jpg', function(texture) {
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.offset.set(0, 0);
texture.repeat.set(1, 1);
});
sky = new THREE.Mesh(
new THREE.PlaneGeometry(10000, 1000, 8, 8),
new THREE.MeshBasicMaterial({
map: skyTexture
}));
sky.position.z = -60;
scene.add(sky);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth,
Math.round(window.innerHeight * 0.8));
// Load up the figure for the game
var objLoader = new THREE.OBJLoader();
var mtlLoader = new THREE.MTLLoader();
objLoader.setPath('models/');
mtlLoader.setPath('models/');
objLoader.load('Stick_Figure_by_Swp.OBJ',
function(object) {
object.position.set(1, 0, 8);
object.scale.set(0.5, 0.5, 0.5);
player = object;
scene.add(player);
}
);
loadObstacleTypes();
window.onload = function() {
document.body.appendChild(renderer.domElement);
document.addEventListener('keydown', function(event) {
if (event.keyCode === 37) game.input.left = true
if (event.keyCode === 39) game.input.right = true
});
document.addEventListener('keyup', function(event) {
if (event.keyCode === 37) game.input.left = false
if (event.keyCode === 39) game.input.right = false
});
render();
}
}
// The main rendering loop function
function render() {
if (!game.finished) {
floor.position.z += 0.3 * game.speed;
sky.position.x += 0.1;
procGenerateRocks();
moveObstacles();
moveCoins();
detectCollision();
updatePlayer();
game.speed += 0.0001;
game.spawnRate += 0.001;
renderer.render(scene, camera);
requestAnimationFrame(render);
}
}
function updatePlayer() {
if (game.input.left) {
if (player.position.x > -10) player.position.x -= 0.1;
player.rotation.y = 0.3;
} else if (game.input.right) {
if (player.position.x < 10) player.position.x += 0.1;
player.rotation.y = -0.3;
} else {
player.rotation.y = 0;
}
}
function procGenerateRocks() {
newTime = new Date().getTime();
if (newTime - oldTime > 2000) {
oldTime = new Date().getTime();
var spawnNum = Math.round(Math.random() * 10 * game.spawnRate);
var spawnedObs;
for (var i = 0; i < spawnNum; i++) {
spawnedObs = obstacleTypes[Math.floor(obstacleTypes.length * Math.random())];
spawnedObs.position.x = -20 + Math.random() * 30;
spawnedObs.position.z = -20;
spawnedObs.scale.set(1, 1, 1);
scene.add(spawnedObs);
obstacles.push(spawnedObs);
}
spawnNum = Math.round(Math.random() * 3 * game.spawnRate);
for (var i = 0; i < spawnNum; i++) {
spawnedObs = new Coin();
spawnedObs.mesh.position.x = -10 + Math.random() * 20;
spawnedObs.mesh.position.z = -20 - Math.random() * 10;
spawnedObs.mesh.position.y = 0.3;
spawnedObs.mesh.scale.set(0.05, 0.05, 0.05);
coins.push(spawnedObs);
scene.add(spawnedObs.mesh);
}
}
}
function moveObstacles() {
for (var i = 0; i < obstacles.length; i++) {
obstacles[i].position.z += 0.1 * game.speed;
if (obstacles[i].position.z > 9) {
scene.remove(obstacles[i]);
obstacles.splice(i, 1);
}
}
}
function moveCoins() {
for (var i = 0; i < coins.length; i++) {
coins[i].mesh.position.z += 0.3 * game.speed;
if (coins[i].mesh.position.z > 9) {
scene.remove(coins[i].mesh);
coins.splice(i, 1);
}
}
}
function detectCollision() {
for (var i = 0; i < obstacles.length; i++) {
if (player.position.distanceTo(obstacles[i].position) < 1) {
scene.remove(obstacles[i]);
obstacles.splice(i, 1);
initiateCollision("obstacle");
game.finished = true;
var score = document.getElementById("score");
score.innerHTML = "You lost with " + game.points + " points: ";
}
}
for (var i = 0; i < coins.length; i++) {
if (player.position.distanceTo(coins[i].mesh.position) < 1) {
scene.remove(coins[i].mesh);
coins.splice(i, 1);
initiateCollision("coin");
}
}
}
function initiateCollision(obj) {
if (obj == "obstacle") console.log("Encountered obs");
else if (obj == "coin") {
game.points += 1;
var score = document.getElementById("score");
score.innerHTML = game.points;
}
}
function Coin() {
var geom = new THREE.TetrahedronGeometry(8, 2);
var mat = new THREE.MeshPhongMaterial({
color: Colors.gold,
shininess: 0,
specular: 0xffffff,
shading: THREE.FlatShading
});
this.mesh = new THREE.Mesh(geom, mat);
this.mesh.scale.set(0.5, 0.5, 0.5);
this.mesh.castShadow = true;
this.angle = 0;
this.dist = 0;
}
function loadObstacleTypes() {
var objLoader = new THREE.OBJLoader();
var mtlLoader = new THREE.MTLLoader();
objLoader.setPath('models/');
mtlLoader.setPath('models/');
mtlLoader.load('Oak_Dark_01.mtl', function(materials) {
materials.preload();
objLoader.setMaterials(materials);
objLoader.load('Oak_Dark_01.obj', function(object) {
obstacleTypes.push(object);
});
});
mtlLoader.load('Tall_Rock_1_01.mtl', function(materials) {
materials.preload();
objLoader.setMaterials(materials);
objLoader.load('Tall_Rock_1_01.obj', function(object) {
obstacleTypes.push(object);
});
});
mtlLoader.load('Tall_Rock_2_01.mtl', function(materials) {
materials.preload();
objLoader.setMaterials(materials);
objLoader.load('Tall_Rock_2_01.obj', function(object) {
obstacleTypes.push(object);
});
});
mtlLoader.load('Tall_Rock_3_01.mtl', function(materials) {
materials.preload();
objLoader.setMaterials(materials);
objLoader.load('Tall_Rock_3_01.mtl.obj', function(object) {
obstacleTypes.push(object);
});
});
mtlLoader.load('Oak_Fall_01.mtl', function(materials) {
materials.preload();
objLoader.setMaterials(materials);
objLoader.load('Oak_Fall_01.obj', function(object) {
obstacleTypes.push(object);
});
});
mtlLoader.load('Oak_Green_01.mtl', function(materials) {
materials.preload();
objLoader.setMaterials(materials);
objLoader.load('Oak_Green_01.obj', function(object) {
obstacleTypes.push(object);
});
});
mtlLoader.load('Tree_01.mtl', function(materials) {
materials.preload();
objLoader.setMaterials(materials);
objLoader.load('Tree_01.obj', function(object) {
obstacleTypes.push(object);
});
});
mtlLoader.load('Tree_02.mtl', function(materials) {
materials.preload();
objLoader.setMaterials(materials);
objLoader.load('Tree_02.obj', function(object) {
obstacleTypes.push(object);
});
});
}