forked from yakudoo/TheAviator
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgame.js
1967 lines (1534 loc) · 48.3 KB
/
game.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
function createAirplaneMesh() {
const mesh = new THREE.Object3D()
// Cabin
var matCabin = new THREE.MeshPhongMaterial({color: Colors.red, flatShading: true, side: THREE.DoubleSide})
const frontUR = [ 40, 25, -25]
const frontUL = [ 40, 25, 25]
const frontLR = [ 40, -25, -25]
const frontLL = [ 40, -25, 25]
const backUR = [-40, 15, -5]
const backUL = [-40, 15, 5]
const backLR = [-40, 5, -5]
const backLL = [-40, 5, 5]
const vertices = new Float32Array(
utils.makeTetrahedron(frontUL, frontUR, frontLL, frontLR).concat( // front
utils.makeTetrahedron(backUL, backUR, backLL, backLR)).concat( // back
utils.makeTetrahedron(backUR, backLR, frontUR, frontLR)).concat( // side
utils.makeTetrahedron(backUL, backLL, frontUL, frontLL)).concat( // side
utils.makeTetrahedron(frontUL, backUL, frontUR, backUR)).concat( // top
utils.makeTetrahedron(frontLL, backLL, frontLR, backLR)) // bottom
)
const geomCabin = new THREE.BufferGeometry()
geomCabin.setAttribute('position', new THREE.BufferAttribute(vertices, 3))
var cabin = new THREE.Mesh(geomCabin, matCabin)
cabin.castShadow = true
cabin.receiveShadow = true
mesh.add(cabin)
// Engine
var geomEngine = new THREE.BoxGeometry(20,50,50,1,1,1);
var matEngine = new THREE.MeshPhongMaterial({color:Colors.white, flatShading:true,});
var engine = new THREE.Mesh(geomEngine, matEngine);
//позициÑ
engine.position.x = 50;
engine.castShadow = true;
engine.receiveShadow = true;
mesh.add(engine);
// Tail Plane
var geomTailPlane = new THREE.BoxGeometry(15,20,5,1,1,1);
var matTailPlane = new THREE.MeshPhongMaterial({color:Colors.red, flatShading:true,});
var tailPlane = new THREE.Mesh(geomTailPlane, matTailPlane);
//позициÑ
tailPlane.position.set(-40,20,0);
tailPlane.castShadow = true;
tailPlane.receiveShadow = true;
mesh.add(tailPlane);
// Wings
var geomSideWing = new THREE.BoxGeometry(30,5,120,1,1,1);
var matSideWing = new THREE.MeshPhongMaterial({color:Colors.red, flatShading:true,});
var sideWing = new THREE.Mesh(geomSideWing, matSideWing);
//позициÑ
sideWing.position.set(0,15,0);
sideWing.castShadow = true;
sideWing.receiveShadow = true;
mesh.add(sideWing);
var geomWindshield = new THREE.BoxGeometry(3,15,20,1,1,1);
var matWindshield = new THREE.MeshPhongMaterial({color:Colors.white,transparent:true, opacity:.3, flatShading:true,});;
var windshield = new THREE.Mesh(geomWindshield, matWindshield);
windshield.position.set(20,27,0);
windshield.castShadow = true;
windshield.receiveShadow = true;
mesh.add(windshield);
var geomPropeller = new THREE.BoxGeometry(20, 10, 10, 1, 1, 1);
geomPropeller.attributes.position.array[4*3+1] -= 5
geomPropeller.attributes.position.array[4*3+2] += 5
geomPropeller.attributes.position.array[5*3+1] -= 5
geomPropeller.attributes.position.array[5*3+2] -= 5
geomPropeller.attributes.position.array[6*3+1] += 5
geomPropeller.attributes.position.array[6*3+2] += 5
geomPropeller.attributes.position.array[7*3+1] += 5
geomPropeller.attributes.position.array[7*3+2] -= 5
var matPropeller = new THREE.MeshPhongMaterial({color:Colors.brown, flatShading:true,});
const propeller = new THREE.Mesh(geomPropeller, matPropeller);
propeller.castShadow = true;
propeller.receiveShadow = true;
var geomBlade = new THREE.BoxGeometry(1,80,10,1,1,1);
var matBlade = new THREE.MeshPhongMaterial({color:Colors.brownDark, flatShading:true,});
var blade1 = new THREE.Mesh(geomBlade, matBlade);
//позициÑ
blade1.position.set(8,0,0);
blade1.castShadow = true;
blade1.receiveShadow = true;
var blade2 = blade1.clone();
blade2.rotation.x = Math.PI/2;
blade2.castShadow = true;
blade2.receiveShadow = true;
propeller.add(blade1);
propeller.add(blade2);
propeller.position.set(60,0,0);
mesh.add(propeller);
var wheelProtecGeom = new THREE.BoxGeometry(30,15,10,1,1,1);
var wheelProtecMat = new THREE.MeshPhongMaterial({color:Colors.red, flatShading:true,});
var wheelProtecR = new THREE.Mesh(wheelProtecGeom,wheelProtecMat);
//позициÑ
wheelProtecR.position.set(25,-20,25);
mesh.add(wheelProtecR);
var wheelTireGeom = new THREE.BoxGeometry(24,24,4);
var wheelTireMat = new THREE.MeshPhongMaterial({color:Colors.brownDark, flatShading:true,});
var wheelTireR = new THREE.Mesh(wheelTireGeom,wheelTireMat);
//позициÑ
wheelTireR.position.set(25,-28,25);
var wheelAxisGeom = new THREE.BoxGeometry(10,10,6);
var wheelAxisMat = new THREE.MeshPhongMaterial({color:Colors.brown, flatShading:true,});
var wheelAxis = new THREE.Mesh(wheelAxisGeom,wheelAxisMat);
wheelTireR.add(wheelAxis);
mesh.add(wheelTireR);
var wheelProtecL = wheelProtecR.clone();
wheelProtecL.position.z = -wheelProtecR.position.z ;
mesh.add(wheelProtecL);
var wheelTireL = wheelTireR.clone();
wheelTireL.position.z = -wheelTireR.position.z;
mesh.add(wheelTireL);
var wheelTireB = wheelTireR.clone();
wheelTireB.scale.set(.5,.5,.5);
//позициÑ
wheelTireB.position.set(-35,-5,0);
mesh.add(wheelTireB);
var suspensionGeom = new THREE.BoxGeometry(4,20,4);
suspensionGeom.applyMatrix4(new THREE.Matrix4().makeTranslation(0,10,0))
var suspensionMat = new THREE.MeshPhongMaterial({color:Colors.red, flatShading:true,});
var suspension = new THREE.Mesh(suspensionGeom,suspensionMat);
suspension.position.set(-35,-5,0);
suspension.rotation.z = -.3;
mesh.add(suspension)
const pilot = new Pilot()
pilot.mesh.position.set(5,27,0)
mesh.add(pilot.mesh)
mesh.castShadow = true
mesh.receiveShadow = true
return [mesh, propeller, pilot]
}
const utils = {
normalize: function (v, vmin, vmax, tmin, tmax) {
var nv = Math.max(Math.min(v,vmax), vmin)
var dv = vmax-vmin
var pc = (nv-vmin)/dv
var dt = tmax-tmin
var tv = tmin + (pc*dt)
return tv
},
findWhere: function (list, properties) {
for (const elem of list) {
let all = true
for (const key in properties) {
if (elem[key] !== properties[key]) {
all = false
break
}
}
if (all) {
return elem
}
}
return null
},
randomOneOf: function (choices) {
return choices[Math.floor(Math.random() * choices.length)]
},
randomFromRange: function (min, max) {
return min + Math.random() * (max - min)
},
collide: function (mesh1, mesh2, tolerance) {
const diffPos = mesh1.position.clone().sub(mesh2.position.clone())
const d = diffPos.length()
return d < tolerance
},
makeTetrahedron: function (a, b, c, d) {
return [
a[0], a[1], a[2],
b[0], b[1], b[2],
c[0], c[1], c[2],
b[0], b[1], b[2],
c[0], c[1], c[2],
d[0], d[1], d[2],
]
}
}
class SceneManager {
constructor() {
this.list = new Set()
}
add(obj) {
scene.add(obj.mesh)
this.list.add(obj)
}
remove(obj) {
scene.remove(obj.mesh)
this.list.delete(obj)
}
clear() {
for (const entry of this.list) {
this.remove(entry)
}
}
tick(deltaTime) {
for (const entry of this.list) {
if (entry.tick) {
entry.tick(deltaTime)
}
}
}
}
const sceneManager = new SceneManager()
class LoadingProgressManager {
constructor() {
this.promises = []
}
add(promise) {
this.promises.push(promise)
}
then(callback) {
return Promise.all(this.promises).then(callback)
}
catch(callback) {
return Promise.all(this.promises).catch(callback)
}
}
const loadingProgressManager = new LoadingProgressManager()
class AudioManager {
constructor() {
this.buffers = {}
this.loader = new THREE.AudioLoader()
this.listener = new THREE.AudioListener()
this.categories = {}
}
setCamera(camera) {
camera.add(this.listener)
}
load(soundId, category, path) {
const promise = new Promise((resolve, reject) => {
this.loader.load(path,
(audioBuffer) => {
this.buffers[soundId] = audioBuffer
if (category !== null) {
if (!this.categories[category]) {
this.categories[category] = []
}
this.categories[category].push(soundId)
}
resolve()
},
() => {},
reject
)
})
loadingProgressManager.add(promise)
}
play(soundIdOrCategory, options) {
options = options || {}
let soundId = soundIdOrCategory
const category = this.categories[soundIdOrCategory]
if (category) {
soundId = utils.randomOneOf(category)
}
const buffer = this.buffers[soundId]
const sound = new THREE.Audio(this.listener)
sound.setBuffer(buffer)
if (options.loop) {
sound.setLoop(true)
}
if (options.volume) {
sound.setVolume(options.volume)
}
sound.play()
}
}
const audioManager = new AudioManager()
class ModelManager {
constructor(path) {
this.path = path
this.models = {}
}
load(modelName) {
const promise = new Promise((resolve, reject) => {
const loader = new THREE.OBJLoader()
loader.load(this.path+'/'+modelName+'.obj', (obj) => {
this.models[modelName] = obj
resolve()
}, function() {}, reject)
})
loadingProgressManager.add(promise)
}
get(modelName) {
if (typeof this.models[modelName] === 'undefined') {
throw new Error("Can't find model "+modelName)
}
return this.models[modelName]
}
}
const modelManager = new ModelManager('/models')
var Colors = {
red: 0xf25346,
orange: 0xffa500,
white: 0xd8d0d1,
brown: 0x59332e,
brownDark: 0x23190f,
pink: 0xF5986E,
yellow: 0xf4ce93,
blue: 0x68c3c0,
}
const COLOR_COINS = 0xFFD700 // 0x009999
const COLOR_COLLECTIBLE_BUBBLE = COLOR_COINS
///////////////
// GAME VARIABLES
var canDie = true
var world, game
var newTime = new Date().getTime()
var oldTime = new Date().getTime()
let scene, camera, renderer
//SCREEN & MOUSE VARIABLES
var MAX_WORLD_X=1000
//INIT THREE JS, SCREEN AND MOUSE EVENTS
function createScene() {
scene = new THREE.Scene()
camera = new THREE.PerspectiveCamera(50, ui.width/ui.height, 0.1, 10000)
audioManager.setCamera(camera)
scene.fog = new THREE.Fog(0xf7d9aa, 100, 950)
renderer = new THREE.WebGLRenderer({canvas: ui.canvas, alpha: true, antialias: true})
renderer.setSize(ui.width, ui.height)
renderer.setPixelRatio(window.devicePixelRatio? window.devicePixelRatio : 1)
renderer.shadowMap.enabled = true
function setupCamera() {
renderer.setSize(ui.width, ui.height)
camera.aspect = ui.width / ui.height
camera.updateProjectionMatrix()
// setTimeout(() => {
// const rayCaster = new THREE.Raycaster()
// rayCaster.setFromCamera(new THREE.Vector2(1, 1), camera)
// const plane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0)
// const intersectPoint = new THREE.Vector3()
// rayCaster.ray.intersectPlane(plane, intersectPoint)
// console.log('max world x:', intersectPoint.x)
// // MAX_WORLD_X = intersectPoint.x doesn't work with first person view
// }, 500)
}
setupCamera()
ui.onResize(setupCamera)
// const controls = new THREE.OrbitControls(camera, renderer.domElement)
// controls.minPolarAngle = -Math.PI / 2
// controls.maxPolarAngle = Math.PI
// controls.addEventListener('change', () => {
// console.log('camera changed', 'camera=', camera.position, ', airplane=', airplane.position, 'camera.rotation=', camera.rotation)
// })
// setTimeout(() => {
// camera.lookAt(airplane.mesh.position)
// controls.target.copy(airplane.mesh.position)
// }, 100)
// controls.noZoom = true
//controls.noPan = true
// handleWindowResize()
}
// LIGHTS
var ambientLight
function createLights() {
const hemisphereLight = new THREE.HemisphereLight(0xaaaaaa,0x000000, .9)
ambientLight = new THREE.AmbientLight(0xdc8874, .5)
const shadowLight = new THREE.DirectionalLight(0xffffff, .9)
shadowLight.position.set(150, 350, 350)
shadowLight.castShadow = true
shadowLight.shadow.camera.left = -400
shadowLight.shadow.camera.right = 400
shadowLight.shadow.camera.top = 400
shadowLight.shadow.camera.bottom = -400
shadowLight.shadow.camera.near = 1
shadowLight.shadow.camera.far = 1000
shadowLight.shadow.mapSize.width = 4096
shadowLight.shadow.mapSize.height = 4096
scene.add(hemisphereLight)
scene.add(shadowLight)
scene.add(ambientLight)
}
class Pilot {
constructor() {
this.mesh = new THREE.Object3D()
this.angleHairs = 0
var bodyGeom = new THREE.BoxGeometry(15,15,15)
var bodyMat = new THREE.MeshPhongMaterial({
color: Colors.brown,
flatShading: true,
})
var body = new THREE.Mesh(bodyGeom, bodyMat)
body.position.set(2, -12, 0)
this.mesh.add(body)
var faceGeom = new THREE.BoxGeometry(10,10,10)
var faceMat = new THREE.MeshLambertMaterial({color: Colors.pink})
var face = new THREE.Mesh(faceGeom, faceMat)
this.mesh.add(face)
var hairGeom = new THREE.BoxGeometry(4,4,4)
var hairMat = new THREE.MeshLambertMaterial({color:Colors.brown})
var hair = new THREE.Mesh(hairGeom, hairMat)
hair.geometry.applyMatrix4(new THREE.Matrix4().makeTranslation(0,2,0))
var hairs = new THREE.Object3D()
this.hairsTop = new THREE.Object3D()
for (var i=0; i<12; i++) {
var h = hair.clone();
var col = i%3;
var row = Math.floor(i/3);
var startPosZ = -4;
var startPosX = -4;
h.position.set(startPosX + row*4, 0, startPosZ + col*4);
h.geometry.applyMatrix4(new THREE.Matrix4().makeScale(1,1,1));
this.hairsTop.add(h);
}
hairs.add(this.hairsTop);
var hairSideGeom = new THREE.BoxGeometry(12,4,2);
hairSideGeom.applyMatrix4(new THREE.Matrix4().makeTranslation(-6,0,0));
var hairSideR = new THREE.Mesh(hairSideGeom, hairMat);
var hairSideL = hairSideR.clone();
hairSideR.position.set(8,-2,6);
hairSideL.position.set(8,-2,-6);
hairs.add(hairSideR);
hairs.add(hairSideL);
var hairBackGeom = new THREE.BoxGeometry(2,8,10);
var hairBack = new THREE.Mesh(hairBackGeom, hairMat);
hairBack.position.set(-1,-4,0)
hairs.add(hairBack);
hairs.position.set(-5,5,0);
this.mesh.add(hairs);
var glassGeom = new THREE.BoxGeometry(5,5,5);
var glassMat = new THREE.MeshLambertMaterial({color:Colors.brown});
var glassR = new THREE.Mesh(glassGeom,glassMat);
glassR.position.set(6,0,3);
var glassL = glassR.clone();
glassL.position.z = -glassR.position.z
var glassAGeom = new THREE.BoxGeometry(11,1,11);
var glassA = new THREE.Mesh(glassAGeom, glassMat);
this.mesh.add(glassR);
this.mesh.add(glassL);
this.mesh.add(glassA);
var earGeom = new THREE.BoxGeometry(2,3,2);
var earL = new THREE.Mesh(earGeom,faceMat);
earL.position.set(0,0,-6);
var earR = earL.clone();
earR.position.set(0,0,6);
this.mesh.add(earL);
this.mesh.add(earR);
}
updateHairs(deltaTime) {
var hairs = this.hairsTop.children
var l = hairs.length
for (var i=0; i<l; i++) {
var h = hairs[i]
h.scale.y = .75 + Math.cos(this.angleHairs+i/3)*.25
}
this.angleHairs += game.speed * deltaTime * 40
}
}
// GUNS
class SimpleGun {
constructor() {
this.mesh = SimpleGun.createMesh()
this.mesh.position.z = 28
this.mesh.position.x = 25
this.mesh.position.y = -8
}
static createMesh() {
const metalMaterial = new THREE.MeshStandardMaterial({color: 0x222222, flatShading: true, roughness: 0.5, metalness: 1.0})
const BODY_RADIUS = 3
const BODY_LENGTH = 20
const full = new THREE.Group()
const body = new THREE.Mesh(
new THREE.CylinderGeometry(BODY_RADIUS, BODY_RADIUS, BODY_LENGTH),
metalMaterial,
)
body.rotation.z = Math.PI/2
full.add(body)
const barrel = new THREE.Mesh(
new THREE.CylinderGeometry(BODY_RADIUS/2, BODY_RADIUS/2, BODY_LENGTH),
metalMaterial,
)
barrel.rotation.z = Math.PI/2
barrel.position.x = BODY_LENGTH
full.add(barrel)
return full
}
downtime() {
return 0.1
}
damage() {
return 1
}
shoot(direction) {
const BULLET_SPEED = 0.5
const RECOIL_DISTANCE = 4
const RECOIL_DURATION = this.downtime() / 1.5
const position = new THREE.Vector3()
this.mesh.getWorldPosition(position)
position.add(new THREE.Vector3(5, 0, 0))
spawnProjectile(this.damage(), position, direction, BULLET_SPEED, 0.3, 3)
// Little explosion at exhaust
spawnParticles(position.clone().add(new THREE.Vector3(2,0,0)), 1, Colors.orange, 0.2)
// audio
audioManager.play('shot-soft')
// Recoil of gun
const initialX = this.mesh.position.x
TweenMax.to(this.mesh.position, {
duration: RECOIL_DURATION/2,
x: initialX - RECOIL_DISTANCE,
onComplete: () => {
TweenMax.to(this.mesh.position, {
duration: RECOIL_DURATION/2,
x: initialX,
})
},
})
}
}
class DoubleGun {
constructor() {
this.gun1 = new SimpleGun()
this.gun2 = new SimpleGun()
this.gun2.mesh.position.add(new THREE.Vector3(0, 14, 0))
this.mesh = new THREE.Group()
this.mesh.add(this.gun1.mesh)
this.mesh.add(this.gun2.mesh)
}
downtime() {
return 0.15
}
damage() {
return this.gun1.damage() + this.gun2.damage()
}
shoot(direction) {
this.gun1.shoot(direction)
this.gun2.shoot(direction)
}
}
class BetterGun {
constructor() {
this.mesh = BetterGun.createMesh()
this.mesh.position.z = 28
this.mesh.position.x = -3
this.mesh.position.y = -5
}
static createMesh() {
const metalMaterial = new THREE.MeshStandardMaterial({color: 0x222222, flatShading: true, roughness: 0.5, metalness: 1.0})
const BODY_RADIUS = 5
const BODY_LENGTH = 30
const full = new THREE.Group()
const body = new THREE.Mesh(
new THREE.CylinderGeometry(BODY_RADIUS, BODY_RADIUS, BODY_LENGTH),
metalMaterial,
)
body.rotation.z = Math.PI/2
body.position.x = BODY_LENGTH/2
full.add(body)
const BARREL_RADIUS = BODY_RADIUS/2
const BARREL_LENGTH = BODY_LENGTH * 0.66
const barrel = new THREE.Mesh(
new THREE.CylinderGeometry(BARREL_RADIUS, BARREL_RADIUS, BARREL_LENGTH),
metalMaterial,
)
barrel.rotation.z = Math.PI/2
barrel.position.x = BODY_LENGTH + BARREL_LENGTH/2
full.add(barrel)
const TIP_RADIUS = BARREL_RADIUS * 1.3
const TIP_LENGTH = BODY_LENGTH/4
const tip = new THREE.Mesh(
new THREE.CylinderGeometry(TIP_RADIUS, TIP_RADIUS, TIP_LENGTH),
metalMaterial,
)
tip.rotation.z = Math.PI/2
tip.position.x = BODY_LENGTH + BARREL_LENGTH + TIP_LENGTH/2
full.add(tip)
return full
}
downtime() {
return 0.1
}
damage() {
return 5
}
shoot(direction) {
const BULLET_SPEED = 0.5
const RECOIL_DISTANCE = 4
const RECOIL_DURATION = this.downtime() / 3
// position = position.clone().add(new THREE.Vector3(11.5, -1.3, 7.5))
const position = new THREE.Vector3()
this.mesh.getWorldPosition(position)
position.add(new THREE.Vector3(12, 0, 0))
spawnProjectile(this.damage(), position, direction, BULLET_SPEED, 0.8, 6)
// Little explosion at exhaust
spawnParticles(position.clone().add(new THREE.Vector3(2,0,0)), 3, Colors.orange, 0.5)
// audio
audioManager.play('shot-hard')
// Recoil of gun
const initialX = this.mesh.position.x
TweenMax.to(this.mesh.position, {
duration: RECOIL_DURATION,
x: initialX - RECOIL_DISTANCE,
onComplete: () => {
TweenMax.to(this.mesh.position, {
duration: RECOIL_DURATION,
x: initialX,
})
},
})
}
}
class Airplane {
constructor() {
const [mesh, propeller, pilot] = createAirplaneMesh()
this.mesh = mesh
this.propeller = propeller
this.pilot = pilot
this.weapon = null
this.lastShot = 0
}
equipWeapon(weapon) {
if (this.weapon) {
this.mesh.remove(this.weapon.mesh)
}
this.weapon = weapon
if (this.weapon) {
this.mesh.add(this.weapon.mesh)
}
}
shoot() {
if (!this.weapon) {
return
}
// rate-limit the shooting
const nowTime = new Date().getTime() / 1000
const ready = nowTime-this.lastShot > this.weapon.downtime()
if (!ready) {
return
}
this.lastShot = nowTime
// fire the shot
let direction = new THREE.Vector3(10, 0, 0)
direction.applyEuler(airplane.mesh.rotation)
this.weapon.shoot(direction)
// recoil airplane
const recoilForce = this.weapon.damage()
TweenMax.to(this.mesh.position, {
duration: 0.05,
x: this.mesh.position.x - recoilForce,
})
}
tick(deltaTime) {
this.propeller.rotation.x += 0.2 + game.planeSpeed * deltaTime*.005
if (game.status === 'playing') {
game.planeSpeed = utils.normalize(ui.mousePos.x, -0.5, 0.5, world.planeMinSpeed, world.planeMaxSpeed)
let targetX = utils.normalize(ui.mousePos.x, -1, 1, -world.planeAmpWidth*0.7, -world.planeAmpWidth)
let targetY = utils.normalize(ui.mousePos.y, -0.75, 0.75, world.planeDefaultHeight-world.planeAmpHeight, world.planeDefaultHeight+world.planeAmpHeight)
game.planeCollisionDisplacementX += game.planeCollisionSpeedX
targetX += game.planeCollisionDisplacementX
game.planeCollisionDisplacementY += game.planeCollisionSpeedY
targetY += game.planeCollisionDisplacementY
this.mesh.position.x += (targetX - this.mesh.position.x) * deltaTime * world.planeMoveSensivity
this.mesh.position.y += (targetY - this.mesh.position.y) * deltaTime * world.planeMoveSensivity
this.mesh.rotation.x = (this.mesh.position.y - targetY) * deltaTime * world.planeRotZSensivity
this.mesh.rotation.z = (targetY - this.mesh.position.y) * deltaTime * world.planeRotXSensivity
if (game.fpv) {
camera.position.y = this.mesh.position.y + 20
// camera.setRotationFromEuler(new THREE.Euler(-1.490248, -1.4124514, -1.48923231))
// camera.updateProjectionMatrix ()
} else {
camera.fov = utils.normalize(ui.mousePos.x, -30, 1, 40, 80)
camera.updateProjectionMatrix()
camera.position.y += (this.mesh.position.y - camera.position.y) * deltaTime * world.cameraSensivity
}
}
game.planeCollisionSpeedX += (0-game.planeCollisionSpeedX)*deltaTime * 0.03;
game.planeCollisionDisplacementX += (0-game.planeCollisionDisplacementX)*deltaTime *0.01;
game.planeCollisionSpeedY += (0-game.planeCollisionSpeedY)*deltaTime * 0.03;
game.planeCollisionDisplacementY += (0-game.planeCollisionDisplacementY)*deltaTime *0.01;
this.pilot.updateHairs(deltaTime)
}
gethit(position) {
const diffPos = this.mesh.position.clone().sub(position)
const d = diffPos.length()
game.planeCollisionSpeedX = 100 * diffPos.x / d
game.planeCollisionSpeedY = 100 * diffPos.y / d
ambientLight.intensity = 2
audioManager.play('airplane-crash')
}
}
function rotateAroundSea(object, deltaTime, speed) {
object.angle += deltaTime * game.speed * world.collectiblesSpeed
if (object.angle > Math.PI*2) {
object.angle -= Math.PI*2
}
object.mesh.position.x = Math.cos(object.angle) * object.distance
object.mesh.position.y = -world.seaRadius + Math.sin(object.angle) * object.distance
}
class Collectible {
constructor(mesh, onApply) {
this.angle = 0
this.distance = 0
this.onApply = onApply
this.mesh = new THREE.Object3D()
const bubble = new THREE.Mesh(
new THREE.SphereGeometry(10, 100, 100),
new THREE.MeshPhongMaterial({
color: COLOR_COLLECTIBLE_BUBBLE,
transparent: true,
opacity: .4,
flatShading: true,
})
)
this.mesh.add(bubble)
this.mesh.add(mesh)
this.mesh.castShadow = true
// for the angle:
// Math.PI*2 * 0.0 => on the right side of the sea cylinder
// Math.PI*2 * 0.1 => on the top right
// Math.PI*2 * 0.2 => directly in front of the plane
// Math.PI*2 * 0.3 => directly behind the plane
// Math.PI*2 * 0.4 => on the top left
// Math.PI*2 * 0.5 => on the left side
this.angle = Math.PI*2 * 0.1
this.distance = world.seaRadius + world.planeDefaultHeight + (-1 + 2*Math.random()) * (world.planeAmpHeight-20)
this.mesh.position.y = -world.seaRadius + Math.sin(this.angle) * this.distance
this.mesh.position.x = Math.cos(this.angle) * this.distance
sceneManager.add(this)
}
tick(deltaTime) {
rotateAroundSea(this, deltaTime, world.collectiblesSpeed)
// rotate collectible for visual effect
this.mesh.rotation.y += deltaTime * 0.002 * Math.random()
this.mesh.rotation.z += deltaTime * 0.002 * Math.random()
// collision?
if (utils.collide(airplane.mesh, this.mesh, world.collectibleDistanceTolerance)) {
this.onApply()
this.explode()
}
// passed-by?
else if (this.angle > Math.PI) {
sceneManager.remove(this)
}
}
explode() {
spawnParticles(this.mesh.position.clone(), 15, COLOR_COLLECTIBLE_BUBBLE, 3)
sceneManager.remove(this)
audioManager.play('bubble')
const DURATION = 1
setTimeout(() => {
const itemMesh = new THREE.Group()
for (let i=1; i<this.mesh.children.length; i+=1) {
itemMesh.add(this.mesh.children[i])
}
scene.add(itemMesh)
itemMesh.position.y = 120
itemMesh.position.z = 50
const initialScale = itemMesh.scale.clone()
TweenMax.to(itemMesh.scale, {
duration: DURATION / 2,
x: initialScale.x * 4,
y: initialScale.y * 4,
z: initialScale.z * 4,
ease: 'Power2.easeInOut',
onComplete: () => {
TweenMax.to(itemMesh.scale, {
duration: DURATION / 2,
x: 0,
y: 0,
z: 0,
ease: 'Power2.easeInOut',
onComplete: () => {
scene.remove(itemMesh)
},
})
},
})
}, 200)
}
}
function spawnSimpleGunCollectible() {
const gun = SimpleGun.createMesh()
gun.scale.set(0.25, 0.25, 0.25)
gun.position.x = -2
new Collectible(gun, () => {
airplane.equipWeapon(new SimpleGun())
})
}
function spawnBetterGunCollectible() {
const gun = BetterGun.createMesh()
gun.scale.set(0.25, 0.25, 0.25)
gun.position.x = -7
new Collectible(gun, () => {
airplane.equipWeapon(new BetterGun())
})