-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
1532 lines (1340 loc) · 51 KB
/
main.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
'use strict';
/* ======= Canvas Setup ======= */
/** @type {HTMLCanvasElement} */
const canv = /** @type {HTMLCanvasElement} */ (document.getElementById('c'));
const ctx = canv.getContext('2d');
const height = 1000;
let actual_height = 600;
let width;
function recalculate_sizing() {
let actual_width = actual_height * canv.clientWidth / canv.clientHeight;
canv.width = actual_width;
canv.height = actual_height;
width = actual_width * height / actual_height;
boss_x_screen = width - 150;
}
window.addEventListener('resize', recalculate_sizing);
recalculate_sizing();
/* ======= Utilities ======= */
function lerp(start, end, x) {
return start + Math.max(0, Math.min(1, x)) * (end - start)
}
function rlerp(start, end) {
return lerp(start, end, Math.random());
}
function rand_element_form_arr(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
function triangle_wave(x) {
// Period 1
return 2 * Math.abs(x - Math.floor(x + 0.5));
}
function offset_triangle_wave(x) {
return triangle_wave(x) - 0.5;
}
const get_time = () => new Date() / 1000;
/* ======= Global Variables ======= */
let elapsed_time = 0;
let player_x = 0;
let cam_x = 0;
let player_y = height / 2;
let grav_direction = 1;
let player_y_velocity = 0;
let forward_velocity = 330;
let last_explosion_time = -Infinity;
let difficulty = 0; // [0, 1]
let player_health = 1; // [0, 1]
let displayed_player_health = 1;
let coins_gotten = 0;
let coins_needed = 0;
let regenerator_repaired = false;
let boss_y = height / 2;
var boss_x_screen;
let boss_last_action = -Infinity;
let boss_phase = 0;
let last_boss_laser_fire = -Infinity;
let boss_health = 1;
let boss_last_damaged = -Infinity;
const TOP_BAR = 22;
const SIDE_BARS_WIDTH = 22;
let stage = 0;
let substage = 0;
let stage_elapsed = 0;
let start_of_stage_x = 0;
const player_points = [
[30, 0],
[0, -20],
[-40, -20],
[-60, -30],
[-70, -40],
[-80, -40],
[-80, -30],
[-70, -30],
[-60, -20],
[-60, 20],
[-70, 30],
[-80, 30],
[-80, 40],
[-70, 40],
[-60, 30],
[-40, 20],
[0, 20]
];
let exhaust_particles = [];
let last_exhaust_emission = 0;
const time_between_exhausts = 0.2;
let obstacles = [];
let obstacle_generation_x = 600;
let lasers = []; // {x: number, y: number}
let laser_enabled = false;
const dialogue_box_width = 700;
const dialogue_box_margin = 50;
const dialogue_rate = 0.05;
let skip_dialogue_pressed = false;
let graphics_mode = Number(window.localStorage['xav-space-js13k-graphics']); // 0 fancy, 1 normal, 2 basic
if(![0, 1, 2].includes(graphics_mode)) {
graphics_mode = 1;
}
process_graphics_change();
/* ======= Rendering ======= */
function process_graphics_change() {
actual_height = [1000, 600, 300][graphics_mode];
recalculate_sizing();
}
function make_colour_string(r, g, b, a) {
return `rgba(${r}, ${g}, ${b}, ${a})`;
}
function get_explosion_offset() {
return lerp(rlerp(-20, 20), 0, (elapsed_time - last_explosion_time) / 3);
}
let dialogue_lines_done = [];
let characters_to_add_to_line = [];
let dialogue_words_left = [];
let start_of_empty_dialogue = Infinity;
const stages_with_dialogue_screen = [1, 3, 5, 6, 8, 10, 12];
let can_skip_dialogue = true;
let need_space_to_proceed = false;
const HUD_WIDTH = 150;
function dialogue_done_running() {
return dialogue_lines_done.length && !characters_to_add_to_line.length && !dialogue_words_left.length;
}
function render_stars() {
for(let i = 0; i < [1200, 750, 200][graphics_mode]; ++i) {
const offset = i ** 4.7;
const x = (offset * 1.9 % 500) * width / 500;
const y = (offset % 501) * height / 501;
ctx.beginPath();
const r = offset % 6 * Math.sin(i + elapsed_time * (i % 2 ? 0.9 : 0.8)) ** 2
ctx.arc(x, y, r / 2 * [0.8, 1, 1.8][graphics_mode], 0, Math.PI * 2);
const c = 42 * r;
ctx.fillStyle = make_colour_string(c, c, c, c);
ctx.fill();
}
}
function make_font_name(font_size) {
// return `${Math.round(font_size)}px 'Press Start 2P', monospace`;
return `${font_size}px 'Press Start 2P', monospace`;
}
function coin_accent_col() {
if(stage == 4) {
return '#ff0000';
} else if(stage == 7) {
return '#00f02c';
} else {
return '#fff700';
}
}
function coin_fill_col() {
if(stage == 4) {
return '#ff7575';
} else if(stage == 7) {
return '#97fcd1';
} else {
return '#f7c65c';
}
}
function is_fade_away_stage() {
return stage == 5 || stage == 6 || stage == 8 || stage == 10 || stage == 12;
}
function render_coin(x, y, eaten_t, translation_t) {
if(is_fade_away_stage()) return; // darn I should've designed this better
if(eaten_t >= 1) return;
ctx.save();
ctx.translate(lerp(x, player_x, translation_t), lerp(y, player_y, translation_t));
ctx.beginPath();
ctx.arc(0, 0, lerp(40, 0, eaten_t), 0, Math.PI * 2);
ctx.lineWidth = 8;
ctx.fillStyle = coin_fill_col();
ctx.strokeStyle = coin_accent_col();
ctx.fill();
ctx.stroke();
ctx.lineWidth = 2;
ctx.lineCap = 'round';
for(let i = 5.6; i < 32; i += 7) {
const offset = (elapsed_time * i) / 10;
ctx.beginPath();
ctx.arc(0, 0, lerp(i, 0, eaten_t), offset + Math.PI / 4, offset + 3 * Math.PI / 4, false);
ctx.stroke();
ctx.beginPath();
ctx.arc(0, 0, lerp(i, 0, eaten_t), offset + 5 * Math.PI / 4, offset + 7 * Math.PI / 4, false);
ctx.stroke();
}
ctx.restore();
}
function get_boss_alpha() {
if(elapsed_time - boss_last_damaged < 0.1) {
return 1;
}
if(stage == 9) {
if(elapsed_time - boss_last_action < 1) {
return lerp(0, 0.08, elapsed_time - boss_last_action);
} else {
return lerp(0.08, 0, (elapsed_time - boss_last_action - 1) / 4);
}
} else if(boss_phase == 1) {
if(elapsed_time - boss_last_action < 1) {
return lerp(0, 0.7, elapsed_time - boss_last_action);
} else {
return lerp(0.7, 0, (elapsed_time - boss_last_action - 1) / 5);
}
} else if(stage == 0) {
return 0.002;
} else if([4, 5, 6, 7, 8].includes(boss_phase)) {
if(elapsed_time - boss_last_action < 0.5) {
return lerp(0, 0.9, elapsed_time - boss_last_action);
} else {
return lerp(0.9, 0, (elapsed_time - boss_last_action - 0.5) / 6.5);
}
} else if(boss_phase == 10) {
return lerp(0.3, 0, (elapsed_time - boss_last_action) / 1);
} else {
return 0;
}
}
let boss_path = null;
function render_boss() {
ctx.save();
const boss_alpha = get_boss_alpha();
if(boss_alpha == 0) {
boss_path = null;
ctx.restore();
return;
}
ctx.globalAlpha = boss_alpha;
ctx.translate(boss_x_screen, boss_y + lerp(rlerp(-20, 20), 0, (elapsed_time - boss_last_damaged) / 2));
if(boss_phase == 1) {
ctx.rotate(Math.PI);
ctx.translate(boss_x_screen - 300, 0);
}
const head_path = new Path2D();
head_path.arc(120 * Math.sqrt(2) + 5, 0, 250, Math.PI * 3 / 4, Math.PI * 5 / 4, true);
ctx.fillStyle = '#b400eb';
ctx.fill(head_path);
boss_path = head_path;
// ctx.globalCompositeOperation = 'source-in';
ctx.beginPath();
ctx.moveTo(0, -180);
ctx.quadraticCurveTo(-40, 0, 0, 180);
for(let side = 0; side <= 1; side++) {
for(let t = side ? 1 : 0; side ? (t > 0) : (t < 1); t += side ? -0.02 : 0.02) {
const offset_mutliplier = Math.sin(t * Math.PI);
const offset = 7 * offset_mutliplier * Math.sin(t * 10 + 5 * elapsed_time);
const x = lerp(0, 90, t);
const y = lerp(side ? -180 : 180, 0, t);
ctx.lineTo(
x + offset, y + offset * 70 / 120
);
}
}
ctx.closePath();
ctx.fillStyle = '#FFF';
ctx.lineJoin = 'round';
ctx.strokeStyle = '#DDD';
ctx.lineWidth = 4;
ctx.fill();
// ctx.stroke();
ctx.clip();
ctx.fillStyle = '#329ba8';
ctx.beginPath();
const eye_up_down = Math.sin(elapsed_time);
ctx.arc(0, 30 * (eye_up_down > 0 ? 1 : -1) * Math.pow(Math.abs(eye_up_down), 0.7), 40, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
}
function render() {
ctx.save();
ctx.scale(actual_height / height, actual_height / height);
// Render stars
ctx.fillStyle = '#111';
ctx.fillRect(0, 0, width, height);
const hud_x = [0, 1].includes(stage) ? 0 : (stage == 2 ? lerp(0, HUD_WIDTH, (stage_elapsed - 0.2) * 4) : HUD_WIDTH);
ctx.translate(hud_x, 0);
render_stars();
// Render terrain (parallaxed)
for(let i = 0; i < 70; ++i) {
const radius = 1000;
ctx.beginPath();
const t_val = 1 - i / 70;
const parallax_movement = -1 * cam_x * Math.pow(1.4 * (1 - t_val), 2);
const x = ((i + 7) ** 5.1 + parallax_movement) % (2 * width) - width / 2;
const y_offset = lerp(80, 160, (i + 6) ** 5.5 % 1) + t_val * 60;
ctx.arc(x, height + radius - y_offset, radius, 0, Math.PI * 2);
const darkness_multiplier = lerp(1, 0.6, t_val);
ctx.fillStyle = make_colour_string(
darkness_multiplier * lerp(250, 250, t_val),
darkness_multiplier * lerp(90, 175, t_val),
darkness_multiplier * lerp(62, 62, t_val),
lerp(1, 1, t_val)
);
ctx.fill();
}
// Render obstacles + render lasers
ctx.save();
ctx.translate(-cam_x, 0);
ctx.save();
lasers.forEach(laser => {
ctx.beginPath();
ctx.moveTo(laser.x, laser.y);
for(let i = 0; i <= laser.w; i += 2) {
ctx.lineTo(laser.x + i, laser.y + 6 * Math.sin((laser.x + i) / laser.l));
}
ctx.lineWidth = 2;
ctx.strokeStyle = !laser.f ? '#bc42f5' : '#00ff26';
ctx.stroke();
});
if(is_fade_away_stage()) {
ctx.globalAlpha = lerp(1, 0, stage_elapsed * 0.8);
}
obstacles.forEach(o => {
o.r();
});
// Render obstacle hitboxes debug bounding
// obstacles.forEach(o => {
// ctx.strokeStyle = '#F00';
// ctx.lineWidth = 2;
// ctx.strokeRect(o.x, o.y, o.w, o.h);
// });
ctx.restore();
ctx.restore();
// render exhaust
exhaust_particles.forEach(particle => {
ctx.beginPath();
const time_since_particle = elapsed_time - particle.t;
ctx.arc(particle.x - cam_x, particle.y, lerp(4, 30, time_since_particle / 2), 0, Math.PI * 2);
ctx.fillStyle = make_colour_string(210, lerp(50, 120, time_since_particle), 0, 1);
ctx.fill();
});
// Render ship
if(stage != 0) {
ctx.save();
let explosion_t = (Math.sin(elapsed_time * Math.PI * 2 * 5) + 1) / 2 * lerp(1, 0, (elapsed_time - last_explosion_time) / 3);
ctx.fillStyle = make_colour_string(
lerp(255, 176, explosion_t),
lerp(255, 12, explosion_t),
lerp(255, 12, explosion_t),
1
);
ctx.strokeStyle = make_colour_string(
lerp(240, 176, explosion_t),
lerp(240, 12, explosion_t),
lerp(240, 12, explosion_t),
1
);
ctx.translate(
player_x - cam_x + get_explosion_offset(),
player_y + get_explosion_offset()
);
ctx.beginPath();
ctx.moveTo(...player_points[0]);
player_points.forEach((p, i) => {
if(i == 0) return;
ctx.lineTo(...p);
});
ctx.closePath();
ctx.lineJoin = 'round';
ctx.lineWidth = 5;
ctx.fill();
ctx.stroke();
if(laser_enabled) {
ctx.beginPath();
ctx.moveTo(30, 0);
ctx.lineTo(20, 20 / 3);
ctx.lineTo(20, -20 / 3);
ctx.closePath();
ctx.fillStyle = '#00ff26';
ctx.strokeStyle = '#00ff26';
ctx.fill();
ctx.stroke();
}
ctx.fillStyle = '#3BE';
ctx.fillRect(-6, -8, 10, 16);
if(grav_direction == 0) {
ctx.fillRect(-24, -8, 10, 16);
} else {
// Arrow
ctx.beginPath();
ctx.moveTo(-19, grav_direction * 8);
ctx.lineTo(-28, -grav_direction * 8);
ctx.lineTo(-10, -grav_direction * 8);
ctx.closePath();
ctx.fill();
}
ctx.fillRect(-42, -8, 10, 16);
ctx.restore();
}
// Render title
if(stage == 0) {
ctx.fillStyle = make_colour_string(255, 255, 255, lerp(0, 1, stage_elapsed / 1 - 1));
ctx.textAlign = 'center';
ctx.textBaseline = 'hanging';
ctx.font = make_font_name(68 + 16 * Math.sin(1.3 * elapsed_time - 1.3));
ctx.fillText("METAGRAV", width / 2, 50);
const body_offset = lerp(-150, 0, elapsed_time - 2) // 12 * Math.sin(1.3 * elapsed_time);
ctx.fillStyle = make_colour_string(255, 255, 255, lerp(0, 1, stage_elapsed / 1 - 2));
ctx.font = make_font_name(40);
ctx.fillText("Press [SPACE] to Start", width / 2, 300 + body_offset);
ctx.fillText(`AUDIO ${get_muted() ? 'MUTED' : 'ON'} (switch with [m])`, width / 2, 380 + body_offset);
ctx.fillText(`GRAPHICS ${['FANCY', 'NORMAL', 'BASIC'][graphics_mode]} (switch with [g])`, width / 2, 460 + body_offset);
ctx.fillText(`A game by Xavier Cooney for js13k-games`, width / 2, 620 + body_offset); // :]
}
ctx.save();
ctx.translate(-hud_x, 0);
render_boss();
ctx.restore();
// Render HUD
if(![0, 1].includes(stage) && hud_x > 0) {
ctx.fillStyle = '#111';
ctx.fillRect(-hud_x, 0, hud_x, height);
ctx.strokeStyle = '#EEE';
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(0, height);
ctx.lineWidth = 10;
ctx.stroke();
ctx.fillStyle = '#7d1111';
ctx.fillRect(-HUD_WIDTH * 2 / 3, 40, HUD_WIDTH / 3, 700);
const actual_height = 700 * displayed_player_health;
ctx.fillStyle = '#ff1919';
ctx.fillRect(-HUD_WIDTH * 2 / 3, 40 + 700 - actual_height, HUD_WIDTH / 3, actual_height);
if(coins_needed > 0) {
render_coin(-hud_x / 2, 860, 0, 0);
ctx.fillStyle = coin_fill_col();
ctx.font = make_font_name(20);
ctx.textAlign = 'center';
ctx.textBaseline = 'hanging';
ctx.fillText(`${coins_gotten}/${coins_needed}`, -hud_x / 2, 930)
}
}
ctx.translate(-hud_x, 0);
// Render dialogue
if(stages_with_dialogue_screen.includes(stage)) {
let dialogue_window_x = lerp(width + 10, width - dialogue_box_width - dialogue_box_margin, stage_elapsed + (is_fade_away_stage() ? -2 : 0));
ctx.fillStyle = '#DDD';
ctx.fillRect(dialogue_window_x, dialogue_box_margin, dialogue_box_width, height - 2 * dialogue_box_margin);
ctx.strokeStyle = '#888';
ctx.lineWidth = 10;
ctx.lineJoin = 'round';
ctx.strokeRect(dialogue_window_x, dialogue_box_margin, dialogue_box_width, height - 2 * dialogue_box_margin);
let dialogue_offset = 0;
dialogue_lines_done.forEach((line, line_num) => {
ctx.font = "30px 'Press Start 2P', monospace";
ctx.textAlign = 'left';
ctx.textBaseline = 'hanging';
ctx.fillStyle = '#000';
ctx.fillText(line, dialogue_window_x + 20, dialogue_box_margin + 20 + dialogue_offset);
const line_measuremnt = ctx.measureText(line + "|");
dialogue_offset += line_measuremnt.actualBoundingBoxDescent + line_measuremnt.actualBoundingBoxAscent + 9;
if(line_num == dialogue_lines_done.length - 1 && characters_to_add_to_line.length == 0 && dialogue_words_left.length > 0) {
const new_line_width = ctx.measureText(line + ' ' + dialogue_words_left[0]).width;
if(dialogue_words_left[0] == '\n') {
dialogue_words_left.shift();
dialogue_lines_done.push('');
} else if(new_line_width < dialogue_box_width - 40) {
if(dialogue_words_left[0] != '#') {
characters_to_add_to_line = (dialogue_words_left[0] + ' ').split('');
} else {
characters_to_add_to_line = ['#'];
}
dialogue_words_left.shift();
} else {
dialogue_lines_done.push('');
}
}
});
if(dialogue_done_running() && need_space_to_proceed) {
ctx.font = "italic 20px 'Press Start 2P', monospace";
ctx.textAlign = 'center';
ctx.textBaseline = 'bottom';
ctx.fillStyle = '#000';
ctx.fillText('[SPACE] to continue', dialogue_window_x + dialogue_box_width / 2, height - dialogue_box_margin - 20);
}
}
ctx.restore();
}
/* ======= Audio ======= */
/** @type {AudioContext} */
let audio_ctx;
let master_gain_node;
let dialogue_beep;
let dialogue_beep_gain;
function init_audio() {
if(!window.AudioContext === undefined) {
alert("Audio not supported for this browser. Try another browser for best experience");
}
audio_ctx = new AudioContext();
master_gain_node = audio_ctx.createGain();
master_gain_node.gain.value = window.localStorage['xav-space-js13k-mute'] ? 0 : 0.8;
master_gain_node.connect(audio_ctx.destination);
dialogue_beep = audio_ctx.createOscillator();
dialogue_beep.type = 'triangle';
dialogue_beep_gain = audio_ctx.createGain();
dialogue_beep_gain.gain.value = 0;
dialogue_beep.connect(dialogue_beep_gain).connect(master_gain_node);
dialogue_beep.start();
more_music();
}
function set_muted(muted) {
window.localStorage['xav-space-js13k-mute'] = muted ? "y" : "";
if(master_gain_node) master_gain_node.gain.value = muted ? 0 : 0.8;
}
function get_muted() {
return window.localStorage['xav-space-js13k-mute'] == 'y';
}
function do_audio_beep() {
const dialogue_beep_duration = 0.04;
dialogue_beep.frequency.cancelScheduledValues(audio_ctx.currentTime);
dialogue_beep.frequency.setValueAtTime(700, audio_ctx.currentTime);
dialogue_beep.frequency.linearRampToValueAtTime(100, audio_ctx.currentTime + dialogue_beep_duration);
dialogue_beep_gain.gain.cancelScheduledValues(audio_ctx.currentTime);
dialogue_beep_gain.gain.setValueAtTime(0.12, audio_ctx.currentTime);
dialogue_beep_gain.gain.linearRampToValueAtTime(0, audio_ctx.currentTime + dialogue_beep_duration);
}
function do_coin_collect_beep() {
const end_time = audio_ctx.currentTime + 0.3;
const node = audio_ctx.createOscillator();
node.type = 'square';
node.frequency.value = 440;
node.frequency.exponentialRampToValueAtTime(3520, end_time);
node.start(audio_ctx.currentTime);
node.stop(end_time);
const gain_node = audio_ctx.createGain();
gain_node.gain.setValueAtTime(1, audio_ctx.currentTime);
gain_node.gain.exponentialRampToValueAtTime(0.01, end_time);
node.connect(gain_node).connect(master_gain_node);
}
function do_laser_beep() {
const end_time = audio_ctx.currentTime + 0.3;
const node = audio_ctx.createOscillator();
node.type = 'sawtooth';
node.frequency.value = 880;
node.frequency.exponentialRampToValueAtTime(110, end_time);
node.start(audio_ctx.currentTime);
node.stop(end_time);
const gain_node = audio_ctx.createGain();
gain_node.gain.setValueAtTime(0.2, audio_ctx.currentTime);
gain_node.gain.exponentialRampToValueAtTime(0.01, end_time);
node.connect(gain_node).connect(master_gain_node);
}
let explosion_buffer = null;
function audio_sound_explosion() {
const duration = 1.5;
const buffer_size = audio_ctx.sampleRate * duration;
if(explosion_buffer === null) {
const buffer = audio_ctx.createBuffer(1, buffer_size, audio_ctx.sampleRate);
let data = buffer.getChannelData(0);
let phase = 0;
let last_sample = 0;
for (let i = 0; i < buffer_size; i++) {
const t = i / audio_ctx.sampleRate;
const frequency = 1200 * Math.pow(0.5, t);
phase += frequency / audio_ctx.sampleRate;
if(phase > 1) {
phase -= Math.floor(phase);
last_sample = rlerp(-1, 1);
}
data[i] = last_sample;
}
explosion_buffer = buffer;
}
let noise = audio_ctx.createBufferSource();
noise.buffer = explosion_buffer;
let bandpass = audio_ctx.createBiquadFilter();
bandpass.type = 'lowpass';
bandpass.frequency.value = 440;
let gain_node = audio_ctx.createGain();
gain_node.gain.setValueAtTime(4, audio_ctx.currentTime);
gain_node.gain.linearRampToValueAtTime(0, audio_ctx.currentTime + duration);
noise.connect(bandpass).connect(gain_node).connect(master_gain_node);
noise.start(audio_ctx.currentTime);
}
const base_note = 220;
const music_bpm = 70;
const channel_time_till = [0, 0];
function do_music_for_channel(channel) {
while(channel_time_till[channel] - audio_ctx.currentTime < 10) {
// This turned out not to be major pentatonic (missing a third??)
const notes_to_choose_from = [1, 1.5, 0.75, 1.125, 2 / 3];
const random_ratio = rand_element_form_arr(notes_to_choose_from);
const node = audio_ctx.createOscillator();
node.type = channel ? 'sine' : 'sawtooth';
node.frequency.value = random_ratio * base_note;
node.start(channel_time_till[channel]);
const duration = 60 * rand_element_form_arr([1, 1, 1, 1, 0.5, 2]) / music_bpm;
node.stop(channel_time_till[channel] + duration + 0.03);
const gain_node = audio_ctx.createGain();
gain_node.gain.setValueAtTime(channel ? 0.6 : 0.065, channel_time_till[channel]);
gain_node.gain.linearRampToValueAtTime(0.03, channel_time_till[channel] + duration);
node.connect(gain_node).connect(master_gain_node);
channel_time_till[channel] += duration;
}
}
function more_music() {
// Complete prototype at the moment, will change
do_music_for_channel(0);
do_music_for_channel(1);
setTimeout(() => more_music(), 100);
}
/* ======= Updating ======= */
let last_dialogue_character_added = 0;
function cause_explosion() {
last_explosion_time = elapsed_time;
audio_sound_explosion();
}
function add_dialogue(dialogue) {
if(dialogue_lines_done.length == 0) {
dialogue_lines_done = [''];
}
dialogue_words_left.push(...dialogue.split(' '));
}
function dialogue_newline() {
dialogue_words_left.push('\n');
}
function add_dialogue_nl(dialogue) {
add_dialogue(dialogue);
dialogue_newline();
}
function reset_y_pos(dt) {
player_y -= (player_y - height / 4) / 3 * dt;
player_y_velocity = 0;
grav_direction = 0;
}
function deal_damage(damage, reason) {
if(elapsed_time - last_explosion_time < 2 || (is_fade_away_stage() && stage != 10)) {
return;
}
if(stage == 2 || stages_with_dialogue_screen.includes(stage) || boss_phase == 10) {
// you can never die in stage 2 or when there's dialogue on screen
player_health *= (1 - damage);
player_health = Math.max(player_health, 0.05);
} else {
player_health -= damage;
player_health = Math.max(0, player_health);
if(player_health <= 0) {
player_died();
}
}
cause_explosion();
}
function do_injured_sound(f) {
const end_time = audio_ctx.currentTime + 5;
const node = audio_ctx.createOscillator();
node.type = 'sine';
node.frequency.value = 450 * f + 5 * Math.random();
node.frequency.exponentialRampToValueAtTime(430 * f + 3 * Math.random(), end_time);
node.start(audio_ctx.currentTime);
node.stop(end_time);
const gain_node = audio_ctx.createGain();
gain_node.gain.setValueAtTime(0.9, audio_ctx.currentTime);
gain_node.gain.exponentialRampToValueAtTime(0.0001, end_time);
node.connect(gain_node).connect(master_gain_node);
}
function boss_hurt() {
if(elapsed_time - boss_last_damaged < 3) {
return;
}
boss_last_damaged = elapsed_time;
boss_health -= 1 / 6;
if(boss_health <= 0) {
boss_health = 0;
boss_phase = 10;
boss_last_action = elapsed_time;
audio_sound_explosion();
}
// do_injured_sound(1 / Math.sqrt(2));
do_injured_sound(1);
do_injured_sound(Math.sqrt(2));
do_injured_sound(2);
do_injured_sound(2 * Math.sqrt(2));
// do_injured_sound(4);
}
let player_died_from_stage;
function player_died() {
player_died_from_stage = stage;
set_stage(5);
}
function make_plasma_obstacle(y_top, h) {
const x = obstacle_generation_x;
let last_laser_hit = -1;
function get_internal_colouring() {
const colour_offset = 40 * Math.sin(elapsed_time * 3 + x);
const green_t = (elapsed_time - last_laser_hit) / 3;
return make_colour_string(
lerp(89, 210 + colour_offset, green_t),
lerp(228, 54, green_t),
lerp(54, 228 + colour_offset, green_t),
1
);
}
function make_path_and_set_style() {
const path_2d = new Path2D();
path_2d.moveTo(x, y_top);
for(let y_offset = y_top; y_offset <= y_top + h; y_offset += [1, 2, 4][graphics_mode]) {
let wave_offset = 5 * offset_triangle_wave(y_offset / 100 + elapsed_time)
+ 1 * offset_triangle_wave(y_offset / 30 - elapsed_time * 3);
wave_offset *= 6;
wave_offset *= triangle_wave((y_offset - y_top) / h);
path_2d.lineTo(x + wave_offset, y_offset);
}
ctx.lineWidth = 12 + 4 * Math.cos((player_x - x) / 100);
ctx.strokeStyle = get_internal_colouring();
ctx.lineCap = 'round';
return path_2d;
}
return {
x: x - 100,
w: 200,
y: y_top - 100,
h: h + 200,
r: () => {
ctx.beginPath();
function render_circ(circ_y, r) {
ctx.beginPath();
ctx.arc(x, circ_y, r, 0, Math.PI * 2);
ctx.fill();
}
ctx.fillStyle = get_internal_colouring();
let circle_radius = lerp(16, 12, 0.5 + 0.5 * Math.cos((player_x - x) / 100));
render_circ(y_top, circle_radius);
render_circ(y_top + h, circle_radius);
const main_path = make_path_and_set_style();
ctx.stroke(main_path);
ctx.lineWidth = 3;
ctx.strokeStyle = '#3d36ff';
ctx.stroke(main_path);
},
i: () => {
const main_path = make_path_and_set_style();
for(let i = 0; i < player_points.length; ++i) {
if(ctx.isPointInStroke(main_path, player_points[i][0] + player_x, player_points[i][1] + player_y)) {
deal_damage(1 / 4, 'plasma');
}
};
},
lh: () => { // laser hit
last_laser_hit = elapsed_time;
},
lhy: y_top,
lhh: h,
lhx: x
};
}
function make_meteorite(x, y) {
const RADIUS = rlerp(15, 25);
const final_y = y;
const final_x = x;
let speed_y = rlerp(-0.1, 0.5);
if(Math.random() < 0.6) {
speed_y = 0;
}
speed_y = 0;
const speed_x = 0 // Math.max(0, rlerp(-0.3, 0.2));
function offset_function(theta) {
return 1.3 * (
Math.sin(5 * theta + elapsed_time * 0.5)
+ Math.sin(6 * theta - elapsed_time * 0.7)
+ 0.5 * Math.sin(11 * theta + elapsed_time * 10)
);
}
function make_path_and_set_style() {
const path = new Path2D();
path.moveTo(x + RADIUS + offset_function(0), y);
for(let theta = 0; theta <= Math.PI * 2; theta += [0.06, 0.08, 0.15][graphics_mode]) {
path.lineTo(
x + (RADIUS + offset_function(theta)) * Math.cos(theta),
y + (RADIUS + offset_function(theta)) * Math.sin(theta)
);
}
path.closePath();
ctx.lineWidth = 4;
return path;
}
obstacles.push({
x: x - 20,
y: y - 20,
w: 40,
h: 40,
r: () => {
ctx.strokeStyle = '#3d130a';
const path = make_path_and_set_style();
let gradient = ctx.createRadialGradient(x, y, 0, x , y, RADIUS);
gradient.addColorStop(0, '#66291c');
gradient.addColorStop(1, '#964230');
ctx.fillStyle = gradient;
ctx.fill(path);
ctx.stroke(path);
},
i: () => {
// TODO: combine this behaviour with the plasma field and and more points to the player ship
const main_path = make_path_and_set_style();
for(let i = 0; i < player_points.length; ++i) {
// if(ctx.isPointInStroke(main_path, player_points[i][0] + player_x, player_points[i][1] + player_y)) {
// return true;
// }
if(ctx.isPointInPath(main_path, player_points[i][0] + player_x, player_points[i][1] + player_y)) {
deal_damage(1 / 9, 'meteor');
}
};
},
u: () => {
y = final_y + (player_x - final_x) * speed_y;
x = final_x + (player_x - final_x) * speed_x;
}
})
}
function make_coin(x, y) {
if(Math.random() > 0.6) {
// There's no guarentees in life
return;
}
if(stage == 2 || stage == 11) return;
let time_eaten = Infinity;
let has_been_eaten = false;
obstacles.push({
x: x - 40,
w: 80,
y: y - 40,
h: 80,
r: () => {
const eaten_t = lerp((Math.sin(elapsed_time * 3) + 1) / 10, 1, (elapsed_time - time_eaten) / 0.4);
const translation_t = (elapsed_time - time_eaten) / 0.42;
render_coin(x, y, eaten_t, translation_t);
},
i: () => {
time_eaten = Math.min(time_eaten, elapsed_time);
if(!has_been_eaten) {
has_been_eaten = true;
coin_collected();
}
}
});
}
function generate_obstacle() {
const decision_main = Math.random();
const start_of_obstacle_x = obstacle_generation_x;
if(decision_main < 0.1) {
// centre plasma obstacle (separator)
obstacles.push(make_plasma_obstacle(rlerp(0, 450), rlerp(250, 350)));
obstacle_generation_x += 700;
} else if(decision_main < 0.5) {
// plasma field
const end_of_obstacle_x = start_of_obstacle_x + (stage == 2 ? 1000 : rlerp(1800, 3200));
while(obstacle_generation_x <= end_of_obstacle_x) {
// 'Pillars of doom'
const sub_decision = Math.random();
if(sub_decision < 0.2) {
// Middle
obstacles.push(make_plasma_obstacle(250, 400));
make_coin(obstacle_generation_x + 300, 450)
obstacle_generation_x += rlerp(650, 800);
} else if(sub_decision < 0.4) {
// Vertical
obstacles.push(make_plasma_obstacle(-100, 650));
make_coin(obstacle_generation_x + 300, 300)
obstacle_generation_x += rlerp(700, 800);
} else if(sub_decision < 0.6) {
obstacles.push(make_plasma_obstacle(250, 1750));
make_coin(obstacle_generation_x + 300, 600)
obstacle_generation_x += rlerp(700, 800);
}