-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVaporwaveEndurance.html
423 lines (365 loc) · 16.7 KB
/
VaporwaveEndurance.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<html lang="en">
<head>
<title>Vaporwave Endurance</title>
<style>
body {
background-color: rgb(32, 18, 77);
position: absolute;
top: 47px;
left: 0px;
margin: 0px;
overflow: hidden;
}
p{
position: absolute;
top: -50px;
left: 100px;
font-family: "Courier New", 'Roboto Mono', monospace;
color: magenta;
z-index: 2;
}
</style>
</head>
<body>
<script type="text/javascript" src="ThreeJS/build/three.min.js"></script>
<script type="text/javascript" src="ThreeJS/examples/js/libs/stats.min.js"></script>
<script type="text/javascript" src="ThreeJS/examples/js/WebGL.js"></script>
<script type="text/javascript" src="ThreeJS/examples/js/loaders/OBJLoader.js"></script>
<script type="text/javascript" src="ThreeJS/examples/js/controls/PointerLockControls.js"></script>
<script type="text/javascript" src="ThreeJS/src/core/Object3D.js"></script>
<script type="text/javascript" src="js/Detector.js"></script>
<script type="text/javascript" src="js/Map.js"></script>
<script type="text/javascript" src="js/Creep.js"></script>
<script type="text/javascript" src="js/Score.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<p><span id="endGame"></span>Score:<span id="score"></span>     Cash:<span id="cash"></span>     Wave:<span id="wave"></span>     Tower Health:<span id="towerHealth"></span>     Cannon Damage:<span id="cannonDamage"></span>     Upgrade Available:<span id="upgradesAvailable"></span></p>
<script id="game" type="text/javascript">
//Variables used in endgame situations
let path = "fabiodis.github.io/finalproject-vaporwaveendurance/";
let winPath = "https://" + path + "WinPage.html";
let losePath = "https://" + path + "LosePage.html";
let flag = true;
let camera, renderer, container;
//Variables for printing datas on the top of the page
let outScore = document.getElementById("score");
let waveNum = document.getElementById("wave");
let outCash = document.getElementById("cash");
let outTowerHealth = document.getElementById("towerHealth");
let outCannonDamage = document.getElementById("cannonDamage");
let outAvailableUpgrades = document.getElementById("upgradesAvailable");
let endGameScreen = document.getElementById("endGame");
//Printing FPS
let stats = new Stats();
document.body.appendChild(stats.dom);
//Declaring hierarchical models
let base = new THREE.Group();
let base2 = new THREE.Group();
let canvas;
//Texture loader
let loaderObj = new THREE.OBJLoader();
let SCREEN_HEIGHT = window.innerHeight, SCREEN_WIDTH = window.innerWidth, MARGIN = 0;
let PLANE_HEIGHT = 10000, PLANE_WIDTH = 10000, PLANE_S_H_W = 50;
//Initializing THREE.js scene
let scene = new THREE.Scene();
scene.add(base);
let gameOn;
let theta = 45;
//Various declaration for bullets and animations
let speed = .01;
let speedModifier = 1;
let lastUpdate = new Date().getTime();
let bullet;
let bullets = [];
let emitter = new THREE.Object3D();
let speedBullet = 25000;
let clock = new THREE.Clock();
let delta = 0;
let counter = 0;
//Checking webgl
$(document).ready(function () {
if (!Detector.webgl)
Detector.addGetWebGLMessage();
else {
init();
animate();
function init() {
container = document.createElement('div');
container.setAttribute('id', "renderWindow");
container.style.position = "absolute";
container.style.top = '0';
container.style.left = '0';
document.body.appendChild(container);
//Initializing all
Map.initialize(PLANE_HEIGHT, PLANE_WIDTH, PLANE_S_H_W);
Map.generate();
Creep.initialize();
Score.initialize();
//Defining bullets
bullet = new THREE.Mesh(new THREE.SphereGeometry(10, 8, 4), new THREE.MeshBasicMaterial({color: Score.myColor}));
bullet.name = 'Bullet';
//Game parameters
gameOn = true;
setTimeout("Creep.runLevel()", 10000);
//Camera settings
camera = new THREE.PerspectiveCamera(60, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 60000);
camera.updateProjectionMatrix();
camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
//Light source
let ambientLight = new THREE.AmbientLight( 0xffffff );
scene.add(ambientLight);
//Audio player
let listener = new THREE.AudioListener();
camera.add( listener );
let sound = new THREE.Audio( listener );
let audioLoader = new THREE.AudioLoader();
audioLoader.load( 'song.ogg', function( buffer ) {
sound.setBuffer( buffer );
sound.setLoop( true );
sound.setVolume( 0.5 );
sound.play();
});
//Canvas definition
canvas = document.createElement('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let context = canvas.getContext('2d');
context.fillStyle = 'rgb( 200, 200, 200 )';
context.fillRect(0, 0, canvas.width, canvas.height);
//Drawing elements
drawBox();
drawElements();
//Some parameters for hierarchical models
base.position.set(0, 0, 100);
base.scale.set(0.8,0.8,0.8);
//Defining the end of the path
let rollOverGeo = new THREE.BoxGeometry( 160, 160, 160 );
rollOverMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000, opacity: 0.4, transparent: false } );
rollOverMesh = new THREE.Mesh( rollOverGeo, rollOverMaterial );
rollOverMesh.position.set(0, 0, 100);
scene.add( rollOverMesh );
//Adding webgl renderer
renderer = new THREE.WebGLRenderer();
renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
renderer.sortObjects = false;
renderer.autoClear = false;
container.appendChild(renderer.domElement);
//Adding controls for the visual
let controls = new THREE.PointerLockControls(camera, renderer.domElement);
//let controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.rotateSpeed = 2;
//controls.enableZoom = false;
//controls.enablePan = false;
//controls.target.set(0, 1200 , 100);
//Other camera settings
camera.position.x = base.position.x;
camera.position.y = base.position.y+1300;
camera.position.z = base.position.z;
camera.lookAt(new THREE.Vector3(0,1200,-100));
camera.add(base2);
//Adding the bullet emitter in front of the cannon
emitter.position.set(2, -1, -5);
camera.add(emitter);
//Adding the crosshair to the camera
let crosshair = new THREE.RingGeometry( 10, 14, 16 );
let ch_material = new THREE.MeshBasicMaterial( { color: 0xff0000, side: THREE.DoubleSide } );
let ch_mesh = new THREE.Mesh( crosshair, ch_material );
ch_mesh.position.set(0, 0, -1000);
camera.add( ch_mesh );
scene.add(camera);
//Event listeners
window.addEventListener('resize', onWindowResize, false);
window.addEventListener("mousemove", function(){}, false);
window.addEventListener("mousedown", onMouseDown, false);
window.addEventListener("mouseup", onMouseUp, false);
window.addEventListener( 'click', function () {
controls.lock();
}, false );
window.addEventListener('keydown',function(strut){
//Checking if P key is down(for pause) or U key is down (for upgrades)
temp = `${strut.code}`;
switch(temp){
case 'KeyP':
gameOn = !gameOn;
if(gameOn){
setTimeout("Creep.runLevel()", 10000);
}
break;
case 'KeyU':{
Score.upgrade();
bullet = new THREE.Mesh(new THREE.SphereGeometry(10, 8, 4), new THREE.MeshBasicMaterial({color: Score.myColor}));
bullet.name = 'Bullet';
}
}
}, false);
}
//All of these functions are commented in the report
function checkCollision(){
for (let i = bullets.length - 1; i >= 0; i--){
let b = bullets[i];
let p = b.getWorldPosition();
for (let j = Creep.creeps.length - 1; j >= 0; j-- ){
let a = Creep.creeps[j];
let c = a.getWorldPosition();
if(p.x <= c.x + 380 && p.x >= c.x - 380 && p.z <= c.z + 380 && p.z >= c.z -380 && p.y <= c.y + 330 && p.y >= c.y - 330){
if(gameOn === true) {
Creep.isHit(j,Score.damage);
bullets.splice(i,1);
scene.remove(b);
break;
}
}
}
}
}
function onMouseDown(){
bullet.quaternion.copy(camera.quaternion);
bullet.position.copy(emitter.getWorldPosition());
scene.add(bullet);
bullets.push(bullet);
if(Score.winCondition === true){
if(flag === true){
window.open(winPath);
flag = false;
gameOn = false;
}
}
if(Score.loseCondition === true){
if(flag === true){
window.open(losePath);
flag = false;
gameOn = false;
}
}
}
function onMouseUp(){
}
function onWindowResize() {
SCREEN_HEIGHT = window.innerHeight;
SCREEN_WIDTH = window.innerWidth;
renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
camera.updateProjectionMatrix();
camera.radius = (SCREEN_WIDTH + SCREEN_HEIGHT) / 4;
}
function drawBox() {
let cube = new THREE.BoxGeometry(30000, 30000, 30000);
let cubeMaterials = [
// back side
new THREE.MeshBasicMaterial({
map: new THREE.TextureLoader().load('textures/side.jpg'),
side: THREE.DoubleSide
}),
// front side
new THREE.MeshBasicMaterial({
map: new THREE.TextureLoader().load('textures/side.jpg'),
side: THREE.DoubleSide
}),
// Top side
new THREE.MeshBasicMaterial({
map: new THREE.TextureLoader().load('textures/top.png'),
side: THREE.DoubleSide
}),
// Bottom side
new THREE.MeshBasicMaterial({
map: new THREE.TextureLoader().load('textures/grid.png'),
side: THREE.DoubleSide
}),
// right side
new THREE.MeshBasicMaterial({
map: new THREE.TextureLoader().load('textures/side.jpg'),
side: THREE.DoubleSide
}),
// left side
new THREE.MeshBasicMaterial({
map: new THREE.TextureLoader().load('textures/side.jpg'),
side: THREE.DoubleSide
})
];
let cubeMaterial = new THREE.MeshFaceMaterial(cubeMaterials);
let mesh = new THREE.Mesh(cube, cubeMaterial);
scene.add(mesh);
}
function drawElements() {
let parameters = new THREE.TextureLoader().load('textures/dolphin.jpg');
parameters.wrapS = THREE.RepeatWrapping;
parameters.wrapT = THREE.RepeatWrapping;
parameters.repeat.set(2, 2);
let parameters1 = new THREE.TextureLoader().load('textures/tower.jpg');
parameters1.wrapS = THREE.RepeatWrapping;
parameters1.wrapT = THREE.RepeatWrapping;
parameters1.repeat.set(2, 2);
let material = new THREE.MeshLambertMaterial({map: parameters});
let material1 = new THREE.MeshLambertMaterial({map: parameters1});
loaderObj.load('models/tower.obj', function (tower) {
tower.scale.x = 2000;
tower.scale.y = 2000;
tower.scale.z = 2000;
tower.rotation.y = Math.PI;
tower.traverse(function (node) {
if (node.isMesh) node.material = material1;
});
base.add(tower);
});
loaderObj.load('models/cannon.obj', function (cannon) {
cannon.scale.x = 1;
cannon.scale.y = 1;
cannon.scale.z = 1;
cannon.position.set(2, -.5, -1);
cannon.rotation.y = Math.PI * - 0.5;
cannon.rotation.x = Math.PI * -0.06;
cannon.traverse(function (node) {
if (node.isMesh) node.material = material;
});
base2.add(cannon);
});
}
function refreshValues(){
outScore.innerHTML = Score.getScore();
waveNum.innerHTML = Creep.getWave();
outCash.innerHTML = Score.getCash();
outTowerHealth.innerHTML = Score.getHealth();
outCannonDamage.innerHTML = Score.getDamage();
outAvailableUpgrades.innerHTML = Score.upgradeCheck();
}
function animate() {
requestAnimationFrame(animate);
delta = clock.getDelta();
checkCollision();
refreshValues();
stats.update();
render();
}
function render() {
counter = 0;
bullets.forEach(b => {
b.translateZ(-speedBullet* delta);
if (b.position.y <= 300){
scene.remove(b);
bullets.splice(counter, 1);
}
counter++;
});
if (gameOn === true) {
let now = new Date().getTime();
dt = (now - lastUpdate) / 1000;
if (dt > speed){
Creep.update();
lastUpdate = now;
}
}
if (gameOn === false) {
if (Score.loseCondition === true) {
endGameScreen.innerHTML = "You lose. Click everywhere to exit the game. Here are your results: "
}
if (Score.winCondition === true) {
endGameScreen.innerHTML = "You win! Click everywhere to exit the game. Here are your results: "
}
}
renderer.clear();
renderer.render(scene, camera);
}
}
});
</script>
</body>
</html>