-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_low_spec.js
1971 lines (1775 loc) · 100 KB
/
game_low_spec.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
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const canvas = document.getElementById("renderCanvas"); // Get the canvas element
window.addEventListener('load', function(e) { //Focus the canvas when the page is loaded (otherwise you had to click once to focus it and play the game)
canvas.focus();
});
const engine = new BABYLON.Engine(canvas, true); // Generate the BABYLON 3D engine
var frameRate = engine.getFps(); //To keep the application frame rate independent
//Utility function to measure the distance between the boss and the player
function cartesianDistance(a, b) {
//a and b are Vector3
//the distance of the ground plane is measured with x and z components
var distance = Math.sqrt(Math.pow(b.x-a.x, 2) + Math.pow(b.z-a.z, 2));
return distance;
}
const createScene = async function () {
// Creates a basic Babylon Scene object
const scene = new BABYLON.Scene(engine);
//**CAMERA**
const debugCamera = new BABYLON.FreeCamera("debugCamera", new BABYLON.Vector3(0.0, 2.0, 0.0), scene, true);
//const debugCamera = new BABYLON.ArcRotateCamera("debugCamera", -Math.PI / 2, Math.PI / 2.5, 6, new BABYLON.Vector3(0.0, 0.0, 0.0));
debugCamera.attachControl(canvas, true);
//debugCamera.wheelPrecision = 100;
debugCamera.minZ = 0.01;
debugCamera.inertia = 0.5;
const camera = new BABYLON.FollowCamera("followCam", new BABYLON.Vector3(20.0, 3.0, 0.0), scene);
camera.radius = 6.2;
camera.heightOffset = 1.2;
camera.rotationOffset = 180.0;
camera.cameraAcceleration = 0.07;
camera.maxCameraSpeed = 10.0;
//camera.attachControl(canvas, true);
scene.activeCamera = camera;
//**NO FOG EFFECT IN LOW SPEC MODE**
//scene.clearColor = new BABYLON.Color3(0.5, 0.8, 0.5);
/*scene.ambientColor = new BABYLON.Color3(0.3, 0.3, 0.3);
scene.fogMode = BABYLON.Scene.FOGMODE_EXP2;
scene.fogDensity = 0.006;
scene.fogColor = new BABYLON.Color3(0.45, 0.4, 0.35);*/
//**LIGHTS SETTING**
const externalLight = new BABYLON.HemisphericLight("externalLight", new BABYLON.Vector3(0, 1, 0));
externalLight.intensity = 1.2;
externalLight.diffuse = new BABYLON.Color3(0.45, 0.4, 0.35);
externalLight.specular = new BABYLON.Color3(0.45, 0.4, 0.35);
const light = new BABYLON.PointLight("pointLight", new BABYLON.Vector3(-5.0, 8.0, 0.0), scene);
light.intensity = 1.1;
light.diffuse = new BABYLON.Color3(0.42, 0.4, 0.4);
light.specular = new BABYLON.Color3(0.42, 0.4, 0.4);
light.range = 55.0;
const light2 = new BABYLON.PointLight("pointLight2", new BABYLON.Vector3(10.0, 8.0, 0.0), scene);
light2.intensity = 1.1;
light2.diffuse = new BABYLON.Color3(0.42, 0.4, 0.4);
light2.specular = new BABYLON.Color3(0.42, 0.4, 0.4);
light2.range = 60.0;
//**GROUND AND SKYBOX**
const groundMat = new BABYLON.StandardMaterial("groundMat");
//groundMat.diffuseColor = new BABYLON.Color3(0.9, 0.9, 0.9);
groundMat.diffuseTexture = new BABYLON.Texture("assets/Chapel_Arena_fin/m15_wall_white_01.png", scene);
groundMat.diffuseTexture.uScale = 20.0; //to make the texture replicate instead of stretching ("tiling")
groundMat.diffuseTexture.vScale = 16.0;
const ground = BABYLON.MeshBuilder.CreateGround("ground", {width:120, height:100});
ground.material = groundMat;
ground.position = new BABYLON.Vector3(15.0, -6.5, 0.0);
const skybox = BABYLON.MeshBuilder.CreateBox("skybox", {size:200}, scene);
const skyboxMaterial = new BABYLON.StandardMaterial("skyboxMaterial", scene);
skyboxMaterial.backFaceCulling = false;
skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("assets/Skybox/skybox", scene); //LEGGE CARTELLA "textures" E POI CERCA FILE CON INIZIO NOME "skybox" E DESINENZA "_nx", "_ny", ecc.
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;
skybox.position = new BABYLON.Vector3(0.0, 50.0, 0.0);
//I HAD TO CONVERT TO .BABYLON FORMAT BECAUSE .OBJ DOESN'T HAVE SKELETON
//**MODELS IMPORTATION**
//Player model (darkwraith)
var model;
var skeleton;
//I use asynchronous function with "await" to avoid the lost of reference to "model" and "skeleton" and to improve mesh loading
const result = await BABYLON.SceneLoader.ImportMeshAsync("", "assets/Darkwraith_fin2/", "c2390.babylon", scene);
model = result.meshes[0];
skeleton = result.skeletons[0];
//model.receiveShadows = true; //NO SHADOWS IN LOW SPEC MODE
//model.checkCollisions = true;
model.alwaysSelectAsActiveMesh = true;
for (let i=0; i<model.material.subMaterials.length; i++)
model.material.subMaterials[i].maxSimultaneousLights = 6;
skeleton.bones[skeleton.getBoneIndexByName("main")].position = new BABYLON.Vector3(20.0, 0.0, 0.0);
skeleton.bones[skeleton.getBoneIndexByName("main")].rotation = new BABYLON.Vector3(0.0, -Math.PI/2, 0.0);
skeleton.bones[skeleton.getBoneIndexByName("main")].scaling.x *= -1.0; //to solve the mirrored imported model (the angles will be computed as opposite)
console.log(skeleton.bones);
//****REMINDER FOR BONES NAMES****
//main chest neck head
//shoulder L upper_arm L lower_arm L palm L thumb L fingers L upper_leg L lower_leg L foot L
//shoulder R upper_arm R lower_arm R palm R thumb R fingers R upper_leg R lower_leg R foot R
//sword sheath(only for darkwraith) cape(only for silver knight)
//Boss model (silver knight)
var model2;
var skeleton2;
const result2 = await BABYLON.SceneLoader.ImportMeshAsync("", "assets/Silver_Knight_fin2/", "c2410.babylon", scene);
model2 = result2.meshes[0];
skeleton2 = result2.skeletons[0];
//model2.receiveShadows = true; //NO SHADOWS IN LOW SPEC MODE
//model2.checkCollisions = true;
skeleton2.bones[skeleton2.getBoneIndexByName("main")].position = new BABYLON.Vector3(-7.0, 0.0, 0.0);
skeleton2.bones[skeleton2.getBoneIndexByName("main")].rotation = new BABYLON.Vector3(0.0, Math.PI/2, 0.0);
skeleton2.bones[skeleton2.getBoneIndexByName("main")].scaling.x *= -1.0; //to solve the mirrored imported model (the angles will be computed as opposite)
skeleton2.bones[skeleton2.getBoneIndexByName("cape")].rotation = new BABYLON.Vector3(-11*Math.PI/12, 0.0, 0.0); //only "cape" is modified before, because it is animated not from the default t-pose position/rotation
console.log(skeleton2.bones);
model2.alwaysSelectAsActiveMesh = true; //very important, otherwise the second model disappears or glitches out from certain camera angles
for (let i=0; i<model2.material.subMaterials.length; i++)
model2.material.subMaterials[i].maxSimultaneousLights = 6;
//Cathedral
var cathedral;
const result3 = await BABYLON.SceneLoader.ImportMeshAsync("", "assets/Chapel_Arena_fin/", "chapel_arena.babylon", scene);
cathedral = result3.meshes[0];
cathedral.position = new BABYLON.Vector3(0.0, 7.5, 0.0);
cathedral.rotation = new BABYLON.Vector3(0.0, Math.PI, 0.0);
//cathedral.receiveShadows = true; //NO SHADOWS IN LOW SPEC MODE
for (let i=0; i<cathedral.material.subMaterials.length; i++)
cathedral.material.subMaterials[i].maxSimultaneousLights = 6;
//Chandeliers
var chandelier;
const result4 = await BABYLON.SceneLoader.ImportMeshAsync("", "assets/Chandelier/", "chandelier.babylon", scene);
chandelier = result4.meshes[0];
chandelier.position = new BABYLON.Vector3(-5.0, 11.0, 0.0);
//chandelier.receiveShadows = true;
for (let i=0; i<chandelier.material.subMaterials.length; i++)
chandelier.material.subMaterials[i].maxSimultaneousLights = 6;
var chandelier2 = chandelier.clone();
chandelier2.position = new BABYLON.Vector3(10.0, 11.0, 0.0);
//chandelier2.receiveShadows = true;
//Victory chest
var zeldaChest;
var chestSkeleton;
const result5 = await BABYLON.SceneLoader.ImportMeshAsync("", "assets/Zelda_Chest/", "Zelda_chest.babylon", scene);
zeldaChest = result5.meshes[0];
chestSkeleton = result5.skeletons[0];
chestSkeleton.bones[chestSkeleton.getBoneIndexByName("base")].scaling.x *= 1.4;
chestSkeleton.bones[chestSkeleton.getBoneIndexByName("base")].scaling.y *= 1.2;
chestSkeleton.bones[chestSkeleton.getBoneIndexByName("base")].scaling.z *= 1.2;
chestSkeleton.bones[chestSkeleton.getBoneIndexByName("base")].position = new BABYLON.Vector3(-10.5, 0.0, 0.0);
const tempRot = chestSkeleton.bones[chestSkeleton.getBoneIndexByName("base")].rotation;
chestSkeleton.bones[chestSkeleton.getBoneIndexByName("base")].rotation = new BABYLON.Vector3(tempRot.x, tempRot.y-Math.PI/2, tempRot.z);
zeldaChest.alwaysSelectAsActiveMesh = true;
//zeldaChest.receiveShadows = true; //NO SHADOWS IN LOW SPEC MODE
zeldaChest.material.maxSimultaneousLights = 6;
for (let i=0; i<zeldaChest.material.subMaterials.length; i++)
zeldaChest.material.subMaterials[i].maxSimultaneousLights = 6;
zeldaChest.isVisible = false;
//Moonlight sword (victory prize)
var moonlightSword;
const result6 = await BABYLON.SceneLoader.ImportMeshAsync("", "assets/Moonlight_Sword/", "moonlight_sword.babylon", scene);
moonlightSword = result6.meshes[0];
moonlightSword.position = new BABYLON.Vector3(-10.0, -1.0, 0.0);
moonlightSword.rotation = new BABYLON.Vector3(0.0, Math.PI/2, 0.0);
moonlightSword.scaling = new BABYLON.Vector3(0.92, 0.92, 0.92);
moonlightSword.material.subMaterials[1].alpha = 0.6; //the external edge is slightly transparent
for (let i=0; i<moonlightSword.material.subMaterials.length; i++)
moonlightSword.material.subMaterials[i].maxSimultaneousLights = 6;
moonlightSword.alwaysSelectAsActiveMesh = true;
//moonlightSword.receiveShadows = true; //NO SHADOWS IN LOW SPEC MODE
moonlightSword.isVisible = false;
//(Moonlight sword luminosity effect)
const moonlightLight = new BABYLON.PointLight("moonlightLight", new BABYLON.Vector3(-10.3, 1.8, 0.0), scene);
//moonlightLight.parent = moonlightSword;
moonlightLight.intensity = 2.5;
moonlightLight.diffuse = new BABYLON.Color3(0.7, 0.95, 0.9);
moonlightLight.specular = new BABYLON.Color3(0.7, 0.95, 0.9);
moonlightLight.range = 5.5;
moonlightLight.setEnabled(false);
const moonlightLight2 = new BABYLON.PointLight("moonlightLight2", new BABYLON.Vector3(-9.7, 1.8, 0.0), scene);
//moonlightLight.parent = moonlightSword;
moonlightLight2.intensity = 2.5;
moonlightLight2.diffuse = new BABYLON.Color3(0.7, 0.95, 0.9);
moonlightLight2.specular = new BABYLON.Color3(0.7, 0.95, 0.9);
moonlightLight2.range = 5.5;
moonlightLight2.setEnabled(false);
//Small particle system effect for the chest and moonlight sword
//NO PARTICLE SYSTEMS IN LOW SPEC MODE
/*const moonlightSparkles = new BABYLON.ParticleSystem("moonlightSparkles", 1000, scene);
moonlightSparkles.particleTexture = new BABYLON.Texture("assets/flare_moonlight_2.jpg", scene);
moonlightSparkles.emitter = new BABYLON.Vector3(-10.0, 0.0, 0.0);
moonlightSparkles.minEmitBox = new BABYLON.Vector3(-0.8, 0, -0.8);
moonlightSparkles.maxEmitBox = new BABYLON.Vector3(0.8, 0, 0.8);
//moonlightSparkles.color1 = new BABYLON.Color4(0.7, 0.8, 1.0, 1.0);
//moonlightSparkles.color2 = new BABYLON.Color4(0.2, 0.5, 1.0, 1.0);
//moonlightSparkles.blendMode = BABYLON.ParticleSystem.BLENDMODE_ONEONE;
moonlightSparkles.minSize = 0.05;
moonlightSparkles.maxSize = 0.08;
moonlightSparkles.minLifeTime = 0.5;
moonlightSparkles.maxLifeTime = 2.0;
moonlightSparkles.emitRate = 800;
moonlightSparkles.direction1 = new BABYLON.Vector3(-1, 8, 1);
moonlightSparkles.direction2 = new BABYLON.Vector3(1, 8, -1);
moonlightSparkles.minEmitPower = 0.5;
moonlightSparkles.maxEmitPower = 0.8;
moonlightSparkles.updateSpeed = 0.01;
moonlightSparkles.gravity = new BABYLON.Vector3(0, -4.405, 0);*/
//Easter egg objects
var chestFFX;
const result7 = await BABYLON.SceneLoader.ImportMeshAsync("", "assets/FFX_Chest/", "FFX_chest.babylon", scene);
chestFFX = result7.meshes[0];
chestFFX.position = new BABYLON.Vector3(-18.5, 0.8, 1.0);
chestFFX.rotation = new BABYLON.Vector3(0.0, -Math.PI/2, 0.0);
//chestFFX.receiveShadows = true; //NO SHADOWS IN LOW SPEC MODE
chestFFX.material.maxSimultaneousLights = 6;
var royalHelm;
const result8 = await BABYLON.SceneLoader.ImportMeshAsync("", "assets/Royal_Helm_fin/", "royal_helm.babylon", scene);
royalHelm = result8.meshes[0];
royalHelm.position = new BABYLON.Vector3(-18.5, 0.8, -1.0);
royalHelm.rotation = new BABYLON.Vector3(0.0, -2*Math.PI/3, 0.0);
royalHelm.scaling = new BABYLON.Vector3(1.2, 1.2, 1.2);
//royalHelm.receiveShadows = true; //NO SHADOWS IN LOW SPEC MODE
for (let i=0; i<royalHelm.material.subMaterials.length; i++)
royalHelm.material.subMaterials[i].maxSimultaneousLights = 6;
//Healing flare effect
//NO PARTICLE SYSTEMS IN LOW SPEC MODE
/*const healingParticles = new BABYLON.ParticleSystem("healingParticles", 1000, scene);
healingParticles.particleTexture = new BABYLON.Texture("assets/healing_flare_2.jpg", scene);
var tempPos = skeleton.bones[skeleton.getBoneIndexByName("main")].position;
healingParticles.emitter = new BABYLON.Vector3(tempPos.x, tempPos.y+1.2, tempPos.z);
healingParticles.minEmitBox = new BABYLON.Vector3(-0.2, 0, -0.2);
healingParticles.maxEmitBox = new BABYLON.Vector3(0.2, 0, 0.2);
//healingParticles.color1 = new BABYLON.Color4(0.7, 0.8, 1.0, 1.0);
//healingParticles.color2 = new BABYLON.Color4(0.2, 0.5, 1.0, 1.0);
//healingParticles.blendMode = BABYLON.ParticleSystem.BLENDMODE_ONEONE;
healingParticles.minSize = 0.05;
healingParticles.maxSize = 0.08;
healingParticles.minLifeTime = 0.2;
healingParticles.maxLifeTime = 1.0;
healingParticles.emitRate = 800;
healingParticles.direction1 = new BABYLON.Vector3(-1, 8, 1);
healingParticles.direction2 = new BABYLON.Vector3(1, 8, -1);
healingParticles.minEmitPower = 0.25;
healingParticles.maxEmitPower = 0.5;
healingParticles.updateSpeed = 0.01;
healingParticles.gravity = new BABYLON.Vector3(0, -4.405, 0);*/
//**NO SHADOWS IN LOW SPEC MODE**
/*const shadowGenerator1 = new BABYLON.ShadowGenerator(1024, light);
shadowGenerator1.addShadowCaster(model);
shadowGenerator1.addShadowCaster(model2);
shadowGenerator1.addShadowCaster(cathedral);
//shadowGenerator1.addShadowCaster(chandelier2);
shadowGenerator1.addShadowCaster(zeldaChest);
shadowGenerator1.addShadowCaster(moonlightSword);
shadowGenerator1.addShadowCaster(chestFFX);
shadowGenerator1.addShadowCaster(royalHelm);
shadowGenerator1.usePoissonSampling = true;
const shadowGenerator2 = new BABYLON.ShadowGenerator(1024, light2);
shadowGenerator2.addShadowCaster(model);
shadowGenerator2.addShadowCaster(model2);
shadowGenerator2.addShadowCaster(cathedral);
shadowGenerator2.addShadowCaster(zeldaChest);
shadowGenerator2.addShadowCaster(moonlightSword);
//shadowGenerator1.addShadowCaster(chandelier);
shadowGenerator2.usePoissonSampling = true;
const shadowGenerator3 = new BABYLON.ShadowGenerator(1024, moonlightLight);
shadowGenerator3.addShadowCaster(zeldaChest);
shadowGenerator3.addShadowCaster(model);
shadowGenerator3.usePoissonSampling = true;*/
//I HAD TO DISABLE BACKFACE CULLING FROM BLENDER (from some meshes, not all) BECAUSE OF THE MIRRORING OF THE MODELS EXPORTER (THE APP IS SLIGHTLY HEAVIER)
//**HITBOXES CREATION**
//Player hitboxes
const res1 = playerHitboxes(scene, skeleton);
const playerHitbox = res1[0];
const playerSwordHitbox = res1[1];
const playerFrontHitbox = res1[2];
const playerBackHitbox = res1[3];
const playerLeftHitbox = res1[4];
const playerRightHitbox = res1[5];
camera.lockedTarget = playerFrontHitbox; //I had to lock the target on the front hitbox because I formally don't move the mesh, but only the skeleton (since "lockedTarget" requires a mesh)
//Boss hitboxes
const res2 = bossHitboxes(scene, skeleton2);
const bossHitbox = res2[0];
const bossSwordHitbox = res2[1];
const arenaHitboxes = mapHitboxes(scene, cathedral);
var linkLine = null; //linking line between boss and player to check for obstacles (I didn't implement path finding or crowd agents)
var allHitboxes = []; //just for the debug mode (to visualize all the hitboxes)
for(let i=0; i<res1.length; i++) allHitboxes.push(res1[i]);
for(let i=0; i<res2.length; i++) allHitboxes.push(res2[i]);
for(let i=0; i<arenaHitboxes.length; i++) allHitboxes.push(arenaHitboxes[i]);
const chestHitbox = treasureChestHitbox(scene, chestSkeleton.bones[chestSkeleton.getBoneIndexByName("base")]); //I don't add this to the "arenaHitboxes" array now, because I add it later when the chest appears
//**SOUNDS ASSETS IMPORT**
//Music
const soundtrack = new BABYLON.Sound("soundtrack", "sounds/Music/Dark Souls 3 OST - Lorian, Elder Prince & Lothric, Younger Prince [HQ].mp3", scene, null, { loop: true, autoplay: true, volume: 0.3 });
const failureMusic = new BABYLON.Sound("failureMusic", "sounds/Music/Mission Failed - Ace Combat 7.mp3", scene, null, { loop: false, volume: 0.6 });
const victoryMusic = new BABYLON.Sound("victoryMusic", "sounds/Music/Gwynevere, Princess of Sunlight.mp3", scene, null, { loop: true, volume: 0.3 });
//Weapons sounds
const playerLightSwing1 = new BABYLON.Sound("playerLightSwing1", "sounds/Weapons_sounds/player_light_swing_1.wav", scene, null, { loop: false, volume: 0.4 });
const playerLightSwing2 = new BABYLON.Sound("playerLightSwing2", "sounds/Weapons_sounds/player_light_swing_2.wav", scene, null, { loop: false, volume: 0.4 });
const playerHeavySwing1 = new BABYLON.Sound("playerHeavySwing1", "sounds/Weapons_sounds/player_heavy_swing_1.wav", scene, null, { loop: false, volume: 0.4 });
const playerHeavySwing2 = new BABYLON.Sound("playerHeavySwing2", "sounds/Weapons_sounds/player_heavy_swing_2.wav", scene, null, { loop: false, volume: 0.4 });
const playerLightHit1 = new BABYLON.Sound("playerLightHit1", "sounds/Weapons_sounds/player_light_hit_1.wav", scene, null, { loop: false, volume: 0.4 });
const playerLightHit2 = new BABYLON.Sound("playerLightHit2", "sounds/Weapons_sounds/player_light_hit_2.wav", scene, null, { loop: false, volume: 0.4 });
const playerHeavyHit1 = new BABYLON.Sound("playerHeavyHit1", "sounds/Weapons_sounds/player_heavy_hit_1.wav", scene, null, { loop: false, volume: 0.4 });
const playerHeavyHit2 = new BABYLON.Sound("playerHeavyHit2", "sounds/Weapons_sounds/player_heavy_hit_2.wav", scene, null, { loop: false, volume: 0.4 });
const bossSwordSwing1 = new BABYLON.Sound("bossSwordSwing1", "sounds/Weapons_sounds/boss_swing_1.wav", scene, null, { loop: false, volume: 0.4 });
const bossSwordSwing2 = new BABYLON.Sound("bossSwordSwing2", "sounds/Weapons_sounds/boss_swing_2.wav", scene, null, { loop: false, volume: 0.4 });
const bossSwordHit1 = new BABYLON.Sound("bossSwordHit1", "sounds/Weapons_sounds/boss_sword_hit_1.wav", scene, null, { loop: false, volume: 0.4 });
const bossSwordHit2 = new BABYLON.Sound("bossSwordHit2", "sounds/Weapons_sounds/boss_sword_hit_2.wav", scene, null, { loop: false, volume: 0.4 });
//Footsteps sounds
const playerFootstep1 = new BABYLON.Sound("playerFootstep1", "sounds/Footsteps_sounds/player_footstep_1.wav", scene, null, { loop: false, volume: 0.3 });
const playerFootstep2 = new BABYLON.Sound("playerFootstep2", "sounds/Footsteps_sounds/player_footstep_2.wav", scene, null, { loop: false, volume: 0.3 });
const playerFootstep3 = new BABYLON.Sound("playerFootstep3", "sounds/Footsteps_sounds/player_footstep_3.wav", scene, null, { loop: false, volume: 0.3 });
const bossFootstep1 = new BABYLON.Sound("bossFootstep1", "sounds/Footsteps_sounds/boss_footstep_1.wav", scene, null, { loop: false, volume: 0.3 });
const bossFootstep2 = new BABYLON.Sound("bossFootstep2", "sounds/Footsteps_sounds/boss_footstep_2.wav", scene, null, { loop: false, volume: 0.3 });
const dodgeSound1 = new BABYLON.Sound("dodgeSound1", "sounds/Footsteps_sounds/dodge_1.wav", scene, null, { loop: false, volume: 0.3 });
const dodgeSound2 = new BABYLON.Sound("dodgeSound2", "sounds/Footsteps_sounds/dodge_2.wav", scene, null, { loop: false, volume: 0.3 });
//Boss voice sounds
const bossVoiceLightHit1 = new BABYLON.Sound("bossVoiceLightHit1", "sounds/Cavaliere_Angelo_sounds/Cav_Angelo_hit_4.mp3", scene, null, { loop: false, volume: 0.4 });
const bossVoiceLightHit2 = new BABYLON.Sound("bossVoiceLightHit2", "sounds/Cavaliere_Angelo_sounds/Cav_Angelo_hit_3.mp3", scene, null, { loop: false, volume: 0.4 });
const bossVoiceHeavyHit1 = new BABYLON.Sound("bossVoiceHeavyHit1", "sounds/Cavaliere_Angelo_sounds/Cav_Angelo_hit_1.mp3", scene, null, { loop: false, volume: 0.4 });
const bossVoiceHeavyHit2 = new BABYLON.Sound("bossVoiceHeavyHit2", "sounds/Cavaliere_Angelo_sounds/Cav_Angelo_hit_2.mp3", scene, null, { loop: false, volume: 0.4 });
const bossVoiceDeath1 = new BABYLON.Sound("bossVoiceDeath1", "sounds/Cavaliere_Angelo_sounds/Cav_Angelo_death_1_effects.mp3", scene, null, { loop: false, volume: 0.7 });
const bossVoiceDeath2 = new BABYLON.Sound("bossVoiceDeath2", "sounds/Cavaliere_Angelo_sounds/Cav_Angelo_death_2_effects.mp3", scene, null, { loop: false, volume: 0.7 });
//Chest opening sound (Dark Cloud sound)
const chestOpeningSound = new BABYLON.Sound("chestOpeningSound", "sounds/chest_item.wav", scene, null, { loop: false, volume: 0.4 });
//**GUI/HUD (it is not responsive to a dynamic change of resolution, so if you change it, just reload the page)**
const GUITexture = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("GUITexture", true);
//Player bars container
var playerBarsContainer = new BABYLON.GUI.Rectangle();
playerBarsContainer.height = "56px";
playerBarsContainer.width = "520px";
playerBarsContainer.thickness = 0;
playerBarsContainer.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
playerBarsContainer.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_TOP;
playerBarsContainer.top = "4%"; //to make the GUI device independent, I use a percentage of the screen resolution (relative to the alignement)
playerBarsContainer.left = "3%";
GUITexture.addControl(playerBarsContainer);
//Player health bar
var playerHealthBarContainer = new BABYLON.GUI.Rectangle();
//playerHealthBarContainer.adaptWidthToChildren = true;
playerHealthBarContainer.height = "22px";
playerHealthBarContainer.width = "500px";
playerHealthBarContainer.cornerRadius = 2;
playerHealthBarContainer.color = "#333333";
playerHealthBarContainer.thickness = 2;
playerHealthBarContainer.background = "#808080";
playerHealthBarContainer.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
playerHealthBarContainer.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_TOP;
playerBarsContainer.addControl(playerHealthBarContainer);
var playerHealthBar = new BABYLON.GUI.Rectangle();
//playerHealthBar.adaptWidthToChildren = true;
playerHealthBar.height = "20px";
playerHealthBar.width = "500px";
playerHealthBar.cornerRadius = 1;
playerHealthBar.color = "#a61022";
playerHealthBar.thickness = 2;
playerHealthBar.background = "#a61022";
playerHealthBar.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
playerHealthBarContainer.addControl(playerHealthBar);
//Player stamina bar
var staminaBarContainer = new BABYLON.GUI.Rectangle();
//staminaBarContainer.adaptWidthToChildren = true;
staminaBarContainer.height = "22px";
staminaBarContainer.width = "400px";
staminaBarContainer.cornerRadius = 2;
staminaBarContainer.color = "#333333";
staminaBarContainer.thickness = 2;
staminaBarContainer.background = "#808080";
staminaBarContainer.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
staminaBarContainer.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM;
playerBarsContainer.addControl(staminaBarContainer);
var staminaBar = new BABYLON.GUI.Rectangle();
//staminaBar.adaptWidthToChildren = true;
staminaBar.height = "20px";
staminaBar.width = "400px";
staminaBar.cornerRadius = 1;
staminaBar.color = "#386f48";
staminaBar.thickness = 2;
staminaBar.background = "#386f48";
staminaBar.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
staminaBarContainer.addControl(staminaBar);
//Player healing counter
var healingContainer = new BABYLON.GUI.Rectangle();
//bossHealthBar.adaptWidthToChildren = true;
healingContainer.height = "70px";
healingContainer.width = "154px";
//healingContainer.color = "#2e2523";
healingContainer.thickness = 0;
//healingContainer.background = "#2e2523";
healingContainer.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
healingContainer.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM;
healingContainer.left = "4%";
healingContainer.top = "-8%";
GUITexture.addControl(healingContainer);
var healingTextArea = new BABYLON.GUI.TextBlock("healingTextArea", "Healing herbs:");
healingTextArea.color = "#dcdcdc";
healingTextArea.height = "24px";
healingTextArea.width = "150px";
healingTextArea.fontSize = "18px";
healingTextArea.fontFamily = "Palatino Linotype";
healingTextArea.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
healingTextArea.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_TOP;
healingContainer.addControl(healingTextArea);
var healingNumberArea = new BABYLON.GUI.TextBlock("healingNumberArea", "1 / 1");
healingNumberArea.color = "#dcdcdc";
healingNumberArea.height = "42px";
healingNumberArea.width = "100px";
healingNumberArea.fontSize = "36px";
healingNumberArea.fontFamily = "Palatino Linotype";
healingNumberArea.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
healingNumberArea.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM;
healingContainer.addControl(healingNumberArea);
//Boss health bar
var bossHealthBarArea = new BABYLON.GUI.Rectangle();
//bossrHealthBarContainer.adaptWidthToChildren = true;
bossHealthBarArea.height = "50px";
bossHealthBarArea.width = "800px";
bossHealthBarArea.cornerRadius = 0;
//bossHealthBarArea.color = "#ffffff";
bossHealthBarArea.alpha = 1.0;
bossHealthBarArea.thickness = 0;
bossHealthBarArea.top = "38%";
bossHealthBarArea.left = 0;
GUITexture.addControl(bossHealthBarArea);
var bossHealthBarContainer = new BABYLON.GUI.Rectangle();
//bossrHealthBarContainer.adaptWidthToChildren = true;
bossHealthBarContainer.height = "20px";
bossHealthBarContainer.width = "800px";
bossHealthBarContainer.cornerRadius = 2;
bossHealthBarContainer.color = "#333333";
bossHealthBarContainer.thickness = 2;
bossHealthBarContainer.background = "#808080";
bossHealthBarContainer.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
bossHealthBarContainer.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_BOTTOM;
bossHealthBarArea.addControl(bossHealthBarContainer);
var bossHealthBar = new BABYLON.GUI.Rectangle();
//bossHealthBar.adaptWidthToChildren = true;
bossHealthBar.height = "18px";
bossHealthBar.width = "800px";
bossHealthBar.cornerRadius = 1;
bossHealthBar.color = "#a61022";
bossHealthBar.thickness = 2;
bossHealthBar.background = "#a61022";
bossHealthBar.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
bossHealthBarContainer.addControl(bossHealthBar);
var bossTextArea = new BABYLON.GUI.TextBlock("bossTextArea", "Silver Knight, Guardian of the Cathedral");
bossTextArea.color = "#dcdcdc";
bossTextArea.height = "28px";
bossTextArea.width = "360px";
bossTextArea.fontSize = "20px";
bossTextArea.fontFamily = "Palatino Linotype";
bossTextArea.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
bossTextArea.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_TOP;
bossTextArea.left = "10px";
bossHealthBarArea.addControl(bossTextArea);
//Defeat text box
var defeatContainer = new BABYLON.GUI.Rectangle();
//defeatContainer.adaptWidthToChildren = true;
defeatContainer.height = "150px";
defeatContainer.width = (window.screen.width).toString() + "px";
defeatContainer.cornerRadius = 2;
defeatContainer.color = "#000000";
defeatContainer.thickness = 0; //I remove the different border color
defeatContainer.background = "#000000";
defeatContainer.alpha = 0.0; //to be animated
defeatContainer.top = 0;
defeatContainer.left = 0;
GUITexture.addControl(defeatContainer);
defeatContainer.isVisible = false;
var defeatTextArea = new BABYLON.GUI.TextBlock("defeatTextArea", "YOU DIED");
defeatTextArea.color = "#791924";
defeatTextArea.height = "56px";
defeatTextArea.width = "320px";
defeatTextArea.fontSize = "52px";
defeatTextArea.alpha = 0.0;
defeatTextArea.fontFamily = "Palatino Linotype";
defeatTextArea.paddingTop = "-60px";
GUITexture.addControl(defeatTextArea); //I don't attach it to the container because the alpha is in common so also the text would be half transparent
defeatTextArea.isVisible = false;
var defeatRestartButton = BABYLON.GUI.Button.CreateSimpleButton("defeatRestartButton", "Retry the battle");
defeatRestartButton.height = "32px";
defeatRestartButton.width = "168px";
defeatRestartButton.cornerRadius = 3;
defeatRestartButton.color = "#cccccc";
defeatRestartButton.thickness = 2;
defeatRestartButton.alpha = 0.0;
defeatRestartButton.background = "#636363";
defeatRestartButton.fontFamily = "Palatino Linotype";
defeatRestartButton.fontSize = "20px";
defeatRestartButton.top = "24px";
GUITexture.addControl(defeatRestartButton);
defeatRestartButton.isVisible = false;
defeatRestartButton.isEnabled = false; //to be reactivated when it appears
//Victory text box
var victoryContainer = new BABYLON.GUI.Rectangle();
//victoryContainer.adaptWidthToChildren = true;
victoryContainer.height = "150px";
victoryContainer.width = (window.screen.width).toString() + "px";
victoryContainer.cornerRadius = 2;
victoryContainer.color = "#000000";
victoryContainer.thickness = 0; //I remove the different border color
victoryContainer.background = "#000000";
victoryContainer.alpha = 0.0; //to be animated
victoryContainer.top = 0;
victoryContainer.left = 0;
GUITexture.addControl(victoryContainer);
victoryContainer.isVisible = false; //visible when animation starts, stays for some seconds, then disappears
var victoryTextArea = new BABYLON.GUI.TextBlock("victoryTextArea", "VICTORY");
victoryTextArea.color = "#ffd400";
victoryTextArea.height = "56px";
victoryTextArea.width = "320px";
victoryTextArea.alpha = 0.0;
victoryTextArea.fontSize = "52px";
victoryTextArea.fontFamily = "Palatino Linotype";
victoryTextArea.paddingTop = "-60px";
GUITexture.addControl(victoryTextArea); //I don't attach it to the container because the alpha is in common so also the text would be half transparent
victoryTextArea.isVisible = false;
var victorySubtextArea = new BABYLON.GUI.TextBlock("victorySubtextArea", "You can open the chest to obtain the final treasure");
victorySubtextArea.color = "#dcdcdc";
victorySubtextArea.height = "24px";
victorySubtextArea.width = "440px";
victorySubtextArea.alpha = 0.0;
victorySubtextArea.fontSize = "20px";
victorySubtextArea.fontFamily = "Palatino Linotype";
victorySubtextArea.paddingTop = "50px";
GUITexture.addControl(victorySubtextArea); //I don't attach it to the container because the alpha is in common so also the text would be half transparent
victorySubtextArea.isVisible = false;
//Treasure item text box
var itemTextContainer = new BABYLON.GUI.Rectangle();
//itemTextContainer.adaptWidthToChildren = true;
itemTextContainer.height = "80px";
itemTextContainer.width = "520px";
itemTextContainer.cornerRadius = 1;
itemTextContainer.color = "#2e2523";
itemTextContainer.thickness = 2;
itemTextContainer.background = "#2e2523";
itemTextContainer.alpha = 0.8;
itemTextContainer.top = "25%";
itemTextContainer.left = 0;
GUITexture.addControl(itemTextContainer);
itemTextContainer.isVisible = false;
var itemTextArea = new BABYLON.GUI.TextBlock("itemTextArea", "You obtained the Legendary Moonlight Sword. You won!");
itemTextArea.color = "#dcdcdc";
itemTextArea.height = "24px";
itemTextArea.width = "480px";
itemTextArea.fontSize = "18px";
itemTextArea.fontFamily = "Palatino Linotype";
itemTextArea.top = "22%";
GUITexture.addControl(itemTextArea); //I don't attach it to the container because the alpha is in common so also the text would be half transparent
itemTextArea.isVisible = false;
var victoryRestartButton = BABYLON.GUI.Button.CreateSimpleButton("victoryRestartButton", "Restart the game");
victoryRestartButton.height = "32px";
victoryRestartButton.width = "168px";
victoryRestartButton.cornerRadius = 3;
victoryRestartButton.color = "#cccccc";
victoryRestartButton.thickness = 2;
victoryRestartButton.background = "#636363";
victoryRestartButton.fontFamily = "Palatino Linotype";
victoryRestartButton.fontSize = "20px";
victoryRestartButton.top = "27%";
GUITexture.addControl(victoryRestartButton);
victoryRestartButton.isVisible = false;
victoryRestartButton.isEnabled = false; //to be reactivated when it appears
//**ARRAY OF VECTOR3 TO KEEP THE POSITIONS AND ROTATIONS OF THE INITIAL T-POSES OF THE TWO CHARACTERS**
const DWinitialTposePos = [];
for(let i=0; i<skeleton.bones.length; i++) {
var temp = skeleton.bones[i].position.clone();
DWinitialTposePos.push(temp);
}
const DWinitialTposeRot = [];
for(let i=0; i<skeleton.bones.length; i++) {
var temp = skeleton.bones[i].rotation.clone();
DWinitialTposeRot.push(temp);
}
const SKinitialTposePos = [];
for(let i=0; i<skeleton2.bones.length; i++) {
var temp = skeleton2.bones[i].position.clone();
SKinitialTposePos.push(temp);
}
const SKinitialTposeRot = [];
for(let i=0; i<skeleton2.bones.length; i++) {
var temp = skeleton2.bones[i].rotation.clone();
SKinitialTposeRot.push(temp);
}
//Since the fingers are open by default, I added these lines to close them (and basically I'll never move them again). Same for sheath and cape
skeleton.bones[skeleton.getBoneIndexByName("fingers R")].rotation = new BABYLON.Vector3(0.0, 0.0, Math.PI/2.35);
skeleton.bones[skeleton.getBoneIndexByName("fingers L")].rotation = new BABYLON.Vector3(0.0, 0.0, -Math.PI/6);
skeleton2.bones[skeleton2.getBoneIndexByName("fingers R")].rotation = new BABYLON.Vector3(0.0, 0.0, Math.PI/2.35);
skeleton2.bones[skeleton2.getBoneIndexByName("fingers L")].rotation = new BABYLON.Vector3(0.0, 0.0, -Math.PI/6);
skeleton.bones[skeleton.getBoneIndexByName("sheath")].position = new BABYLON.Vector3(0.35, -0.7, 0.0);
skeleton.bones[skeleton.getBoneIndexByName("sheath")].rotation = new BABYLON.Vector3(0.0, 0.0, 0.12);
frameRate = engine.getFps();
//**BLENDING PRODUCED AN UGLY EFFECT ON MOVEMENTS, SO I REMOVED IT**
// Enable animation blending for all animations
/*scene.animationPropertiesOverride = new BABYLON.AnimationPropertiesOverride();
scene.animationPropertiesOverride.enableBlending = true;
scene.animationPropertiesOverride.blendingSpeed = 0.05;
scene.animationPropertiesOverride.loopMode = 1;*/
//**PLAYER AND BOSS GAMEPLAY PARAMETERS**
var playerHealth = 2000;
const playerMaxHealth = 2000;
var bossHealth = 12000;
const bossMaxHealth = 12000;
const playerLightDmg = 750;
const playerHeavyDmg = 900;
const bossLightDmg = 250;
const bossHeavyDmg = 350;
const playerMaxStamina = 200.0;
var playerCurrStamina = 200.0;
const lightAtckStaminaCost = 50.0;
const heavyAtckStaminaCost = 75.0;
const dodgeStaminaCost = 50.0;
var healingNumber = 1; //you have one healing herb
const healingValue = 500;
//**ANIMATIONS INITIALIZATIONS**
//I declare a 10 elements static array for the "main" animation, so that deterministically the walk forward is in 0, the walk back is in 1, etc.
skeleton.bones[skeleton.getBoneIndexByName("main")].animations = [null, null, null, null, null, null, null, null, null, null];
playerIdleAnimation(skeleton, frameRate, DWinitialTposePos, DWinitialTposeRot);
walkForward(skeleton, frameRate, DWinitialTposePos, DWinitialTposeRot);
walkBack(skeleton, frameRate, DWinitialTposePos, DWinitialTposeRot);
walkRight(skeleton, frameRate, DWinitialTposePos, DWinitialTposeRot);
walkLeft(skeleton, frameRate, DWinitialTposePos, DWinitialTposeRot);
rotateRight(skeleton, frameRate, DWinitialTposePos, DWinitialTposeRot);
rotateLeft(skeleton, frameRate, DWinitialTposePos, DWinitialTposeRot);
playerLightAttack(skeleton, frameRate, DWinitialTposePos, DWinitialTposeRot);
playerHeavyAttack(skeleton, frameRate, DWinitialTposePos, DWinitialTposeRot);
dodgeForward(skeleton, frameRate, DWinitialTposePos, DWinitialTposeRot);
dodgeBack(skeleton, frameRate, DWinitialTposePos, DWinitialTposeRot);
dodgeRight(skeleton, frameRate, DWinitialTposePos, DWinitialTposeRot);
dodgeLeft(skeleton, frameRate, DWinitialTposePos, DWinitialTposeRot);
playerDeath(skeleton, frameRate, DWinitialTposePos, DWinitialTposeRot);
playerHealing(skeleton, frameRate, DWinitialTposePos, DWinitialTposeRot);
bossIdleAnimation(skeleton2, frameRate, SKinitialTposePos, SKinitialTposeRot);
bossWalkForward(skeleton2, frameRate, SKinitialTposePos, SKinitialTposeRot);
bossWalkBack(skeleton2, frameRate, SKinitialTposePos, SKinitialTposeRot);
bossLightAttack(skeleton2, frameRate, SKinitialTposePos, SKinitialTposeRot);
bossHeavyAttack(skeleton2, frameRate, SKinitialTposePos, SKinitialTposeRot);
bossDeath(skeleton2, frameRate, SKinitialTposePos, SKinitialTposeRot);
bossDeathVanishing(model2, frameRate); //Boss disappearing animation after death
mainForward(skeleton, frameRate);
mainBack(skeleton, frameRate);
mainRight(skeleton, frameRate);
mainLeft(skeleton, frameRate);
mainRightRot(skeleton, frameRate);
mainLeftRot(skeleton, frameRate);
mainDodgeFwd(skeleton, frameRate);
mainDodgeBack(skeleton, frameRate);
mainDodgeRight(skeleton, frameRate);
mainDodgeLeft(skeleton, frameRate);
chestMoonlight(chestSkeleton, moonlightSword, frameRate);
//(For GUI, initialization and grouping is together for brevity)
const defeatGUIAnimationGroup = defeatTextBoxFadeIn(defeatContainer, defeatTextArea, defeatRestartButton, frameRate);
const victoryGUIAnimationGroup = victoryTextBoxFadeInOut(victoryContainer, victoryTextArea, victorySubtextArea, frameRate);
//**ANIMATIONS GROUPING**
//Player
const idleAnimationGroup = groupIdle(skeleton);
const walkFwdAnimationGroup = groupWalkFwd(skeleton);
walkFwdAnimationGroup.speedRatio = 1.25;
const walkBackAnimationGroup = groupWalkBack(skeleton);
walkBackAnimationGroup.speedRatio = 1.25;
const walkRightAnimationGroup = groupWalkRight(skeleton);
walkRightAnimationGroup.speedRatio = 1.25;
const walkLeftAnimationGroup = groupWalkLeft(skeleton);
walkLeftAnimationGroup.speedRatio = 1.25;
const rotateRightAnimationGroup = groupRotateRight(skeleton);
const rotateLeftAnimationGroup = groupRotateLeft(skeleton);
const playerLAttackAnimationGroup = groupPlayerLAttack(skeleton);
const playerHAttackAnimationGroup = groupPlayerHAttack(skeleton);
const dodgeFwdAnimationGroup = groupDodgeFwd(skeleton);
const dodgeBackAnimationGroup = groupDodgeBack(skeleton);
const dodgeRightAnimationGroup = groupDodgeRight(skeleton);
const dodgeLeftAnimationGroup = groupDodgeLeft(skeleton);
const playerDeathAnimationGroup = groupPlayerDeath(skeleton);
const playerHealingAnimationGroup = groupPlayerHealing(skeleton);
//Boss
const bossIdleAnimationGroup = groupBossIdle(skeleton2);
const bossWalkFwdAnimationGroup = groupBossWalkFwd(skeleton2);
bossWalkFwdAnimationGroup.speedRatio = 0.9;
const bossWalkBackAnimationGroup = groupBossWalkBack(skeleton2);
bossWalkBackAnimationGroup.speedRatio = 0.9;
const bossLAttackAnimationGroup = groupBossLAttack(skeleton2);
bossLAttackAnimationGroup.speedRatio = 0.9;
const bossHAttackAnimationGroup = groupBossHAttack(skeleton2);
const bossDeathAnimationGroup = groupBossDeath(skeleton2, model2);
//bossDeathAnimationGroup.normalize(0, 3*frameRate);
//Treasure chest (final prize)
const chestAnimationGroup = groupChestMoonlight(chestSkeleton, moonlightSword);
//chestAnimationGroup.normalize(0, 4*frameRate);
//To make the boss always look at the player character (I CAN'T USE IT ON MAIN BONE BECAUSE IT MIRRORS THE MODEL FOR NO REASON)
const bossLookAt = new BABYLON.BoneLookController(model2, skeleton2.bones[skeleton2.getBoneIndexByName("head")], skeleton.bones[skeleton.getBoneIndexByName("main")].position, {adjustPitch: -0.4, minYaw: -1.2, maxYaw: 1.2});
var currAnimationGroup;
var bossCurrAnimationGroup;
currAnimationGroup = idleAnimationGroup;
currAnimationGroup.play(true);
bossCurrAnimationGroup = bossIdleAnimationGroup;
bossCurrAnimationGroup.play(true);
//button flags to detect multiple keys together (mainly for dodge commands)
var wFlag = false;
var sFlag = false;
var dFlag = false;
var aFlag = false;
var eFlag = false;
var qFlag = false;
var clickLFlag = false;
var clickRFlag = false;
var spaceFlag = false;
var pFlag = false;
var rFlag = false;
var lFlag = false;
var animationIsPlaying = false; //flag to understand if I'm holding down a walk button for more than one "tick"
var mainAnimatable = scene.beginDirectAnimation(skeleton.bones[skeleton.getBoneIndexByName("main")], [skeleton.bones[skeleton.getBoneIndexByName("main")].animations[0]], 0, 11*frameRate/6, true);
mainAnimatable.stop(); //"dummy" initialization to avoid initialization errors
var playerDeathFlag = false;
//**PLAYER INPUT KEYS**
scene.onKeyboardObservable.add((kbInfo) => {
switch (kbInfo.type) {
case BABYLON.KeyboardEventTypes.KEYDOWN:
if(!playerDeathFlag) { //non ho indentato tutto il blocco sotto
switch (kbInfo.event.key) {
case "w":
case "W":
wFlag = true;
if(!animationIsPlaying && !playerLAttackAnimationGroup.isPlaying && !playerHAttackAnimationGroup.isPlaying) {
currAnimationGroup.stop();
mainAnimatable.stop();
currAnimationGroup = walkFwdAnimationGroup;
currAnimationGroup.play(true);
mainForward(skeleton, frameRate);
mainAnimatable = scene.beginDirectAnimation(skeleton.bones[skeleton.getBoneIndexByName("main")], [skeleton.bones[skeleton.getBoneIndexByName("main")].animations[0]], 0, 11*frameRate/6, true);
mainAnimatable.speedRatio = 1.25;
animationIsPlaying = true;
}
break;
case "s":
case "S":
sFlag = true;
if(!animationIsPlaying && !playerLAttackAnimationGroup.isPlaying && !playerHAttackAnimationGroup.isPlaying) {
currAnimationGroup.stop();
mainAnimatable.stop();
currAnimationGroup = walkBackAnimationGroup;
currAnimationGroup.play(true);
mainBack(skeleton, frameRate);
mainAnimatable = scene.beginDirectAnimation(skeleton.bones[skeleton.getBoneIndexByName("main")], [skeleton.bones[skeleton.getBoneIndexByName("main")].animations[1]], 0, 11*frameRate/6, true);
mainAnimatable.speedRatio = 1.25;
animationIsPlaying = true;
}
break;
case "a":
case "A":
aFlag = true;
if(!animationIsPlaying && !playerLAttackAnimationGroup.isPlaying && !playerHAttackAnimationGroup.isPlaying) {
currAnimationGroup.stop();
mainAnimatable.stop();
currAnimationGroup = walkLeftAnimationGroup;
currAnimationGroup.play(true);
mainLeft(skeleton, frameRate);
mainAnimatable = scene.beginDirectAnimation(skeleton.bones[skeleton.getBoneIndexByName("main")], [skeleton.bones[skeleton.getBoneIndexByName("main")].animations[3]], 0, 11*frameRate/6, true);
mainAnimatable.speedRatio = 1.25;
animationIsPlaying = true;
}
break;
case "d":
case "D":
dFlag = true;
if(!animationIsPlaying && !playerLAttackAnimationGroup.isPlaying && !playerHAttackAnimationGroup.isPlaying) {
currAnimationGroup.stop();
mainAnimatable.stop();
currAnimationGroup = walkRightAnimationGroup;
currAnimationGroup.play(true);
mainRight(skeleton, frameRate);
mainAnimatable = scene.beginDirectAnimation(skeleton.bones[skeleton.getBoneIndexByName("main")], [skeleton.bones[skeleton.getBoneIndexByName("main")].animations[2]], 0, 11*frameRate/6, true);
mainAnimatable.speedRatio = 1.25;
animationIsPlaying = true;
}
break;
case "e":
case "E":
eFlag = true;
if(!animationIsPlaying && !playerLAttackAnimationGroup.isPlaying && !playerHAttackAnimationGroup.isPlaying) {
currAnimationGroup.stop();
mainAnimatable.stop();
currAnimationGroup = rotateRightAnimationGroup;
currAnimationGroup.play(true);
mainRightRot(skeleton, frameRate);
mainAnimatable = scene.beginDirectAnimation(skeleton.bones[skeleton.getBoneIndexByName("main")], [skeleton.bones[skeleton.getBoneIndexByName("main")].animations[4]], 0, 2*frameRate/3, true);
//mainAnimatable.speedRatio = 1.25;
animationIsPlaying = true;
}
break;
case "q":
case "Q":
qFlag = true;
if(!animationIsPlaying && !playerLAttackAnimationGroup.isPlaying && !playerHAttackAnimationGroup.isPlaying) {
currAnimationGroup.stop();
mainAnimatable.stop();
currAnimationGroup = rotateLeftAnimationGroup;
currAnimationGroup.play(true);
mainLeftRot(skeleton, frameRate);
mainAnimatable = scene.beginDirectAnimation(skeleton.bones[skeleton.getBoneIndexByName("main")], [skeleton.bones[skeleton.getBoneIndexByName("main")].animations[5]], 0, 2*frameRate/3, true);
//mainAnimatable.speedRatio = 1.25;
animationIsPlaying = true;
}
break;
case " ":
if(playerCurrStamina > 0) { //non ho indentato il blocco sotto
if(wFlag && !playerLAttackAnimationGroup.isPlaying && !playerHAttackAnimationGroup.isPlaying && !dodgeFwdAnimationGroup.isPlaying && !dodgeBackAnimationGroup.isPlaying && !dodgeRightAnimationGroup.isPlaying && !dodgeLeftAnimationGroup.isPlaying) {
currAnimationGroup.stop();
mainAnimatable.stop();
currAnimationGroup = dodgeFwdAnimationGroup;
currAnimationGroup.play(false);
mainDodgeFwd(skeleton, frameRate);
mainAnimatable = scene.beginDirectAnimation(skeleton.bones[skeleton.getBoneIndexByName("main")], [skeleton.bones[skeleton.getBoneIndexByName("main")].animations[6]], 0, 5*frameRate/6, false);
//mainAnimatable.speedRatio = 1.25;
playerCurrStamina -= dodgeStaminaCost;
var tempWidth;
if(playerCurrStamina<=0) tempWidth = '0';
else tempWidth = (Math.round(playerCurrStamina*400/playerMaxStamina)).toString(); //it must be an integer
staminaBar.width = tempWidth + 'px';
animationIsPlaying = true;
}
else if(sFlag && !playerLAttackAnimationGroup.isPlaying && !playerHAttackAnimationGroup.isPlaying && !dodgeFwdAnimationGroup.isPlaying && !dodgeBackAnimationGroup.isPlaying && !dodgeRightAnimationGroup.isPlaying && !dodgeLeftAnimationGroup.isPlaying) {
currAnimationGroup.stop();
mainAnimatable.stop();
currAnimationGroup = dodgeBackAnimationGroup;
currAnimationGroup.play(false);
mainDodgeBack(skeleton, frameRate);
mainAnimatable = scene.beginDirectAnimation(skeleton.bones[skeleton.getBoneIndexByName("main")], [skeleton.bones[skeleton.getBoneIndexByName("main")].animations[7]], 0, 5*frameRate/6, false);
//mainAnimatable.speedRatio = 1.25;
playerCurrStamina -= dodgeStaminaCost;
var tempWidth;
if(playerCurrStamina<=0) tempWidth = '0';
else tempWidth = (Math.round(playerCurrStamina*400/playerMaxStamina)).toString(); //it must be an integer
staminaBar.width = tempWidth + 'px';
animationIsPlaying = true;
}
else if(dFlag && !playerLAttackAnimationGroup.isPlaying && !playerHAttackAnimationGroup.isPlaying && !dodgeFwdAnimationGroup.isPlaying && !dodgeBackAnimationGroup.isPlaying && !dodgeRightAnimationGroup.isPlaying && !dodgeLeftAnimationGroup.isPlaying) {
currAnimationGroup.stop();
mainAnimatable.stop();
currAnimationGroup = dodgeRightAnimationGroup;
currAnimationGroup.play(false);
mainDodgeRight(skeleton, frameRate);
mainAnimatable = scene.beginDirectAnimation(skeleton.bones[skeleton.getBoneIndexByName("main")], [skeleton.bones[skeleton.getBoneIndexByName("main")].animations[8]], 0, 5*frameRate/6, false);
//mainAnimatable.speedRatio = 1.25;
playerCurrStamina -= dodgeStaminaCost;
var tempWidth;
if(playerCurrStamina<=0) tempWidth = '0';
else tempWidth = (Math.round(playerCurrStamina*400/playerMaxStamina)).toString(); //it must be an integer
staminaBar.width = tempWidth + 'px';
animationIsPlaying = true;
}
else if(aFlag && !playerLAttackAnimationGroup.isPlaying && !playerHAttackAnimationGroup.isPlaying && !dodgeFwdAnimationGroup.isPlaying && !dodgeBackAnimationGroup.isPlaying && !dodgeRightAnimationGroup.isPlaying && !dodgeLeftAnimationGroup.isPlaying) {
currAnimationGroup.stop();
mainAnimatable.stop();
currAnimationGroup = dodgeLeftAnimationGroup;
currAnimationGroup.play(false);
mainDodgeLeft(skeleton, frameRate);
mainAnimatable = scene.beginDirectAnimation(skeleton.bones[skeleton.getBoneIndexByName("main")], [skeleton.bones[skeleton.getBoneIndexByName("main")].animations[9]], 0, 5*frameRate/6, false);
//mainAnimatable.speedRatio = 1.25;
playerCurrStamina -= dodgeStaminaCost;
var tempWidth;
if(playerCurrStamina<=0) tempWidth = '0';
else tempWidth = (Math.round(playerCurrStamina*400/playerMaxStamina)).toString(); //it must be an integer
staminaBar.width = tempWidth + 'px';
animationIsPlaying = true;
}
spaceFlag = true;
}
break;
case "r":
case "R":
rFlag = true;
if(healingNumber > 0) {
if(!playerHealingAnimationGroup.isPlaying && !playerLAttackAnimationGroup.isPlaying && !playerHAttackAnimationGroup.isPlaying && !dodgeFwdAnimationGroup.isPlaying && !dodgeBackAnimationGroup.isPlaying && !dodgeRightAnimationGroup.isPlaying && !dodgeLeftAnimationGroup.isPlaying) {
currAnimationGroup.stop();
mainAnimatable.stop();
currAnimationGroup = playerHealingAnimationGroup;
currAnimationGroup.play(false);
healingNumber--;
animationIsPlaying = true;
}