-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
3251 lines (2803 loc) · 99.6 KB
/
Copy pathmain.js
File metadata and controls
3251 lines (2803 loc) · 99.6 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
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
import * as THREE from "three";
//Import GLTFLoader
import { GLTFLoader } from "GLTFLoader";
//Import orbit controls
import { OrbitControls } from "OrbitControls";
//Import font loader
import { FontLoader } from "FontLoader";
//Import TextGeometry for 3D text
import { TextGeometry } from "TextGeometry";
//Camera Keyboard
// Keyboard input variables
let moveCameraForward = false;
let moveCameraBackward = false;
let moveCameraLeft = false;
let moveCameraRight = false;
var arrow;
var arrowDirection = new THREE.Vector3(1, 0, 0); // Direzione della freccia
var arrowLength = 10; // Lunghezza della freccia
var arrowColor = 0xff0000; // Colore della freccia
var clickX = 0;
var clickY = 0;
var clickZ = 0;
var mouseX = 0;
var mouseY = 0;
var mouseZ = 0;
var endgame = false;
var thisrobot = 0;
var k,j;
var ballvx = 0;
var ballvy = 0;
// TIMES
var times = 0;
var circle;
var turn =1;
//BALL
var normball = 0;
var normball2 = 0;
var cameraX = 0;
var cameraY = 15;
var cameraZ = 10;
var n_click = 1;
var cone;
// Handle mouse events
var mouseDown = false;
var endMouseX = 0;
var endMouseY = 0;
var line;
var moveFrom;
var moveTo;
var velocity;
var isMoving = false;
var isRobotMoving = true;
var isRobotMoving2 = false;
var isRobotMoving3 = false;
var isRobotMoving4 = false;
var isRobotMoving5 = false;
var isRobotMoving6 = false;
var done = false;
var increment = 0.05;
var n_touch = 0;
var flagBlueGoal = false;
var flagRedGoal = false;
var flagBlueWin = false;
var flagRedWin = false;
var goal_time = 0;
var load_time = 0;
var confetti_time = 0;
//ROBOT BLU SOTTO
var robot_1;
//ROBOT BLU SOPRA
var robot_2;
//ROBOT ROSA SOPRA
var robot_3;
//ROBOT ROSA SOTTO
var robot_4;
//ROBOT BLU CENTRO
var robot_5;
//ROBOT ROSA CENTRO
var robot_6;
var football_pitch;
var ball;
var ball2;
//Bounding boxes
var box_robot1;
var box_robot2;
var box_robot3;
var box_robot4;
var box_robot5;
var box_robot6;
var box_ball;
//Mesh components robot_1
var spine_1;
var spine1_1;
var spine2_1;
var neck_1;
var head_1;
var left_shoulder_1;
var left_arm_1;
var left_fore_arm_1;
var left_hand_1;
var right_shoulder_1;
var right_arm_1;
var right_fore_arm_1;
var right_hand_1;
var left_up_leg_1;
var left_leg_1;
var left_foot_1;
var right_up_leg_1;
var right_leg_1;
var right_foot_1;
//Mesh components robot_2
var spine_2;
var spine1_2;
var spine2_2;
var neck_2;
var head_2;
var left_shoulder_2;
var left_arm_2;
var left_fore_arm_2;
var left_hand_2;
var right_shoulder_2;
var right_arm_2;
var right_fore_arm_2;
var right_hand_2;
var left_up_leg_2;
var left_leg_2;
var left_foot_2;
var right_up_leg_2;
var right_leg_2;
var right_foot_2;
//Mesh components robot_3
var spine_3;
var spine1_3;
var spine2_3;
var neck_3;
var head_3;
var left_shoulder_3;
var left_arm_3;
var left_fore_arm_3;
var left_hand_3;
var right_shoulder_3;
var right_arm_3;
var right_fore_arm_3;
var right_hand_3;
var left_up_leg_3;
var left_leg_3;
var left_foot_3;
var right_up_leg_3;
var right_leg_3;
var right_foot_3;
//Mesh components robot_4
var spine_4;
var spine1_4;
var spine2_4;
var neck_4;
var head_4;
var left_shoulder_4;
var left_arm_4;
var left_fore_arm_4;
var left_hand_4;
var right_shoulder_4;
var right_arm_4;
var right_fore_arm_4;
var right_hand_4;
var left_up_leg_4;
var left_leg_4;
var left_foot_4;
var right_up_leg_4;
var right_leg_4;
var right_foot_4;
//Mesh components robot_5
var spine_5;
var spine1_5;
var spine2_5;
var neck_5;
var head_5;
var left_shoulder_5;
var left_arm_5;
var left_fore_arm_5;
var left_hand_5;
var right_shoulder_5;
var right_arm_5;
var right_fore_arm_5;
var right_hand_5;
var left_up_leg_5;
var left_leg_5;
var left_foot_5;
var right_up_leg_5;
var right_leg_5;
var right_foot_5;
//Mesh components robot_6
var spine_6;
var spine1_6;
var spine2_6;
var neck_6;
var head_6;
var left_shoulder_6;
var left_arm_6;
var left_fore_arm_6;
var left_hand_6;
var right_shoulder_6;
var right_arm_6;
var right_fore_arm_6;
var right_hand_6;
var left_up_leg_6;
var left_leg_6;
var left_foot_6;
var right_up_leg_6;
var right_leg_6;
var right_foot_6;
// Get references to the menu elements
const menu = document.getElementById('menu');
const startButton = document.getElementById('startButton');
const instructionsButton = document.getElementById('instructionsButton');
const modelsButton = document.getElementById('modelsButton');
const instructionsContainer = document.getElementById('instructionsContainer');
// Add event listeners to the buttons
startButton.addEventListener('click', startGame);
instructionsButton.addEventListener('click', showInstructions);
modelsButton.addEventListener('click', showModels);
//Function to show the 3D models
function showModels() {
// Hide the menu and start the game
menu.style.display = 'none';
//Setup the scene
const scene = new THREE.Scene();
var robot_to_show = 0;
// Handle key down events
document.addEventListener('keydown', function(event) {
switch (event.code) {
case 'ArrowLeft':
if(robot_to_show == 0){
robot_to_show = 4;
}else{
robot_to_show -= 1;
}
break;
case 'ArrowRight':
if(robot_to_show == 4){
robot_to_show = 0;
}else{
robot_to_show += 1;
}
break;
}
});
//camera
var camera;
var cameraRadius = 5;
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 0, cameraRadius);
window.addEventListener('resize', resizeCanvas);
function resizeCanvas() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
renderer.outputEncoding = THREE.sRGBEncoding;
// Add a skybox
const t_loader = new THREE.CubeTextureLoader();
const skyboxTextures = [
'./textures/skybox/lightblue/left.png',
'./textures/skybox/lightblue/right.png',
'./textures/skybox/lightblue/top.png',
'./textures/skybox/lightblue/bot.png',
'./textures/skybox/lightblue/back.png',
'./textures/skybox/lightblue/front.png',
];
const skyboxTexture = t_loader.load(skyboxTextures);
scene.background = skyboxTexture;
//lighting
var lightProps = {
"ambientColor": 0xffffff,
"ambientIntensity": 0.1,
"lightColor": 0xffffff,
"lightIntensity": 0.5,
"distance": 100,
}
var ambient = new THREE.AmbientLight(lightProps.ambientColor, lightProps.ambientIntensity)
scene.add(ambient)
//Add first light
const light = new THREE.DirectionalLight(0xffffff, 0.5);
light.position.set(10, 10, 10);
scene.add(light);
//Add 3D models
const loader = new GLTFLoader();
var robot = [];
const geometry = new THREE.BoxGeometry( 1, 1, 1 );
const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
ball = new THREE.Mesh( geometry, material );
ball.position.set(0,0,0);
loader.load('./models/blueBot/blueBot.gltf', function (gltf) {
robot[0] = gltf.scene;
robot[0].traverse(function (child) {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
robot[0].position.x = 0;
robot[0].position.y = -2;
robot[0].position.z = 0;
robot[0].rotation.y = 0 * (Math.PI / 180.0);
robot[0].scale.set(1.7, 1.7, 1.7);
//SPINE
spine_1 = robot[0].getObjectByName('mixamorigSpine');
spine1_1 = robot[0].getObjectByName('mixamorigSpine1');
spine2_1 = robot[0].getObjectByName('mixamorigSpine2');
//NECK AND HEAD
neck_1 = robot[0].getObjectByName('mixamorigNeck');
head_1 = robot[0].getObjectByName('mixamorigHead');
// LEFT ARM
left_shoulder_1 = robot[0].getObjectByName('mixamorigLeftShoulder');
left_arm_1 = robot[0].getObjectByName('mixamorigLeftArm');
left_fore_arm_1 = robot[0].getObjectByName('mixamorigLeftForeArm');
left_hand_1 = robot[0].getObjectByName('mixamorigLeftHand');
// RIGHT ARM
right_shoulder_1 = robot[0].getObjectByName('mixamorigRightShoulder');
right_arm_1 = robot[0].getObjectByName('mixamorigRightArm');
right_fore_arm_1 = robot[0].getObjectByName('mixamorigRightForeArm');
right_hand_1 = robot[0].getObjectByName('mixamorigRightHand');
// LEFT LEG
left_up_leg_1 = robot[0].getObjectByName('mixamorigLeftUpLeg');
left_leg_1 = robot[0].getObjectByName('mixamorigLeftLeg');
left_foot_1 = robot[0].getObjectByName('mixamorigLeftFoot');
// RIGHT LEG
right_up_leg_1 = robot[0].getObjectByName('mixamorigRightUpLeg');
right_leg_1 = robot[0].getObjectByName('mixamorigRightLeg');
right_foot_1 = robot[0].getObjectByName('mixamorigRightFoot');
left_arm_1.rotation.z = Math.PI * -0.35;
right_arm_1.rotation.z = Math.PI * 0.35;
}, undefined, function (error) {
console.error(error);
});
loader.load('./models/blueBot/blueBot.gltf', function (gltf) {
robot[1] = gltf.scene;
robot[1].traverse(function (child) {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
robot[1].position.x = 0;
robot[1].position.y = -2;
robot[1].position.z = 0;
robot[1].rotation.y = 0 * (Math.PI / 180.0);
robot[1].scale.set(1.7, 1.7, 1.7);
//SPINE
spine_2 = robot[1].getObjectByName('mixamorigSpine');
spine1_2 = robot[1].getObjectByName('mixamorigSpine1');
spine2_2 = robot[1].getObjectByName('mixamorigSpine2');
//NECK AND HEAD
neck_2 = robot[1].getObjectByName('mixamorigNeck');
head_2 = robot[1].getObjectByName('mixamorigHead');
// LEFT ARM
left_shoulder_2 = robot[1].getObjectByName('mixamorigLeftShoulder');
left_arm_2 = robot[1].getObjectByName('mixamorigLeftArm');
left_fore_arm_2 = robot[1].getObjectByName('mixamorigLeftForeArm');
left_hand_2 = robot[1].getObjectByName('mixamorigLeftHand');
// RIGHT ARM
right_shoulder_2 = robot[1].getObjectByName('mixamorigRightShoulder');
right_arm_2 = robot[1].getObjectByName('mixamorigRightArm');
right_fore_arm_2 = robot[1].getObjectByName('mixamorigRightForeArm');
right_hand_2 = robot[1].getObjectByName('mixamorigRightHand');
// LEFT LEG
left_up_leg_2 = robot[1].getObjectByName('mixamorigLeftUpLeg');
left_leg_2 = robot[1].getObjectByName('mixamorigLeftLeg');
left_foot_2 = robot[1].getObjectByName('mixamorigLeftFoot');
// RIGHT LEG
right_up_leg_2 = robot[1].getObjectByName('mixamorigRightUpLeg');
right_leg_2 = robot[1].getObjectByName('mixamorigRightLeg');
right_foot_2 = robot[1].getObjectByName('mixamorigRightFoot');
left_arm_2.rotation.z = Math.PI * -0.35;
right_arm_2.rotation.z = Math.PI * 0.35;
}, undefined, function (error) {
console.error(error);
});
loader.load('./models/blueBot/blueBot.gltf', function (gltf) {
robot[2] = gltf.scene;
robot[2].traverse(function (child) {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
robot[2].position.x = 0;
robot[2].position.y = -2;
robot[2].position.z = 0;
robot[2].rotation.y = 0 * (Math.PI / 180.0);
robot[2].scale.set(1.7, 1.7, 1.7);
//SPINE
spine_3 = robot[2].getObjectByName('mixamorigSpine');
spine1_3 = robot[2].getObjectByName('mixamorigSpine1');
spine2_3 = robot[2].getObjectByName('mixamorigSpine2');
//NECK AND HEAD
neck_3 = robot[2].getObjectByName('mixamorigNeck');
head_3 = robot[2].getObjectByName('mixamorigHead');
// LEFT ARM
left_shoulder_3 = robot[2].getObjectByName('mixamorigLeftShoulder');
left_arm_3 = robot[2].getObjectByName('mixamorigLeftArm');
left_fore_arm_3 = robot[2].getObjectByName('mixamorigLeftForeArm');
left_hand_3 = robot[2].getObjectByName('mixamorigLeftHand');
// RIGHT ARM
right_shoulder_3 = robot[2].getObjectByName('mixamorigRightShoulder');
right_arm_3 = robot[2].getObjectByName('mixamorigRightArm');
right_fore_arm_3 = robot[2].getObjectByName('mixamorigRightForeArm');
right_hand_3 = robot[2].getObjectByName('mixamorigRightHand');
// LEFT LEG
left_up_leg_3 = robot[2].getObjectByName('mixamorigLeftUpLeg');
left_leg_3 = robot[2].getObjectByName('mixamorigLeftLeg');
left_foot_3 = robot[2].getObjectByName('mixamorigLeftFoot');
// RIGHT LEG
right_up_leg_3 = robot[2].getObjectByName('mixamorigRightUpLeg');
right_leg_3 = robot[2].getObjectByName('mixamorigRightLeg');
right_foot_3 = robot[2].getObjectByName('mixamorigRightFoot');
left_arm_3.rotation.z = Math.PI * -0.35;
right_arm_3.rotation.z = Math.PI * 0.35;
}, undefined, function (error) {
console.error(error);
});
loader.load('./models/blueBot/blueBot.gltf', function (gltf) {
robot[3] = gltf.scene;
robot[3].traverse(function (child) {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
robot[3].position.x = 0;
robot[3].position.y = -2;
robot[3].position.z = 0;
robot[3].rotation.y = 0 * (Math.PI / 180.0);
robot[3].scale.set(1.7, 1.7, 1.7);
//NECK AND HEAD
robot[3].getObjectByName('mixamorigNeck');
robot[3].getObjectByName('mixamorigHead');
// LEFT ARM
robot[3].getObjectByName('mixamorigLeftArm').rotation.set(0,0,Math.PI*-0.4);
robot[3].getObjectByName('mixamorigLeftForeArm');
// RIGHT ARM
robot[3].getObjectByName('mixamorigRightArm').rotation.set(0,0,Math.PI*0.4);
robot[3].getObjectByName('mixamorigRightForeArm');
// LEFT LEG
robot[3].getObjectByName('mixamorigLeftUpLeg').rotation.set(Math.PI*-0.5,0,0);
robot[3].getObjectByName('mixamorigLeftLeg').rotation.set(Math.PI*0.5,0,0);
// RIGHT LEG
robot[3].getObjectByName('mixamorigRightUpLeg').rotation.set(Math.PI*-0.5,0,0);
robot[3].getObjectByName('mixamorigRightLeg').rotation.set(Math.PI*0.5,0,0);
// Create an AnimationMixer
const mixer = new THREE.AnimationMixer(robot[3]);
// Get the animation clips from the gltf.animations array
const clips = gltf.animations;
// Create a rotation animation for the head
// x^2 + y^2 + z^2 + w^2 = 1 for the quaternion
const sportFanAnimation = new THREE.AnimationClip('fanAnimation', -1, [
new THREE.QuaternionKeyframeTrack('mixamorigHead.quaternion', [0, 1, 2], [0.0881962701678276, 0.16120882791280746, -0.020003503188490868, 0.9954652786254883, 0.0881962701678276, -0.16120882791280746, -0.015541743487119675, 0.9979622960090637, 0.0881962701678276, 0.16120882791280746, -0.020003503188490868, 0.9954652786254883]),
new THREE.QuaternionKeyframeTrack('mixamorigSpine.quaternion', [0, 1, 2], [0.0181962701678276, 0.029498670250177383, -0.010003503188490868, 0.7954652786254883, -0.02331532657146454, 0.031420882791280746, -0.015541743487119675, 0.9979622960090637, 0.0181962701678276, 0.029498670250177383, -0.010003503188490868, 0.7954652786254883]),
new THREE.QuaternionKeyframeTrack('mixamorigLeftArm.quaternion', [0, 1, 2], [
0, -0.605, -0.408, 0.797,
0, -0.605, -0.408, 0.797,
0, -0.605, -0.408, 0.797]),
new THREE.QuaternionKeyframeTrack('mixamorigRightArm.quaternion', [0, 1, 2], [
0, 0.605, 0.408, 0.797,
0, 0.605, 0.408, 0.797,
0, 0.605, 0.408, 0.797]),
new THREE.QuaternionKeyframeTrack('mixamorigLeftForeArm.quaternion', [0, 1, 2], [
0, 0.505, -0.321, -0.646,
0, 0.05, -0.321, -0.646,
0, 0.505, -0.321, -0.646]),
new THREE.QuaternionKeyframeTrack('mixamorigRightForeArm.quaternion', [0, 1, 2], [
0, -0.505, 0.321, -0.646,
0, -0.05, 0.321, -0.646,
0, -0.505, 0.321, -0.646]),
new THREE.QuaternionKeyframeTrack('mixamorigLeftHand.quaternion', [0, 1, 2], [
0.6, -0.15, -0.15, -0.6,
0.6, -0.15, -0.15, -0.6,
0.6, -0.15, -0.15, -0.6]),
new THREE.QuaternionKeyframeTrack('mixamorigRightHand.quaternion', [0, 1, 2], [
0.6, 0.15, 0.15, -0.6,
0.6, 0.15, 0.15, -0.6,
0.6, 0.15, 0.15, -0.6])
]);
// Add the rotation animation to the clips array
clips.push(sportFanAnimation);
//console.log(clips);
// Play the animation
const action = mixer.clipAction(clips[7]);
action.play();
// Create a clock
const clock = new THREE.Clock();
// Update the animation in each frame
function updateAnimation() {
const deltaTimeInSeconds = clock.getDelta();
mixer.update(deltaTimeInSeconds);
// Call the updateAnimation function recursively on the next frame
requestAnimationFrame(updateAnimation);
}
// Start the animation loop
updateAnimation();
}, undefined, function (error) {
console.error(error);
});
loader.load('./models/blueBot/blueBot.gltf', function (gltf) {
robot[4] = gltf.scene;
robot[4].traverse(function (child) {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
robot[4].position.x = 0;
robot[4].position.y = -2;
robot[4].position.z = 0;
robot[4].rotation.y = 0 * (Math.PI / 180.0);
robot[4].scale.set(1.7, 1.7, 1.7);
//SPINE
spine_4 = robot[4].getObjectByName('mixamorigSpine');
spine1_4 = robot[4].getObjectByName('mixamorigSpine1');
spine2_4 = robot[4].getObjectByName('mixamorigSpine2');
//NECK AND HEAD
neck_4 = robot[4].getObjectByName('mixamorigNeck');
head_4 = robot[4].getObjectByName('mixamorigHead');
// LEFT ARM
left_shoulder_4 = robot[4].getObjectByName('mixamorigLeftShoulder');
left_arm_4 = robot[4].getObjectByName('mixamorigLeftArm');
left_fore_arm_4 = robot[4].getObjectByName('mixamorigLeftForeArm');
left_hand_4 = robot[4].getObjectByName('mixamorigLeftHand');
// RIGHT ARM
right_shoulder_4 = robot[4].getObjectByName('mixamorigRightShoulder');
right_arm_4 = robot[4].getObjectByName('mixamorigRightArm');
right_fore_arm_4 = robot[4].getObjectByName('mixamorigRightForeArm');
right_hand_4 = robot[4].getObjectByName('mixamorigRightHand');
// LEFT LEG
left_up_leg_4 = robot[4].getObjectByName('mixamorigLeftUpLeg');
left_leg_4 = robot[4].getObjectByName('mixamorigLeftLeg');
left_foot_4 = robot[4].getObjectByName('mixamorigLeftFoot');
// RIGHT LEG
right_up_leg_4 = robot[4].getObjectByName('mixamorigRightUpLeg');
right_leg_4 = robot[4].getObjectByName('mixamorigRightLeg');
right_foot_4 = robot[4].getObjectByName('mixamorigRightFoot');
left_arm_4.rotation.z = Math.PI * -0.35;
right_arm_4.rotation.z = Math.PI * 0.35;
}, undefined, function (error) {
console.error(error);
});
loader.load('./models/football_ball/scene.gltf', function (gltf7) {
ball2 = gltf7.scene;
ball2.traverse(function (child) {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
ball2.position.x = 0;
ball2.position.y = -1.5;
ball2.position.z = 0.7;
ball2.scale.set(0.2, 0.2, 0.2);
}, undefined, function (error) {
console.error(error);
});
const gui = new dat.GUI()
const lightFolder = gui.addFolder('Light')
lightFolder.add(light.position, "x", -100, 100, 1);
lightFolder.add(light.position, "y", -100, 100, 1);
lightFolder.add(light.position, "z", -100, 100, 1);
lightFolder.add(light, "intensity", 0, 5, 0.5);
lightFolder.open()
const cameraFolder = gui.addFolder('Camera')
cameraFolder.add(camera.position, 'x', 0, 10)
cameraFolder.add(camera.position, 'y', 0, 10)
cameraFolder.add(camera.position, 'z', 0, 10)
cameraFolder.open()
// Create OrbitControls and attach them to the camera
const controls = new OrbitControls(camera, renderer.domElement);
function animate() {
requestAnimationFrame( animate );
if(load_time > 200){
switch(robot_to_show){
case 0:
scene.remove(robot[1]);
scene.remove(robot[2]);
scene.remove(robot[3]);
scene.remove(robot[4]);
scene.remove(ball2);
scene.add(robot[0]);
running(robot[0], left_arm_1, right_arm_1, left_fore_arm_1, right_fore_arm_1, left_up_leg_1, left_leg_1, right_up_leg_1, right_leg_1, neck_1, head_1);
break;
case 1:
scene.remove(robot[0]);
scene.remove(robot[2]);
scene.remove(robot[3]);
scene.remove(robot[4]);
scene.remove(ball2);
scene.add(robot[1]);
standing(robot[1], left_arm_2, right_arm_2, left_fore_arm_2, right_fore_arm_2, left_up_leg_2, left_leg_2, right_up_leg_2, right_leg_2, neck_2, head_2);
break;
case 2:
scene.remove(robot[0]);
scene.remove(robot[1]);
scene.remove(robot[3]);
scene.remove(robot[4]);
scene.remove(ball2);
scene.add(robot[2]);
exultation(robot[2], left_arm_3, right_arm_3, left_fore_arm_3, right_fore_arm_3, left_up_leg_3, left_leg_3, right_up_leg_3, right_leg_3, neck_3, head_3, spine_3);
break;
case 3:
scene.remove(robot[0]);
scene.remove(robot[1]);
scene.remove(robot[2]);
scene.remove(robot[4]);
scene.remove(ball2);
scene.add(robot[3]);
break;
case 4:
scene.remove(robot[0]);
scene.remove(robot[1]);
scene.remove(robot[2]);
scene.remove(robot[3]);
scene.add(robot[4]);
scene.add(ball2);
palleggio(robot[4], left_arm_4, right_arm_4, left_fore_arm_4, right_fore_arm_4, left_up_leg_4, left_leg_4, right_up_leg_4, right_leg_4, neck_4, head_4, ball2);
break;
}
controls.update();
}else{
load_time++;
}
renderer.render( scene, camera );
}
animate();
}
// Function to start the game
function startGame() {
// Hide the menu and start the game
menu.style.display = 'none';
//Setup the scene
const scene = new THREE.Scene();
// Handle key down events
document.addEventListener('keydown', function(event) {
switch (event.code) {
case 'ArrowUp':
moveCameraForward = true;
break;
case 'ArrowDown':
moveCameraBackward = true;
break;
case 'ArrowLeft':
moveCameraLeft = true;
break;
case 'ArrowRight':
moveCameraRight = true;
break;
case 'KeyQ':
if(n_click == 1){
clickX = 0;
clickZ = 0;
scene.remove(line);
scene.remove(cone);
if ( turn == 1){
if(thisrobot ==0){
scene.remove(circle);
createCircle(robot_5);
thisrobot=4;
}
else if(thisrobot ==1){
scene.remove(circle);
createCircle(robot_1);
thisrobot=0;
}
else if(thisrobot ==4){
scene.remove(circle);
createCircle(robot_2);
thisrobot=1;
}
}else if(turn ==2 ){
if(thisrobot ==2){
scene.remove(circle);
createCircle(robot_6);
thisrobot=5;
}
else if(thisrobot ==3){
scene.remove(circle);
createCircle(robot_3);
thisrobot=2;
}
else if(thisrobot ==5){
scene.remove(circle);
createCircle(robot_4);
thisrobot=3;
}
}
setThisRobot(thisrobot);
}
break;
case 'KeyE':
if(n_click == 1){
clickX = 0;
clickZ = 0;
scene.remove(line);
scene.remove(cone);
if ( turn == 1){
if(thisrobot ==4){
scene.remove(circle);
createCircle(robot_1);
thisrobot=0;
}
else if(thisrobot ==0){
scene.remove(circle);
createCircle(robot_2);
thisrobot=1;
}
else if(thisrobot ==1){
scene.remove(circle);
createCircle(robot_5);
thisrobot=4;
}
}else if(turn ==2 ){
if(thisrobot ==5){
scene.remove(circle);
createCircle(robot_3);
thisrobot=2;
}
else if(thisrobot ==2){
scene.remove(circle);
createCircle(robot_4);
thisrobot=3;
}
else if(thisrobot ==3){
scene.remove(circle);
createCircle(robot_6);
thisrobot=5;
}
}
setThisRobot(thisrobot);
}
break;
}
// Handle key up events
document.addEventListener('keyup', function(event) {
switch (event.code) {
case 'ArrowUp':
moveCameraForward = false;
break;
case 'ArrowDown':
moveCameraBackward = false;
break;
case 'ArrowLeft':
moveCameraLeft = false;
break;
case 'ArrowRight':
moveCameraRight = false;
break;
}
});
});
document.addEventListener('mousedown', handleMouseDown);
document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseup', handleMouseUp);
function handleMouseDown(event) {
mouseDown = true;
scene.remove(line);
scene.remove(cone);
endMouseX = event.clientX;
endMouseY = event.clientY;
}
function handleMouseMove(event) {
if(n_click == 1){
const normalizedX = (event.clientX / window.innerWidth) * 2 - 1;
const normalizedY = -(event.clientY / window.innerHeight) * 2 + 1;
const vector = new THREE.Vector3(normalizedX, normalizedY, 0);
//Use the unproject method to convert the vector from NDC to world space
vector.unproject(camera);
//Retrieve the camera's position and direction
const cameraPosition = camera.position;
const cameraDirection = vector.sub(cameraPosition).normalize();
//Calculate the distance along the camera direction to find the 3D position
const mousedistance = -cameraPosition.y / cameraDirection.y;
const mousePosition = cameraPosition.clone().add(cameraDirection.multiplyScalar(mousedistance))
mouseX = mousePosition.x;
mouseY = mousePosition.y;
mouseZ = mousePosition.z;
//console.log("thisrobot"+thisrobot);
if(thisrobot == 0){
scene.remove(line);
scene.remove(cone);
createArrow(robot_1, mouseX, mouseZ);
scene.add(line);
}
if(thisrobot == 1){
scene.remove(line);
scene.remove(cone);
createArrow(robot_2, mouseX, mouseZ);
scene.add(line);
}
if(thisrobot == 2){
scene.remove(line);
scene.remove(cone);
createArrow(robot_3, mouseX, mouseZ);
scene.add(line);
}
if(thisrobot == 3){
scene.remove(line);
scene.remove(cone);
createArrow(robot_4, mouseX, mouseZ);
scene.add(line);
}
if(thisrobot == 4){
scene.remove(line);
scene.remove(cone);
createArrow(robot_5, mouseX, mouseZ);
scene.add(line);
}
if(thisrobot == 5){
scene.remove(line);
scene.remove(cone);
createArrow(robot_6, mouseX, mouseZ);
scene.add(line);
}
}
}
function handleMouseUp(event) {
//console.log("click on pre mouse up: "+n_click);
if(n_click == 1){
n_click += 1;
//console.log("click on post mouse up: "+n_click);
mouseDown = false;
// console.log("Not normalized: "+endMouseX+" "+endMouseY);
//Convert the viewport coordinates to normalized device coordinates (NDC)
const normalizedX = (endMouseX / window.innerWidth) * 2 - 1;
const normalizedY = -(endMouseY / window.innerHeight) * 2 + 1;
//Create a vector with the normalized device coordinates
const vector = new THREE.Vector3(normalizedX, normalizedY, 0);
//Use the unproject method to convert the vector from NDC to world space
vector.unproject(camera);
//Retrieve the camera's position and direction
const cameraPosition = camera.position;
const cameraDirection = vector.sub(cameraPosition).normalize();
//Calculate the distance along the camera direction to find the 3D position
const distance = -cameraPosition.y / cameraDirection.y;
const clickPosition = cameraPosition.clone().add(cameraDirection.multiplyScalar(distance));
//Use the clickPosition vector to access the x, y, and z coordinates
clickX = clickPosition.x;
clickY = clickPosition.y;
clickZ = clickPosition.z;
//console.log("Normalized: "+clickX+" "+clickY+" "+clickZ);
if(isRobotMoving && !endgame){
scene.remove(circle);
//createArrow(robot_1, clickX, clickZ);
//createCircle(robot_1);
scene.add(line);
scene.add(cone);
moveFrom = new THREE.Vector3(robot_1.position.x, robot_1.position.y, robot_1.position.z);
moveTo = new THREE.Vector3(clickX, robot_1.position.y, clickZ);
velocity = 0.1;
}
if(isRobotMoving2 && !endgame){
scene.remove(circle);
//createArrow(robot_2, clickX, clickZ);
//createCircle(robot_2);
scene.add(line);
scene.add(cone);
moveFrom = new THREE.Vector3(robot_2.position.x, robot_2.position.y, robot_2.position.z);
moveTo = new THREE.Vector3(clickX, robot_2.position.y, clickZ);
velocity = 0.1;
}
if(isRobotMoving3 && !endgame){
scene.remove(circle);
//createArrow(robot_3, clickX, clickZ);
//createCircle(robot_3);