Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
5473 lines (5086 loc) · 164 KB
/
Copy pathscript.js
File metadata and controls
5473 lines (5086 loc) · 164 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
/* ============================================
E-WASTE ESCAPE — Complete Game Engine
Vanilla JavaScript + HTML5 Canvas
============================================ */
"use strict";
// ============================================
// EDUCATIONAL DATABASE
// ============================================
const FACTS = [
{
text: "Only 17% of global e-waste is properly recycled each year.",
icon: "♻️",
},
{
text: "In 2022, the world generated 62 million tonnes of electronic waste.",
icon: "📊",
},
{
text: "Batteries can leak toxic chemicals like mercury and lead into soil and groundwater.",
icon: "🔋",
},
{
text: "Repairing one smartphone saves up to 70kg of CO₂ emissions.",
icon: "📱",
},
{
text: "Gold, silver, copper, and palladium can all be recovered from old electronics.",
icon: "⚙️",
},
{
text: "E-waste is the fastest-growing waste stream in the world.",
icon: "📈",
},
{
text: "It takes 3x more energy to make a new device than to repair an existing one.",
icon: "⚡",
},
{
text: "Informal e-waste recycling exposes workers to toxic fumes and chemicals.",
icon: "☠️",
},
{
text: "A single circuit board contains over 60 different elements from the periodic table.",
icon: "🔬",
},
{
text: "Right to Repair laws let you legally fix your own devices and save money.",
icon: "🔧",
},
{
text: "Only 20 countries have national e-waste legislation and policies.",
icon: "🌍",
},
{
text: "Children in developing nations sort e-waste by hand, risking their health.",
icon: "👧",
},
{
text: "Extending a phone's life by 1 year reduces its carbon footprint by 30%.",
icon: "🌱",
},
{
text: "The circular economy could eliminate 45% of global CO₂ emissions.",
icon: "🔄",
},
{
text: "Tech companies can design products to be modular, repairable, and recyclable.",
icon: "💡",
},
];
const QUIZZES = [
{
question: "What percentage of global e-waste is properly recycled?",
options: ["17%", "45%", "62%", "80%"],
correct: 0,
explanation:
"Only 17% of e-waste is properly recycled. The rest ends up in landfills or informal processing sites.",
},
{
question: "Which toxic metal is commonly found in old batteries?",
options: ["Gold", "Copper", "Lead", "Titanium"],
correct: 2,
explanation:
"Lead and mercury are toxic metals commonly found in old batteries, and can leach into soil and water.",
},
{
question: "How much CO₂ can be saved by repairing one smartphone?",
options: ["5kg", "20kg", "70kg", "200kg"],
correct: 2,
explanation:
"Repairing a smartphone instead of replacing it saves approximately 70kg of CO₂ emissions.",
},
{
question: "What is the 'Right to Repair' movement about?",
options: [
"Free repairs from manufacturers",
"Legal right to fix your own devices",
"Repairing roads",
"Government repair programs",
],
correct: 1,
explanation:
"Right to Repair is a movement advocating for consumers' legal right to repair their own electronics and access parts and manuals.",
},
];
const WORLD_INFO_TOPICS = [
{
title: "E-Waste Basics",
icon: "♻",
text: "E-waste means discarded electronics: phones, chargers, laptops, batteries, circuit boards, TVs and other powered devices. Many contain valuable metals, but unsafe dumping can leak toxic materials into soil and water.",
},
{
title: "Repair First",
icon: "🔧",
text: "Repairing or reusing a working device usually saves more energy and materials than recycling it immediately. Keep devices longer, replace batteries when safe, and donate usable gear.",
},
{
title: "Battery Safety",
icon: "🔋",
text: "Lithium batteries should not go into ordinary trash. Tape exposed terminals, keep damaged batteries away from heat, and take them to approved collection points.",
},
{
title: "Game Tip",
icon: "⚡",
text: "In World Mode, collectibles are rare. Watch for info pages, keep energy for EMP blasts, and use dash to create distance before bots close in.",
},
{
title: "Creator",
icon: "★",
text: "E-Waste Escape was created by Asher King as an arcade game about repair, recycling, and learning how electronic waste affects the planet.",
},
{
title: "Responsible Recycling",
icon: "🌍",
text: "Use certified recyclers when possible. They recover copper, gold, aluminum, glass and plastics while reducing exposure to lead, mercury and flame retardants.",
},
{
title: "Toxic Alley",
icon: "🏙",
text: "Toxic Alley is where mixed household electronics pile up first. Phones, remotes and chargers are easy to lose here, but they still contain copper, gold and plastics that can be recovered.",
},
{
title: "Battery Graveyard",
icon: "⚠",
text: "Battery Graveyard is dangerous because punctured cells can overheat or leak. Keep batteries dry, avoid crushing them, and never burn them.",
},
{
title: "Repair District",
icon: "🛠",
text: "Repair District rewards careful exploration. Repairing a device keeps the whole product in use, which usually saves more material than recycling it early.",
},
{
title: "MegaDump City",
icon: "🏭",
text: "MegaDump City shows why design matters. Products that are easier to open, repair and separate are safer and cheaper to recycle.",
},
{
title: "Movement Tip",
icon: "↔",
text: "Use zoom to scan for notes, then zoom back in near bots and hazards. Dash is best for escaping corners or crossing streets quickly.",
},
{
title: "EMP Tip",
icon: "💥",
text: "EMP clears nearby bots in World Mode, but it costs energy. Let energy refill before entering crowded streets.",
},
{
title: "Sorting Tip",
icon: "📦",
text: "Separate batteries, screens, cables and circuit boards when you can. Sorting makes recycling safer and improves material recovery.",
},
];
const WORLD_CITY_CONTEXT = [
{
name: "TOXIC ALLEY",
caption: "Phones, screens, remotes and cables arrive here.",
},
{
name: "BATTERY GRAVEYARD",
caption: "Damaged cells and barrels make this zone risky.",
},
{
name: "REPAIR DISTRICT",
caption: "Workshops salvage parts and extend device life.",
},
{
name: "MEGADUMP CITY",
caption: "Waste towers surround the recycling core.",
},
];
const LEVEL_DATA = [
{
id: 1,
name: "TOXIC ALLEY",
theme: "Broken phones • CRT TVs • Wires • Trash piles",
mission: "Collect 50 recyclable electronics",
missionTarget: 50,
missionType: "collect",
story:
"Nova enters the heart of Toxic Alley. The streets are paved with cracked screens and tangled wires. The Green Circuit's mission: collect recyclable devices before the Smoke Crusher compacts them forever.",
boss: {
name: "SMOKE CRUSHER",
desc: "A massive trash compactor robot armed with pneumatic crushers and toxic smoke vents. Its weak point: the exhaust vents on its back.",
color: "#808080",
},
eduFact: FACTS[0],
quiz: QUIZZES[0],
bgColors: ["#5eceff", "#a8e6ff", "#d4f5ff"],
groundColor: "#4caf50",
groundAccent: "#388e3c",
speed: 3,
},
{
id: 2,
name: "BATTERY GRAVEYARD",
theme:
"Acid pools • Exploding batteries • Toxic barrels • Electric hazards",
mission: "Collect 100 batteries safely",
missionTarget: 100,
missionType: "collect",
story:
"The Battery Graveyard reeks of sulfuric acid. Millions of lithium cells leak toxic chemicals into cracked earth. Nova must navigate exploding batteries and acid pools to reach the Acid Core.",
boss: {
name: "ACID CORE",
desc: "A corrupted battery reactor, bloated with toxic energy. It launches acid projectiles and creates electric fields. Target the cooling vents to stop the meltdown.",
color: "#44ff00",
},
eduFact: FACTS[2],
quiz: QUIZZES[1],
bgColors: ["#ff9de2", "#ffc8f0", "#ffe8fa"],
groundColor: "#ff7043",
groundAccent: "#bf360c",
speed: 3.8,
},
{
id: 3,
name: "REPAIR DISTRICT",
theme: "Repair labs • Salvage stations • Circuit boards • Broken laptops",
mission: "Repair 150 broken devices",
missionTarget: 150,
missionType: "repair",
story:
"The Repair District was once the heart of the city's tech industry. Now the Obsolescence Engine forces every device into premature failure. Nova must use repair kits to restore devices before they're lost forever.",
boss: {
name: "OBSOLESCENCE ENGINE",
desc: "A sinister machine designed to make electronics fail. It broadcasts a jamming signal that corrupts circuits. Destroy the signal towers to expose its central processor.",
color: "#ff8800",
},
eduFact: FACTS[9],
quiz: QUIZZES[3],
bgColors: ["#aaf7c4", "#ccfbe0", "#e8fff4"],
groundColor: "#7b1fa2",
groundAccent: "#4a148c",
speed: 4.5,
},
{
id: 4,
name: "MEGADUMP CITY",
theme:
"E-waste mountains • Toxic storms • Industrial drones • Recycling core",
mission: "Collect 200 recycling core components",
missionTarget: 200,
missionType: "collect",
story:
"At the heart of MegaDump City stands the ancient Recycling Core, buried under centuries of e-waste. The Waste Titan guards it with an army of drones and toxic storm generators. This is the final battle for Earth's future.",
boss: {
name: "WASTE TITAN",
desc: "A colossal AI garbage machine towering over the cityscape. It commands drone swarms, toxic storms, and gravity crushers. Destroy its four power cores to shut down this mechanical menace forever.",
color: "#ff0040",
},
eduFact: FACTS[13],
quiz: QUIZZES[2],
bgColors: ["#c5b8ff", "#ddd4ff", "#f0ecff"],
groundColor: "#e65100",
groundAccent: "#bf360c",
speed: 5.5,
},
];
const LEVEL_PALETTES = [
{
bgColors: ["#5eceff", "#a8e6ff", "#d4f5ff"],
groundColor: "#4caf50",
groundAccent: "#388e3c",
},
{
bgColors: ["#ff9de2", "#ffc8f0", "#ffe8fa"],
groundColor: "#ff7043",
groundAccent: "#bf360c",
},
{
bgColors: ["#aaf7c4", "#ccfbe0", "#e8fff4"],
groundColor: "#7b1fa2",
groundAccent: "#4a148c",
},
{
bgColors: ["#c5b8ff", "#ddd4ff", "#f0ecff"],
groundColor: "#e65100",
groundAccent: "#bf360c",
},
];
function applyLevelPalettes() {
LEVEL_DATA.forEach((level, index) => {
Object.assign(level, LEVEL_PALETTES[index]);
});
}
applyLevelPalettes();
// ============================================
// SAVE SYSTEM
// ============================================
const Save = {
key: "ewaste_escape_v1",
data: null,
load() {
try {
const raw = localStorage.getItem(this.key);
this.data = raw ? JSON.parse(raw) : this.defaultData();
} catch (e) {
this.data = this.defaultData();
}
return this.data;
},
save() {
try {
localStorage.setItem(this.key, JSON.stringify(this.data));
} catch (e) {}
},
defaultData() {
return { unlockedLevels: [1], highScores: [], totalScore: 0 };
},
unlockLevel(id) {
if (!this.data.unlockedLevels.includes(id)) {
this.data.unlockedLevels.push(id);
this.save();
}
},
addScore(score, level) {
this.data.highScores.push({ score, level, date: Date.now() });
this.data.highScores.sort((a, b) => b.score - a.score);
this.data.highScores = this.data.highScores.slice(0, 10);
this.data.totalScore += score;
this.save();
return (
(this.data.highScores[0].score === score &&
this.data.highScores.length === 1) ||
score >= (this.data.highScores[1]?.score ?? 0)
);
},
};
// ============================================
// AUDIO SYSTEM (Web Audio API)
// ============================================
const Audio = {
ctx: null,
enabled: true,
muted: false,
prefKey: "ewaste_escape_audio_muted",
loadPreference() {
try {
this.muted = localStorage.getItem(this.prefKey) === "1";
} catch (e) {
this.muted = false;
}
this.updateButton();
},
savePreference() {
try {
localStorage.setItem(this.prefKey, this.muted ? "1" : "0");
} catch (e) {}
},
setMuted(muted) {
this.muted = !!muted;
this.savePreference();
this.updateButton();
},
toggleMute() {
this.setMuted(!this.muted);
},
updateButton() {
const btn = document.getElementById("btn-mute-toggle");
if (!btn) return;
btn.classList.toggle("muted", this.muted);
btn.setAttribute(
"aria-label",
this.muted ? "Unmute game audio" : "Mute game audio",
);
btn.title = this.muted ? "Unmute game audio" : "Mute game audio";
},
init() {
try {
this.ctx = new (window.AudioContext || window.webkitAudioContext)();
} catch (e) {
this.enabled = false;
}
},
play(type, freq = 440, duration = 0.1, vol = 0.3) {
if (!this.enabled || this.muted || !this.ctx) return;
try {
const osc = this.ctx.createOscillator();
const gain = this.ctx.createGain();
osc.connect(gain);
gain.connect(this.ctx.destination);
osc.type = type;
osc.frequency.setValueAtTime(freq, this.ctx.currentTime);
gain.gain.setValueAtTime(vol, this.ctx.currentTime);
gain.gain.exponentialRampToValueAtTime(
0.001,
this.ctx.currentTime + duration,
);
osc.start(this.ctx.currentTime);
osc.stop(this.ctx.currentTime + duration);
} catch (e) {}
},
jump() {
this.play("square", 400, 0.12, 0.25);
setTimeout(() => this.play("square", 600, 0.08, 0.15), 60);
},
collect() {
this.play("sine", 660, 0.08, 0.2);
setTimeout(() => this.play("sine", 880, 0.08, 0.2), 80);
setTimeout(() => this.play("sine", 1100, 0.1, 0.15), 160);
},
hurt() {
this.play("sawtooth", 200, 0.2, 0.4);
setTimeout(() => this.play("sawtooth", 150, 0.15, 0.2), 100);
},
bossHit() {
this.play("square", 300, 0.15, 0.35);
setTimeout(() => this.play("square", 200, 0.1, 0.25), 80);
},
bossDefeat() {
[200, 300, 400, 600, 800].forEach((f, i) =>
setTimeout(() => this.play("sine", f, 0.2, 0.3), i * 120),
);
},
powerUp() {
[440, 550, 660, 880].forEach((f, i) =>
setTimeout(() => this.play("sine", f, 0.12, 0.25), i * 80),
);
},
gameOver() {
[400, 300, 200, 150].forEach((f, i) =>
setTimeout(() => this.play("sawtooth", f, 0.3, 0.5), i * 200),
);
},
levelComplete() {
[523, 659, 784, 1047].forEach((f, i) =>
setTimeout(() => this.play("sine", f, 0.25, 0.4), i * 150),
);
},
emp() {
this.play("sawtooth", 100, 0.3, 0.6);
this.play("sine", 2000, 0.1, 0.4);
},
resume() {
if (!this.muted && this.ctx && this.ctx.state === "suspended")
this.ctx.resume();
},
};
// ============================================
// PARTICLE SYSTEM
// ============================================
class ParticleSystem {
constructor() {
this.particles = [];
}
emit(x, y, count, color, options = {}) {
const mobileEffects = window.matchMedia?.("(pointer: coarse)")?.matches;
const actualCount = mobileEffects
? Math.max(1, Math.ceil(count * 0.45))
: count;
for (let i = 0; i < actualCount; i++) {
const angle =
options.angle !== undefined
? options.angle + (Math.random() - 0.5) * (options.spread || Math.PI)
: Math.random() * Math.PI * 2;
const speed = options.speed || 2 + Math.random() * 4;
this.particles.push({
x,
y,
vx: Math.cos(angle) * speed * (0.5 + Math.random()),
vy:
Math.sin(angle) * speed * (0.5 + Math.random()) -
(options.upward ? 2 : 0),
life: 1,
decay: options.decay || 0.02 + Math.random() * 0.03,
size: options.size || 2 + Math.random() * 3,
color,
gravity: options.gravity !== undefined ? options.gravity : 0.15,
glow: options.glow || false,
shape: options.shape || "circle",
});
}
}
update() {
this.particles = this.particles.filter((p) => {
p.x += p.vx;
p.y += p.vy;
p.vy += p.gravity;
p.vx *= 0.98;
p.life -= p.decay;
return p.life > 0;
});
}
draw(ctx) {
this.particles.forEach((p) => {
ctx.save();
ctx.globalAlpha = p.life;
if (p.glow) {
ctx.shadowColor = p.color;
ctx.shadowBlur = 8;
}
ctx.fillStyle = p.color;
if (p.shape === "square") {
ctx.fillRect(p.x - p.size / 2, p.y - p.size / 2, p.size, p.size);
} else {
ctx.beginPath();
ctx.arc(p.x, p.y, p.size / 2, 0, Math.PI * 2);
ctx.fill();
}
ctx.restore();
});
}
}
// ============================================
// CAMERA
// ============================================
class Camera {
constructor() {
this.x = 0;
this.y = 0;
this.shake = 0;
this.shakeX = 0;
this.shakeY = 0;
}
addShake(amount) {
this.shake = Math.max(this.shake, amount);
}
update() {
if (this.shake > 0) {
this.shakeX = (Math.random() - 0.5) * this.shake;
this.shakeY = (Math.random() - 0.5) * this.shake;
this.shake *= 0.85;
if (this.shake < 0.3) {
this.shake = 0;
this.shakeX = 0;
this.shakeY = 0;
}
}
}
}
// ============================================
// PLAYER
// ============================================
class Player {
constructor(canvas) {
this.canvas = canvas;
this.reset();
}
reset() {
this.x = 100;
this.y = 300;
this.w = 28;
this.h = 36;
this.vx = 0;
this.vy = 0;
this.onGround = false;
this.jumpsLeft = 2;
this.sliding = false;
this.dashing = false;
this.dashCooldown = 0;
this.dashTimer = 0;
this.invincible = 0;
this.health = 100;
this.healthRegenDelay = 0;
this.healthRegenRate = 0.12;
this.energy = 100;
this.energyRegen = 0.15;
this.animFrame = 0;
this.animTimer = 0;
this.facingRight = true;
this.trail = [];
this.powers = {};
this.empCooldown = 0;
this.dead = false;
this.bossMode = false; // free movement during boss
}
jump() {
if (this.jumpsLeft > 0) {
this.vy = -14;
this.jumpsLeft--;
Audio.jump();
return true;
}
return false;
}
slide(active) {
if (active && this.onGround) {
this.sliding = true;
this.h = 20;
} else {
this.sliding = false;
this.h = 36;
}
}
fastFall() {
if (!this.onGround) {
this.vy = Math.max(this.vy, 14);
return true;
}
return false;
}
dash() {
if (this.dashCooldown <= 0 && this.energy >= 25) {
this.dashing = true;
this.dashTimer = 18;
this.dashCooldown = 60;
this.energy -= 25;
this.vx = this.facingRight ? 24 : -24;
Audio.jump();
return true;
}
return false;
}
emp(enemies) {
if (this.empCooldown <= 0 && this.energy >= 40) {
this.energy -= 40;
this.empCooldown = 180;
Audio.emp();
return true;
}
return false;
}
takeDamage(amount) {
if (this.invincible > 0) return false;
if (this.powers.shield) return false;
this.health = Math.max(0, this.health - amount);
this.healthRegenDelay = 60;
this.invincible = 90;
Audio.hurt();
if (this.health <= 0) this.dead = true;
return true;
}
update(groundY, keys, canvasW) {
if (this.dead) return;
const speed = 5 * (this.powers.speed ? 1.6 : 1);
// Horizontal movement
if (!this.dashing) {
if (this.bossMode) {
// Free movement during boss
if (keys["ArrowLeft"] || keys["KeyA"] || keys.TouchLeft) {
this.vx = -speed;
this.facingRight = false;
} else if (keys["ArrowRight"] || keys["KeyD"] || keys.TouchRight) {
this.vx = speed;
this.facingRight = true;
} else {
this.vx *= 0.7;
}
} else {
this.vx = speed; // auto-runner
this.facingRight = true;
}
}
// Dash
if (this.dashTimer > 0) {
this.dashTimer--;
if (this.dashTimer <= 0) this.dashing = false;
}
// Cooldowns
if (this.dashCooldown > 0) this.dashCooldown--;
if (this.empCooldown > 0) this.empCooldown--;
if (this.invincible > 0) this.invincible--;
if (this.healthRegenDelay > 0) {
this.healthRegenDelay--;
} else if (this.health < 100) {
this.health = Math.min(100, this.health + this.healthRegenRate);
}
// Energy regen
this.energy = Math.min(100, this.energy + this.energyRegen);
// Physics
this.vy += 0.7; // gravity
if (this.vy > 18) this.vy = 18;
this.x += this.vx;
this.y += this.vy;
// Ground collision
const ground = groundY - this.h;
if (this.y >= ground) {
this.y = ground;
this.vy = 0;
this.onGround = true;
this.jumpsLeft = 2;
} else {
this.onGround = false;
}
// Keep player in bounds
if (this.bossMode) {
if (this.x < 20) this.x = 20;
if (canvasW && this.x > canvasW - this.w - 20)
this.x = canvasW - this.w - 20;
} else {
if (this.x < 80) this.x = 80;
if (this.x > 200) this.x = 200;
}
if (this.dashing || this.powers.speed) {
this.trail.push({
x: this.x + this.w / 2,
y: this.y + this.h / 2,
life: 1,
speedTrail: !!this.powers.speed,
});
if (this.dashing) {
this.trail.push({
x: this.x + this.w / 2 - 14,
y: this.y + this.h / 2 + (Math.random() - 0.5) * 10,
life: 0.85,
speedTrail: false,
});
}
}
if (this.trail.length > 22) this.trail.splice(0, this.trail.length - 22);
this.trail.forEach((t) => (t.life -= 0.08));
this.trail = this.trail.filter((t) => t.life > 0);
// Animation
this.animTimer++;
if (this.animTimer > 6) {
this.animFrame = (this.animFrame + 1) % 4;
this.animTimer = 0;
}
// Power-up timers
Object.keys(this.powers).forEach((k) => {
if (this.powers[k] > 0) this.powers[k]--;
if (this.powers[k] <= 0) delete this.powers[k];
});
}
draw(ctx, cam) {
const x = Math.round(this.x - cam.x + cam.shakeX);
const y = Math.round(this.y - cam.y + cam.shakeY);
const w = this.w;
const h = this.h;
// Trail (dash/speed effect)
if (this.trail.length > 0) {
this.trail.forEach((t, i) => {
ctx.save();
ctx.globalAlpha = t.life * (t.speedTrail ? 0.45 : 0.35);
ctx.fillStyle = t.speedTrail ? "#ffb000" : "#2979ff";
ctx.shadowColor = t.speedTrail ? "#ff7c00" : "#2979ff";
ctx.shadowBlur = t.speedTrail ? 10 : 6;
const tw = t.speedTrail ? w + 8 + i * 0.4 : w;
const th = this.sliding ? 20 : h;
ctx.fillRect(t.x - cam.x - tw / 2, t.y - cam.y - th / 2, tw, th);
ctx.restore();
});
}
// Invincible flash
if (this.invincible > 0 && Math.floor(this.invincible / 5) % 2 === 0)
return;
ctx.save();
if (this.powers.shield) {
ctx.shadowColor = "#2979ff";
ctx.shadowBlur = 20;
} else if (this.powers.magnet) {
ctx.shadowColor = "#a020f0";
ctx.shadowBlur = 12;
} else {
ctx.shadowColor = "#ff2d78";
ctx.shadowBlur = 8;
}
// --- Draw Nova (pixel art) ---
const drawH = this.sliding ? 20 : h;
// Body — bright teal suit
ctx.fillStyle = "#0097a7";
ctx.fillRect(x + 4, y + (h - drawH), w - 8, drawH);
// Suit highlight strips
ctx.fillStyle = "#ff2d78";
ctx.fillRect(x + 6, y + (h - drawH) + 2, 4, drawH - 4);
ctx.fillRect(x + w - 10, y + (h - drawH) + 2, 4, drawH - 4);
if (!this.sliding) {
// Head — bright orange helmet
ctx.fillStyle = "#ff7c00";
ctx.fillRect(x + 6, y, w - 12, 14);
// Visor — bright blue
ctx.fillStyle = "#2979ff";
ctx.fillRect(x + 8, y + 3, w - 16, 6);
// Visor shine
ctx.fillStyle = "#ffffff";
ctx.fillRect(x + 10, y + 4, 4, 2);
// Run animation legs
const legOff = [0, 4, 0, -4][this.animFrame];
ctx.fillStyle = "#1a0a3a";
ctx.fillRect(x + 5, y + h - 10, 8, 10 + legOff);
ctx.fillRect(x + w - 13, y + h - 10, 8, 10 - legOff);
// Boots — bright yellow
ctx.fillStyle = "#ffe600";
ctx.fillRect(x + 4, y + h - 3 + legOff, 10, 3);
ctx.fillRect(x + w - 14, y + h - 3 - legOff, 10, 3);
// Arms
const armOff = [-3, 0, 3, 0][this.animFrame];
ctx.fillStyle = "#00bcd4";
ctx.fillRect(x + 1, y + 16 + armOff, 5, 10);
ctx.fillRect(x + w - 6, y + 16 - armOff, 5, 10);
} else {
// Slide boots
ctx.fillStyle = "#ffe600";
ctx.fillRect(x + 2, y + h - 4, w - 4, 4);
}
// EMP indicator — hot pink dot
if (this.empCooldown <= 0) {
ctx.fillStyle = "#ff2d78";
ctx.fillRect(x + w - 8, y + 8, 6, 6);
}
ctx.restore();
}
}
// ============================================
// COLLECTIBLE
// ============================================
class Collectible {
constructor(x, y, type) {
this.x = x;
this.y = y;
this.type = type; // 'phone', 'tablet', 'laptop', 'circuit', 'battery', 'charger', 'repair_kit'
this.w = 24;
this.h = 24;
this.collected = false;
this.bobTimer = Math.random() * Math.PI * 2;
this.glowTimer = 0;
const types = {
phone: { color: "#00ffff", label: "PHONE", points: 100, icon: "📱" },
tablet: { color: "#00ff88", label: "TABLET", points: 150, icon: "📺" },
laptop: { color: "#88ffff", label: "LAPTOP", points: 200, icon: "💻" },
circuit: { color: "#ffff00", label: "CIRCUIT", points: 120, icon: "⚙️" },
battery: { color: "#00ff44", label: "BATTERY", points: 80, icon: "🔋" },
charger: { color: "#aaffaa", label: "CHARGER", points: 90, icon: "🔌" },
repair_kit: {
color: "#ffaa00",
label: "REPAIR",
points: 250,
icon: "🔧",
},
};
Object.assign(this, types[type] || types.phone);
}
update() {
this.bobTimer += 0.08;
this.glowTimer += 0.1;
}
draw(ctx, cam, scrollX) {
if (this.collected) return;
const sx = this.x - scrollX;
const x = Math.round(sx - cam.x + cam.shakeX);
const y = Math.round(
this.y + Math.sin(this.bobTimer) * 4 - cam.y + cam.shakeY,
);
ctx.save();
ctx.shadowColor = this.color;
ctx.shadowBlur = 12 + Math.sin(this.glowTimer) * 4;
// Draw pixel-art device
ctx.fillStyle = this.color;
if (this.type === "phone") {
ctx.fillRect(x + 6, y, 12, 20);
ctx.fillStyle = "#002233";
ctx.fillRect(x + 7, y + 2, 10, 14);
ctx.fillStyle = this.color;
ctx.fillRect(x + 9, y + 17, 6, 2);
} else if (this.type === "laptop") {
ctx.fillRect(x + 2, y + 4, 20, 14);
ctx.fillStyle = "#002233";
ctx.fillRect(x + 4, y + 6, 16, 9);
ctx.fillStyle = this.color;
ctx.fillRect(x, y + 18, 24, 4);
ctx.fillRect(x + 6, y + 20, 12, 2);
} else if (this.type === "circuit") {
ctx.fillRect(x + 4, y + 4, 16, 16);
ctx.fillStyle = "#002200";
ctx.fillRect(x + 7, y + 7, 10, 10);
ctx.fillStyle = this.color;
ctx.fillRect(x, y + 10, 4, 2);
ctx.fillRect(x + 20, y + 10, 4, 2);
ctx.fillRect(x + 10, y, 2, 4);
ctx.fillRect(x + 10, y + 20, 2, 4);
} else if (this.type === "battery") {
ctx.fillRect(x + 5, y + 2, 14, 20);
ctx.fillStyle = "#002200";
ctx.fillRect(x + 7, y + 4, 10, 14);
ctx.fillStyle = this.color;
ctx.fillRect(x + 9, y, 6, 2);
const charge = 0.7;
ctx.fillRect(x + 7, y + 18 - 14 * charge, 10, 14 * charge);
} else if (this.type === "repair_kit") {
ctx.fillRect(x + 2, y + 6, 20, 16);
ctx.fillStyle = "#664400";
ctx.fillRect(x + 6, y + 2, 12, 8);
ctx.fillStyle = this.color;
ctx.fillRect(x + 10, y + 10, 4, 8);
ctx.fillRect(x + 6, y + 14, 12, 4);
} else {
// Generic device
ctx.fillRect(x + 3, y + 2, 18, 20);
ctx.fillStyle = "#002233";
ctx.fillRect(x + 5, y + 4, 14, 12);
ctx.fillStyle = this.color;
ctx.fillRect(x + 8, y + 18, 8, 2);
}
// Glow ring
ctx.strokeStyle = this.color;
ctx.globalAlpha = 0.4 + Math.sin(this.glowTimer) * 0.2;
ctx.lineWidth = 1;
ctx.strokeRect(x - 3, y - 3, this.w + 6, this.h + 6);
ctx.restore();
}
getBounds(scrollX) {
return { x: this.x - scrollX, y: this.y, w: this.w, h: this.h };
}
}
// ============================================
// HAZARD
// ============================================
class Hazard {
constructor(x, y, type) {
this.x = x;
this.y = y;
this.type = type;
this.animTimer = Math.random() * Math.PI * 2;
this.w = 32;
this.h = 28;
const types = {
acid_barrel: { color: "#88ff00", damage: 50, w: 28, h: 32 },
toxic_cloud: { color: "#44aa00", damage: 50, w: 50, h: 40 },
broken_glass: { color: "#aaaaff", damage: 50, w: 36, h: 18 },
electric: { color: "#ffff00", damage: 50, w: 20, h: 40 },
sludge: { color: "#338800", damage: 50, w: 60, h: 16 },
};
Object.assign(this, types[type] || types.acid_barrel);
this.life = Infinity;
this.expired = false;
}