-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorld.html
More file actions
730 lines (631 loc) · 34.4 KB
/
Copy pathWorld.html
File metadata and controls
730 lines (631 loc) · 34.4 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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Daniel Arlt</title>
<style>
html, body {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#renderCanvas {
width: 100%;
height: 100%;
touch-action: none;
}
</style>
<script src="https://preview.babylonjs.com/babylon.js"></script>
<script src="https://preview.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>
<script src="https://code.jquery.com/pep/0.4.3/pep.js"></script>
<script src="https://cdn.babylonjs.com/Oimo.js"></script>
<script src="https://cdn.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>
<script src="https://preview.babylonjs.com/gui/babylon.gui.min.js"></script>
</head>
<body>
<canvas id="renderCanvas" touch-action="none"></canvas> //touch-action="none" for best results from PEP
<script>
let canvas = document.getElementById("renderCanvas"); // Get the canvas element
let engine = new BABYLON.Engine(canvas, true); // Generate the BABYLON 3D engine
/******* Add the create scene function ******/
var createScene = function () {
let gun, ground, viewModel, blueMat, grid, gunMat, target, targetMat, target2Mat, ray, gunshot,
impact, music, ding, splash, cooldown, bridge, bridge2, grid2, targetNum = 0, hud, win;
let targetList = [];
// Setup scene
var scene = new BABYLON.Scene(engine);
scene.collisionsEnabled = true;
scene.gravity = new BABYLON.Vector3(0, -0.3, 0);
// Initialize particle systems
var smokeSystem = new BABYLON.ParticleSystem("smoke", 100, scene);
var sparkSystem = new BABYLON.ParticleSystem("spark", 1000, scene);
var hitSystem = new BABYLON.ParticleSystem("hit", 1000, scene);
// Setup camera
var camera = new BABYLON.UniversalCamera("UniversalCamera", new BABYLON.Vector3(0, 7, -50), scene);
//camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 2, 2, new BABYLON.Vector3(0,20,0), scene);
// Allow control of camera with WASD
camera.keysDown.push(83);
camera.keysUp.push(87);
camera.keysLeft.push(65);
camera.keysRight.push(68);
camera._needMoveForGravity = true;
camera.speed = 0.5;
camera.fov = 1.25;
camera.applyGravity = true;
camera.ellipsoid = new BABYLON.Vector3(1.25, 1.5, 1.25);
camera.checkCollisions = true;
camera.attachControl(canvas, true);
camera.inputs.remove(camera.inputs.attached.mouse);
function setupLights() {
var light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(1, 1, 0), scene);
}
function setupMaterials() {
blueMat = new BABYLON.StandardMaterial("blueMat", scene);
blueMat.diffuseColor = new BABYLON.Color3(0, 1, 1);
blueMat.specularColor = new BABYLON.Color3(1, 1, 1);
grid = new BABYLON.StandardMaterial("grid", scene);
grid.diffuseTexture = new BABYLON.Texture("Texture/grid1.png", scene);
grid.specularColor = new BABYLON.Color3(0.2, 0.2, 0.2);
grid2 = new BABYLON.StandardMaterial("grid", scene);
grid2.diffuseTexture = new BABYLON.Texture("Texture/grid2.png", scene);
grid2.specularColor = new BABYLON.Color3(0.2, 0.2, 0.2);
gunMat = new BABYLON.StandardMaterial("gunMat", scene);
gunMat.diffuseTexture = new BABYLON.Texture("Gun/blend/textures/handgun_C.jpg", scene);
gunMat.specularColor = new BABYLON.Color3(0.2, 0.2, 0.2);
targetMat = new BABYLON.StandardMaterial("targetMat", scene);
targetMat.diffuseTexture = new BABYLON.Texture("Texture/Target.png", scene);
targetMat.specularColor = new BABYLON.Color3(0.2, 0.2, 0.2);
target2Mat = new BABYLON.StandardMaterial("targetMat", scene);
target2Mat.diffuseTexture = new BABYLON.Texture("Texture/Target2.png", scene);
target2Mat.specularColor = new BABYLON.Color3(0.2, 0.2, 0.2);
}
function setupWater() {
water = BABYLON.Mesh.CreateGroundFromHeightMap("water", "HeightMap/watermap.jpg", 500, 100, 512, 0, 2, scene, false);
water.material = blueMat;
water.position.y = 0;
water.visibility = 0.5;
}
function setupGround() {
BABYLON.SceneLoader.ImportMesh(null, "./", "HeightMap/heightMap2.babylon", scene, function (meshes, particleSystems, skeletons) {
ground = meshes[0];
ground.scaling = new BABYLON.Vector3(50, 15, 50);
ground.checkCollisions = true;
ground.position.y = 2;
ground.material = grid;
})
}
function setupViewModel() {
BABYLON.SceneLoader.ImportMesh(null, "./", "Gun/Blend/Handgun_Packed3.babylon", scene, function (meshes, particleSystems, skeletons) {
gun = meshes[0];
//gun.position = new BABYLON.Vector3(1.2, 4.25, 52);
gun.rotation.y = -Math.PI/2;
gun.material = gunMat;
gun.setParent(camera);
gun.isPickable = false;
smokeSystem.emitter = gun;
sparkSystem.emitter = gun;
})
}
function setupBridge() {
BABYLON.SceneLoader.ImportMesh(null, "./", "Models/bridge.babylon", scene, function (meshes, particleSystems, skeletons) {
bridge = meshes[0];
bridge.position = new BABYLON.Vector3(12, -1.2, 0);
bridge.material = grid2;
bridge.checkCollisions = true;
bridge.scaling = new BABYLON.Vector3(3.2, 3.2, 3.185);
bridge2 = bridge.clone();
bridge2.position = new BABYLON.Vector3(-12, -1.2, 0);
})
}
function setupWall(position, rotx, roty, rotz, ht, wd) {
var wall = new BABYLON.MeshBuilder.CreateBox("wall", {height: ht, width: wd, depth: 2}, scene);
wall.position = position;
wall.rotation.x = rotx;
wall.rotation.y = roty;
wall.rotation.z = rotz;
wall.material = grid2;
wall.checkCollisions = true;
}
function setupWalls() {
// Spawn side
var w1 = setupWall(new BABYLON.Vector3(-25, 5, -81), 0, Math.PI/4, 0, 8, 60);
var w2 = setupWall(new BABYLON.Vector3(25, 5, -81), 0, -Math.PI/4, 0, 8, 60);
var w3 = setupWall(new BABYLON.Vector3(75, 5, -40), 0, 0, 0, 8, 60);
var w4 = setupWall(new BABYLON.Vector3(-75, 5, -40), 0, 0, 0, 8, 60);
var w5 = setupWall(new BABYLON.Vector3(106, 5, -59), 0, Math.PI/2, 0, 8, 40);
var w6 = setupWall(new BABYLON.Vector3(-106, 5, -59), 0, Math.PI/2, 0, 8, 40);
var w7 = setupWall(new BABYLON.Vector3(87, 5, -80), 0, 0, 0, 8, 40);
var w8 = setupWall(new BABYLON.Vector3(-87, 5, -80), 0, 0, 0, 8, 40);
var w9 = setupWall(new BABYLON.Vector3(66, 2.5, -61), 0, Math.PI/2, 0, 4, 40);
var w10 = setupWall(new BABYLON.Vector3(-66, 2.5, -61), 0, Math.PI/2, 0, 4, 40);
var w11 = setupWall(new BABYLON.Vector3(66, 7.5, -61), 0, Math.PI/2, 0, 3, 40);
var w12 = setupWall(new BABYLON.Vector3(-66, 7.5, -61), 0, Math.PI/2, 0, 3, 40);
var w13 = setupWall(new BABYLON.Vector3(137, 5, -40), 0, 0, 0, 8, 60);
var w14 = setupWall(new BABYLON.Vector3(-137, 5, -40), 0, 0, 0, 8, 60);
var w13 = setupWall(new BABYLON.Vector3(166, 5, -71), 0, Math.PI/2, 0, 8, 60);
var w14 = setupWall(new BABYLON.Vector3(-166, 5, -71), 0, Math.PI/2, 0, 8, 60);
var w15 = setupWall(new BABYLON.Vector3(145.5, 4.535, -97), Math.PI/2.25, Math.PI/2, Math.PI/2, 8, 40);
var w16 = setupWall(new BABYLON.Vector3(-145.5, 4.535, -97), -Math.PI/2.25, Math.PI/2, Math.PI/2, 8, 40);
var w17 = setupWall(new BABYLON.Vector3(186.5, 4.535, -97), -Math.PI/2.25, Math.PI/2, Math.PI/2, 8, 40);
var w18 = setupWall(new BABYLON.Vector3(-186.5, 4.535, -97), Math.PI/2.25, Math.PI/2, Math.PI/2, 8, 40);
var w19 = setupWall(new BABYLON.Vector3(86.3, 1.875, -60), Math.PI/2.5, Math.PI/2, Math.PI/2, 8, 40);
var w20 = setupWall(new BABYLON.Vector3(-86.3, 1.875, -60),- Math.PI/2.5, Math.PI/2, Math.PI/2, 8, 40);
// Opposite side
var w21 = setupWall(new BABYLON.Vector3(-25, 5, 81), 0, -Math.PI/4, 0, 8, 60);
var w22 = setupWall(new BABYLON.Vector3(25, 5, 81), 0, Math.PI/4, 0, 8, 60);
var w23 = setupWall(new BABYLON.Vector3(75, 5, 40), 0, 0, 0, 8, 60);
var w24 = setupWall(new BABYLON.Vector3(-75, 5, 40), 0, 0, 0, 8, 60);
var w25 = setupWall(new BABYLON.Vector3(106, 5, 59), 0, Math.PI/2, 0, 8, 40);
var w26 = setupWall(new BABYLON.Vector3(-106, 5, 59), 0, Math.PI/2, 0, 8, 40);
var w27 = setupWall(new BABYLON.Vector3(87, 5, 80), 0, 0, 0, 8, 40);
var w28 = setupWall(new BABYLON.Vector3(-87, 5, 80), 0, 0, 0, 8, 40);
var w29 = setupWall(new BABYLON.Vector3(66, 2.5, 61), 0, Math.PI/2, 0, 4, 40);
var w30 = setupWall(new BABYLON.Vector3(-66, 2.5, 61), 0, Math.PI/2, 0, 4, 40);
var w31 = setupWall(new BABYLON.Vector3(66, 7.5, 61), 0, Math.PI/2, 0, 3, 40);
var w32 = setupWall(new BABYLON.Vector3(-66, 7.5, 61), 0, Math.PI/2, 0, 3, 40);
var w33 = setupWall(new BABYLON.Vector3(137, 5, 40), 0, 0, 0, 8, 60);
var w34 = setupWall(new BABYLON.Vector3(-137, 5, 40), 0, 0, 0, 8, 60);
var w35 = setupWall(new BABYLON.Vector3(166, 5, 71), 0, Math.PI/2, 0, 8, 60);
var w36 = setupWall(new BABYLON.Vector3(-166, 5, 71), 0, Math.PI/2, 0, 8, 60);
var w37 = setupWall(new BABYLON.Vector3(145.5, 4.535, 97), Math.PI/2.25, Math.PI/2, Math.PI/2, 8, 40);
var w38 = setupWall(new BABYLON.Vector3(-145.5, 4.535, 97), -Math.PI/2.25, Math.PI/2, Math.PI/2, 8, 40);
var w39 = setupWall(new BABYLON.Vector3(186.5, 4.535, 97), -Math.PI/2.25, Math.PI/2, Math.PI/2, 8, 40);
var w40 = setupWall(new BABYLON.Vector3(-186.5, 4.535, 97), Math.PI/2.25, Math.PI/2, Math.PI/2, 8, 40);
var w41 = setupWall(new BABYLON.Vector3(86.3, 1.875, 60), Math.PI/2.5, Math.PI/2, Math.PI/2, 8, 40);
var w42 = setupWall(new BABYLON.Vector3(-86.3, 1.875, 60),- Math.PI/2.5, Math.PI/2, Math.PI/2, 8, 40);
}
function setupSprites() {
var spriteManagerTrees = new BABYLON.SpriteManager("treesManager", "Sprites/tree.png", 100, {width: 1787, height: 2260}, scene);
for (let step = -225; step < 226; step += 25) {
var tree1 = setupTree(spriteManagerTrees, new BABYLON.Vector3(step, 9.6, -28), 17)
var tree2 = setupTree(spriteManagerTrees, new BABYLON.Vector3(step, 9.6, 28), 17)
}
}
function setupTree(spriteManagerTrees, position, size) {
var tree = new BABYLON.Sprite("sprite", spriteManagerTrees);
tree.position = position;
tree.size = size;
}
function setupTarget(position, rotx, roty) {
target = new BABYLON.MeshBuilder.CreateCylinder("target", {diameter: 2, subdivisions: 32, height: 0.2}, scene);
target.position = position;
target.rotation.x = rotx;
target.rotation.y = roty;
target.uniqueId = 1;
target.scaling = new BABYLON.Vector3(2, 2, 2);
target.material = targetMat;
target.checkCollisions = true;
targetNum++;
targetList.push(target);
return target;
}
function setupMovingTarget(position, rotx, roty, dist) {
target = setupTarget(position, rotx, roty);
var animate = new BABYLON.Animation("translate", "position.x", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
var keys = new Array();
keys.push({
frame: 0,
value: target.position.x
});
keys.push({
frame: 50,
value: target.position.x + dist
});
keys.push({
frame: 100,
value: target.position.x
});
animate.setKeys(keys);
target.animations.push(animate);
scene.beginAnimation(target, 0, 100, true);
}
function hideTargets() {
// Bridge targets
var t1 = setupTarget(new BABYLON.Vector3(7.5, 2, -0.15), Math.PI/2, Math.PI/2);
var t2 = setupTarget(new BABYLON.Vector3(16.5, 2, -0.15), Math.PI/2, Math.PI/2);
var t3 = setupTarget(new BABYLON.Vector3(-7.5, 2, -0.15), Math.PI/2, Math.PI/2);
var t4 = setupTarget(new BABYLON.Vector3(-16.5, 2, -0.15), Math.PI/2, Math.PI/2);
// Spawn side targets
var t5 = setupTarget(new BABYLON.Vector3(0, 7, -101), Math.PI/2, 0);
var t6 = setupTarget(new BABYLON.Vector3(105, 6, -53), Math.PI/2, Math.PI/2);
var t7 = setupTarget(new BABYLON.Vector3(-105, 6, -53), Math.PI/2, Math.PI/2);
var t8 = setupTarget(new BABYLON.Vector3(105, 6, -45), Math.PI/2, Math.PI/2);
var t9 = setupTarget(new BABYLON.Vector3(-105, 6, -45), Math.PI/2, Math.PI/2);
var t10 = setupTarget(new BABYLON.Vector3(105, 6, -67), Math.PI/2, Math.PI/2);
var t11 = setupTarget(new BABYLON.Vector3(-105, 6, -67), Math.PI/2, Math.PI/2);
var t12 = setupTarget(new BABYLON.Vector3(105, 6, -75), Math.PI/2, Math.PI/2);
var t13 = setupTarget(new BABYLON.Vector3(-105, 6, -75), Math.PI/2, Math.PI/2);
var t14 = setupTarget(new BABYLON.Vector3(11, 6, -99), Math.PI/2, Math.PI/2);
var t15 = setupTarget(new BABYLON.Vector3(-11, 6, -99), Math.PI/2, Math.PI/2);
var t16 = setupTarget(new BABYLON.Vector3(169, 4.5, -101), Math.PI/2, 0);
var t17 = setupTarget(new BABYLON.Vector3(-169, 4.5, -101), Math.PI/2, 0);
// Moving targets
var t18 = setupMovingTarget(new BABYLON.Vector3(112, 6, -43), Math.PI/2, 0, 48);
var t18 = setupMovingTarget(new BABYLON.Vector3(-160, 6, -43), Math.PI/2, 0, 48);
// Opposite side targets
var t19 = setupTarget(new BABYLON.Vector3(0, 7, 101), Math.PI/2, 0);
var t20 = setupTarget(new BABYLON.Vector3(105, 6, 53), Math.PI/2, Math.PI/2);
var t21 = setupTarget(new BABYLON.Vector3(-105, 6, 53), Math.PI/2, Math.PI/2);
var t22 = setupTarget(new BABYLON.Vector3(105, 6, 45), Math.PI/2, Math.PI/2);
var t23 = setupTarget(new BABYLON.Vector3(-105, 6, 45), Math.PI/2, Math.PI/2);
var t24 = setupTarget(new BABYLON.Vector3(105, 6, 67), Math.PI/2, Math.PI/2);
var t25 = setupTarget(new BABYLON.Vector3(-105, 6, 67), Math.PI/2, Math.PI/2);
var t26 = setupTarget(new BABYLON.Vector3(105, 6, 75), Math.PI/2, Math.PI/2);
var t27 = setupTarget(new BABYLON.Vector3(-105, 6, 75), Math.PI/2, Math.PI/2);
var t28 = setupTarget(new BABYLON.Vector3(11, 6, 99), Math.PI/2, Math.PI/2);
var t29 = setupTarget(new BABYLON.Vector3(-11, 6, 99), Math.PI/2, Math.PI/2);
var t30 = setupTarget(new BABYLON.Vector3(169, 4.5, 101), Math.PI/2, 0);
var t31 = setupTarget(new BABYLON.Vector3(-169, 4.5, 101), Math.PI/2, 0);
// Moving targets
var t32 = setupMovingTarget(new BABYLON.Vector3(112, 6, 43), Math.PI/2, 0, 48);
var t33 = setupMovingTarget(new BABYLON.Vector3(-160, 6, 43), Math.PI/2, 0, 48);
}
function setupParticles() {
// Smoke
smokeSystem.particleTexture = new BABYLON.Texture("Particles/smoke.png", scene);
smokeSystem.targetStopDuration = 0.5;
smokeSystem.isLocal = true;
smokeSystem.minEmitBox = new BABYLON.Vector3(1.4, .7, 0);
smokeSystem.maxEmitBox = new BABYLON.Vector3(1.4, .7, 0);
smokeSystem.colorDead = new BABYLON.Color4(1, 1, 1, 0)
smokeSystem.blendMode = BABYLON.ParticleSystem.BLENDMODE_STANDARD;
smokeSystem.emitRate = 100;
smokeSystem.minSize = 0.1;
smokeSystem.maxSize = 0.2;
smokeSystem.addSizeGradient(0, 0.25);
smokeSystem.addSizeGradient(0.25, 0.5);
smokeSystem.minLifeTime = 0.3;
smokeSystem.maxLifeTime = 0.4;
smokeSystem.minAngularSpeed = -Math.PI;
smokeSystem.maxAngularSpeed = Math.PI;
smokeSystem.minEmitPower = 0.5;
smokeSystem.maxEmitPower = 2;
smokeSystem.updateSpeed = 0.0075;
// Spark
sparkSystem.particleTexture = new BABYLON.Texture("Particles/spark.png", scene);
sparkSystem.targetStopDuration = 0.02;
sparkSystem.isLocal = true;
sparkSystem.minEmitBox = new BABYLON.Vector3(1.4, .7, 0);
sparkSystem.maxEmitBox = new BABYLON.Vector3(1.4, .7, 0);
sparkSystem.colorDead = new BABYLON.Color4(1, 1, 1, 0)
sparkSystem.blendMode = BABYLON.ParticleSystem.BLENDMODE_STANDARD;
sparkSystem.emitRate = 50;
sparkSystem.minSize = 0.1;
sparkSystem.maxSize = 0.2;
sparkSystem.addSizeGradient(0.1, 0.2);
sparkSystem.addSizeGradient(0.2, 0.3);
sparkSystem.minLifeTime = 0.9;
sparkSystem.maxLifeTime = 1;
sparkSystem.minAngularSpeed = -Math.PI;
sparkSystem.maxAngularSpeed = Math.PI;
sparkSystem.direction1 = new BABYLON.Vector3(1, -0.4, -0.4);
sparkSystem.direction2 = new BABYLON.Vector3(1, 0.4, 0.4);
sparkSystem.minEmitPower = 0.5;
sparkSystem.maxEmitPower = 0.5;
sparkSystem.updateSpeed = 0.075;
// Hit
hitSystem.particleTexture = new BABYLON.Texture("Particles/spark.png", scene);
hitSystem.targetStopDuration = 0.05;
hitSystem.isLocal = true;
hitSystem.minEmitBox = new BABYLON.Vector3(0, 0, 0);
hitSystem.maxEmitBox = new BABYLON.Vector3(0, 0, 0);
hitSystem.colorDead = new BABYLON.Color4(1, 1, 1, 0)
hitSystem.blendMode = BABYLON.ParticleSystem.BLENDMODE_STANDARD;
hitSystem.emitRate = 50;
hitSystem.minSize = 0.1;
hitSystem.maxSize = 0.2;
hitSystem.addSizeGradient(0.1, 0.2);
hitSystem.addSizeGradient(0.2, 0.3);
hitSystem.minLifeTime = 0.4;
hitSystem.maxLifeTime = 0.5;
hitSystem.minAngularSpeed = -Math.PI;
hitSystem.maxAngularSpeed = Math.PI;
hitSystem.minEmitPower = 0.5;
hitSystem.maxEmitPower = 0.5;
hitSystem.updateSpeed = 0.01;
}
function setupSounds() {
// Setup gunshot sound
gunshot = new BABYLON.Sound("Gunshot", "Sounds/pistol_fire2.mp3", scene, null, {
volume: 0.5
});
// Setup impact sound
impact = new BABYLON.Sound("Impact", "Sounds/ric1.wav", scene, null, {
volume: 0.6,
spatialSound: true,
distanceModel: "inverse"
});
// Setup background music
music = new BABYLON.Sound("Music", "Sounds/music.wav", scene, null, {
volume: 0.1,
loop: true,
autoplay: true
});
// Setup target ding
ding = new BABYLON.Sound("Ding", "Sounds/Ding.mp3", scene, null, {
volume: 0.3,
});
// Setup splash
splash = new BABYLON.Sound("Splash", "Sounds/splash.mp3", scene, null, {
volume: 0.3,
});
// Setup cooldown
cooldown = new BABYLON.Sound("Cooldown", "Sounds/cooldown.wav", scene, null, {
volume: 0.3,
});
// Setup win
win = new BABYLON.Sound("Cooldown", "Sounds/party.mp3", scene, null, {
volume: 0.5,
});
}
function setupGUI() {
// GUI
var advancedTexture = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");
// This shouldn't be a button, but I'm lazy and it works
hud = BABYLON.GUI.Button.CreateSimpleButton("but1", "Targets remaining: " + targetNum);
hud.width = "200px"
hud.height = "50px";
hud.color = "black";
hud.cornerRadius = 10;
hud.background = "red";
hud.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_TOP;
advancedTexture.addControl(hud);
}
function setupSkybox() {
var skybox = BABYLON.MeshBuilder.CreateBox("skyBox", {size:1000.0}, scene);
var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
skyboxMaterial.backFaceCulling = false;
skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("Skybox/Skybox", scene, ["_px.png", "_py.png", "_pz.png", "_nx.png", "_ny.png", "_nz.png"]);
skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
skybox.material = skyboxMaterial;
}
function setupOrientationLines() {
let xLine = BABYLON.MeshBuilder.CreateLines("xLine", {
points: [ new BABYLON.Vector3(0, 0.01, 0), new BABYLON.Vector3(4, 0.01, 0) ],
colors: [new BABYLON.Color4(1, 0, 0, 1), new BABYLON.Color4(1, 0, 0, 1)]
}, scene);
let yLine = BABYLON.MeshBuilder.CreateLines("yLine", {
points: [ new BABYLON.Vector3(0, 0.01, 0), new BABYLON.Vector3(0, 4.01, 0) ],
colors: [new BABYLON.Color4(0, 1, 0, 1), new BABYLON.Color4(0, 1, 0, 1)]
}, scene);
let zLine = BABYLON.MeshBuilder.CreateLines("zLine", {
points: [ new BABYLON.Vector3(0, 0.01, 0), new BABYLON.Vector3(0, 0.01, 4) ],
colors: [new BABYLON.Color4(0, 0, 1, 1), new BABYLON.Color4(0, 0, 1, 1)]
}, scene);
}
// Render functions
setupLights();
setupMaterials();
setupWater();
setupGround();
hideTargets();
setupBridge();
setupWalls();
setupViewModel();
setupSprites();
setupParticles();
setupSounds();
setupGUI();
setupSkybox();
//setupOrientationLines();
let shots = 0;
let shotTime = 1000000000000;
let canShoot = true;
function shoot() {
if (canShoot) {
var animation = new BABYLON.Animation("shoot", "rotation.z", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
var keys = [
{
frame: 0,
value: 0
},
{
frame: 2,
value: Math.PI/12
},
{
frame: 10,
value: 0
}
];
//gun.rotate(BABYLON.Axis.Z, Math.PI/12);
animation.setKeys(keys);
var easingFunction = new BABYLON.SineEase();
easingFunction.setEasingMode(BABYLON.EasingFunction.EASINGMODE_EASEOUT);
animation.setEasingFunction(easingFunction);
gun.animations.push(animation);
gunshot.play();
// Sparks only work some of the time. idk why.
sparkSystem.start();
var today = new Date();
if (Math.floor(today.getTime()/100) - shotTime < 3) {
shots++
if (shots > 5) {
smokeSystem.start();
cooldown.play();
canShoot = false;
}
shotTime = Math.floor(today.getTime()/100)
}
else {
shots = 0;
shotTime = 1000000000000;
}
scene.beginAnimation(gun, 0, 10, false, 2);
return true;
}
else {
setTimeout(() => { canShoot = true; }, 1300)
return false;
}
}
let canSplash = true;
scene.registerBeforeRender(function() {
ray = camera.getForwardRay();
// Handle "floating" in water by inverting gravity
if (camera.position.y < 2) {
scene.gravity = new BABYLON.Vector3(0, 0.05, 0);
if (canSplash) {
splash.play();
canSplash = false;
}
}
else if (camera.position.y > 4) {
scene.gravity = new BABYLON.Vector3(0, -0.3, 0);
canSplash = true;
}
});
// Used for stopping holding left mouse from triggering multiple animations
var lPointerDown = false;
scene.onPointerObservable.add((pointerInfo) => {
// mouse move when pointer is locked
if(document.pointerLockElement === canvas || document.mozPointerLockElement === canvas) {
var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
// apply rotation to active camera
scene.activeCamera.rotation.y += movementX * 0.002;
scene.activeCamera.rotation.x += movementY * 0.002;
scene.activeCamera.rotation.x = Math.max(-Math.PI / 2, Math.min(Math.PI / 2, scene.activeCamera.rotation.x));
}
// pointerInfo.event.button == 2 RIGHT mouse button
if (pointerInfo.event.buttons & 2) {
// Aim-in while holding right click
camera.fov = 1;
gun.position = new BABYLON.Vector3(0, -0.75, 1.5);
} else if ((pointerInfo.event.buttons & 2) === 0) {
// Revert back to original view on release
camera.fov = 1.25;
gun.position = new BABYLON.Vector3(1.2, -1.5, 2.2);
}
// pointerInfo.event.button == 0 LEFT mouse button
if (pointerInfo.event.buttons & 1) {
if (!lPointerDown) {
// Shoot gun with left click if allowed
if (shoot()) {
pickedResult = scene.pickWithRay(ray);
if (pickedResult.hit) {
// Get hit location for collision particles
hitSystem.direction1 = new BABYLON.Vector3(-ray.direction.x - 0.4, -ray.direction.y - 0.4, -ray.direction.z - 0.4);
hitSystem.direction2 = new BABYLON.Vector3(-ray.direction.x + 0.4, -ray.direction.y + 0.4, -ray.direction.z + 0.4);
hitSystem.emitter = pickedResult.pickedPoint;
hitSystem.start();
// Set position for ricochet found
impact.setPosition(pickedResult.pickedPoint);
// Play ricochet on slight delay
setTimeout(() => { impact.play(); }, 100)
// console.log(pickedResult.pickedPoint);
var mesh = pickedResult.pickedMesh;
// Check for target
if (mesh.uniqueId == 1) {
// Change to green target
mesh.material = target2Mat;
mesh.uniqueId = 0;
targetNum--;
// Check for win con
if (targetNum != 0) {
hud.textBlock.text = "Targets remaining: " + targetNum;
}
else {
hud.textBlock.text = "You win!!";
win.play();
setTimeout(() => {
var check = confirm("You won! Reset the targets?");
if (check) {
for (i = 0; i < targetList.length; i++) {
targetList[i].material = targetMat;
targetList[i].uniqueId = 1;
targetNum++;
}
hud.textBlock.text = "Targets remaining: " + targetNum;
}
}, 3000)
}
ding.play();
}
}
}
}
lPointerDown = true;
}
else {
lPointerDown = false;
}
});
return scene;
}
/******* End of the create scene function ******/
let scene = createScene(); //Call the createScene function
// Register a render loop to repeatedly render the scene
engine.runRenderLoop(function () {
scene.render();
});
let oldTime = 0;
function jump() {
camera = scene.cameras[0];
var animation = new BABYLON.Animation("jump", "position.y", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
var keys = [
{
frame: 0,
value: camera.position.y
},
{
frame: 30,
value: camera.position.y + 4
}
];
animation.setKeys(keys);
var easingFunction = new BABYLON.SineEase();
easingFunction.setEasingMode(BABYLON.EasingFunction.EASINGMODE_EASEOUT);
animation.setEasingFunction(easingFunction);
camera.animations.push(animation);
// This section restricts infinite jumping
var today = new Date();
if (Math.floor(today.getTime()/100) - oldTime > 7) {
scene.beginAnimation(camera, 0, 30, false, 2);
oldTime = Math.floor(today.getTime()/100)
}
}
// Watch for browser/canvas resize events
window.addEventListener("resize", function () {
engine.resize();
});
// canvas pointer lock
canvas.addEventListener('click', function (event) {
canvas.requestPointerLock = canvas.requestPointerLock || canvas.msRequestPointerLock || canvas.mozRequestPointerLock || canvas.webkitRequestPointerLock;
if (canvas.requestPointerLock) canvas.requestPointerLock();
});
var fly = false;
document.addEventListener("keydown", function (e) {
// console.log(e.keyCode);
if (e.keyCode == 27) {
document.exitPointerLock();
}
else if (e.keyCode == 32) {
jump();
}
else if (e.keyCode == 70) {
if (!fly) {
scene.cameras[0].applyGravity = false;
scene.cameras[0].checkCollisions = false;
fly = true;
}
else {
scene.cameras[0].applyGravity = true;
scene.cameras[0].checkCollisions = true;
fly = false;
}
}
else if (e.keyCode == 16) {
scene.cameras[0].speed = 0.8;
}
else {
scene.cameras[0].speed = 0.5;
}
});
</script>
</body>
</html>