-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
1336 lines (1115 loc) · 52.8 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
class Game {
constructor(state) {
this.state = state;
this.state.lights = [];
this.canvas = document.querySelector('canvas');
this.plane = getObject(state, "tempPlane");
this.light = getObject(state, "lightSource");
this.lightSource = state.pointLights.find(light => light.name === "lightSource");
this.playerLight = state.pointLights.find(light => light.name === "playerLight");
this.weaponLight = state.pointLights.find(light => light.name === "weaponLight");
this.enemy1light = state.pointLights.find(light => light.name === "Enemy1Light");
this.controlCamera = false;
this.cameraSpeed = 15;
this.isFirstPersonCamera = false;
this.handleMouseDown = this.handleMouseDown.bind(this);
this.mouseDownPosition = { x: 0, y: 0 };
this.mouseSensitivity = 0.002;
this.mousePosition = {
x: 0,
y: 0
};
this.prevMousePosition = {
x: 0,
y: 0
};
this.isMousePressed = false;
this.keyPressed = {
w: false,
a: false,
s: false,
d: false,
z: false,
x: false,
q: false,
e: false
};
this.spaceship = getObject(state, "SpaceShip");
this.spaceshipSpeed = 0.7;
this.spaceshipVelocity = { x: 0, y: 0 }; // Track spaceship's velocity
this.spaceshipSpawnDelay = 2;
this.spaceshipSpawned = false;
this.cubeSpawnInterval = null; // Store the interval ID
this.cubeSpawnRate = 100; // Time in milliseconds between cube spawns
this.cubeSpeed = 55;
this.shootingCurve = 10; // How much the player can change projectile trajectory
this.wave = 0;
this.waveNumber = document.getElementById('waveNumber');
this.waveComplete = false; // For round intermission
this.enemy1 = getObject(state, "Enemy1");
this.enemySpeed = 1;
this.enemy1Health = 200;
this.enemy2Health = 0;
this.enemy3Health = 0;
this.enemy4Health = 0;
this.enemy5Health = 0;
this.lastWaveHealth = 200; // Base health
this.enemyNum = 1; // Number of enemies in play
this.enemyHealthTotal = 200;
this.maxEnemyHealthTotal = 200; // Max health of the enemy health pool
this.enemyHealthBar = document.getElementById('enemyHealth');
this.enemy1.xMovementFactor = 1;
this.enemy1.yMovementFactor = 1;
this.enemy1.zMovementFactor = 1;
this.enemy1MoveDirection = [];
this.enemy1Velocity = [];
this.enemyIsAttacking = false;
this.enemyAttackIntervalSet = false;
this.enemyFireRate = 350;
this.enemy1Killed = false;
this.enemyInvulnerable = false;
this.intermission = true;
// this.enemy2 = getObject(state, "Enemy2");
// this.enemy3 = getObject(state, "Enemy3");
// this.enemy4 = getObject(state, "Enemy4");
// this.enemy5 = getObject(state, "Enemy5");
// this.enemy6 = getObject(state, "Enemy6");
// this.enemy7 = getObject(state, "Enemy7");
// this.enemy8 = getObject(state, "Enemy8");
// this.enemy9 = getObject(state, "Enemy9");
// this.enemy10 = getObject(state, "Enemy10");
this.asteroidPool = [];
//this.spawnAsteroidField();
this.rollAngle = 0; // Current roll angle
this.currentRoll = 0;
this.maxRollAngle = Math.PI / 6; // Maximum roll angle for full roll
this.rollSpeed = 0.0003; // Speed at which the ship rolls
this.rollReturnSpeed = 0.00004; // Speed at which the ship returns to no roll
this.shipUp = vec3.fromValues(0, 1, 0); // Up vector for the ship
this.spaceshipTotalRotation = 0;
this.isShipPointedAtMouse = false;
this.updatePositionDelay = 2; // Delay in seconds
this.updatePositionStarted = false;
this.playerHealth = 100;
this.playerHealthBar = document.getElementById('healthBar');
this.playerBoost = 50;
this.playerScore = 0;
this.isBoosting = false; // to be added later
this.currentBoost = this.maxBoost;
this.boostDecayRate = 1;
this.boostRegenRate = 0.5;
this.spawnedObjects = [];
this.collidableObjects = [];
this.lastSpawnZ = 0;
this.layerCount = 0;
this.spawnQueue = []; // Queue of spawn taskss
this.isSpawning = false; // Flag to indicate if we're currently spawning
this.spawnThreshold = 20; // Adjust as needed based on camera speed
this.spawnDistanceAhead = 20; // How far ahead to spawn the new field
this.gameOver = false;
this.frame = 0;
}
initializeMouseInput() {
document.addEventListener("mousemove", (e) => this.handleMouseMove(e));
document.addEventListener("mousedown", (e) => this.handleMouseDown(e));
document.addEventListener("mouseup", (e) => this.handleMouseUp(e));
}
toggleControls() {
this.controlCamera = !this.controlCamera;
console.log(`Controls toggled. Camera control is now ${this.controlCamera ? 'on' : 'off'}.`);
}
updateCameraPosition() {
// Calculate direction vectors
let forward = vec3.clone(this.state.camera.front);
let right = vec3.create();
vec3.cross(right, this.state.camera.up, forward);
vec3.normalize(right, right);
let movementDirection = vec3.create();
// Combine directions based on key states
if (this.keyPressed.w) vec3.add(movementDirection, movementDirection, forward);
if (this.keyPressed.s) vec3.subtract(movementDirection, movementDirection, forward);
if (this.keyPressed.a) vec3.add(movementDirection, movementDirection, right);
if (this.keyPressed.d) vec3.subtract(movementDirection, movementDirection, right);
if (this.keyPressed.z) vec3.add(movementDirection, movementDirection, this.state.camera.up);
if (this.keyPressed.x) vec3.subtract(movementDirection, movementDirection, this.state.camera.up);
vec3.scale(movementDirection, movementDirection, 0.1);
// Update the camera's position
vec3.add(this.state.camera.position, this.state.camera.position, movementDirection);
// Update the view matrix
mat4.lookAt(
this.state.camera.viewMatrix,
this.state.camera.position,
vec3.add(vec3.create(), this.state.camera.position, this.state.camera.front),
this.state.camera.up
);
}
updateFirstPersonCamera() {
// Constant z-offset
const zOffset = -3;
// Calculate the dynamic offset based on the ship's up vector
let dynamicOffset = vec3.create();
vec3.scale(dynamicOffset, this.shipUp, 1); // Scale the ship's up vector by 1 unit
dynamicOffset[2] = zOffset; // Set the z component to the constant offset
// Add the dynamic offset to the spaceship's position for the camera's world position
let cameraWorldPosition = vec3.create();
vec3.add(cameraWorldPosition, this.spaceship.model.position, dynamicOffset);
this.state.camera.position = cameraWorldPosition;
this.state.camera.front = vec3.fromValues(0, 0, 1);
this.state.camera.up = vec3.clone(this.shipUp);
// Update the camera view matrix
mat4.lookAt(
this.state.camera.viewMatrix,
this.state.camera.position,
vec3.add(vec3.create(), this.state.camera.position, this.state.camera.front),
this.state.camera.up
);
}
resetCamera() {
// Reset the position with the current z-position of the spaceship
const zPosition = this.spaceship.model.position[2];
this.state.camera.position = vec3.fromValues(0, 5, zPosition - 7);
// Reset the up vector
this.state.camera.up = vec3.fromValues(0, 1, 0);
// Reset the lookat
this.state.camera.front = vec3.fromValues(-0.01313674737364058, -0.33873793690267073, 0.9407890496659511) // Original values
// Update the view matrix to reflect these changes
mat4.lookAt(
this.state.camera.viewMatrix,
this.state.camera.position,
vec3.add(vec3.create(), this.state.camera.position, this.state.camera.front),
this.state.camera.up
);
}
rotateCamera(angle) {
// Create a rotation matrix around the Y-axis
let rotationMatrix = mat4.create();
mat4.rotateY(rotationMatrix, rotationMatrix, angle);
// Apply the rotation to the camera's front vector
let rotatedFront = vec3.create();
vec3.transformMat4(rotatedFront, this.state.camera.front, rotationMatrix);
this.state.camera.front = rotatedFront;
// Update the view matrix
mat4.lookAt(
this.state.camera.viewMatrix,
this.state.camera.position,
vec3.add(vec3.create(), this.state.camera.position, this.state.camera.front),
this.state.camera.up
);
}
// Movement involved with the player's ship
updateSpaceshipPosition(deltaTime) {
let moveDirection = vec3.create();
// Movement keys
const movementIncrement = 0.003;
if (this.isFirstPersonCamera) { // For first person mode
// Calculate the direction vectors based on the ship's up vector
let forward = vec3.fromValues(0, 0, 1);
let right = vec3.create();
vec3.cross(right, this.shipUp, forward);
vec3.normalize(right, right);
if (this.keyPressed.w) {
vec3.add(moveDirection, moveDirection, this.shipUp);
}
if (this.keyPressed.s) {
vec3.subtract(moveDirection, moveDirection, this.shipUp);
}
if (this.keyPressed.a) {
vec3.add(moveDirection, moveDirection, right);
this.targetRollAngle = this.maxRollAngle;
}
if (this.keyPressed.d) {
vec3.subtract(moveDirection, moveDirection, right);
this.targetRollAngle = -this.maxRollAngle;
}
// Apply the movement
vec3.scale(moveDirection, moveDirection, movementIncrement);
this.spaceshipVelocity.x += moveDirection[0];
this.spaceshipVelocity.y += moveDirection[1];
} else {
if (this.keyPressed.w) {
this.spaceshipVelocity.y += movementIncrement;
}
if (this.keyPressed.s) {
this.spaceshipVelocity.y -= movementIncrement;
}
if (this.keyPressed.a) {
this.spaceshipVelocity.x += movementIncrement;
this.targetRollAngle = this.maxRollAngle;
}
if (this.keyPressed.d) {
this.spaceshipVelocity.x -= movementIncrement;
this.targetRollAngle = -this.maxRollAngle;
}
}
if (this.keyPressed.q) {
this.targetRollAngle = this.maxRollAngle;
}
if (this.keyPressed.e) {
this.targetRollAngle = -this.maxRollAngle;
}
// Clamp the currentRollAngle to prevent excessive rolling
this.currentRollAngle = Math.max(-this.maxRollAngle, Math.min(this.maxRollAngle, this.currentRollAngle));
if (this.isFirstPersonCamera) {
this.rollAngle = Math.max(-0.03, Math.min(0.01, this.rollAngle));
} else {
this.rollAngle = Math.max(-0.1, Math.min(0.1, this.rollAngle));
}
// Gradually adjust the roll angle towards the target roll angle
if (this.isFirstPersonCamera && (this.keyPressed.a || this.keyPressed.d)) { // Slower roll speed in first person A and D keys
const firstPersonRollSpeed = this.rollSpeed / 6;
if (this.rollAngle < this.targetRollAngle) {
this.rollAngle = Math.min(this.rollAngle + firstPersonRollSpeed, this.targetRollAngle);
} else if (this.rollAngle > this.targetRollAngle) {
this.rollAngle = Math.max(this.rollAngle - firstPersonRollSpeed, this.targetRollAngle);
}
} else {
// Original logic for outside first-person mode
if (this.rollAngle < this.targetRollAngle) {
this.rollAngle = Math.min(this.rollAngle + this.rollSpeed, this.targetRollAngle);
} else if (this.rollAngle > this.targetRollAngle) {
this.rollAngle = Math.max(this.rollAngle - this.rollSpeed, this.targetRollAngle);
}
}
// Gradually return the roll angle to zero when no left/right movement
if (!this.keyPressed.a && !this.keyPressed.d) {
if (this.rollAngle > 0) {
this.rollAngle = Math.max(this.rollAngle - this.rollReturnSpeed, 0);
} else if (this.rollAngle < 0) {
this.rollAngle = Math.min(this.rollAngle + this.rollReturnSpeed, 0);
}
this.targetRollAngle = 0; // Reset target roll angle
}
// Apply rotation
this.spaceship.rotate('z', this.rollAngle);
// Update the total rotation of the spaceship
const scaleFactor = 180 / 1.5; // Calibrated to match the actual rotation
this.spaceshipTotalRotation += (this.rollAngle * deltaTime) * scaleFactor;
this.spaceshipTotalRotation = (this.spaceshipTotalRotation + 2 * Math.PI) % (2 * Math.PI); // Normalize the total rotation
// Calculate the up vector components based on the scaled rotation
let upVectorX = Math.cos(this.spaceshipTotalRotation - Math.PI / 2);
let upVectorY = -Math.sin(this.spaceshipTotalRotation - Math.PI / 2);
// Normalize the up vector
const length = Math.sqrt(upVectorX * upVectorX + upVectorY * upVectorY);
upVectorX /= length;
upVectorY /= length;
// Set the up vector for the spaceship
this.shipUp = vec3.fromValues(upVectorX, upVectorY, 0);
// Limit the spaceship's velocity
const spaceshipVelocityMaximum = 0.1;
this.spaceshipVelocity.x = Math.max(-spaceshipVelocityMaximum, Math.min(spaceshipVelocityMaximum, this.spaceshipVelocity.x));
this.spaceshipVelocity.y = Math.max(-spaceshipVelocityMaximum, Math.min(spaceshipVelocityMaximum, this.spaceshipVelocity.y));
// Apply deceleration when movement keys are not pressed
const decelerationFactor = 0.99; // More pronounced deceleration
if (!this.keyPressed.w && !this.keyPressed.s) {
this.spaceshipVelocity.y *= decelerationFactor;
}
if (!this.keyPressed.a && !this.keyPressed.d) {
this.spaceshipVelocity.x *= decelerationFactor;
}
// Define clamping bounds with a slight overstep allowance
const minX = -7, maxX = 7, minY = -3, maxY = 5;
// Update spaceship's position based on its velocity
let newX = this.spaceship.model.position[0] + this.spaceshipVelocity.x;
let newY = this.spaceship.model.position[1] + this.spaceshipVelocity.y;
vec3.scale(moveDirection, moveDirection, this.spaceshipSpeed);
// Check if spaceship is out of bounds and adjust position accordingly
let outOfBoundsX = newX < minX || newX > maxX;
let outOfBoundsY = newY < minY || newY > maxY;
const maxReturnSpeed = 0.06; // Maximum speed for returning to bounds
const overstepMargin = 0.2; // Allow some overstepping
const returnAcceleration = 0.002; // Acceleration when returning to bounds
if (outOfBoundsX) {
let boundaryX = newX < minX ? minX : maxX;
let distanceX = Math.abs(newX - boundaryX) - overstepMargin;
let directionX = newX < minX ? 1 : -1;
this.spaceshipVelocity.x += directionX * Math.max(0, distanceX) * returnAcceleration;
this.spaceshipVelocity.x = Math.min(maxReturnSpeed, Math.max(-maxReturnSpeed, this.spaceshipVelocity.x));
newX += this.spaceshipVelocity.x;
} else {
this.spaceshipVelocity.x *= decelerationFactor;
}
if (outOfBoundsY) {
let boundaryY = newY < minY ? minY : maxY;
let distanceY = Math.abs(newY - boundaryY) - overstepMargin;
let directionY = newY < minY ? 1 : -1;
this.spaceshipVelocity.y += directionY * Math.max(0, distanceY) * returnAcceleration;
this.spaceshipVelocity.y = Math.min(maxReturnSpeed, Math.max(-maxReturnSpeed, this.spaceshipVelocity.y));
newY += this.spaceshipVelocity.y;
} else {
this.spaceshipVelocity.y *= decelerationFactor;
}
// Allow slight overstep before clamping
this.spaceship.model.position[0] = Math.max(minX - overstepMargin, Math.min(maxX + overstepMargin, newX));
this.spaceship.model.position[1] = Math.max(minY - overstepMargin, Math.min(maxY + overstepMargin, newY));
// Apply boosting
// if (this.isBoosting && this.currentBoost > 0) {
// vec3.scale(moveDirection, moveDirection, this.spaceshipSpeed * 1.3); // Boosted speed
// this.currentBoost -= this.boostDecayRate;
// } else {
// vec3.scale(moveDirection, moveDirection, this.spaceshipSpeed);
// this.currentBoost = Math.min(this.maxBoost, this.currentBoost + this.boostRegenRate);
// }
// Translate the position of the spaceship
this.spaceship.translate(vec3.fromValues(
newX - this.spaceship.model.position[0],
newY - this.spaceship.model.position[1],
0
));
}
screenToWorld(mousePosition) {
const normalizedX = (mousePosition.x / this.canvas.width) * 2 - 1;
const normalizedY = -((mousePosition.y / this.canvas.height) * 2 - 1);
// Convert to world coordinates at a fixed Z depth
let worldX = (normalizedX * 10) - 5; // Scale factor for world coordinates
let worldY = (normalizedY * 10) + 5; // Scale factor for world coordinates
let worldZ = this.state.camera.position[2] + 5;
return vec3.fromValues(worldX, worldY, worldZ);
}
// Handle mouse move events to get the current mouse position
handleMouseMove(e) {
const rect = this.canvas.getBoundingClientRect();
this.mousePosition.x = e.clientX - rect.left;
this.mousePosition.y = e.clientY - rect.top;
const mouseWorldPosition = this.screenToWorld(this.mousePosition);
if (this.isMousePressed) {
this.updateFiringDirection();
}
}
handleMouseDown(e) {
this.isMousePressed = true;
this.startCubeSpawning(e);
// Start an interval to continuously update the direction while firing
if (!this.updateDirectionInterval) {
this.updateDirectionInterval = setInterval(() => {
if (this.isMousePressed) {
this.updateFiringDirection();
}
}, this.cubeSpawnRate);
}
}
handleMouseUp() {
this.isMousePressed = false;
clearInterval(this.cubeSpawnInterval);
this.cubeSpawnInterval = null;
clearInterval(this.updateDirectionInterval);
this.updateDirectionInterval = null;
}
handleKeyPress(e) {
if (e.key === '`') {
this.toggleControls();
} else if (this.controlCamera) {
// Camera control logic
if (['w', 'a', 's', 'd', 'z', 'x'].includes(e.key)) {
this.keyPressed[e.key] = true;
}
switch (e.key) {
case 'q':
// Rotate the camera left
this.rotateCamera(0.02);
break;
case 'e':
// Rotate the camera right
this.rotateCamera(-0.02);
break;
}
} else {
// Spaceship control logic
if (['w', 'a', 's', 'd', 'z', 'x', 'q', 'e'].includes(e.key)) {
this.keyPressed[e.key] = true;
}
if (e.key === 'Shift') {
this.isBoosting = true;
}
if (e.key === 'v') {
this.isFirstPersonCamera = !this.isFirstPersonCamera;
console.log(`First-person camera mode is now ${this.isFirstPersonCamera ? 'on' : 'off'}.`);
if (!this.isFirstPersonCamera) {
this.resetCamera(); // Reset camera when exiting first-person mode
}
}
}
}
handleKeyRelease(e) {
if (this.controlCamera) {
this.keyPressed[e.key] = false;
} else {
if (['w', 'a', 's', 'd', 'z', 'x', 'q', 'e'].includes(e.key)) {
this.keyPressed[e.key] = false;
}
if (e.key === 'Shift') {
this.isBoosting = false;
}
}
}
initializeControls() {
document.addEventListener("keypress", (e) => this.handleKeyPress(e));
document.addEventListener("keyup", (e) => this.handleKeyRelease(e));
}
initializeCamera() {
const fov = 45 * Math.PI / 180; // Field of view in radians
const aspect = this.canvas.clientWidth / this.canvas.clientHeight;
const near = 0.1;
const far = 100.0;
this.state.camera.projectionMatrix = mat4.create();
mat4.perspective(this.state.camera.projectionMatrix, fov, aspect, near, far);
}
getDirectionToMouse(spaceshipPosition, mouseWorldPosition) {
let direction = vec3.create();
vec3.subtract(direction, mouseWorldPosition, spaceshipPosition);
vec3.normalize(direction, direction);
return direction;
}
// Update the firing direction based on the current mouse position
updateFiringDirection() {
const mouseWorldPosition = this.screenToWorld(this.mousePosition);
// Iterate over all cubes and update their direction
this.state.objects.forEach(object => {
if (object.name.startsWith('Cube-') && object.model.position[2] < this.spaceship.model.position[2] + this.shootingCurve) {
let direction = vec3.create();
vec3.subtract(direction, vec3.fromValues(-mouseWorldPosition[0] - 5, mouseWorldPosition[1] - 5, mouseWorldPosition[2] + 25), this.spaceship.model.position);
vec3.normalize(direction, direction);
object.direction = direction;
}
});
}
flashWeaponLight(state) {
const weaponLight = state.pointLights.find(light => light.name === "weaponLight");
if (weaponLight) {
const originalIntensity = 0.001;
const originalStrength = 0.01;
// Make the weapon light brighter
weaponLight.intensity += 1;
weaponLight.strength += 3;
// Restore the original strength/intensity
setTimeout(() => {
weaponLight.intensity = originalIntensity;
weaponLight.strength = originalStrength;
}, 100); // miliseconds
}
}
spawnCubeOnInterval(e) {
const rect = this.canvas.getBoundingClientRect();
const mouseX = e.clientX - rect.left;
const mouseY = e.clientY - rect.top;
const mousePosition = { x: mouseX, y: mouseY };
const spaceship = getObject(this.state, "SpaceShip");
const mouseWorldPosition = this.screenToWorld(mousePosition);
// Adjust the mouse world position
const targetPoint = vec3.clone(spaceship.model.position);
targetPoint[2] += 20; // Move 25 units in front
targetPoint[0] = -mouseWorldPosition[0] - 5; // Align with mouse X
targetPoint[1] = mouseWorldPosition[1] - 5; // Align with mouse Y
// Direction from the spaceship to the target point
let direction = vec3.create();
vec3.subtract(direction, targetPoint, spaceship.model.position);
vec3.normalize(direction, direction);
let spawnPosition = vec3.clone(spaceship.model.position);
spawnPosition[2] += 0.9;
// this.spawnCube(this.state, spawnPosition);
// this.state.objects[this.state.objects.length - 1].direction = direction;
const cube = this.spawnCube(this.state, spawnPosition);
cube.direction = direction;
}
startCubeSpawning(e) {
// Initial spawn
this.spawnCubeOnInterval(e);
// Set up interval for continuous spawning
this.cubeSpawnInterval = setInterval(() => {
this.spawnCubeOnInterval(e);
}, this.cubeSpawnRate);
}
spawnCube(state, spaceshipPosition) {
// Define the initial properties of the cube
const upOffset = -0.5;
const leftOffset = 0.24;
let adjustedPosition = vec3.clone(spaceshipPosition);
adjustedPosition[0] -= leftOffset; // Move left
adjustedPosition[1] += upOffset; // Move up
const cubeConfig = {
name: `Cube-${Date.now()}`,
model: "Space_Invader.obj",
type: "mesh",
material: {
diffuse: [0, 1, 0],
alpha: 0.9,
},
position: adjustedPosition,
scale: vec3.fromValues(0.2, 0.2, 0.2),
};
// Create a light source associated with the cube
const lightConfig = {
type: 'pointLights',
position: vec3.clone(adjustedPosition),
colour: [0, 1, 0],
intensity: 100,
strength: 30,
};
const cube = addCube(cubeConfig, state);
this.addLight(lightConfig, state);
cube.lightSource = lightConfig;
this.flashWeaponLight(state);
return cube;
}
spawnEnemyCube(state, spawnPosition, direction) {
const cubeConfig = {
name: `EnemyCube-${Date.now()}`,
type: "cube",
material: {
diffuse: [1, 0, 0],
alpha: 0.99,
},
position: spawnPosition,
scale: vec3.fromValues(0.5, 0.5, 0.5),
};
// Create a light source associated with the enemy's projectile
const lightConfig = {
type: 'pointLights',
position: vec3.clone(spawnPosition),
colour: [1, 0, 0],
intensity: 100,
strength: 30,
};
const cube = addCube(cubeConfig, state);
//this.addLight(lightConfig, state);
cube.direction = direction;
//cube.lightSource = lightConfig;
}
updateCubes(deltaTime, state) {
const cubeSpeed = this.cubeSpeed;
const spaceshipOffset = { x: 0, y: 0, z: 0 };
state.objects.forEach(object => {
if (object.type === 'enemyCube') {
let adjustedSpaceshipPosition = vec3.create();
vec3.add(adjustedSpaceshipPosition, this.spaceship.model.position, vec3.fromValues(spaceshipOffset.x, spaceshipOffset.y, spaceshipOffset.z));
// Calculate new direction towards the player's spaceship
let direction = vec3.create();
vec3.subtract(direction, adjustedSpaceshipPosition, object.model.position);
vec3.normalize(direction, direction);
object.direction = direction;
// Update position based on the new direction
vec3.scaleAndAdd(
object.model.position,
object.model.position,
object.direction,
cubeSpeed * deltaTime
);
} else if (object.type === 'cube') {
vec3.scaleAndAdd(
object.model.position,
object.model.position,
object.direction,
cubeSpeed * deltaTime
);
}
});
}
addLight(lightConfig, state) {
// state.pointLights.push(lightConfig);
// console.log("Created light:", lightConfig);
// console.log("State Point Lights: ", state.pointLights.length);
// return lightConfig;
}
spawnAsteroidField() {
const numAsteroids = 1;
for (let i = 0; i < numAsteroids; i++) {
const zPos = 200 + (i * (400 / numAsteroids));
this.spawnAsteroid(0, 0, zPos);
}
}
spawnAsteroid(xPos, yPos, zPos) {
// Define the range for spawning asteroids (adjust as needed)
// const xMin = -7; // Adjusted minimum X position
// const xMax = 12; // Adjusted maximum X position
// const yMin = -6; // Adjusted minimum Y position
// const yMax = 7; // Adjusted maximum Y position
// Randomly choose an asteroid model
const asteroidModels = ["asteroid3.obj", "asteroid4.obj", "asteroid5.obj",
"asteroid6.obj", "asteroid7.obj", "asteroid8.obj", "asteroid9.obj", "asteroid10.obj"];
const asteroidModel = asteroidModels[Math.floor(Math.random() * asteroidModels.length)];
// Random size variation
const sizeFactor = 0.3 + Math.random() * 1.7; // Random scale
// Define the asteroid configuration
const asteroidConfig = {
name: `Asteroid-${Date.now()}`,
model: asteroidModel,
type: "mesh",
material: {
diffuse: [0.1, 0.1, 0.1],
ambient: [0.05, 0.05, 0.05],
specular: [0.1, 0.1, 0.1],
n: 10,
alpha: 1,
shaderType: 3
},
position: vec3.fromValues(
xPos,
yPos,
zPos
),
scale: vec3.fromValues(sizeFactor, sizeFactor, sizeFactor),
diffuseTexture: "DefaultMaterial_albedo.jpg",
normalTexture: "DefaultMaterial_normal.png"
};
// Add the asteroid to the game state
const newAsteroid = spawnObject(asteroidConfig, this.state);
this.spawnedObjects.push(newAsteroid);
this.asteroidPool.push(asteroidConfig);
if (newAsteroid && newAsteroid.model) { // Check if the model property exists ?? Doesn't work
// Add to the asteroid pool
console.log(this.asteroidPool[0]);
}
}
repositionAsteroids() {
const xMin = -80;
const xMax = 80;
const yMin = -10;
const yMax = 10;
const zMin = 400;
const zMax = 800;
this.state.objects.forEach((object) => {
if (object.name.startsWith('Asteroid-')) {
// Check if the asteroid is behind the spaceship
if (object.model.position[2] < this.spaceship.model.position[2] - 17.5) {
const newZ = this.spaceship.model.position[2] + Math.random() * (zMax - zMin) + zMin;
// Randomly determine new X and Y positions within a specified range
const newX = Math.random() * (xMax - xMin) + xMin - 20;
const newY = Math.random() * (yMax - yMin) + yMin + 5;
// Reposition the asteroid
vec3.set(object.model.position, newX, newY, newZ);
//console.log("Asteroid repositioned: ", object.model.position);
}
}
});
}
updateEnemyMovement(deltaTime) {
// Define the range of movement along the x-axis and y-axis
const minX = -40;
const maxX = 40;
const minY = -7;
const maxY = 15;
const minZ = 12;
const maxZ = 35;
// Movement speed along x-axis and y-axis
const xSpeed = 10 * this.enemySpeed;
const ySpeed = 3 * this.enemySpeed;
const zSpeed = 8 * this.enemySpeed;
// Check if the enemy is at or beyond the x-axis boundaries
if (this.enemy1.model.position[0] >= maxX || this.enemy1.model.position[0] <= minX) {
this.enemy1.xMovementFactor *= -1;
}
// Check if the enemy is at or beyond the y-axis boundaries
if (this.enemy1.model.position[1] >= maxY || this.enemy1.model.position[1] <= minY) {
this.enemy1.yMovementFactor *= -1;
}
// Check if the enemy is at or beyond the z-axis boundaries
if (this.enemy1.model.position[2] >= maxZ || this.enemy1.model.position[2] <= minZ) {
this.enemy1.zMovementFactor *= -1;
}
this.enemy1Velocity = {
x: this.enemy1.xMovementFactor * xSpeed * deltaTime,
y: this.enemy1.yMovementFactor * ySpeed * deltaTime,
z: this.enemy1.zMovementFactor * zSpeed * deltaTime
};
// Update position
this.enemy1.model.position[0] += this.enemy1.xMovementFactor * xSpeed * deltaTime;
this.enemy1.model.position[1] += this.enemy1.yMovementFactor * ySpeed * deltaTime;
this.enemy1.model.position[2] += this.enemy1.zMovementFactor * zSpeed * deltaTime;
}
enemyAttack() {
// Spawn point
let spawnPosition = vec3.clone(this.enemy1.model.position);
spawnPosition[1] -= 1;
spawnPosition[2] += 45;
// Target point (adjusted for enemy's predicted position)
let targetPosition = vec3.clone(this.spaceship.model.position);
targetPosition[0] -= 0.25;
targetPosition[2] += 18; // 22
// Calculate the direction from the enemy's future position to the spaceship
let direction = vec3.create();
vec3.subtract(direction, targetPosition, spawnPosition);
vec3.normalize(direction, direction);
// Check if the direction vector is valid
if (!vec3.length(direction)) {
console.error("Invalid direction vector. Check calculations.");
return;
}
// Spawn the projectile (cube) from the enemy
this.spawnEnemyCube(this.state, spawnPosition, direction);
}
enemyKilled() {
// Update wave number text
this.waveNumber.innerText = "Complete"; // remove later
this.enemy1Killed = true; // Will need a parameter and switch later
// Stop enemy from shooting
this.stopEnemyAttacks();
// Flash enemy light
this.flashEnemyLight();
}
stopEnemyAttacks() {
clearInterval(this.enemyAttackInterval);
this.enemyAttackInterval = null;
}
flashEnemyLight() {
let enemyLight = this.enemy1light;
// Flash the light on and off
if (enemyLight) {
let flashInterval = setInterval(() => {
enemyLight.strength = enemyLight.strength === 0 ? 40 : 0;
}, 333);
// Stop flashing after a certain duration
setTimeout(() => {
clearInterval(flashInterval);
enemyLight.strength = 10; // Reset light
}, 3300);
}
}
// Increment the difficulty as the player progresses
initializeWave() {
this.wave += 1;
console.log("Wave: ", this.wave);
this.waveNumber.innerText = `Wave ${this.wave}`;
if (this.wave === 1) { // Starting wave
this.enemy1Health = this.lastWaveHealth;
this.enemyNum = 1;
this.enemyHealthTotal = this.enemy1Health + this.enemy2Health + this.enemy3Health + this.enemy4Health + this.enemy5Health;
this.waveComplete = false;
return;
} else {
this.enemy1Health = this.lastWaveHealth * 1.2; // Increment enemy health every wave
this.lastWaveHealth = this.enemy1Health;
}
// Player increments
this.cameraSpeed *= 1.2; // 10% increase in spaceship speed
this.cubeSpeed *= 1.2; // Accomodate cube speed
this.playerHealth = 100; // Reset player health
// Reposition enemies
this.enemy1.model.position = [0, 9, 300 + this.spaceship.model.position[2]];
this.enemy1.model.rotation = [1, 0, 1.518794157107095e-8, 0, -1.518794157107095e-8, 0.0000023556663109047804, 1, 0, -3.6014726221894264e-14, -1, 0.0000023556663109047804, 0, 0, 0, 0, 1];
// Update total and max enemy health
this.enemyHealthTotal = this.enemy1Health;
this.maxEnemyHealthTotal = this.enemy1Health;
this.enemyFireRate *= 0.9;
this.enemySpeed *= 1.15;
this.enemy1light.strength = 15;
this.enemyAttackIntervalSet = false;
this.enemy1Killed = false;
// Add total enemy health
this.maxEnemyHealthTotal = this.enemy1Health + this.enemy2Health + this.enemy3Health + this.enemy4Health + this.enemy5Health;
console.log(this.maxEnemyHealthTotal);
this.waveComplete = false;
this.updateHealthBar();
}
updateHealthBar() {
const playerHealthPercent = this.playerHealth;
const playerHealthBar = document.getElementById('healthBar');
const enemyHealthPercent = (this.enemyHealthTotal / this.maxEnemyHealthTotal) * 100;
const enemyHealthBar = document.getElementById('enemyHealthBar');
playerHealthBar.style.width = `${playerHealthPercent}%`;
enemyHealthBar.style.width = `${enemyHealthPercent}%`;
// Change color based on health of player
if (this.playerHealth < 20) {
healthBar.style.backgroundColor = 'red';
} else if (this.playerHealth < 40) {
healthBar.style.backgroundColor = 'orange';
} else {
healthBar.style.backgroundColor = 'green';
}
}
playerKilled() {
this.gameOver = true;
this.stopEnemyAttacks();
showGameOverMessage();
}
cleanupObjectsBehindCamera() { // Does this even work? Maybe change to outside of plane
// Check that the camera and its position are defined
if (this.state.camera && Array.isArray(this.state.camera.position)) {
this.spawnedObjects = this.spawnedObjects.filter(object => {
// Only retain objects that have a defined position and are in front of the camera
return Array.isArray(object.position) && object.position[2] > this.state.camera.position[2];
});
} else {
console.error('Camera position is not defined.');
}
}
// example - create a collider on our object with various fields we might need (you will likely need to add/remove/edit how this works)
createSphereCollider(object, radius, onCollide = null) {
object.collider = {
type: "SPHERE",
radius: radius,
onCollide: onCollide ? onCollide : (otherObject) => {
console.log(`Collided with ${otherObject.name}`);
}
};
this.collidableObjects.push(object);
}
checkCollision(object) {