-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
1675 lines (1637 loc) · 58.4 KB
/
main.js
File metadata and controls
1675 lines (1637 loc) · 58.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
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 () {
"use strict";
var CANVAS = document.getElementById('gameCanvas');
var CTX = CANVAS.getContext('2d');
var screenBoundX;
var screenBoundY;
var screenX;
var screenY;
var statusText = "";
var lastStatus = 0;
var statusTimeout = 4000;
var statusColor = "#00FFFF";
var score = 0;
const HUD_HEIGHT = 80;
const MINIMAP_SCALE = 30;
const MAX_DISTANCE = 1500;
const MAX_TIME_DELTA = 1000; // less than 1 fps is not worth interpolating
const MINER_RADIUS = 14;
const SHOOTER_RADIUS = 17;
const BOSS_RADIUS = 100;
const MAX_SCREEN_BOUND_X = 100;
const MAX_SCREEN_BOUND_Y = 50;
const UP_KEY = 38;
const LEFT_KEY = 37;
const RIGHT_KEY = 39;
function startGame() {
// Setup
const TIMESTEP = 10; // How finely the state is interpolated between frames. Higher = choppier.
var lastFrameTimestamp = 0;
var timeDelta = 0;
var mmLeft = CANVAS.width / 2 - HUD_HEIGHT / 2;
var index;
var entRef;
var entLookup = {
entities: [],
playerRef: null,
bossRef: null,
miners: [],
asteroids: [],
crystals: [],
crystalInd: 0,
shooters: [],
bossPieces: [],
playerBullets: [],
playerBulletInd: 0,
enemyBullets: [],
enemyBulletInd: 0,
bombs: [],
bombInd: 0
};
var lifeSymbolLines = new Array(6);
function keyDownHandler(e) {
switch(e.keyCode) {
case UP_KEY: // up
entLookup.playerRef.throttle = true;
break;
case LEFT_KEY: // left
entLookup.playerRef.turningLeft = true;
entLookup.playerRef.lastTurnKey = e.keyCode;
break;
case RIGHT_KEY: // right
entLookup.playerRef.turningRight = true;
entLookup.playerRef.lastTurnKey = e.keyCode;
break;
case 32: // space
entLookup.playerRef.shooting = true;
break;
case 90: // Z
entLookup.playerRef.pendingBomb = true;
break;
default:
break;
}
};
function keyUpHandler(e) {
switch(e.keyCode) {
case UP_KEY: // up
entLookup.playerRef.throttle = false;
break;
case LEFT_KEY: // left
entLookup.playerRef.turningLeft = false;
break;
case RIGHT_KEY: // right
entLookup.playerRef.turningRight = false;
break;
case 32: // space
entLookup.playerRef.shooting = false;
break;
default:
break;
}
};
function mainLoop(timeStamp) {
var entIndex;
var curEnt;
var mmX; // minimap x, y
var mmY;
var plrRef = entLookup.playerRef;
timeDelta += timeStamp - lastFrameTimestamp;
timeDelta = timeDelta > MAX_TIME_DELTA ? MAX_TIME_DELTA : timeDelta;
lastFrameTimestamp = timeStamp;
// Interpolate state updates
while (timeDelta >= TIMESTEP) {
for (entIndex = 0; entIndex < entLookup.entities.length; entIndex++) {
if (entLookup.entities[entIndex].active) {
entLookup.entities[entIndex].updateState(TIMESTEP, entLookup);
}
}
timeDelta -= TIMESTEP;
}
// Drawing
CTX.clearRect(0, 0, CANVAS.width, CANVAS.height); // clear the canvas
// Draw entities
for (entIndex = 0; entIndex < entLookup.entities.length; entIndex++) {
curEnt = entLookup.entities[entIndex];
if (curEnt.active) {
entLookup.entities[entIndex].draw();
}
}
// Draw HUD
CTX.fillStyle = "#404040";
CTX.fillRect(0, 0, CANVAS.width, HUD_HEIGHT);
CTX.fillStyle = "#000040";
CTX.fillRect(mmLeft, 0, 80, 80);
// visible distance rectangle
CTX.beginPath();
CTX.strokeStyle = "#FFFF00";
CTX.rect(mmLeft + HUD_HEIGHT / 2 - (plrRef.x - screenX + CANVAS.width / 2) / MINIMAP_SCALE,
HUD_HEIGHT / 2 - (plrRef.y - screenY + (CANVAS.height - HUD_HEIGHT) / 2) / MINIMAP_SCALE,
CANVAS.width / MINIMAP_SCALE,
(CANVAS.height - HUD_HEIGHT) / MINIMAP_SCALE);
CTX.stroke();
// status outline
CTX.beginPath();
CTX.strokeStyle = statusColor;
CTX.rect(0, 0, CANVAS.width, HUD_HEIGHT);
CTX.stroke();
// Draw minimap blips
for (entIndex = 0; entIndex < entLookup.entities.length; entIndex++) {
curEnt = entLookup.entities[entIndex];
if (!curEnt.active || !curEnt.blipColor) {
continue;
}
mmX = mmLeft + HUD_HEIGHT / 2 - 1 + (curEnt.x - plrRef.x) / MINIMAP_SCALE;
if (mmX - 1 < mmLeft || mmX + 1 >= mmLeft + HUD_HEIGHT) {
continue;
}
mmY = HUD_HEIGHT / 2 - 1 + (curEnt.y - plrRef.y) / MINIMAP_SCALE;
if (mmY < 0 || mmY + 1 >= HUD_HEIGHT) {
continue;
}
CTX.fillStyle = curEnt.blipColor;
CTX.fillRect(mmX, mmY, 2, 2);
}
// Draw bomb count
for (entIndex = 0; entIndex < entLookup.playerRef.bombs; entIndex++) {
drawCircle(20 + entIndex * 10 - (CANVAS.width / 2), -(CANVAS.height / 2 - 20), Crystal.prototype.collRadius, "#5555FF");
}
// Draw lives
for (entIndex = 0; entIndex < entLookup.playerRef.lives; entIndex++) {
CTX.beginPath();
CTX.moveTo(30 + (entIndex * 35) + lifeSymbolLines[0], 25 + lifeSymbolLines[1]);
for (index = 2; index < lifeSymbolLines.length - 1; index += 2) {
CTX.lineTo(30 + (entIndex * 35) + lifeSymbolLines[index], 25 + lifeSymbolLines[index + 1]);
}
CTX.lineTo(20 + (entIndex * 10) + lifeSymbolLines[0], 25 + lifeSymbolLines[1]);
CTX.fillStyle = "#00FF00";
CTX.fill();
}
// Draw status text
if (statusText) {
CTX.fillStyle = "#FFFF00";
CTX.textAlign = "center";
CTX.font = "30px Impact";
CTX.fillText(statusText, CANVAS.width / 2, CANVAS.height / 2);
if (performance.now() - lastStatus > statusTimeout) {
statusText = "";
}
}
// Draw score
CTX.fillStyle = "#FFFFFF";
CTX.textAlign = "left";
CTX.font = "10px Courier New";
CTX.fillText(score.toString().padStart(7, "0"), 17, 75);
requestAnimationFrame(mainLoop);
};
Player.prototype.updateShape(lifeSymbolLines, 0);
document.onkeydown = keyDownHandler;
document.onkeyup = keyUpHandler;
entLookup.bossRef = new Boss();
entLookup.entities.push(entLookup.bossRef);
for (index = 0; index < 9; index++) {
entLookup.entities.push(new BossPiece(index));
entLookup.bossPieces[entLookup.bossPieces.length] = entLookup.entities[entLookup.entities.length - 1];
}
entLookup.bombs = new Array();
for (index = 0; index < 15; index++) {
entLookup.entities.push(new Bomb());
entLookup.bombs[index] = entLookup.entities[entLookup.entities.length - 1];
}
entLookup.playerRef = new Player();
entLookup.entities.push(entLookup.playerRef);
screenX = entLookup.playerRef.x;
screenY = entLookup.playerRef.y;
CTX.lineWidth = 1;
entLookup.asteroids = new Array(50);
for (index = 0; index < 50; index++) {
entLookup.entities.push(new Asteroid(
(Math.random() >= .5 ? 1 : -1) * Math.random() * (MAX_DISTANCE - 100) + 200,
(Math.random() >= .5 ? 1 : -1) * Math.random() * (MAX_DISTANCE - 100) + 200));
entRef = entLookup.entities[entLookup.entities.length - 1];
// move out of the way if on top of the player
if (distance(entRef.x, entRef.y, entLookup.playerRef.x, entLookup.playerRef.y) < 100) {
entRef.x += entRef.collRadius;
}
// register each asteroid
entLookup.asteroids[index] = entRef;
}
entLookup.miners = new Array();
for (index = 0; index < 15; index++) {
entLookup.entities.push(new Miner());
entLookup.miners.push(entLookup.entities[entLookup.entities.length - 1]);
}
entLookup.enemyBullets = new Array();
for (index = 0; index < 20; index++) {
entLookup.entities.push(new EnemyBullet());
entLookup.enemyBullets[index] = entLookup.entities[entLookup.entities.length - 1];
}
entLookup.shooters = new Array();
for (index = 0; index < 15; index++) {
entLookup.entities.push(new Shooter());
entLookup.shooters.push(entLookup.entities[entLookup.entities.length - 1]);
}
entLookup.crystals = new Array();
for (index = 0; index < 30; index++) {
entLookup.entities.push(new Crystal());
entLookup.crystals[index] = entLookup.entities[entLookup.entities.length - 1];
}
entLookup.playerBullets = new Array();
for (index = 0; index < 20; index++) {
entLookup.entities.push(new PlayerBullet());
entLookup.playerBullets[index] = entLookup.entities[entLookup.entities.length - 1];
}
score = 0;
entLookup.bossRef.activate(entLookup);
requestAnimationFrame(mainLoop); // Begin loop
};
function distance(x0, y0, x1, y1) {
return Math.sqrt(Math.pow(x1 - x0, 2) + Math.pow(y1 - y0, 2));
};
function lineCheck(x1, y1, x2, y2, x3, y3, x4, y4) {
var a_dx = x2 - x1;
var a_dy = y2 - y1;
var b_dx = x4 - x3;
var b_dy = y4 - y3;
var s = (-a_dy * (x1 - x3) + a_dx * (y1 - y3)) / (-b_dx * a_dy + a_dx * b_dy);
var t = (+b_dx * (y1 - y3) - b_dy * (x1 - x3)) / (-b_dx * a_dy + a_dx * b_dy);
return (s >= 0 && s <= 1 && t >= 0 && t <= 1);
};
function circleToLineCheck(x1, y1, x2, y2, cX, cY, r) {
var shiftX1 = x1 - cX;
var shiftY1 = y1 - cY;
var shiftX2 = x2 - cX;
var shiftY2 = y2 - cY;
var m = (shiftY2 - shiftY1) / (shiftX2 - shiftX1); // slope
var b = shiftY1 - m * shiftX1; // b = y - mx
var underRadical = Math.pow(r, 2) * Math.pow(m, 2) + Math.pow(r, 2) - Math.pow(b, 2);
return !(underRadical < 0);
};
function collidingWith(subject, other) {
var sLineInd;
var oLineInd;
// There are algorithms that can do faster polygonal collsion detection.
// Maybe replace this with one of those later.
if (distance(subject.x, subject.y, other.x, other.y) < subject.collRadius + other.collRadius) {
for (sLineInd = 0; sLineInd < subject.collLines.length; sLineInd += 2) {
for (oLineInd = 0; oLineInd < other.collLines.length; oLineInd += 2) {
if (lineCheck(
subject.collLines[sLineInd] + subject.x,
subject.collLines[sLineInd + 1] + subject.y,
subject.collLines[(sLineInd + 2) % subject.collLines.length] + subject.x,
subject.collLines[(sLineInd + 3) % subject.collLines.length] + subject.y,
other.collLines[oLineInd] + other.x,
other.collLines[oLineInd + 1] + other.y,
other.collLines[(oLineInd + 2) % other.collLines.length] + other.x,
other.collLines[(oLineInd + 3) % other.collLines.length] + other.y
)) {
return other;
}
}
}
}
return null;
};
function circleCollidingWith(subject, other) {
var lineInd;
if (distance(subject.x, subject.y, other.x, other.y) < subject.collRadius + other.collRadius) {
for (lineInd = 0; lineInd < other.collLines.length; lineInd += 2) {
if (circleToLineCheck(
other.collLines[lineInd] + other.x,
other.collLines[lineInd + 1] + other.y,
other.collLines[(lineInd + 2) % other.collLines.length] + other.x,
other.collLines[(lineInd + 3) % other.collLines.length] + other.y,
subject.x,
subject.y,
subject.collRadius
)) {
return other;
}
}
}
return null;
};
function checkCollision(subject, elu) {
var entIndex;
var other;
var ret;
if (subject.collLines.length > 0) {
for (entIndex = 2 + elu.bossPieces.length + elu.bombs.length;
entIndex < elu.entities.length; entIndex++) {
other = elu.entities[entIndex];
if (!other.active || other == subject) {
continue;
}
ret = collidingWith(subject, other);
if (ret) {
return ret;
}
}
}
return null;
};
function getRandomIndex(arr, length) {
return arr[Math.round(Math.random() * (length - 1))];
};
function wrapAngle(angle) {
if (angle > 2 * Math.PI) {
return angle - 2 * Math.PI;
}
if (angle < 0) {
return angle + 2 * Math.PI;
}
return angle;
};
function getAngleTo(self, other) {
var angleTo = Math.atan2(-(other.y - self.y), other.x - self.x) - Math.PI / 2;
angleTo = wrapAngle(angleTo);
return angleTo;
};
function kill(ent) {
var displaceAngle = Math.random() * 2 * Math.PI;
ent.x += Math.cos(displaceAngle) * MAX_DISTANCE;
ent.y += Math.sin(-displaceAngle) * MAX_DISTANCE;
};
function fieldWrap(ent, playerRef) {
if (ent.x - playerRef.x > MAX_DISTANCE) {
ent.x = playerRef.x - MAX_DISTANCE + 10;
} else if (ent.x - playerRef.x < -MAX_DISTANCE) {
ent.x = playerRef.x + MAX_DISTANCE - 10;
}
if (ent.y - playerRef.y > MAX_DISTANCE) {
ent.y = playerRef.y - MAX_DISTANCE + 10;
} else if (ent.y - playerRef.y < -MAX_DISTANCE) {
ent.y = playerRef.y + MAX_DISTANCE - 10;
}
};
function translateToOrigin(ent) {
if (ent.x > 0) {
ent.x = -(MAX_DISTANCE) + (ent.x % (MAX_DISTANCE * 2));
} else {
ent.x = MAX_DISTANCE + (ent.x % (MAX_DISTANCE * 2));
}
if (ent.y > 0) {
ent.y = -(MAX_DISTANCE) + (ent.y % (MAX_DISTANCE * 2));
} else {
ent.y = (MAX_DISTANCE) + (ent.y % (MAX_DISTANCE * 2));
}
};
function placeAwayFrom(x, y, ent) {
var theta = Math.random() * (2 * Math.PI);
ent.x = x + Math.cos(theta) * MAX_DISTANCE;
ent.y = y + Math.sin(-theta) * MAX_DISTANCE;
};
function updateTriangle(vectors, angle, radius) {
vectors[0] = Math.cos(Math.PI / 2 + angle) * radius;
vectors[1] = Math.sin(-Math.PI / 2 - angle) * radius;
vectors[2] = Math.cos(Math.PI * (4 / 3) + angle) * radius;
vectors[3] = Math.sin(-Math.PI * (4 / 3) - angle) * radius;
vectors[4] = Math.cos(Math.PI * (5 / 3) + angle) * radius;
vectors[5] = Math.sin(-Math.PI * (5 / 3) - angle) * radius;
};
function drawCircle(x, y, r, fill) {
CTX.beginPath();
CTX.arc(CANVAS.width / 2 + x,
CANVAS.height / 2 + HUD_HEIGHT / 2 + y,
r, 0, 2 * Math.PI);
CTX.fillStyle = fill;
CTX.fill();
};
function drawPolygon(x, y, collLines, fill) {
var index;
CTX.beginPath();
CTX.moveTo(CANVAS.width / 2 + (x - screenX) + collLines[0],
CANVAS.height / 2 + (y + HUD_HEIGHT / 2 - screenY) + collLines[1]);
for (index = 2; index < collLines.length - 1; index += 2) {
CTX.lineTo(CANVAS.width / 2 + (x - screenX) + collLines[index],
CANVAS.height / 2 + HUD_HEIGHT / 2 + (y - screenY) + collLines[index + 1]);
}
CTX.lineTo(CANVAS.width / 2 + (x - screenX) + collLines[0],
CANVAS.height / 2 + HUD_HEIGHT / 2 + (y - screenY) + collLines[1]);
CTX.fillStyle = fill;
CTX.fill();
};
function getNearestActive(self, entList) {
var entInd;
var ent = entList[0];
for (entInd = 0; entInd < entList.length; entInd++) {
if (entList[entInd].active &&
distance(self.x, self.y, entList[entInd].x, entList[entInd].y) <
distance(self.x, self.y, ent.x, ent.y)) {
ent = entList[entInd];
}
}
return ent;
};
function trackScreenToEntity(self, maxSpeed) {
screenBoundX = Math.abs(self.xVel / maxSpeed) * MAX_SCREEN_BOUND_X;
screenBoundY = Math.abs(self.yVel / maxSpeed) * MAX_SCREEN_BOUND_Y;
if (self.x - screenX > screenBoundX) {
screenX = self.x - screenBoundX;
} else if (self.x - screenX < -screenBoundX) {
screenX = self.x + screenBoundX;
}
if (self.y - screenY > screenBoundY) {
screenY = self.y - screenBoundY;
} else if (self.y - screenY < -screenBoundY) {
screenY = self.y + screenBoundY;
}
};
function assignMinerToCrystal(elu, crysRef) {
var miner;
var minerInd;
var startInd = 0;
miner = elu.miners[0];
while (elu.miners[startInd++].hasCrystal && startInd < elu.miners.length) {
miner = elu.miners[startInd];
}
for (minerInd = 1; minerInd < elu.miners.length; minerInd++) {
if (elu.miners[minerInd].active &&
!(elu.miners[minerInd].target instanceof Crystal) &&
!elu.miners[minerInd].hasCrystal &&
distance(crysRef.x, crysRef.y, elu.miners[minerInd].x, elu.miners[minerInd].y) <
distance(crysRef.x, crysRef.y, miner.x, miner.y)) {
miner = elu.miners[minerInd];
}
}
miner.target = crysRef;
};
function updateStatus(text) {
statusText = text;
lastStatus = performance.now();
};
// Objects
function Entity() {};
Entity.prototype = {
x: 0,
y: 0,
angle: 0,
xVel: 0,
yVel: 0,
xVelDelta: 0,
yVelDelta: 0,
throttle: false,
angleDelta: 0,
collLines: [],
collisionOn: true,
active: false,
moveSelf: function (delta, elu) {
var oldAngle;
var velSign;
var other;
var angleToOther;
var iterations = 0;
oldAngle = this.angle;
this.angle += this.angleDelta * delta;
this.updateCollLines();
// angular collision
if (this.collisionOn && checkCollision(this, elu)) {
this.angle = oldAngle;
this.updateCollLines();
}
if (this.throttle) {
this.xVelDelta = this.accel * Math.cos(this.angle + Math.PI / 2);
this.yVelDelta = this.accel * Math.sin(-(this.angle + Math.PI / 2));
} else {
// slow to a stop
if (Math.abs(this.xVel) > this.accel) {
this.velSign = this.xVel >= 0 ? -1 : 1;
this.xVelDelta = this.velSign * (this.accel / 3);
} else {
this.xVelDelta = 0;
this.xVel = 0;
}
if (Math.abs(this.yVel) > this.accel) {
this.velSign = this.yVel >= 0 ? -1 : 1;
this.yVelDelta = this.velSign * (this.accel / 3);
} else {
this.yVelDelta = 0;
this.yVel = 0;
}
}
// Velocity is in units of pixels per millisecond.
this.xVel += this.xVelDelta * delta;
this.yVel += this.yVelDelta * delta;
if (this.xVel > this.maxVel) {
this.xVel = this.maxVel;
}
if (this.yVel > this.maxVel) {
this.yVel = this.maxVel;
}
if (this.xVel < -this.maxVel) {
this.xVel = -this.maxVel;
}
if (this.yVel < -this.maxVel) {
this.yVel = -this.maxVel;
}
this.x += this.xVel * delta;
this.y += this.yVel * delta;
other = (!this.collisionOn ? null : checkCollision(this, elu));
if (other) {
this.xVel = -this.xVel;
this.yVel = -this.yVel;
if ((this.xVel > 0 && this.xVel < other.xVel) ||
(this.xVel < 0 && -1 * this.xVel < other.xVel) ||
(this.xVel < 0 && this.xVel > other.xVel) ||
(this.xVel > 0 && -1 * this.xVel > other.xVel) ||
this.xVel == 0) {
this.xVel = other.xVel * 2;
}
if ((this.yVel > 0 && this.yVel < other.yVel) ||
(this.yVel < 0 && -1 * this.yVel < other.yVel) ||
(this.yVel < 0 && this.yVel > other.yVel) ||
(this.yVel > 0 && -1 * this.yVel > other.yVel) ||
this.yVel == 0) {
this.yVel = other.yVel * 2;
}
// bounce away cleanly
do {
angleToOther = getAngleTo(other, this);
this.x += Math.abs(this.xVel) * Math.cos(angleToOther + Math.PI / 2) * delta;
this.y -= Math.abs(this.yVel) * Math.sin(angleToOther + Math.PI / 2) * delta;
} while (collidingWith(this, other) && ++iterations < 100);
return other;
}
return null;
},
updateCollLines: function () {},
turnToTarget: function (delta) {
var angleToTarget;
this.angle = wrapAngle(this.angle);
angleToTarget = getAngleTo(this, this.target);
// find the shortest arc and turn towards the target
if (Math.abs(this.angle - angleToTarget) > Math.PI) {
if (this.angle > angleToTarget) {
this.angle += this.turnSpeed * delta;
} else {
this.angle -= this.turnSpeed * delta;
}
} else {
if (this.angle > angleToTarget) {
this.angle -= this.turnSpeed * delta;
} else {
this.angle += this.turnSpeed * delta;
}
}
}
};
function Player() {};
Player.prototype = Object.create(Entity.prototype);
Player.prototype.blipColor = "#FFFFFF";
Player.prototype.accel = .0004;
Player.prototype.angleDelta = 0;
Player.prototype.maxVel = .3;
Player.prototype.turnSpeed = .005;
Player.prototype.throttle = false;
Player.prototype.turningLeft = false;
Player.prototype.turningRight = false;
Player.prototype.lastTurnKey = 0;
Player.prototype.pendingBomb = false;
Player.prototype.collRadius = 15;
Player.prototype.collLines = [];
Player.prototype.active = true;
Player.prototype.shooting = false;
Player.prototype.lastShotTime = 0;
Player.prototype.coolDown = 250;
Player.prototype.lastDeath = 0;
Player.prototype.respawnDelay = 4000;
Player.prototype.lives = 2;
Player.prototype.bombs = 0;
Player.prototype.MAX_BOMBS = 15;
Player.prototype.MAX_LIVES = 10;
Player.prototype.warpDelay = 3000;
Player.prototype.level = 0;
Player.prototype.updateCollLines = function () {
this.updateShape(this.collLines, this.angle);
};
Player.prototype.updateState = function (delta, elu) {
if (!elu.bossRef.active) {
this.xVel = this.maxVel * 3;
this.yVel = this.maxVel * 3;
this.warp(delta, elu);
} else {
if (this.turningLeft && this.turningRight) {
if (this.lastTurnKey == LEFT_KEY) {
this.angleDelta = this.turnSpeed;
} else if (this.lastTurnKey == RIGHT_KEY) {
this.angleDelta = -this.turnSpeed;
}
} else if (this.turningLeft) {
this.angleDelta = this.turnSpeed;
} else if (this.turningRight) {
this.angleDelta = -this.turnSpeed;
} else {
this.angleDelta = 0;
}
this.moveSelf(delta, elu);
trackScreenToEntity(this, this.maxVel);
}
// shoot
if (this.shooting && performance.now() - this.lastShotTime >= this.coolDown) {
elu.playerBullets[elu.playerBulletInd].activate(this.x, this.y, this.angle + Math.PI / 2);
elu.playerBulletInd = (elu.playerBulletInd + 1) % elu.playerBullets.length;
this.lastShotTime = performance.now();
}
if (this.pendingBomb && !elu.bossRef.caught) {
elu.playerRef.shootBomb(elu);
}
};
Player.prototype.activate = function (elu) {
var entInd;
this.lives--;
this.x = 0;
this.y = 0;
this.angle = 0;
this.xVel = 0;
this.yVel = 0;
for (entInd = 0; entInd < elu.asteroids.length; entInd++) {
translateToOrigin(elu.asteroids[entInd]);
}
for (entInd = 0; entInd < elu.shooters.length; entInd++) {
placeAwayFrom(this.x, this.y, elu.shooters[entInd]);
}
for (entInd = 0; entInd < elu.miners.length; entInd++) {
placeAwayFrom(this.x, this.y, elu.miners[entInd]);
}
placeAwayFrom(this.x, this.y, elu.bossRef);
for (entInd = 0; entInd < elu.shooters.length; entInd++) {
elu.shooters[entInd].kill(elu);
}
elu.bossRef.lastShooterSpawn = performance.now();
this.active = true;
};
Player.prototype.kill = function (elu) {
var bombInd;
this.pendingBomb = false;
this.active = false;
this.lastDeath = performance.now();
for (bombInd = 0; bombInd < elu.bombs.length; bombInd++) {
elu.bombs[bombInd].active = false;
}
};
Player.prototype.addBomb = function () {
if (this.bombs < this.MAX_BOMBS) {
this.bombs++;
}
};
Player.prototype.shootBomb = function (elu) {
if (this.bombs > 0) {
elu.bombs[elu.bombInd].activate(elu, this.x, this.y);
elu.bombInd = (elu.bombInd + 1) % elu.bombs.length;
this.bombs--;
}
this.pendingBomb = false;
};
Player.prototype.warp = function (delta, elu) {
var timeSince = performance.now() - elu.bossRef.timeOfDeath;
var entInd;
const destAngle = (5 / 4) * Math.PI;
this.angle = wrapAngle(this.angle);
if (timeSince > this.warpDelay) {
this.x += delta * this.xVel;
this.y += delta * this.yVel;
trackScreenToEntity(this, this.maxVel * 3);
} else {
if (Math.abs(this.angle - destAngle) > Math.PI) {
if (this.angle > destAngle) {
this.angle += this.turnSpeed * delta;
} else {
this.angle -= this.turnSpeed * delta;
}
} else {
if (this.angle > destAngle) {
this.angle -= this.turnSpeed * delta;
} else {
this.angle += this.turnSpeed * delta;
}
}
if (Math.abs(this.angle - destAngle) <= this.turnSpeed + .05) {
this.angle = destAngle;
}
}
this.updateCollLines();
if (timeSince > this.warpDelay * 3) {
this.level++;
elu.bossRef.activate(elu);
}
};
Player.prototype.updateShape = function (lines, angle) {
lines[0] = Math.cos(1.6373644905707205 + angle) * 15.033296378372908;
lines[1] = Math.sin(-1.6373644905707205 - angle) * 15.033296378372908;
lines[2] = Math.cos(1.7975951748487824 + angle) * 13.341664064126334;
lines[3] = Math.sin(-1.7975951748487824 - angle) * 13.341664064126334;
lines[4] = Math.cos(2.5535900500422257 + angle) * 3.605551275463989;
lines[5] = Math.sin(-2.5535900500422257 - angle) * 3.605551275463989;
lines[6] = Math.cos(-2.5535900500422257 + angle) * 18.027756377319946;
lines[7] = Math.sin(2.5535900500422257 - angle) * 18.027756377319946;
lines[8] = Math.cos(-2.390663591191853 + angle) * 20.518284528683193;
lines[9] = Math.sin(2.390663591191853 - angle) * 20.518284528683193;
lines[10] = Math.cos(-2.321725389192837 + angle) * 20.518284528683193;
lines[11] = Math.sin(2.321725389192837 - angle) * 20.518284528683193;
lines[12] = Math.cos(-2.2455372690184494 + angle) * 19.209372712298546;
lines[13] = Math.sin(2.2455372690184494 - angle) * 19.209372712298546;
lines[14] = Math.cos(-2.0344439357957027 + angle) * 8.94427190999916;
lines[15] = Math.sin(2.0344439357957027 - angle) * 8.94427190999916;
lines[16] = Math.cos(-1.1071487177940904 + angle) * 8.94427190999916;
lines[17] = Math.sin(1.1071487177940904 - angle) * 8.94427190999916;
lines[18] = Math.cos(-0.8960553845713439 + angle) * 19.209372712298546;
lines[19] = Math.sin(0.8960553845713439 - angle) * 19.209372712298546;
lines[20] = Math.cos(-0.8198672643969563 + angle) * 20.518284528683193;
lines[21] = Math.sin(0.8198672643969563 - angle) * 20.518284528683193;
lines[22] = Math.cos(-0.7509290623979403 + angle) * 20.518284528683193;
lines[23] = Math.sin(0.7509290623979403 - angle) * 20.518284528683193;
lines[24] = Math.cos(-0.5880026035475675 + angle) * 18.027756377319946;
lines[25] = Math.sin(0.5880026035475675 - angle) * 18.027756377319946;
lines[26] = Math.cos(0.5880026035475675 + angle) * 3.605551275463989;
lines[27] = Math.sin(-0.5880026035475675 - angle) * 3.605551275463989;
lines[28] = Math.cos(1.3439974787410107 + angle) * 13.341664064126334;
lines[29] = Math.sin(-1.3439974787410107 - angle) * 13.341664064126334;
lines[30] = Math.cos(1.5042281630190728 + angle) * 15.033296378372908;
lines[31] = Math.sin(-1.5042281630190728 - angle) * 15.033296378372908;
};
Player.prototype.draw = function () {
drawPolygon(this.x, this.y, this.collLines, "#00FF00");
};
// TODO: fix asteroids clumping on the bottom right when entering planetoid zone
function Asteroid(x, y) {
var pointInd;
var angle;
var vertexCount = 10;
var angleInc = (2 * Math.PI) / vertexCount;
var minRadius = 10;
var maxRadius = 50;
var maxAppliedRadius = maxRadius;
var radius;
this.x = x;
this.y = y;
this.xVel = -.1 + Math.random() * .2;
this.yVel = -.1 + Math.random() * .2;
this.collLines = [];
// Generate polygon points
for (pointInd = 0; pointInd < vertexCount; pointInd++) {
angle = pointInd * angleInc + Math.random() * (angleInc - .01);
radius = minRadius + Math.random() * (maxRadius - minRadius);
if (radius < maxAppliedRadius) {
radius = maxAppliedRadius;
maxAppliedRadius -= 10;
}
if (radius > this.collRadius) {
this.collRadius = radius;
}
this.collLines.push(Math.round(Math.cos(angle) * radius));
this.collLines.push(Math.round(Math.sin(angle) * radius));
}
this.active = true;
};
Asteroid.prototype = Object.create(Entity.prototype);
Asteroid.prototype.collRadius = 0;
Asteroid.prototype.blipColor = "#777777";
Asteroid.prototype.heat = 0;
Asteroid.prototype.maxHeat = 5;
Asteroid.prototype.minCooldown = 1500;
Asteroid.prototype.lastCrystalTime = 0;
Asteroid.prototype.updateState = function (delta, elu) {
if (this.heat > 0) {
this.heat -= .002 * delta;
if (this.heat < 0) {
this.heat = 0;
} else if (this.heat > 0) {
if (performance.now() - this.lastCrystalTime > this.minCooldown * (this.maxHeat / this.heat)) {
elu.crystals[elu.crystalInd].activate(this.x, this.y, Math.random() * 2 * Math.PI, elu);
elu.crystalInd = (elu.crystalInd + 1) % elu.crystals.length;
this.lastCrystalTime = performance.now();
}
}
}
this.x += this.xVel * delta;
this.y += this.yVel * delta;
fieldWrap(this, elu.playerRef);
};
Asteroid.prototype.heatUp = function (amount, elu) {
var displaceAngle;
if (this.heat == 0) {
this.lastCrystalTime = performance.now();
}
this.heat += amount;
if (this.heat > this.maxHeat) {
this.heat = 0;
this.kill(elu.playerRef);
}
};
Asteroid.prototype.kill = function (pRef) {
placeAwayFrom(pRef.x, pRef.y, this);
};
Asteroid.prototype.draw = function () {
var rChannel;
var gbChannel;
var fill;
if (this.heat == 0) {
fill = "#AAAAAA";
} else {
rChannel = parseInt(170 + this.heat / this.maxHeat * 85);
gbChannel = parseInt(170 - this.heat / this.maxHeat * 170);
fill = "rgba(" + rChannel + "," + gbChannel + "," + gbChannel + ", 1)";
}
drawPolygon(this.x, this.y, this.collLines, fill);
};
function Miner() {
this.collLines = new Array(22);
this.throttle = true;
};
Miner.prototype = Object.create(Entity.prototype);
Miner.prototype.blipColor = "#FF0000";
Miner.prototype.accel = .0015;
Miner.prototype.maxVel = .21;
Miner.prototype.collRadius = MINER_RADIUS;
Miner.prototype.target = null;
Miner.prototype.angleToTarget = 0;
Miner.prototype.turnSpeed = .007;
Miner.prototype.bumpCooldown = 200;
Miner.prototype.lastBump = 0;
Miner.prototype.hasCrystal = false;
Miner.prototype.avoiding = false;
Miner.prototype.turnSign = 1;
Miner.prototype.nearTarget = false;
Miner.prototype.lastStop = 0;
Miner.prototype.stopLength = 700;
Miner.prototype.nearDistance = 100;
Miner.prototype.updateCollLines = function () {
this.collLines[0] = Math.cos(1.9513027039072615 + this.angle) * 5.385164807134504;
this.collLines[1] = Math.sin(-1.9513027039072615 - this.angle) * 5.385164807134504;
this.collLines[2] = Math.cos(1.7359450042095235 + this.angle) * 12.165525060596439;
this.collLines[3] = Math.sin(-1.7359450042095235 - this.angle) * 12.165525060596439;
this.collLines[4] = Math.cos(1.892546881191539 + this.angle) * 12.649110640673518;
this.collLines[5] = Math.sin(-1.892546881191539 - this.angle) * 12.649110640673518;
this.collLines[6] = Math.cos(2.0344439357957027 + this.angle) * 8.94427190999916;
this.collLines[7] = Math.sin(-2.0344439357957027 - this.angle) * 8.94427190999916;
this.collLines[8] = Math.cos(-2.5535900500422257 + this.angle) * 10.816653826391969;
this.collLines[9] = Math.sin(2.5535900500422257 - this.angle) * 10.816653826391969;
this.collLines[10] = Math.cos(-1.8157749899217608 + this.angle) * 12.36931687685298;
this.collLines[11] = Math.sin(1.8157749899217608 - this.angle) * 12.36931687685298;
this.collLines[12] = Math.cos(-1.3258176636680326 + this.angle) * 12.36931687685298;
this.collLines[13] = Math.sin(1.3258176636680326 - this.angle) * 12.36931687685298;
this.collLines[14] = Math.cos(-0.5880026035475675 + this.angle) * 10.816653826391969;
this.collLines[15] = Math.sin(0.5880026035475675 - this.angle) * 10.816653826391969;
this.collLines[16] = Math.cos(1.1071487177940904 + this.angle) * 8.94427190999916;
this.collLines[17] = Math.sin(-1.1071487177940904 - this.angle) * 8.94427190999916;
this.collLines[18] = Math.cos(1.2490457723982544 + this.angle) * 12.649110640673518;
this.collLines[19] = Math.sin(-1.2490457723982544 - this.angle) * 12.649110640673518;
this.collLines[20] = Math.cos(1.4056476493802699 + this.angle) * 12.165525060596439;
this.collLines[21] = Math.sin(-1.4056476493802699 - this.angle) * 12.165525060596439;
this.collLines[22] = Math.cos(1.1902899496825317 + this.angle) * 5.385164807134504;
this.collLines[23] = Math.sin(-1.1902899496825317 - this.angle) * 5.385164807134504;
};
Miner.prototype.updateState = function (delta, elu) {
var angleToTarget = 0;
var minTargetDist = this.target instanceof Asteroid ? 200 : 5;
var distanceToTarget = distance(this.x, this.y, this.target.x, this.target.y);
// update target
if (this.hasCrystal && !elu.bossRef.alive && this.target != elu.bossRef) {
this.target = elu.bossRef;
this.nearTarget = false;
} else if (!this.target.active || distanceToTarget < minTargetDist ||
(this.target == elu.bossRef && elu.bossRef.alive)) {
this.target = getRandomIndex(elu.asteroids, elu.bossRef.maxAsteroids);
}
// movement
this.angle = wrapAngle(this.angle);
angleToTarget = getAngleTo(this, this.target);
if (performance.now() - this.lastBump <= this.bumpCooldown) {
// turn to fly around asteroids
if (!this.avoiding) {
this.avoiding = true;
this.turnSign *= -1;
}
this.angle += this.turnSign * this.turnSpeed * delta;
} else {
if (Math.abs(this.angle - angleToTarget) > Math.PI) {
// find the shortest arc and turn towards the target
if (this.angle > angleToTarget) {
this.angle += this.turnSpeed * delta;
} else {
this.angle -= this.turnSpeed * delta;
}
if (this.avoiding) {
this.avoiding = false;
}
} else {
if (this.angle > angleToTarget) {
this.angle -= this.turnSpeed * delta;
} else {
this.angle += this.turnSpeed * delta;
}
}
// no longer trying to avoid an obstacle
if (this.avoiding) {
this.avoiding = false;
}
}
// stop before going towards a small target
if ((this.target instanceof Crystal || this.target instanceof Boss) &&
!this.nearTarget &&
distance(this.x, this.y, this.target.x, this.target.y) < this.nearDistance) {
this.nearTarget = true;
this.lastStop = performance.now();
this.throttle = false;
} else if (performance.now() - this.lastStop >= this.stopLength) {
this.throttle = true;
}
if (this.moveSelf(delta, elu) instanceof Asteroid) {
this.lastBump = performance.now();
}
fieldWrap(this, elu.playerRef);
};
Miner.prototype.activate = function (x, y, elu) {
this.x = x;
this.y = y;
this.target = getRandomIndex(elu.asteroids, elu.bossRef.maxAsteroids);
this.active = true;
};
Miner.prototype.kill = function (elu) {
if (this.hasCrystal) {
this.active = false;
elu.crystals[elu.crystalInd].activate(this.x, this.y, this.angle + Math.PI / 2, elu);
elu.crystalInd = (elu.crystalInd + 1) % elu.crystals.length;
this.active = true;
this.hasCrystal = false;
} else if (this.target instanceof Crystal) {
assignMinerToCrystal(elu, this.target);
}
this.target = getRandomIndex(elu.asteroids, elu.bossRef.maxAsteroids);
this.throttle = true;
kill(this);
};
Miner.prototype.draw = function () {
drawPolygon(this.x, this.y, this.collLines, "#FF0000");
if (this.hasCrystal) {
drawCircle(this.x - screenX, this.y - screenY, Crystal.prototype.collRadius, "#5555FF");
}
};
function Shooter() {
};