-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathstarship-sorades-13k.js
1349 lines (1281 loc) · 36.6 KB
/
starship-sorades-13k.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
// Global variable for the main game loop
var interval;
// Initialize a single static level object
var l = {
// Currently a lot of objects and speeds don't scale well with the size of the canvas
WIDTH: 1024,
HEIGHT: 768,
// Scrolling speed for the background
SPEED: 2,
// Number of ticks for the screen flash effect
MAX_BOMB: 5,
y: 0,
bomb: 0,
text: {
MAX_T: 3 * 30,
t: 0
},
p: 0,
points: {
WIDTH: 32,
HEIGHT: 48,
STEP: 24,
images: []
}
};
// Initialize a single static object for the players ship
var ship = {
R: Math.max(l.WIDTH, l.HEIGHT) / 16 | 0,
ACC: 1.5,
ACC_FACTOR: .9,
ANGLE_FACTOR: .8,
MAX_ANGLE: 10,
MAX_OSD: 6 * 30,
x: l.WIDTH / 2,
y: l.HEIGHT * 7 / 8 | 0,
xAcc: 0,
yAcc: 0,
angle: 0,
e: 100,
timeout: 0,
weapon: 0,
reload: 0,
shield: {
MAX_T: 5 * 30,
t: 0
}
};
var bullets = [];
bullets.R = 8;
bullets.MAX_T = 35;
var explosions = [];
var bonus = [];
bonus.R = 16;
bonus.images = {};
var torpedos = [];
torpedos.R = 16;
// Global sprite index for the rotation of all torpedos
torpedos.frame = 0;
var enemies = [];
function toggleFullscreen()
{
if (document['fullscreenElement'] || document['mozFullScreen'] || document['webkitIsFullScreen'])
{
// Opera 12.50 is first again
if (document['exitFullscreen'])
document['exitFullscreen']();
else if (document['mozCancelFullScreen'])
document['mozCancelFullScreen']();
else if (document['webkitCancelFullScreen'])
document['webkitCancelFullScreen']();
}
else
{
var c = document.getElementsByTagName('canvas')[0];
if (c['requestFullscreen'])
c['requestFullscreen']();
else if (c['mozRequestFullScreen'])
c['mozRequestFullScreen']();
else if (c['webkitRequestFullScreen'])
c['webkitRequestFullScreen'](Element['ALLOW_KEYBOARD_INPUT']);
}
return false;
}
// Export for the Closure Compiler
window['toggleFullscreen'] = toggleFullscreen;
// Helper function to hurt the player, or not if the shield is active
function hurt(e)
{
if (ship.shield.t)
{
play(2);
return;
}
// Merge multiple shots in a short time like they are a single shot
if (ship.timeout < 0)
{
ship.e -= e;
if (ship.e < 0)
ship.e = 0;
ship.timeout = 10;
}
if (!ship.e && !l.paused)
{
// Giant explosion
explode(ship.x, ship.y, 512);
explode(ship.x, ship.y, 1024);
l.paused = true;
spawnText("GAME OVER", -1);
// Stop the main game loop
if (interval)
window.clearInterval(interval);
var text = 'I survived ' + ((l.level || 1) - 1) + ' waves and got ' + l.p + ' points in SORADES 13K:';
// Always use my domain as canonical link
var url = /^\w+:\/\/maettig\.com/.test(location.href)
? location.href
: 'http://maettig.com/code/canvas/starship-sorades-13k/';
tweet.getElementsByTagName('A')[0].href = 'https://twitter.com/share?text=' +
encodeURIComponent(text) + '&url=' + encodeURIComponent(url) + '&via=maettig';
tweet.style.display = '';
var input = tweet.getElementsByTagName('INPUT')[0];
input.value = text + ' ' + url;
input.onfocus = function() { this.select(); }
input.focus();
play(14); // Game over
}
else if (ship.e < 25)
play(17); // Low energy
play(1);
// Remove some weapons if the player was hurt
if (ship.weapon > 2)
ship.weapon--;
ship.osd = ship.MAX_OSD;
}
function fire(xAcc, yAcc)
{
// The acceleration of the bullets changes with the acceleration of the ship
xAcc += ship.xAcc / 2;
yAcc += ship.yAcc / 2;
bullets.push({
t: bullets.MAX_T,
x: ship.x + Math.random() * xAcc,
y: ship.y + Math.random() * yAcc,
xAcc: xAcc,
yAcc: yAcc
});
}
function explode(x, y, size)
{
explosions.push({
x: x,
y: y,
size: size ? size : Math.random() * 64,
angle: Math.random(),
d: Math.random() * .4 - .2,
alpha: 1
});
}
function spawnBonus(o, i)
{
if (!i)
{
var r = Math.random();
// A chance of 10 percent for every bonus, everything else is just money
i = r > .9 ? '+' : (r > .8 ? 'E' : (r > .7 ? 'S' : (r > .6 ? 'B' : '10')));
}
// Lazy loading, this allows me to use every character I want whenever I want it
if (!bonus.images[i])
bonus.images[i] = render(function(c, a)
{
a.shadowBlur = 6;
a.fillStyle = i.length > 1 ? '#FEB' : '#EFF';
a.shadowColor = a.fillStyle;
a.arc(c.width / 2, c.height / 2, c.width / 2 - a.shadowBlur, 0, Math.PI * 2);
a.fill();
a.fillStyle = 'rgba(0,0,0,.5)';
a.font = 'bold ' + (c.width / 1.8 - i.length * 7 + 7 | 0) + 'px sans-serif';
a.textAlign = 'center';
a.textBaseline = 'middle';
a.fillText(i, c.width / 2, c.height / 2);
}, bonus.R * 2);
bonus.push({
i: i,
x: o.x || l.WIDTH / 2,
y: o.y || -bonus.R,
xAcc: o.xAcc ? o.xAcc / 2 : Math.random() * l.SPEED - l.SPEED / 2,
yAcc: (o.yAcc ? o.yAcc / 2 : 0) + l.SPEED / 2
});
}
function spawnTorpedo(o, angle, maxAngle)
{
var y = o.y + (o.yOffset || 0) | 0;
// Never spawn any torpedos if the enemy is still to far away
if (y < -l.HEIGHT / 4)
return false
if (maxAngle)
{
if (angle > Math.PI)
angle -= Math.PI * 2;
if (angle > maxAngle)
angle = maxAngle;
if (angle < -maxAngle)
angle = -maxAngle;
}
// All torpedos share the same speed, no matter which enemy fires them
var speed = 3 + l.level / 2;
torpedos.push({
x: o.x | 0,
y: y,
xAcc: angle ? Math.sin(angle) * speed : 0,
yAcc: angle ? Math.cos(angle) * speed : speed,
e: 0
});
return true;
}
// Little helper function to spawn a new enemy, calls the spawn method from the enemy object
function spawnEnemy(i, y)
{
// Can be used to spawn a random enemy, currently unused
if (i < 0 || i >= enemies.TYPES.length)
i = Math.random() * enemies.TYPES.length | 0;
var type = enemies.TYPES[i];
if (!type.R)
type.R = Math.max(l.WIDTH, l.HEIGHT) / 16 | 0;
if (!type.image)
type.image = render(type.render, type.R * 2);
type.spawn(y);
}
function play(i)
{
if (sfx[i] && sfx[i][sfx[i].i])
{
// Maybe I should have added a stop() call here, but I'm not sure about this
sfx[i][sfx[i].i++].play(true);
sfx[i].i %= sfx[i].length;
}
}
// Helper function to render all the static buffered canvas image objects
function render(f, w, h, c)
{
if (!c)
c = document.createElement('canvas');
c.width = w | 0;
c.height = (h || w) | 0;
f(c, c.getContext('2d'));
return c;
}
// The players ship and all enemies do have a bright diamond shape in their center
function renderHeart(a, x, y)
{
var p = ship.R / 6 | 0;
a.beginPath();
a.moveTo(x - p, y);
a.lineTo(x, y + p);
a.lineTo(x + p, y);
a.lineTo(x, y - p);
a.closePath();
a.globalCompositeOperation = 'lighter';
a.shadowColor = '#FFF';
a.stroke();
}
// Helper function to spawn the title, "WAVE 1", "PAUSE" and "GAME OVER" text objects
function spawnText(text, t)
{
// Always render the buffered image first
l.text.image = render(function(c, a)
{
a.shadowBlur = c.height / 10 | 0;
a.font = 'bold ' + (c.height * .9 - a.shadowBlur * 2 | 0) + 'px Consolas,monospace';
a.textAlign = 'center';
a.textBaseline = 'middle';
var maxWidth = c.width - a.shadowBlur * 2;
a.fillStyle = '#62F';
a.shadowColor = '#FFF';
for (var i = 2; i--; )
a.fillText(text, c.width / 2, c.height / 2, maxWidth);
a.fillStyle = '#FFF';
a.shadowBlur /= 4;
a.shadowColor = 'rgba(0,0,0,.5)';
a.lineWidth = a.shadowBlur;
a.lineJoin = 'round';
a.strokeStyle = '#FFF';
a.shadowColor = '#000';
a.strokeText(text, c.width / 2, c.height / 2, maxWidth);
a.globalAlpha = .2;
a.globalCompositeOperation = 'source-atop';
a.fillStyle = '#62F';
// It seems I forgot to set shadowBlur = 0 here
for (var i = 0; i < c.height; i += 3)
a.fillRect(0, i, c.width, 1);
}, l.WIDTH / 1.6, l.WIDTH / 8, l.text.image);
l.text.x = (l.WIDTH - l.text.image.width) / 2 | 0;
l.text.y = 16;
l.text.yAcc = l.SPEED / 2;
l.text.t = t || l.text.MAX_T;
// The "PAUSE" and "GAME OVER" texts need to be drawn instantly
if (t < 0)
{
l.text.t = 0;
a.globalAlpha = 1;
a.drawImage(l.text.image, l.text.x, l.text.y);
}
}
// Render the ten numbers for the points in the upper right corner of the screen
for (var number = 10; number--; )
l.points.images[number] = render(function(c, a)
{
a.shadowBlur = 6;
a.font = 'bold ' + (c.width * 1.3 | 0) + 'px Consolas,monospace';
a.textAlign = 'center';
a.textBaseline = 'middle';
a.lineWidth = 2;
a.lineJoin = 'round';
a.shadowColor = '#9F0';
a.strokeText(number, c.width / 2, c.height / 2);
a.strokeStyle = '#9F0';
a.strokeText(number, c.width / 2, c.height / 2);
}, l.points.WIDTH, l.points.HEIGHT);
// Render the level background
l.background = render(function(c, a)
{
a.fillStyle = '#000';
a.fillRect(0, 0, c.width, c.height);
a.globalCompositeOperation = 'lighter';
a.beginPath();
for (var i = 6; i--; )
{
a.moveTo(c.width * (i + 1) / 4, -c.height);
a.lineTo(c.width * (i - 2) / 4, c.height * 2);
a.moveTo(-c.width, c.height * (i - 2) / 4);
a.lineTo(c.width * 2, c.height * (i + 1) / 4);
}
a.lineWidth = 3;
a.shadowBlur = a.lineWidth * 2;
a.strokeStyle = '#111';
a.shadowColor = '#444';
a.stroke();
// Mirror the image and render it on top of itself
a.shadowBlur = 0;
a.globalAlpha = .25;
a.translate(c.width, 0);
a.scale(-1, 1);
a.drawImage(c, 0, 0);
}, 256);
// Render the graphic for the players ship
ship.image = render(function(c, a)
{
a.beginPath();
for (var i = 5; i--; )
{
a.moveTo(c.width / 2, c.height * (1 + i) / 10);
a.lineTo(c.width * (11 + i) / 16, c.height * (15 - i) / 16);
a.lineTo(c.width * ( 5 - i) / 16, c.height * (15 - i) / 16);
a.closePath();
}
a.lineWidth = c.width / 17 | 0;
a.shadowBlur = a.lineWidth * 2;
a.strokeStyle = '#9F0';
a.shadowColor = a.strokeStyle;
a.stroke();
a.stroke();
// Maybe I could have used the renderHeart() function here but it's slightly different and I was to lazy
var p = c.width / 6 | 0;
a.beginPath();
a.moveTo(c.width / 2 - p, c.height / 2);
a.lineTo(c.width / 2, c.height / 2 + p);
a.lineTo(c.width / 2 + p, c.height / 2);
a.lineTo(c.width / 2, c.height / 2 - p);
a.closePath();
a.strokeStyle = '#FFF';
a.shadowColor = a.strokeStyle;
a.stroke();
a.stroke();
}, ship.R, ship.R * 2);
// Render the shield graphic for the players ship
ship.shield.image = render(function(c, a)
{
var d = 8;
a.lineWidth = 18;
a.shadowBlur = a.lineWidth;
a.strokeStyle = '#000';
a.shadowColor = '#CF0';
a.beginPath();
a.arc(c.width / 2, c.height / 2, c.width / 2 + a.lineWidth / 2 - d, 0, Math.PI * 2);
a.stroke();
// Draw a black arc to clip the outer half of the shadow
a.lineWidth = 26 + d;
a.shadowBlur = 0;
a.beginPath();
a.arc(c.width / 2, c.height / 2, c.width / 2 + a.lineWidth / 2 - d, 0, Math.PI * 2);
a.stroke();
}, ship.R * 2);
// A bullet is a tiny diamond shape with a bright glowing shadow
bullets.image = render(function(c, a)
{
a.beginPath();
var p = 6;
a.moveTo(c.width / 2, p);
a.lineTo(c.width - p, c.height / 2);
a.lineTo(c.width / 2, c.height - p);
a.lineTo(p, c.height / 2);
a.closePath();
a.lineWidth = 3;
a.shadowBlur = a.lineWidth * 2;
a.strokeStyle = '#CF0';
a.shadowColor = a.strokeStyle;
a.stroke();
a.stroke();
}, bullets.R * 2);
// The explosion sprite is only 16px but is painted in all sizes up to 1000px
explosions.image = render(function(c, a)
{
a.fillStyle = '#F63'
a.shadowBlur = 6;
a.shadowColor = a.fillStyle;
var p = a.shadowBlur;
// Overlaying multiple blured rectangles make them almost disapear
for (var i = 5; i--; )
a.fillRect(p, p, c.width - p * 2, c.height - p * 2);
// Draw a tiny, tiny cross in the middle
a.lineWidth = .3;
a.strokeStyle = '#FC6'
p *= .8;
a.beginPath();
a.moveTo(p, p);
a.lineTo(c.width - p, c.height - p);
a.moveTo(c.width - p, p);
a.lineTo(p, c.height - p);
a.stroke();
}, 16);
// Buffer a sprite sheet instead of rotating the possible many, many torpedos on screen
torpedos.images = [];
var count = 8;
for (var i = count; i--; )
torpedos.images.push(render(function(c, a)
{
a.translate(c.width / 2, c.height / 2);
a.rotate(Math.PI / -2 * i / count);
a.translate(-c.width / 2, -c.height / 2);
a.beginPath();
a.lineWidth = 3;
a.shadowBlur = a.lineWidth * 2;
a.strokeStyle = '#62F';
a.shadowColor = a.strokeStyle;
var p = a.shadowBlur;
a.moveTo(c.width / 2, p);
a.lineTo(c.width - p, c.height / 2);
a.lineTo(c.width / 2, c.height - p);
a.lineTo(p, c.height / 2);
a.closePath();
a.stroke();
a.stroke();
}, torpedos.R * 2));
// I wanted to use real inheritance but this is not so easy in JavaScript, unfortunately
enemies.TYPES = [
// Enemy number 0 is the smallest
{
render: function(c, a)
{
a.lineWidth = 3;
a.shadowBlur = a.lineWidth * 2;
a.strokeStyle = '#62F';
a.shadowColor = a.strokeStyle;
a.miterLimit = 128;
a.beginPath();
for (var i = 5; i--; )
{
var x1 = c.width * (6 - i) / 11, y1 = c.height * (6 - i) / 20;
var x2 = c.width * (11 - i) / 26, y2 = c.height * (1 + i) / 9;
a.moveTo(c.width / 2, c.height * (12 - i) / 12 - a.shadowBlur);
a.lineTo(c.width - x1, y1);
a.lineTo(c.width - x2, y2);
a.lineTo(x2, y2);
a.lineTo(x1, y1);
a.closePath();
}
a.stroke();
a.stroke();
renderHeart(a, c.width / 2, c.height / 2);
},
spawn: function(y)
{
for (var i = 2 + l.level; i--; )
{
var yStop = l.HEIGHT / 8 + Math.random() * l.HEIGHT / 4 | 0;
enemies.push({
image: this.image,
x: this.R + (l.WIDTH - this.R * 2) * Math.random() | 0,
y: y ? yStop + y : -this.R,
yStop: yStop,
r: this.R,
angle: 0,
maxAngle: Math.PI / 32,
e: 12 + l.level,
t: Math.random() * 120 | 0,
shoot: this.shoot
});
}
},
shoot: function(angle)
{
//this.t = 120 - l.level * 10;
this.t = 600 / (l.level + 4) | 0;
if (this.t < 5)
this.t = 5;
if (spawnTorpedo(this, angle, this.maxAngle))
play(19);
}
},
// Enemy number 1 is slightly bigger than enemy number 0, but almost the same
{
render: function(c, a)
{
a.lineWidth = 3;
a.shadowBlur = a.lineWidth * 2;
a.strokeStyle = '#62F';
a.shadowColor = a.strokeStyle;
for (var i = 5; i--; )
{
var x1 = c.width * (5 - i) / 14 + a.shadowBlur, y1 = c.height * (16 - i) / 17;
var x2 = c.width * (8 - i) / 22, y2 = c.height * (1 + i) / 11;
a.moveTo(c.width / 2, c.height * (6 - i) / 12);
a.lineTo(c.width - x1, y1);
a.lineTo(c.width - x2, y2);
a.lineTo(x2, y2);
a.lineTo(x1, y1);
a.closePath();
}
a.stroke();
a.stroke();
renderHeart(a, c.width / 2, c.height / 2);
},
spawn: function(y)
{
for (var i = 1 + l.level; i--; )
{
var yStop = l.HEIGHT / 8 + Math.random() * l.HEIGHT / 4 | 0;
enemies.push({
image: this.image,
x: this.R + (l.WIDTH - this.R * 2) * Math.random() | 0,
y: y ? yStop + y : -this.R,
yStop: yStop,
r: this.R,
angle: 0,
maxAngle: Math.PI / 16,
e: 20 + l.level * 2,
t: Math.random() * 120 | 0,
shoot: this.shoot
});
}
},
shoot: function(angle)
{
//this.t = 120 - l.level * 10;
this.t = 600 / (l.level + 4) | 0;
if (this.t < 5)
this.t = 5;
// I can't use the angle calculation from the spawnTorpedo() function because of the two directions
if (angle > Math.PI)
angle -= Math.PI * 2;
if (angle > this.maxAngle)
angle = this.maxAngle;
if (angle < -this.maxAngle)
angle = -this.maxAngle;
if (spawnTorpedo(this, angle + .1) ||
spawnTorpedo(this, angle - .1))
play(20);
}
},
// Enemy number 2 is probably the most dangerous, fires in all directions but does not aim
{
render: function(c, a)
{
a.lineWidth = 3;
a.shadowBlur = a.lineWidth * 2;
a.strokeStyle = '#62F';
a.shadowColor = a.strokeStyle;
a.miterLimit = 32;
a.beginPath();
for(var i = 0; i < Math.PI * 2; i += Math.PI / 4)
{
var d = Math.PI / 12;
var r = c.width / 2 - a.shadowBlur;
var x = c.width / 2 + Math.sin(i + d) * r, y = c.width / 2 + Math.cos(i + d) * r;
if (!i)
a.moveTo(x, y); else a.lineTo(x, y);
d -= Math.PI / 1.45;
a.lineTo(c.width / 2 + Math.sin(i + d) * r, c.width / 2 + Math.cos(i + d) * r);
}
a.closePath();
for(var i = 0; i < Math.PI * 2; i += Math.PI / 4)
{
var r = c.width * .4;
var x = c.width / 2 + Math.sin(i) * r, y = c.width / 2 + Math.cos(i) * r;
if (!i)
a.moveTo(x, y); else a.lineTo(x, y);
}
a.closePath();
a.stroke();
a.stroke();
renderHeart(a, c.width / 2, c.height / 2);
},
spawn: function(y)
{
var yStep = l.HEIGHT * -1.5 / l.level | 0;
for (var i = l.level; i--; )
{
enemies.push({
image: this.image,
x: l.WIDTH / 4 + l.WIDTH / 2 * Math.random() | 0,
y: i * yStep + (y || -this.R),
yStop: l.HEIGHT / 2 | 0,
r: this.R,
angle: 0,
maxAngle: Math.PI * 32,
e: 28 + l.level * 3,
t: Math.random() * 30 | 0,
fireDirection: Math.random() * Math.PI,
shoot: this.shoot
});
}
},
shoot: function(angle)
{
// Insert a bigger pause between every 5 shots
if (!this.tActive)
{
this.tActive = 5;
// But the pause gets shorter every level
//this.t = 60 - l.level * 4;
this.t = 540 / (l.level + 8) | 0;
}
this.tActive--;
if (this.t < 5)
this.t = 5;
this.fireDirection += .2;
var result;
for (var i = Math.PI / 8; i < Math.PI * 2; i += Math.PI)
result = spawnTorpedo(this, this.fireDirection + i);
if (result)
play(18);
}
},
// Enemy number 3 was the first I created, the idea was it grows bigger and bigger like in "Warning Forever"
{
R: Math.max(l.WIDTH, l.HEIGHT) / 8 | 0,
render: function(c, a)
{
a.lineWidth = 3;
a.shadowBlur = a.lineWidth * 2;
a.strokeStyle = '#62F';
a.shadowColor = a.strokeStyle;
a.miterLimit = 32;
a.beginPath();
for (var i = 7; i--; )
{
a.moveTo(c.width / 2, c.height * i / 12 + a.shadowBlur);
var x1 = c.width * (11 + i) / 18 - a.shadowBlur;
var y1 = c.height * (25 - i) / 28;
a.lineTo(x1, y1);
var x2 = c.width * (16 - i) / 16 - a.shadowBlur;
var y2 = c.height * (i + 4) / 28;
a.lineTo(x2, y2);
a.lineTo(c.width / 2, c.height * (i + 30) / 36 - a.shadowBlur);
a.lineTo(c.width - x2, y2);
a.lineTo(c.width - x1, y1);
a.closePath();
}
a.stroke();
a.stroke();
renderHeart(a, c.width / 2, c.height * .8);
},
spawn: function(y)
{
var yStart = y || -this.R * 3;
var e = 36 + l.level * 4;
var tStart = 2 * 30;
enemies.push({
image: this.image,
x: l.WIDTH / 2,
y: yStart,
yOffset: this.R * .6 | 0,
yStop: this.R + 8,
r: this.R,
angle: 0,
maxAngle: Math.PI / 8,
e: e,
t: tStart + Math.random() * 30 | 0,
shoot: this.shoot
});
var size = this.R * 1.3;
var d = size * .4;
var x = d;
for (var i = 1; i < l.level; i++)
{
var y = this.R + 16 - size + Math.sqrt(i) * 64;
if (i % 2)
y += this.R * (.7 / i + .3);
enemies.push({
image: this.image,
x: l.WIDTH / 2 - x | 0,
y: yStart,
yOffset: size / 2 * .6 | 0,
yStop: y | 0,
r: size / 2,
angle: 0,
maxAngle: Math.PI / 8,
e: e / 2 | 0,
t: tStart + Math.random() * 30 | 0,
shoot: this.shoot
});
enemies.push({
image: this.image,
x: l.WIDTH / 2 + x | 0,
y: yStart,
yOffset: size / 2 * .6 | 0,
yStop: y | 0,
r: size / 2,
angle: 0,
maxAngle: Math.PI / 8,
e: e / 2 | 0,
t: tStart + Math.random() * 30 | 0,
shoot: this.shoot
});
x += d;
d *= .84;
size *= .9;
}
},
shoot: function(angle)
{
// Always aim at the player and fire randomly, 1 shot every 3 seconds on average
this.t = Math.random() * 6 * 30 | 0;
if (spawnTorpedo(this, angle))
play(12);
}
}
];
// This global variable will be needed again when the game ends
var tweet = document.getElementById('tweet');
tweet.style.display = 'none';
// Initialization similar to the JS1K shim
var c = document.getElementsByTagName('canvas')[0];
c.width = l.WIDTH;
c.height = l.HEIGHT;
var a = c.getContext('2d');
a.fillStyle = '#000';
a.fillRect(0, 0, c.width, c.height);
spawnText('LOADING', -1);
spawnText('SORADES 13K', 6 * 30);
// Debug only
/*
var fps = Array(10);
fps.i = 0;
fps.t = 1;
*/
// Keyboard handling, cursor keys and X is my main control scheme but several others are provided
var keys = [], keyMap = {
27: 80, // Esc => P
32: 88, // Space => X
48: 88, // 0 => X
50: 40, // 2 => Down
52: 37, // 4 => Left
53: 40, // 5 => Down
54: 39, // 6 => Right
56: 38, // 8 => Up
65: 37, // A => Left
67: 88, // C => X
68: 39, // D => Right
73: 38, // I => Up
74: 37, // J => Left
75: 40, // K => Down
76: 39, // L => Right
83: 40, // S => Down
87: 38, // W => Up
89: 88, // Y => X
90: 88 // Z => X
};
document.onkeydown = function(e)
{
var c = (e || event).keyCode;
keys[keyMap[c] || c] = true;
if (keys[70])
toggleFullscreen();
else if (keys[77])
{
if (ship.originalImage)
{
ship.image = ship.originalImage;
ship.originalImage = null;
}
else
{
// That's all I need for my little easter egg, the code above is to be able to switch back
var image = new Image();
image.onload = function()
{
ship.originalImage = ship.image;
ship.image = image;
}
image.src = 'starship-sorades.jpg';
}
}
// Cheat key skips to the next level
/*
else if (keys[79])
{
l.text.t = 0;
ship.weapon++;
ship.shield.t = ship.shield.MAX_T * 2;
enemies.length = 0;
}
*/
// Pause is not possible if the player is not alive
else if (keys[80] && ship.e)
{
l.paused = !l.paused;
// I'm abusing the messaging system but it's unable to handle multiple messages
if (l.paused && !l.text.t)
spawnText('PAUSE', -1);
}
// Unpause with any of the fire keys
else if (l.paused && keys[88])
l.paused = false;
}
document.onkeyup = function(e)
{
var c = (e || event).keyCode;
// Remove the key code from the key pressed map
keys[keyMap[c] || c] = false;
}
function gameloop()
{
if (l.paused)
return;
if (--ship.reload <= 0 && keys[88])
{
// Weapon 0 and 1 fire in the same direction, but the later is a bit faster
ship.reload = ship.weapon ? 4 : 6;
fire(0, -16);
if (ship.weapon > 1)
{
fire(-8, -8),
fire(8, -8);
if (ship.weapon > 2)
{
fire(0, 16);
if (ship.weapon > 3)
fire(-16, 0),
fire(16, 0);
}
}
play(ship.weapon > 2 ? 16 : ship.weapon > 0 ? 0 : 15);
}
ship.angle *= ship.ANGLE_FACTOR;
if (keys[37])
{
// This is required for a fast turn when stuck at the edge of the screen
if (ship.x >= l.WIDTH && ship.xAcc > 0)
ship.xAcc = 0;
ship.xAcc -= ship.ACC;
ship.angle = (ship.angle + 1) * ship.ANGLE_FACTOR - 1;
}
if (keys[38])
{
if (ship.y >= l.HEIGHT && ship.yAcc > 0)
ship.yAcc = 0;
ship.yAcc -= ship.ACC;
}
if (keys[39])
{
if (ship.x < 0 && ship.xAcc < 0)
ship.xAcc = 0;
ship.xAcc += ship.ACC;
ship.angle = (ship.angle - 1) * ship.ANGLE_FACTOR + 1;
}
if (keys[40])
{
if (ship.y < 0 && ship.yAcc < 0)
ship.yAcc = 0;
ship.yAcc += ship.ACC;
}
// Stop the players ship at all 4 edges of the screen
if (ship.x < 0 && ship.xAcc < 0)
ship.x = 0;
else if (ship.x >= l.WIDTH && ship.xAcc > 0)
ship.x = l.WIDTH - 1;
if (ship.y < 0 && ship.yAcc < 0)
ship.y = 0;
else if (ship.y >= l.HEIGHT && ship.yAcc > 0)
ship.y = l.HEIGHT - 1;
// Accelerate the players ship
ship.x += ship.xAcc;
ship.y += ship.yAcc;
// Decrease the acceleration. I don't need to clip to a maximum, this is enough.
ship.xAcc *= ship.ACC_FACTOR;
ship.yAcc *= ship.ACC_FACTOR;
// Fill the screen with the background tile
var size = l.background.width;
for (var y = (l.y % size - size) | 0; y < l.HEIGHT; y += size)
for (var x = 0; x < l.WIDTH; x += size)
a.drawImage(l.background, x, y);
l.y += l.SPEED;
// Show the current points of the player in the top right corner of the screen
var p = l.p, x = l.WIDTH - l.points.WIDTH - 8;
while (p)
{
a.drawImage(l.points.images[p % 10], x, 4);
p = p / 10 | 0;
x -= l.points.STEP;
}
// Show the current text in the middle of the screen
if (l.text.t)
{
a.globalAlpha = l.text.t < l.text.MAX_T ? l.text.t / l.text.MAX_T : 1;
a.drawImage(l.text.image, l.text.x, l.text.y);
a.globalAlpha = 1;
l.text.t--;
l.text.y += l.text.yAcc;
}
// Distance between a players shot and a torpedo
var d = 12;
for (var i = bullets.length; i--; )
{
// Avoid sub-pixel rendering by trimming all coordinates to whole numbers
a.drawImage(bullets.image, bullets[i].x - bullets.R | 0, bullets[i].y - bullets.R | 0);
bullets[i].x += bullets[i].xAcc;
bullets[i].y += bullets[i].yAcc;
for (var j = torpedos.length; j--; )
{
if (bullets[i].y < torpedos[j].y + d && bullets[i].y > torpedos[j].y - d &&
bullets[i].x < torpedos[j].x + d && bullets[i].x > torpedos[j].x - d)
{
if (--torpedos[j].e < 0)
{
l.p += 5;
if (Math.random() > .75)
spawnBonus(torpedos[j]);
explode(torpedos[j].x, torpedos[j].y);
torpedos[j].y = l.HEIGHT * 2;
play(11);
}
//else
//{
// l.p += 1;
// explode(bullets[i].x, bullets[i].y);
// play(9);
//}
bullets[i].t = 0;
break;
}
}
// There was a bug, the comparison "y < ship.R" was always false because ship.R is undefined
if (--bullets[i].t < 0 || bullets[i].x < -bullets.R ||
bullets[i].x >= l.WIDTH + bullets.R || bullets[i].y >= l.HEIGHT + bullets.R)