-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
1026 lines (902 loc) · 28.3 KB
/
Copy pathgame.js
File metadata and controls
1026 lines (902 loc) · 28.3 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
/*
TVA GLITCH — HTML5 Canvas Graphics API Game
by Janny Jonyo. Inspired by Marvel's Loki series.
[APPLICATION]: Game logic, input, physics, collision
[GEOMETRY]: Transforms (translate, rotate, scale) on all objects
[RASTERISATION]: Pixel-level drawing: transparency, layering, effects
*/
// Canvas Setup
const bgCanvas = document.getElementById("canvas-bg");
const gameCanvas = document.getElementById("canvas-game");
const fxCanvas = document.getElementById("canvas-fx");
const bgCtx = bgCanvas.getContext("2d");
const gCtx = gameCanvas.getContext("2d");
const fxCtx = fxCanvas.getContext("2d");
let W = 800, H = 550;
function resizeCanvas() {
const wrap = document.getElementById("canvas-wrap");
const rect = wrap.getBoundingClientRect();
W = Math.round(rect.width);
H = Math.round(rect.height);
[bgCanvas, gameCanvas, fxCanvas].forEach(c => {
c.width = W;
c.height = H;
c.style.width = "100%";
c.style.height = "100%";
});
drawBackground();
}
globalThis.addEventListener("resize", resizeCanvas);
globalThis.addEventListener("orientationchange", resizeCanvas);
document.addEventListener("DOMContentLoaded", resizeCanvas);
// Audio Engine (Web Audio API)
let audioCtx = null,
muted = false;
function initAudio() {
if (audioCtx) return;
const AudioCtx = globalThis.AudioContext || globalThis.webkitAudioContext;
audioCtx = new AudioCtx();
}
function playTone(freq, type, duration, volume = 0.15, startTime = 0) {
if (muted || !audioCtx) return;
const osc = audioCtx.createOscillator();
const gain = audioCtx.createGain();
osc.connect(gain);
gain.connect(audioCtx.destination);
osc.type = type;
osc.frequency.setValueAtTime(freq, audioCtx.currentTime + startTime);
gain.gain.setValueAtTime(volume, audioCtx.currentTime + startTime);
gain.gain.exponentialRampToValueAtTime(
0.001,
audioCtx.currentTime + startTime + duration,
);
osc.start(audioCtx.currentTime + startTime);
osc.stop(audioCtx.currentTime + startTime + duration + 0.01);
}
function playCollect() {
// Ascending TVA-theme arpeggio
playTone(440, "sine", 0.1, 0.2, 0);
playTone(554, "sine", 0.1, 0.2, 0.08);
playTone(659, "sine", 0.15, 0.25, 0.16);
}
function playPrune() {
playTone(200, "sawtooth", 0.4, 0.3, 0);
playTone(150, "sawtooth", 0.4, 0.3, 0.1);
playTone(100, "square", 0.5, 0.3, 0.2);
}
function playWave() {
[523, 659, 784, 1047].forEach((f, i) =>
playTone(f, "triangle", 0.2, 0.2, i * 0.1),
);
}
function playTick() {
playTone(1200, "square", 0.03, 0.08);
}
// Game State
// [APPLICATION STAGE] — All state managed here before geometry/rasterisation
const state = {
phase: "start", // 'start' | 'playing' | 'paused' | 'over'
score: 0,
lives: 3,
wave: 1,
crystalsCaught: 0,
crystalsPerWave: 5,
frameCount: 0,
tickTimer: 0,
glitchTimer: 0,
shakeX: 0,
shakeY: 0,
};
// Input
const keys = {};
document.addEventListener("keydown", (e) => {
keys[e.key] = true;
if (e.key === "p" || e.key === "P") togglePause();
if (e.key === "m" || e.key === "M") muted = !muted;
e.preventDefault();
});
document.addEventListener("keyup", (e) => {
keys[e.key] = false;
});
function bindTouchControls() {
const movementButtons = document.querySelectorAll("#touch-controls .tc-btn[data-key]");
movementButtons.forEach((btn) => {
const key = btn.dataset.key;
if (!key) return;
let activePointerId = null;
const press = (e) => {
e.preventDefault();
activePointerId = e.pointerId;
btn.setPointerCapture(e.pointerId);
keys[key] = true;
btn.classList.add("is-pressed");
};
const release = (e) => {
e.preventDefault();
if (activePointerId !== null && e.pointerId !== activePointerId) return;
activePointerId = null;
keys[key] = false;
btn.classList.remove("is-pressed");
};
btn.addEventListener("pointerdown", press);
btn.addEventListener("pointerup", release);
btn.addEventListener("pointercancel", release);
});
}
// Variant (Player)
// Object 1: The Variant — geometric object with movement transforms
const variant = {
x: 400,
y: 275,
vx: 0,
vy: 0,
speed: 3.2,
radius: 14,
angle: 0, // [GEOMETRY] rotation angle, updated each frame
trail: [], // [RASTERISATION] motion trail for layering effect
invincible: 0, // frames of invincibility after being hit
};
// Ms. Minutes
// Object 2: Ms. Minutes — clock-face enemy with rotation & pursuit AI
const msMins = {
x: 100,
y: 100,
vx: 1.2,
vy: 0.8,
speed: 1.2,
radius: 26,
angle: 0, // [GEOMETRY] rotates her clock hands over time
handAngle: 0, // inner clock hand rotation
pulseScale: 1.0, // [GEOMETRY] scale pulsing
pulseDir: 1,
warningFlash: 0, // [RASTERISATION] red flash when close to player
corruptAura: 0, // aura radius growth for rasterisation stage
};
// Time Crystals
// Object 3: Collectibles — rotating, pulsing hexagons with alpha layers
let crystals = [];
function spawnCrystal() {
let cx, cy;
do {
cx = 60 + Math.random() * (W - 120);
cy = 60 + Math.random() * (H - 120);
} while (dist(cx, cy, variant.x, variant.y) < 100);
crystals.push({
x: cx,
y: cy,
angle: Math.random() * Math.PI * 2, // [GEOMETRY] starting rotation
scale: 0, // starts at 0, animates in [GEOMETRY] scale transform
scaleTarget: 1,
pulseT: Math.random() * Math.PI * 2, // phase offset for pulsing
alpha: 0, // [RASTERISATION] fades in
collected: false,
collectAnim: 0,
});
}
// TVA Drones
// Object 4: Triangle-shaped TVA drones that patrol and appear in later waves
let drones = [];
function spawnDrone() {
const side = Math.floor(Math.random() * 4);
let dx, dy;
if (side === 0) {
dx = Math.random() * W;
dy = -20;
} else if (side === 1) {
dx = W + 20;
dy = Math.random() * H;
} else if (side === 2) {
dx = Math.random() * W;
dy = H + 20;
} else {
dx = -20;
dy = Math.random() * H;
}
drones.push({
x: dx,
y: dy,
angle: 0, // [GEOMETRY] heading angle, rotated toward player
speed: 0.8 + state.wave * 0.15,
radius: 12,
alpha: 0,
});
}
// Particles
// [RASTERISATION] — particle system for visual effects
let particles = [];
function spawnParticles(x, y, color, count = 12, speed = 3) {
for (let i = 0; i < count; i++) {
const a = (i / count) * Math.PI * 2;
particles.push({
x,
y,
vx: Math.cos(a) * speed * (0.5 + Math.random()),
vy: Math.sin(a) * speed * (0.5 + Math.random()),
radius: 2 + Math.random() * 3,
alpha: 1,
color,
decay: 0.03 + Math.random() * 0.02,
});
}
}
// Glitch Strips
// [RASTERISATION] — horizontal scan-line glitch effect strips
let glitchStrips = [];
function triggerGlitch(intensity = 1) {
state.glitchTimer = 30 * intensity;
state.shakeX = (Math.random() - 0.5) * 8 * intensity;
state.shakeY = (Math.random() - 0.5) * 8 * intensity;
for (let i = 0; i < 5 * intensity; i++) {
glitchStrips.push({
y: Math.random() * H,
h: 2 + Math.random() * 12,
offsetX: (Math.random() - 0.5) * 40,
life: 8 + Math.random() * 12,
alpha: 0.4 + Math.random() * 0.4,
});
}
}
// Utility
function dist(ax, ay, bx, by) {
return Math.hypot(ax - bx, ay - by);
}
function clamp(v, lo, hi) {
return Math.max(lo, Math.min(hi, v));
}
function pad(n, len = 4) {
return String(Math.floor(n)).padStart(len, "0");
}
// Background: Grid Draw
// [APPLICATION STAGE] — set up the static grid once; redraw on wave change
function drawBackground() {
bgCtx.clearRect(0, 0, W, H);
// Deep TVA golden background
bgCtx.fillStyle = "#0a0800";
bgCtx.fillRect(0, 0, W, H);
// [RASTERISATION] — layered grid: major and minor lines with transparency
const gridSize = 40;
bgCtx.strokeStyle = "rgba(245, 200, 66, 0.07)";
bgCtx.lineWidth = 0.5;
for (let x = 0; x <= W; x += gridSize) {
bgCtx.beginPath();
bgCtx.moveTo(x, 0);
bgCtx.lineTo(x, H);
bgCtx.stroke();
}
for (let y = 0; y <= H; y += gridSize) {
bgCtx.beginPath();
bgCtx.moveTo(0, y);
bgCtx.lineTo(W, y);
bgCtx.stroke();
}
// Minor grid
bgCtx.strokeStyle = "rgba(245, 200, 66, 0.03)";
for (let x = 0; x <= W; x += gridSize / 2) {
bgCtx.beginPath();
bgCtx.moveTo(x, 0);
bgCtx.lineTo(x, H);
bgCtx.stroke();
}
for (let y = 0; y <= H; y += gridSize / 2) {
bgCtx.beginPath();
bgCtx.moveTo(0, y);
bgCtx.lineTo(W, y);
bgCtx.stroke();
}
// Corner brackets (TVA aesthetic)
bgCtx.strokeStyle = "rgba(245,200,66,0.25)";
bgCtx.lineWidth = 1;
const bk = 20,
bpad = 10;
[
[bpad, bpad],
[W - bpad, bpad],
[bpad, H - bpad],
[W - bpad, H - bpad],
].forEach(([cx, cy]) => {
const sx = cx === bpad ? 1 : -1;
const sy = cy === bpad ? 1 : -1;
bgCtx.beginPath();
bgCtx.moveTo(cx + sx * bk, cy);
bgCtx.lineTo(cx, cy);
bgCtx.lineTo(cx, cy + sy * bk);
bgCtx.stroke();
});
}
// Draw Ms. Minutes
// Object 2: Complex clock-face object demonstrating:
// [GEOMETRY] — translate + rotate transforms, scale pulsing
// [RASTERISATION] — transparency, layered circles, clock hands, glow
function drawMsMinutes(ctx) {
const m = msMins;
ctx.save();
// [GEOMETRY] — Translate to Ms. Minutes' position, then apply scale transform
ctx.translate(m.x, m.y);
ctx.scale(m.pulseScale, m.pulseScale); // [GEOMETRY] scale transformation
const R = m.radius;
const closeToPlayer = dist(m.x, m.y, variant.x, variant.y) < 120;
// [RASTERISATION] — Outer corrupt aura (semi-transparent, layered)
if (m.corruptAura > 0) {
const grad = ctx.createRadialGradient(0, 0, R, 0, 0, R + m.corruptAura);
grad.addColorStop(0, "rgba(200, 50, 50, 0.25)");
grad.addColorStop(1, "rgba(200, 50, 50, 0)");
ctx.fillStyle = grad;
ctx.beginPath();
ctx.arc(0, 0, R + m.corruptAura, 0, Math.PI * 2);
ctx.fill();
}
// [RASTERISATION] — Clock body (layered: outer ring → face → details)
// Outer ring
ctx.beginPath();
ctx.arc(0, 0, R, 0, Math.PI * 2);
ctx.fillStyle = closeToPlayer
? "rgba(80, 10, 10, 0.95)"
: "rgba(30, 20, 5, 0.95)";
ctx.fill();
ctx.strokeStyle =
m.warningFlash > 0
? `rgba(255, 80, 80, ${0.5 + 0.5 * Math.sin(m.warningFlash * 0.5)})`
: "rgba(245, 200, 66, 0.8)";
ctx.lineWidth = 2;
ctx.stroke();
// Inner face ring
ctx.beginPath();
ctx.arc(0, 0, R - 4, 0, Math.PI * 2);
ctx.strokeStyle = "rgba(245, 200, 66, 0.2)";
ctx.lineWidth = 1;
ctx.stroke();
// Clock tick marks (12 marks around the face)
ctx.strokeStyle = "rgba(245, 200, 66, 0.6)";
ctx.lineWidth = 1;
for (let i = 0; i < 12; i++) {
const a = (i / 12) * Math.PI * 2;
const inner = i % 3 === 0 ? R - 9 : R - 7;
ctx.beginPath();
ctx.moveTo(Math.cos(a) * inner, Math.sin(a) * inner);
ctx.lineTo(Math.cos(a) * (R - 4), Math.sin(a) * (R - 4));
ctx.stroke();
}
// [GEOMETRY] — Rotate the clock hands (inner rotation transform applied here)
ctx.rotate(m.angle); // [GEOMETRY] continuous rotation
// Hour hand
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(0, -(R - 10));
ctx.strokeStyle = "rgba(245, 200, 66, 0.9)";
ctx.lineWidth = 2.5;
ctx.lineCap = "round";
ctx.stroke();
// Minute hand (rotates faster)
ctx.rotate(m.angle * 4); // [GEOMETRY] compound rotation
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(0, -(R - 6));
ctx.strokeStyle = "rgba(255, 100, 100, 0.85)";
ctx.lineWidth = 1.5;
ctx.stroke();
// Center pin
ctx.beginPath();
ctx.arc(0, 0, 3, 0, Math.PI * 2);
ctx.fillStyle = "#f5c842";
ctx.fill();
// [RASTERISATION] — Eyes drawn on top face (emoji-like, layered)
ctx.rotate(-m.angle * 5); // undo rotation for eyes to stay upright
ctx.fillStyle = "rgba(245, 200, 66, 0.9)";
// Left eye
ctx.beginPath();
ctx.arc(-7, -5, 3, 0, Math.PI * 2);
ctx.fill();
// Right eye
ctx.beginPath();
ctx.arc(7, -5, 3, 0, Math.PI * 2);
ctx.fill();
// Pupils (tracking direction toward player for character expression)
ctx.fillStyle = "#0a0800";
const eyeDirX = variant.x - m.x,
eyeDirY = variant.y - m.y;
const eyeDist = Math.hypot(eyeDirX, eyeDirY) || 1;
const pupilOff = 1.2;
ctx.beginPath();
ctx.arc(
-7 + (eyeDirX / eyeDist) * pupilOff,
-5 + (eyeDirY / eyeDist) * pupilOff,
1.5,
0,
Math.PI * 2,
);
ctx.fill();
ctx.beginPath();
ctx.arc(
7 + (eyeDirX / eyeDist) * pupilOff,
-5 + (eyeDirY / eyeDist) * pupilOff,
1.5,
0,
Math.PI * 2,
);
ctx.fill();
// Smile/grimace based on proximity to player
ctx.strokeStyle = "rgba(245, 200, 66, 0.85)";
ctx.lineWidth = 1.5;
ctx.lineCap = "round";
ctx.beginPath();
if (closeToPlayer) {
// Grimace (bared teeth look)
ctx.moveTo(-7, 6);
ctx.lineTo(7, 6);
} else {
// Smile
ctx.arc(0, 4, 7, 0.1 * Math.PI, 0.9 * Math.PI);
}
ctx.stroke();
ctx.restore();
}
// Draw Variant (Player)
// Object 1: Player character — geometric arrow shape
// [GEOMETRY] — translate + rotate based on movement direction
// [RASTERISATION] — motion trail with decreasing alpha, layered glow
function drawVariant(ctx) {
const v = variant;
if (v.invincible > 0 && Math.floor(v.invincible / 4) % 2 === 0) return; // blink
// [RASTERISATION] — Motion trail: each past position drawn with reduced alpha (layering)
v.trail.forEach((t, i) => {
const alpha = (i / v.trail.length) * 0.25; // [RASTERISATION] transparency gradient
ctx.save();
ctx.translate(t.x, t.y);
ctx.rotate(t.angle); // [GEOMETRY] each trail segment preserves rotation
ctx.globalAlpha = alpha;
ctx.fillStyle = "#4ff7b0";
ctx.beginPath();
// Arrow shape pointing up
ctx.moveTo(0, -10);
ctx.lineTo(-7, 8);
ctx.lineTo(0, 4);
ctx.lineTo(7, 8);
ctx.closePath();
ctx.fill();
ctx.restore();
});
ctx.save();
// [GEOMETRY] — Core geometric transform: translate to position, rotate to heading
ctx.translate(v.x, v.y);
ctx.rotate(v.angle); // [GEOMETRY] rotation based on movement direction
// [RASTERISATION] — Outer glow (semi-transparent layer under main shape)
ctx.beginPath();
ctx.moveTo(0, -14);
ctx.lineTo(-10, 11);
ctx.lineTo(0, 6);
ctx.lineTo(10, 11);
ctx.closePath();
ctx.shadowBlur = 16;
ctx.shadowColor = "#4ff7b0";
ctx.fillStyle = "rgba(79, 247, 176, 0.25)"; // [RASTERISATION] transparent glow layer
ctx.fill();
ctx.shadowBlur = 0;
// [RASTERISATION] — Main shape (opaque, on top)
ctx.beginPath();
ctx.moveTo(0, -12);
ctx.lineTo(-8, 10);
ctx.lineTo(0, 5);
ctx.lineTo(8, 10);
ctx.closePath();
ctx.fillStyle = "#4ff7b0";
ctx.fill();
ctx.strokeStyle = "rgba(255,255,255,0.6)";
ctx.lineWidth = 1;
ctx.stroke();
// TVA insignia — small inner diamond
ctx.beginPath();
ctx.moveTo(0, -2);
ctx.lineTo(-3, 3);
ctx.lineTo(0, 1);
ctx.lineTo(3, 3);
ctx.closePath();
ctx.fillStyle = "rgba(10, 8, 0, 0.6)";
ctx.fill();
ctx.restore();
}
// Draw Time Crystal
// Object 3: Collectible crystals — hexagons with rotation and alpha
// [GEOMETRY] — continuous rotation + scale animation
// [RASTERISATION] — layered concentric hexagons, pulsing alpha, inner glow
function drawCrystal(ctx, c) {
if (c.collected && c.collectAnim <= 0) return;
ctx.save();
// [GEOMETRY] — Translate to crystal position
ctx.translate(c.x, c.y);
// [GEOMETRY] — Apply scale transform (spawn animation and pulse)
const pulse = 1 + 0.08 * Math.sin(c.pulseT);
ctx.scale(c.scale * pulse, c.scale * pulse); // [GEOMETRY] scale transform
// [GEOMETRY] — Apply rotation transform
ctx.rotate(c.angle); // [GEOMETRY] rotation
const hexPath = (r) => {
ctx.beginPath();
for (let i = 0; i < 6; i++) {
const a = (i / 6) * Math.PI * 2 - Math.PI / 6;
i === 0
? ctx.moveTo(Math.cos(a) * r, Math.sin(a) * r)
: ctx.lineTo(Math.cos(a) * r, Math.sin(a) * r);
}
ctx.closePath();
};
if (c.collected) {
// [RASTERISATION] — Burst expand animation on collect
ctx.globalAlpha = c.collectAnim * c.alpha;
hexPath(18 * (1 - c.collectAnim / 1.5));
ctx.strokeStyle = "#4ff7b0";
ctx.lineWidth = 2;
ctx.stroke();
} else {
// [RASTERISATION] — Layered concentric hexagons with decreasing alpha (transparency layers)
ctx.globalAlpha = c.alpha * 0.15;
hexPath(22);
ctx.fillStyle = "#4ff7b0";
ctx.fill();
ctx.globalAlpha = c.alpha * 0.25;
hexPath(17);
ctx.fillStyle = "#4ff7b0";
ctx.fill();
ctx.globalAlpha = c.alpha * 0.8;
hexPath(13);
ctx.fillStyle = "rgba(10,8,0,0.7)";
ctx.fill();
ctx.strokeStyle = "#4ff7b0";
ctx.lineWidth = 1.5;
ctx.stroke();
// [RASTERISATION] — Inner glowing core with alpha
ctx.globalAlpha = c.alpha * (0.6 + 0.4 * Math.sin(c.pulseT * 2));
ctx.fillStyle = "#ffffff";
ctx.beginPath();
ctx.arc(0, 0, 3, 0, Math.PI * 2);
ctx.fill();
}
ctx.restore();
}
// Draw TVA Drone
// Object 4: TVA drones — triangles that rotate to face player
// [GEOMETRY] — rotate to face movement direction
// [RASTERISATION] — semi-transparent, fade in on spawn
function drawDrone(ctx, d) {
ctx.save();
ctx.translate(d.x, d.y);
ctx.rotate(d.angle); // [GEOMETRY] rotation transform to face player
ctx.globalAlpha = d.alpha; // [RASTERISATION] transparency for spawn fade
// Triangle drone shape
ctx.beginPath();
ctx.moveTo(0, -d.radius);
ctx.lineTo(-d.radius * 0.7, d.radius * 0.6);
ctx.lineTo(d.radius * 0.7, d.radius * 0.6);
ctx.closePath();
ctx.fillStyle = "rgba(180, 80, 20, 0.7)"; // [RASTERISATION] semi-transparent fill
ctx.fill();
ctx.strokeStyle = "#f5c842";
ctx.lineWidth = 1;
ctx.stroke();
// TVA label
ctx.rotate(-d.angle); // undo rotation for text
ctx.fillStyle = "rgba(245,200,66,0.9)";
ctx.font = "bold 6px Courier New";
ctx.textAlign = "center";
ctx.fillText("TVA", 0, 2);
ctx.restore();
}
// Draw Particles
// [RASTERISATION] — pure pixel-level visual effects, all transparency-based
function drawParticles(ctx) {
particles.forEach((p) => {
ctx.save();
ctx.globalAlpha = p.alpha; // [RASTERISATION] alpha for each particle
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
ctx.fillStyle = p.color;
ctx.fill();
ctx.restore();
});
}
// Glitch Rasterisation Effect
// [RASTERISATION] — horizontal scan-line strips offset for CRT glitch look
function drawGlitch(ctx) {
if (state.glitchTimer <= 0) return;
glitchStrips.forEach((strip) => {
ctx.save();
ctx.globalAlpha = strip.alpha * (strip.life / 20);
// [RASTERISATION] — Sample and shift a horizontal band of pixels
try {
const imgData = gameCanvas
.getContext("2d")
.getImageData(0, strip.y, W, strip.h);
ctx.putImageData(imgData, strip.offsetX, strip.y);
} catch (e) {}
ctx.restore();
});
// Scanline overlay
ctx.save();
ctx.globalAlpha = 0.04 * (state.glitchTimer / 30); // [RASTERISATION] overlay transparency
for (let y = 0; y < H; y += 3) {
ctx.fillStyle = "#000000";
ctx.fillRect(0, y, W, 1);
}
ctx.restore();
}
// Application Stage Update Loop
// [APPLICATION STAGE] — all game logic, physics, collision before drawing
function update() {
if (state.phase !== "playing") return;
state.frameCount++;
// Player Input & Velocity
// [APPLICATION STAGE] — input processing
let dx = 0,
dy = 0;
if (keys["ArrowLeft"] || keys["a"] || keys["A"]) dx -= 1;
if (keys["ArrowRight"] || keys["d"] || keys["D"]) dx += 1;
if (keys["ArrowUp"] || keys["w"] || keys["W"]) dy -= 1;
if (keys["ArrowDown"] || keys["s"] || keys["S"]) dy += 1;
if (dx !== 0 || dy !== 0) {
const mag = Math.hypot(dx, dy);
variant.vx = (dx / mag) * variant.speed;
variant.vy = (dy / mag) * variant.speed;
// [GEOMETRY] — Compute rotation angle from velocity vector
variant.angle = Math.atan2(variant.vy, variant.vx) + Math.PI / 2;
} else {
variant.vx *= 0.85; // deceleration
variant.vy *= 0.85;
}
// Apply velocity and clamp to canvas bounds
variant.x = clamp(variant.x + variant.vx, 20, W - 20);
variant.y = clamp(variant.y + variant.vy, 20, H - 20);
// [RASTERISATION] — Update motion trail (history of past positions)
variant.trail.push({ x: variant.x, y: variant.y, angle: variant.angle });
if (variant.trail.length > 12) variant.trail.shift();
if (variant.invincible > 0) variant.invincible--;
// Ms. Minutes AI & Geometry Update
// [GEOMETRY] — Update her position (translation), rotation angle
// [APPLICATION] — pursuit AI: steer toward player
const toVarX = variant.x - msMins.x;
const toVarY = variant.y - msMins.y;
const toVarDist = Math.hypot(toVarX, toVarY) || 1;
msMins.vx += (toVarX / toVarDist) * 0.08 * (1 + state.wave * 0.1);
msMins.vy += (toVarY / toVarDist) * 0.08 * (1 + state.wave * 0.1);
// Speed cap for Ms. Minutes (increases with wave)
const msSpeed = msMins.speed + state.wave * 0.18;
const msCurSpeed = Math.hypot(msMins.vx, msMins.vy);
if (msCurSpeed > msSpeed) {
msMins.vx = (msMins.vx / msCurSpeed) * msSpeed;
msMins.vy = (msMins.vy / msCurSpeed) * msSpeed;
}
msMins.x = clamp(msMins.x + msMins.vx, 30, W - 30);
msMins.y = clamp(msMins.y + msMins.vy, 30, H - 30);
// [GEOMETRY] — Rotate clock hands continuously
msMins.angle += 0.025 + state.wave * 0.005;
// [GEOMETRY] — Pulse scale animation
msMins.pulseScale += 0.006 * msMins.pulseDir;
if (msMins.pulseScale > 1.08 || msMins.pulseScale < 0.94)
msMins.pulseDir *= -1;
// [RASTERISATION] — Corrupt aura grows when close to player
const msDist = dist(msMins.x, msMins.y, variant.x, variant.y);
msMins.corruptAura = clamp(80 - msDist * 0.5, 0, 40);
msMins.warningFlash = msDist < 100 ? msMins.warningFlash + 1 : 0;
// Crystal Updates
crystals.forEach((c) => {
if (!c.collected) {
// [GEOMETRY] — Rotate crystal each frame
c.angle += 0.018;
c.pulseT += 0.06;
// [GEOMETRY] — Scale animation (spawn in)
if (c.scale < 1) c.scale = Math.min(1, c.scale + 0.06);
// [RASTERISATION] — Alpha fade in
c.alpha = Math.min(1, c.alpha + 0.04);
// [APPLICATION] — Collision detection
if (dist(c.x, c.y, variant.x, variant.y) < variant.radius + 13) {
c.collected = true;
c.collectAnim = 1.5;
state.score += 100 + state.wave * 50;
state.crystalsCaught++;
playCollect();
spawnParticles(c.x, c.y, "#4ff7b0", 16, 4);
updateHUD();
}
} else {
// Collect burst animation
c.collectAnim = Math.max(0, c.collectAnim - 0.08);
}
});
// Advance wave when enough crystals collected
if (state.crystalsCaught >= state.crystalsPerWave) {
advanceWave();
}
// Spawn a new crystal if none remain uncollected
if (crystals.filter((c) => !c.collected).length === 0) {
spawnCrystal();
spawnCrystal();
}
// Drone Updates
drones.forEach((d, i) => {
// [GEOMETRY] — Rotate drone to face variant
const toDx = variant.x - d.x;
const toDy = variant.y - d.y;
d.angle = Math.atan2(toDy, toDx) + Math.PI / 2; // [GEOMETRY] heading angle
d.x += (toDx / (Math.hypot(toDx, toDy) || 1)) * d.speed;
d.y += (toDy / (Math.hypot(toDx, toDy) || 1)) * d.speed;
d.alpha = Math.min(1, d.alpha + 0.02); // [RASTERISATION] fade in
if (
variant.invincible === 0 &&
dist(d.x, d.y, variant.x, variant.y) < variant.radius + d.radius
) {
takeDamage();
drones.splice(i, 1);
}
});
// Drone spawn (wave 2+, periodic)
if (
state.wave >= 2 &&
state.frameCount % Math.max(180, 360 - state.wave * 30) === 0
) {
spawnDrone();
}
// Ms. Minutes Collision
// [APPLICATION STAGE] — collision detection
if (variant.invincible === 0 && msDist < variant.radius + msMins.radius - 4) {
takeDamage();
}
// Particles & Glitch
// [RASTERISATION] — update particle physics
particles.forEach((p) => {
p.x += p.vx;
p.y += p.vy;
p.vx *= 0.96;
p.vy *= 0.96;
p.radius *= 0.97;
p.alpha -= p.decay;
});
particles = particles.filter((p) => p.alpha > 0.01);
glitchStrips.forEach((g) => {
g.life--;
});
glitchStrips = glitchStrips.filter((g) => g.life > 0);
if (state.glitchTimer > 0) state.glitchTimer--;
state.shakeX *= 0.8;
state.shakeY *= 0.8;
// Clock tick audio
state.tickTimer++;
if (state.tickTimer % 30 === 0) playTick();
// Ambient glitch on timeline instability
const integrity = Math.max(0, 100 - state.wave * 8);
document.getElementById("integrity").textContent = integrity + "%";
if (state.frameCount % Math.max(60, 200 - state.wave * 15) === 0) {
triggerGlitch(0.5 + state.wave * 0.1);
}
}
function takeDamage() {
if (variant.invincible > 0) return;
state.lives--;
variant.invincible = 90;
playPrune();
triggerGlitch(2);
spawnParticles(variant.x, variant.y, "#ff4444", 20, 5);
updateHUD();
if (state.lives <= 0) endGame();
}
function advanceWave() {
state.wave++;
state.crystalsCaught = 0;
state.crystalsPerWave = 5 + state.wave;
crystals = [];
drones = [];
playWave();
triggerGlitch(3);
for (let i = 0; i < 2 + state.wave; i++) spawnCrystal();
state.score += state.wave * 200;
updateHUD();
document.getElementById("wave-display").textContent =
`WAVE ${pad(state.wave, 2)}`;
// Ms. Minutes gets faster each wave
msMins.speed = 1.2 + (state.wave - 1) * 0.2;
}
// Main Render Loop
// [GEOMETRY] — Global canvas-shake transform applied each frame
// [RASTERISATION] — Composite all layers
function render() {
// Clear game canvas
gCtx.clearRect(0, 0, W, H);
fxCtx.clearRect(0, 0, W, H);
// [GEOMETRY] — Canvas shake: a global translation applied to the game layer
gCtx.save();
gCtx.translate(state.shakeX, state.shakeY); // [GEOMETRY] camera shake transform
// Draw all game objects
crystals.forEach((c) => drawCrystal(gCtx, c));
drones.forEach((d) => drawDrone(gCtx, d));
drawVariant(gCtx);
drawMsMinutes(gCtx);
drawParticles(gCtx);
gCtx.restore();
// [RASTERISATION] — FX layer: glitch strips on top of everything
drawGlitch(fxCtx);
// [RASTERISATION] — Vignette: radial gradient overlay for cinematic depth
const vig = fxCtx.createRadialGradient(
W / 2,
H / 2,
H * 0.3,
W / 2,
H / 2,
H * 0.9,
);
vig.addColorStop(0, "rgba(0,0,0,0)");
vig.addColorStop(1, "rgba(0,0,0,0.45)");
fxCtx.fillStyle = vig;
fxCtx.fillRect(0, 0, W, H);
}
// HUD Update
function updateHUD() {
document.getElementById("score-display").textContent = pad(state.score, 6);
["h1", "h2", "h3"].forEach((id, i) => {
document.getElementById(id).classList.toggle("empty", i >= state.lives);
});
}
// Game Lifecycle
function startGame() {
initAudio();
state.score = 0;
state.lives = 3;
state.wave = 1;
state.crystalsCaught = 0;
state.crystalsPerWave = 5;
state.frameCount = 0;
state.tickTimer = 0;
state.glitchTimer = 0;
state.phase = "playing";
variant.x = 400;
variant.y = 275;
variant.vx = 0;
variant.vy = 0;
variant.angle = 0;
variant.trail = [];
variant.invincible = 0;
msMins.x = 80;
msMins.y = 80;
msMins.vx = 1;
msMins.vy = 0.5;
msMins.speed = 1.2;
msMins.angle = 0;
msMins.pulseScale = 1;
crystals = [];
drones = [];
particles = [];
glitchStrips = [];
for (let i = 0; i < 3; i++) spawnCrystal();
document.getElementById("screen-start").style.display = "none";
document.getElementById("screen-over").style.display = "none";
document.getElementById("screen-pause").style.display = "none";
document.getElementById("wave-display").textContent = "WAVE 01";
document.getElementById("integrity").textContent = "100%";
updateHUD();
drawBackground();
playWave();
}
function endGame() {
state.phase = "over";
document.getElementById("final-score").textContent = pad(state.score, 6);
document.getElementById("final-wave").textContent =
`REACHED WAVE ${pad(state.wave, 2)}`;
document.getElementById("screen-over").style.display = "flex";
triggerGlitch(5);