-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
16293 lines (14200 loc) · 495 KB
/
script.js
File metadata and controls
16293 lines (14200 loc) · 495 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
const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
const pixelToggle = document.getElementById("pixelToggle");
canvas.width = 800;
canvas.height = 400;
let pixelMode = false;
const editorPreviewMode = new URLSearchParams(window.location.search).has('editorPreview');
const TARGET_FPS = 60;
const FRAME_DURATION = 1000 / TARGET_FPS; // ~16.67ms per frame
let lastFrameTime = 0;
let deltaTime = 0;
let fpsSampleTime = 0;
let fpsFrameCount = 0;
let fpsDisplay = 60;
function isEffectVisibleInWorld(x, y, width = 0, height = 0, padding = 96) {
if (typeof devMapView !== 'undefined' && devMapView) return true;
return (
x + width >= camera.x - padding &&
x <= camera.x + canvas.width + padding &&
y + height >= camera.y - padding &&
y <= camera.y + canvas.height + padding
);
}
// --- Intro / load screen + hold-to-skip logic ---
const _INTRO_HOLD_KEYS = new Set(["Space", "Enter", "ShiftLeft", "ShiftRight"]);
const INTRO_HOLD_MS = 600; // hold duration to skip
let _introActive = true;
let _introHoldStart = 0;
let _introHoldRAF = null;
let _introAutoTimeout = null;
let _creditsTimer = null;
let _creditsIndex = 0;
const introEl = document.getElementById("intro-screen");
const introFill = introEl ? introEl.querySelector('.intro-progress-fill') : null;
const menuEl = document.getElementById("menu");
const creditsEl = introEl ? introEl.querySelector('.intro-credits') : null;
const laughEl = introEl ? introEl.querySelector('.intro-laugh') : null;
function updateIntroFill(frac) {
if (!introFill) return;
introFill.style.width = (Math.max(0, Math.min(1, frac)) * 100) + '%';
}
function finishIntro() {
if (!_introActive) return;
_introActive = false;
updateIntroFill(1);
if (introEl) {
introEl.classList.add('fadeout');
setTimeout(() => introEl.classList.add('hidden'), 420);
}
if (menuEl) menuEl.classList.remove('hidden');
if (_introHoldRAF) { cancelAnimationFrame(_introHoldRAF); _introHoldRAF = null; }
if (_introAutoTimeout) { clearTimeout(_introAutoTimeout); _introAutoTimeout = null; }
stopCredits();
}
function introHoldLoop(ts) {
if (!_introHoldStart) {
_introHoldStart = ts;
}
const elapsed = ts - _introHoldStart;
updateIntroFill(elapsed / INTRO_HOLD_MS);
if (elapsed >= INTRO_HOLD_MS) {
finishIntro();
return;
}
_introHoldRAF = requestAnimationFrame(introHoldLoop);
}
function startIntroHold() {
if (!_introActive) return;
if (_introHoldRAF) return; // already running
_introHoldStart = 0;
_introHoldRAF = requestAnimationFrame(introHoldLoop);
}
function cancelIntroHold() {
if (_introHoldRAF) cancelAnimationFrame(_introHoldRAF);
_introHoldRAF = null;
_introHoldStart = 0;
updateIntroFill(0);
}
// Show intro on first load: hide menu until intro finishes
if (menuEl) menuEl.classList.add('hidden');
document.addEventListener('keydown', (e) => {
if (!_introActive) return;
// Use e.code for consistent key detection
if (_INTRO_HOLD_KEYS.has(e.code)) {
e.preventDefault();
e.stopPropagation();
startIntroHold();
}
});
document.addEventListener('keyup', (e) => {
if (!_introActive) return;
if (_INTRO_HOLD_KEYS.has(e.code)) {
e.preventDefault();
e.stopPropagation();
cancelIntroHold();
}
});
// Pointer / touch hold support on the intro overlay
if (introEl) {
introEl.addEventListener('pointerdown', (ev) => {
if (!_introActive) return;
ev.preventDefault();
startIntroHold();
});
introEl.addEventListener('pointerup', (ev) => {
if (!_introActive) return;
ev.preventDefault();
cancelIntroHold();
});
introEl.addEventListener('pointercancel', () => { cancelIntroHold(); });
}
// Ensure touch / click hold also works reliably (prevent legacy touch scroll)
if (introEl) {
introEl.addEventListener('touchstart', (ev) => { ev.preventDefault(); startIntroHold(); }, {passive:false});
introEl.addEventListener('touchend', (ev) => { ev.preventDefault(); cancelIntroHold(); }, {passive:false});
}
// If user leaves the page or pointer is lost, cancel hold
window.addEventListener('blur', cancelIntroHold);
document.addEventListener('visibilitychange', () => { if (document.hidden) cancelIntroHold(); });
// Auto-finish intro after short load-time so users don't get stuck
_introAutoTimeout = setTimeout(() => { finishIntro(); }, Math.random() * 7500);
// clicking/tapping the intro will also finish it quickly
if (introEl) {
introEl.addEventListener('click', () => finishIntro());
}
// --- credits sequence ---
const _CREDITS = [
{text: 'MasterOfAWolf', cls: 'credit-big'},
{text: 'The Potato Thanks You', cls: ''},
{text: 'Special Thanks — The Community', cls: 'credit-small'},
];
function showCredit(index) {
if (!creditsEl) return;
// Clear existing lines
creditsEl.innerHTML = '';
const info = _CREDITS[index];
const el = document.createElement('div');
el.className = 'credit-line ' + (info.cls || '');
el.textContent = info.text;
creditsEl.appendChild(el);
// force reveal
requestAnimationFrame(() => el.classList.add('visible'));
// the rotating joke loop runs independently of credits (handled elsewhere)
}
// --- rotating jokes loop (longer display, immediate next) ---
const _JOKES = [
"That took longer than my last haircut.",
"Powered by coffee and questionable code.",
"Credits? More like ‘who stole my snacks'.",
"If you see bugs, they're features in beta.",
"Made with too much caffeine and not enough sleep.",
"Loading... please pretend this is intentional.",
"Optimizing… (we are not optimizing).",
"Reticulating splines… whatever that means.",
"Loading faster than my motivation on Mondays.",
"This would be quicker if I knew what I was doing.",
"Compiling confidence… failed successfully.",
"Spawning world… hope it works this time.",
"If this crashes, it’s your fault now.",
"99% loaded… just kidding.",
"Almost there… probably.",
"Fixing bugs by staring at them intensely.",
"Loading assets… and emotional baggage.",
"This is definitely not stalling for time.",
"Generating fun… please wait.",
"If you're reading this, it's already too late.",
"Debugging… step one: panic.",
"Loading… powered by pure hope.",
"One eternity later...",
"Trust the process. There is no process.",
"Running on vibes and stack overflow.",
"Calibrating wolves… 🐺",
"Adding legs… again.",
"Rolling cube not included.",
"Vote system loading… democracy takes time.",
"Multiplayer? Don’t ask.",
"Loading… do not blink.",
"We tested this. Once.",
"This seemed like a good idea at 2 AM.",
"Please wait while we question our life choices.",
"Everything is under control. Probably."
];
let _jokeLoopTimeout = null;
const JOKE_DISPLAY_MS = 2200; // how long each joke stays visible
const JOKE_GAP_MS = 60; // short gap while we swap text
function startJokeLoop() {
if (!laughEl) return;
stopJokeLoop();
function cycle() {
if (!laughEl) return;
laughEl.textContent = _JOKES[Math.floor(Math.random() * _JOKES.length)];
// show
requestAnimationFrame(() => laughEl.classList.add('visible'));
// hide after display time, then immediately start next
_jokeLoopTimeout = setTimeout(() => {
if (laughEl) laughEl.classList.remove('visible');
_jokeLoopTimeout = setTimeout(cycle, JOKE_GAP_MS);
}, JOKE_DISPLAY_MS);
}
cycle();
}
function stopJokeLoop() {
if (_jokeLoopTimeout) { clearTimeout(_jokeLoopTimeout); _jokeLoopTimeout = null; }
if (laughEl) { laughEl.classList.remove('visible'); }
}
function startCredits() {
if (!creditsEl) return;
_creditsIndex = 0;
showCredit(_creditsIndex);
_creditsTimer = setInterval(() => {
_creditsIndex++;
if (_creditsIndex >= _CREDITS.length) {
clearInterval(_creditsTimer);
_creditsTimer = null;
return;
}
showCredit(_creditsIndex);
}, 1500);
}
function stopCredits() {
if (_creditsTimer) { clearInterval(_creditsTimer); _creditsTimer = null; }
}
// kick off credits when script loads
if (_introActive) startCredits();
// Start sprite animations in the intro for a more cinematic feel
function startIntroSprites() {
const p = document.getElementById('intro-player');
const s = document.getElementById('intro-snail');
if (p) {
// small delay to sync with credits — animate into center/visible state
setTimeout(() => { p.classList.add('animate'); }, 400);
}
if (s) {
setTimeout(() => { s.classList.add('animate'); }, 700);
}
}
// Kick sprite animation when intro starts
if (_introActive) startIntroSprites();
if (_introActive) startJokeLoop();
// ------------------ Intro particle system (canvas) ------------------
let _introCanvas = null;
let _introCtx = null;
let _introParticles = [];
let _introParticleRAF = null;
// intro sprite animation state
let _introLastTS = 0;
let _introSnailFrame = 0;
let _introSnailTimer = 0;
const _introSnailInterval = 180; // ms per frame
let _introPlayerScale = 2.0;
let _introSnailScale = 2.2;
function createIntroCanvas() {
if (!introEl) return;
if (_introCanvas) return;
const c = document.createElement('canvas');
c.id = 'intro-canvas';
c.style.width = '100%';
c.style.height = '100%';
// append just inside the intro so it sits above background but under UI
introEl.insertBefore(c, introEl.querySelector('.intro-content'));
_introCanvas = c;
_introCtx = c.getContext('2d');
resizeIntroCanvas();
window.addEventListener('resize', resizeIntroCanvas);
}
function resizeIntroCanvas() {
if (!_introCanvas) return;
const rect = introEl.getBoundingClientRect();
_introCanvas.width = Math.max(1, Math.floor(rect.width * devicePixelRatio));
_introCanvas.height = Math.max(1, Math.floor(rect.height * devicePixelRatio));
_introCanvas.style.width = rect.width + 'px';
_introCanvas.style.height = rect.height + 'px';
if (_introCtx) _introCtx.setTransform(devicePixelRatio, 0, 0, devicePixelRatio, 0, 0);
}
function spawnIntroParticle() {
if (!_introCanvas) return;
const w = _introCanvas.width / devicePixelRatio;
const h = _introCanvas.height / devicePixelRatio;
const x = Math.random() * (w * 0.6) + w * 0.2;
const y = Math.random() * (h * 0.35) + h * 0.1;
const vx = (Math.random() - 0.5) * 0.25;
const vy = (Math.random() * 0.2 + 0.1);
const life = Math.random() * 1800 + 900;
const size = Math.random() * 3 + 2;
const colors = ['#ffd86b', '#ff9a6b', '#9be3d8', '#8fc5ff'];
const color = colors[Math.floor(Math.random() * colors.length)];
_introParticles.push({ x, y, vx, vy, life, maxLife: life, size, color, alpha: 1 });
}
function introParticleTick(ts) {
if (!_introCtx) return;
// delta time
const dt = _introLastTS ? (ts - _introLastTS) : 16;
_introLastTS = ts;
_introCtx.clearRect(0, 0, _introCanvas.width, _introCanvas.height);
for (let i = _introParticles.length - 1; i >= 0; i--) {
const p = _introParticles[i];
p.x += p.vx * (dt / 16);
p.y += p.vy * (dt / 16);
p.life -= dt;
p.alpha = Math.max(0, p.life / p.maxLife);
// glow
_introCtx.beginPath();
_introCtx.fillStyle = p.color;
_introCtx.globalAlpha = 0.08 * p.alpha;
_introCtx.arc(p.x, p.y, p.size * 3.5, 0, Math.PI * 2);
_introCtx.fill();
_introCtx.beginPath();
_introCtx.globalAlpha = 0.9 * p.alpha;
_introCtx.arc(p.x, p.y, p.size, 0, Math.PI * 2);
_introCtx.fill();
_introCtx.globalAlpha = 1;
if (p.life <= 0 || p.y > _introCanvas.height / devicePixelRatio + 30) {
_introParticles.splice(i, 1);
}
}
// occasionally spawn
if (Math.random() < 0.18) spawnIntroParticle();
// draw cinematic sprites (player left, snail right)
try {
const w = _introCanvas.width / devicePixelRatio;
const h = _introCanvas.height / devicePixelRatio;
// snail animation frame update
_introSnailTimer += dt;
if (_introSnailTimer >= _introSnailInterval) {
_introSnailTimer = 0;
_introSnailFrame = (_introSnailFrame + 1) % (snailFrames.length || 1);
}
// draw player
if (playerImg && playerImg.complete && playerImg.naturalWidth) {
const img = playerImg;
const scale = _introPlayerScale;
const sw = img.naturalWidth;
const sh = img.naturalHeight;
const dw = sw * scale;
const dh = sh * scale;
const px = Math.round(w * 0.28 - dw / 2);
const py = Math.round(h * 0.56 - dh / 2);
_introCtx.globalAlpha = 0.95;
_introCtx.drawImage(img, px, py, dw, dh);
_introCtx.globalAlpha = 1;
}
// draw snail (animated frames if available)
let snailFrameImg = snailFrames && snailFrames.length ? snailFrames[_introSnailFrame % snailFrames.length] : snailImg;
if (snailFrameImg && snailFrameImg.complete && snailFrameImg.naturalWidth) {
const img = snailFrameImg;
const scale = _introSnailScale;
const sw = img.naturalWidth;
const sh = img.naturalHeight;
const dw = sw * scale;
const dh = sh * scale;
const sx = Math.round(w * 0.72 - dw / 2);
const sy = Math.round(h * 0.60 - dh / 2);
_introCtx.globalAlpha = 0.95;
_introCtx.drawImage(img, sx, sy, dw, dh);
_introCtx.globalAlpha = 1;
}
} catch (e) {}
_introParticleRAF = requestAnimationFrame(introParticleTick);
}
function startIntroParticles() {
if (_introParticleRAF) return;
createIntroCanvas();
_introParticles.length = 0;
// seed a few
for (let i = 0; i < 8; i++) spawnIntroParticle();
_introParticleRAF = requestAnimationFrame(introParticleTick);
}
function stopIntroParticles() {
if (_introParticleRAF) { cancelAnimationFrame(_introParticleRAF); _introParticleRAF = null; }
_introParticles.length = 0;
if (_introCanvas) {
try { _introCanvas.remove(); } catch (e) {}
_introCanvas = null; _introCtx = null;
}
window.removeEventListener('resize', resizeIntroCanvas);
}
// Hook particles into intro lifecycle
if (_introActive) startIntroParticles();
// Ensure finishIntro stops particle loop
const _origFinishIntro = finishIntro;
finishIntro = function() {
stopIntroParticles();
stopCredits();
stopJokeLoop();
_origFinishIntro();
};
// ============================================================
// ATMOSPHERIC PARTICLE SYSTEM - DUNGEON AMBIANCE
// ============================================================
const atmosphericParticles = {
dustMotes: [],
embers: [],
fireflies: [],
debris: [],
fogLayers: []
};
const ATMOSPHERIC_TARGETS = {
dustMotes: 80,
fireflies: 15,
debris: 30,
fogLayers: 3
};
let atmosphericStepMs = 1000 / 20; // throttle ambient particle sim to 20 FPS
let atmosphericAccumMs = 0;
// Particle class for dust motes floating in light rays
class DustMote {
constructor() {
this.x = Math.random() * MAP_WIDTH;
this.y = Math.random() * MAP_HEIGHT;
this.vx = (Math.random() - 0.5) * 0.3;
this.vy = (Math.random() - 0.5) * 0.2;
this.size = Math.random() * 1.5 + 0.5;
this.opacity = Math.random() * 0.4 + 0.1;
this.wobble = Math.random() * Math.PI * 2;
this.wobbleSpeed = Math.random() * 0.02 + 0.01;
}
update() {
this.wobble += this.wobbleSpeed;
this.x += this.vx + Math.sin(this.wobble) * 0.2;
this.y += this.vy + Math.cos(this.wobble * 0.7) * 0.15;
// Wrap around world boundaries
if (this.x < 0) this.x = MAP_WIDTH;
if (this.x > MAP_WIDTH) this.x = 0;
if (this.y < 0) this.y = MAP_HEIGHT;
if (this.y > MAP_HEIGHT) this.y = 0;
}
draw(ctx) {
ctx.fillStyle = `rgba(200, 180, 150, ${this.opacity})`;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
}
}
// Ember particles - floating upward with glow
class Ember {
constructor(x, y) {
this.x = x || Math.random() * MAP_WIDTH;
this.y = y || MAP_HEIGHT;
this.vx = (Math.random() - 0.5) * 0.5;
this.vy = -(Math.random() * 1.5 + 0.5);
this.size = Math.random() * 2 + 1;
this.life = Math.random() * 120 + 60;
this.maxLife = this.life;
this.brightness = Math.random() * 0.5 + 0.5;
this.flicker = Math.random() * Math.PI * 2;
}
update() {
this.x += this.vx;
this.y += this.vy;
this.vy -= 0.02; // gentle upward acceleration
this.vx *= 0.99;
this.life--;
this.flicker += 0.1;
}
draw(ctx) {
const alpha = (this.life / this.maxLife) * this.brightness;
const flicker = Math.sin(this.flicker) * 0.3 + 0.7;
// Cheap glow: use a larger translucent disc instead of canvas shadow blur.
ctx.fillStyle = `rgba(255, 120, 50, ${alpha * 0.18})`;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size * 2.6, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = `rgba(255, 140, 60, ${alpha * flicker})`;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
// Bright center
ctx.fillStyle = `rgba(255, 220, 150, ${alpha * flicker})`;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size * 0.5, 0, Math.PI * 2);
ctx.fill();
}
}
// Firefly particles - floating with soft pulsing glow
class Firefly {
constructor() {
this.x = Math.random() * MAP_WIDTH;
this.y = Math.random() * MAP_HEIGHT;
this.vx = (Math.random() - 0.5) * 0.4;
this.vy = (Math.random() - 0.5) * 0.4;
this.size = Math.random() * 1.5 + 1;
this.pulsePhase = Math.random() * Math.PI * 2;
this.pulseSpeed = Math.random() * 0.03 + 0.02;
const warmOrange = [255, 165, 90];
const currentPalette = Math.random() > 0.7 ? [100, 255, 150] : [150, 255, 200];
const blend = Math.random(); // 0 = warm orange, 1 = current palette
this.color = [
Math.round(warmOrange[0] + (currentPalette[0] - warmOrange[0]) * blend),
Math.round(warmOrange[1] + (currentPalette[1] - warmOrange[1]) * blend),
Math.round(warmOrange[2] + (currentPalette[2] - warmOrange[2]) * blend)
];
// Very rare color variant.
if (Math.random() < 0.025) {
this.color = [255, 120, 205];
}
this.changeDirection = Math.random() * 120 + 60;
this.timer = 0;
}
update() {
this.timer++;
// Change direction periodically
if (this.timer > this.changeDirection) {
this.vx = (Math.random() - 0.5) * 0.6;
this.vy = (Math.random() - 0.5) * 0.6;
this.changeDirection = Math.random() * 120 + 60;
this.timer = 0;
}
this.x += this.vx;
this.y += this.vy;
this.pulsePhase += this.pulseSpeed;
// Wrap around
if (this.x < 0) this.x = MAP_WIDTH;
if (this.x > MAP_WIDTH) this.x = 0;
if (this.y < 0) this.y = MAP_HEIGHT;
if (this.y > MAP_HEIGHT) this.y = 0;
}
draw(ctx) {
const pulse = Math.sin(this.pulsePhase) * 0.5 + 0.5;
const alpha = pulse * 0.7 + 0.3;
// Use a layered disc glow instead of per-particle shadow blur.
ctx.fillStyle = `rgba(${this.color[0]}, ${this.color[1]}, ${this.color[2]}, ${alpha * 0.14})`;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size * (2.8 + pulse), 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = `rgba(${this.color[0]}, ${this.color[1]}, ${this.color[2]}, ${alpha})`;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size * (0.7 + pulse * 0.3), 0, Math.PI * 2);
ctx.fill();
}
}
// Floating debris - small rocks, dust clouds
class Debris {
constructor() {
this.x = Math.random() * MAP_WIDTH;
this.y = Math.random() * MAP_HEIGHT;
this.vx = (Math.random() - 0.5) * 0.2;
this.vy = (Math.random() - 0.5) * 0.15;
this.width = Math.random() * 3 + 2;
this.height = Math.random() * 3 + 2;
this.rotation = Math.random() * Math.PI * 2;
this.rotationSpeed = (Math.random() - 0.5) * 0.02;
this.opacity = Math.random() * 0.2 + 0.05;
}
update() {
this.x += this.vx;
this.y += this.vy;
this.rotation += this.rotationSpeed;
if (this.x < 0) this.x = MAP_WIDTH;
if (this.x > MAP_WIDTH) this.x = 0;
if (this.y < 0) this.y = MAP_HEIGHT;
if (this.y > MAP_HEIGHT) this.y = 0;
}
draw(ctx) {
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(this.rotation);
ctx.fillStyle = `rgba(80, 70, 60, ${this.opacity})`;
ctx.fillRect(-this.width / 2, -this.height / 2, this.width, this.height);
ctx.restore();
}
}
// Fog layer for depth
class FogLayer {
constructor(depth) {
this.depth = depth; // 0-1, where 0 is foreground
this.x = 0;
this.y = 0;
this.speed = depth * 0.3;
this.opacity = (1 - depth) * 0.05;
this.scale = 1 + depth * 0.5;
}
update() {
this.x += this.speed * 0.2;
if (this.x > MAP_WIDTH) this.x = 0;
}
draw(ctx) {
ctx.save();
ctx.globalAlpha = this.opacity;
// Draw repeating fog pattern
for (let x = -MAP_WIDTH; x < MAP_WIDTH * 2; x += MAP_WIDTH) {
const gradient = ctx.createRadialGradient(
x + this.x + MAP_WIDTH / 2,
MAP_HEIGHT / 2,
0,
x + this.x + MAP_WIDTH / 2,
MAP_HEIGHT / 2,
MAP_WIDTH
);
gradient.addColorStop(0, 'rgba(40, 35, 50, 0.3)');
gradient.addColorStop(0.5, 'rgba(30, 25, 40, 0.1)');
gradient.addColorStop(1, 'rgba(20, 15, 30, 0)');
ctx.fillStyle = gradient;
ctx.fillRect(x + this.x, 0, MAP_WIDTH, MAP_HEIGHT);
}
ctx.restore();
}
}
// Initialize atmospheric particles
function initAtmosphericParticles() {
if (!settings.particles) return;
// Reset pools so restarts don't stack duplicate particles.
atmosphericParticles.dustMotes.length = 0;
atmosphericParticles.embers.length = 0;
atmosphericParticles.fireflies.length = 0;
atmosphericParticles.debris.length = 0;
atmosphericParticles.fogLayers.length = 0;
// Dust motes - lots of them for atmosphere
for (let i = 0; i < ATMOSPHERIC_TARGETS.dustMotes; i++) {
atmosphericParticles.dustMotes.push(new DustMote());
}
// Fireflies - fewer, more special
for (let i = 0; i < ATMOSPHERIC_TARGETS.fireflies; i++) {
atmosphericParticles.fireflies.push(new Firefly());
}
// Floating debris
for (let i = 0; i < ATMOSPHERIC_TARGETS.debris; i++) {
atmosphericParticles.debris.push(new Debris());
}
// Fog layers for depth
for (let i = 0; i < ATMOSPHERIC_TARGETS.fogLayers; i++) {
atmosphericParticles.fogLayers.push(new FogLayer(i / 3));
}
}
// Update atmospheric particles
function updateAtmosphericParticles() {
if (!settings.particles) return;
// Keep ambient particles alive even if settings were toggled during gameplay.
if (atmosphericParticles.dustMotes.length < ATMOSPHERIC_TARGETS.dustMotes && Math.random() < 0.35) {
atmosphericParticles.dustMotes.push(new DustMote());
}
if (atmosphericParticles.fireflies.length < ATMOSPHERIC_TARGETS.fireflies && Math.random() < 0.2) {
atmosphericParticles.fireflies.push(new Firefly());
}
if (atmosphericParticles.debris.length < ATMOSPHERIC_TARGETS.debris && Math.random() < 0.2) {
atmosphericParticles.debris.push(new Debris());
}
while (atmosphericParticles.fogLayers.length < ATMOSPHERIC_TARGETS.fogLayers) {
atmosphericParticles.fogLayers.push(new FogLayer(atmosphericParticles.fogLayers.length / 3));
}
for (let i = 0; i < atmosphericParticles.dustMotes.length; i++) atmosphericParticles.dustMotes[i].update();
for (let i = 0; i < atmosphericParticles.fireflies.length; i++) atmosphericParticles.fireflies[i].update();
for (let i = 0; i < atmosphericParticles.debris.length; i++) atmosphericParticles.debris[i].update();
for (let i = 0; i < atmosphericParticles.fogLayers.length; i++) atmosphericParticles.fogLayers[i].update();
// Update embers
for (let i = atmosphericParticles.embers.length - 1; i >= 0; i--) {
atmosphericParticles.embers[i].update();
if (atmosphericParticles.embers[i].life <= 0) {
atmosphericParticles.embers.splice(i, 1);
}
}
// Spawn new embers near light sources (attach torch effects to lights)
if (settings.particles && typeof lights !== 'undefined' && lights.length) {
for (let i = 0; i < lights.length; i++) {
const L = lights[i];
if (!L) continue;
// only emit if light has meaningful intensity
if ((L.intensity || 0) < 0.08) continue;
// small probability scaled by intensity and spawnRate
const spawnChance = (L.particleSpawnRate || 0.02) * (L.intensity || 1);
if (Math.random() < spawnChance && atmosphericParticles.embers.length < 120) {
// spawn point jitter within a fraction of the light radius
const jitter = Math.max(8, L.radius * 0.18);
const ex = L.x + (Math.random() - 0.5) * jitter;
const ey = L.y + (Math.random() - 0.8) * jitter; // bias upward a bit
const e = new Ember(ex, ey);
// make embers rise more from stronger lights
e.vx += (Math.random() - 0.5) * 0.6;
e.vy -= Math.random() * 0.6 * (L.intensity || 1);
atmosphericParticles.embers.push(e);
}
}
}
}
function updateAtmosphericParticlesThrottled(deltaMs) {
if (!settings.particles) return;
if (atmosphericStepMs <= 0) {
updateAtmosphericParticles();
return;
}
atmosphericAccumMs += deltaMs;
const maxAccum = atmosphericStepMs * 3;
if (atmosphericAccumMs > maxAccum) atmosphericAccumMs = maxAccum;
if (atmosphericAccumMs < atmosphericStepMs) return;
atmosphericAccumMs -= atmosphericStepMs;
updateAtmosphericParticles();
}
// Draw atmospheric particles (call this in your draw function)
function drawAtmosphericParticles() {
if (!settings.particles) return;
// Background layers are drawn earlier in the frame; only render the
// foreground glow particles here so they don't get duplicated.
for (const ember of atmosphericParticles.embers) {
if (isEffectVisibleInWorld(ember.x, ember.y, ember.size * 4, ember.size * 4)) {
ember.draw(ctx);
}
}
for (const firefly of atmosphericParticles.fireflies) {
if (isEffectVisibleInWorld(firefly.x, firefly.y, firefly.size * 8, firefly.size * 8)) {
firefly.draw(ctx);
}
}
}
const tableFrames = [];
for (let i = 0; i < 4; i++) {
const img = new Image();
img.src = `Assets/Sprites/table-frame-${i}.png`; // rename to match your actual filenames
tableFrames.push(img);
}
const bgTile = new Image();
bgTile.src = "Assets/Tiles/Cobblestone BG.png";
const snailImg = new Image();
snailImg.src = "Assets/Sprites/snail-pixilart.png";
const babyMicrowaveImg = new Image();
babyMicrowaveImg.src = "Assets/Sprites/baby-microwave.svg";
const cannonImg = new Image();
cannonImg.src = "Assets/Sprites/potatocannon.png";
const clubImg = new Image();
clubImg.src = "Assets/Sprites/Club.png";
// --- SWORD ATTACK ANIMATION FRAMES ---
const swordAttackSheet = new Image();
swordAttackSheet.src = "Assets/Sprites/Sword Attack Sprite Sheet.png";
const SWORD_FRAME_WIDTH = 34;
const SWORD_FRAME_HEIGHT = 31;
const SWORD_FRAME_COUNT = 7;
const SWORD_ATTACK_EXTENSION_WIDTH = 42;
const SWORD_ATTACK_DRAW_SCALE = 1.5;
const SWORD_DOWN_BOUNCE_VELOCITY = -12;
const CLUB_ATTACK_SWING_SPAN = Math.PI * 1.25;
const CLUB_ATTACK_BASE_ANGLE = -Math.PI / 2;
const CLUB_ATTACK_RADIUS = 60;
const CLUB_ATTACK_BOX_WIDTH = 100;
const CLUB_ATTACK_BOX_HEIGHT = 30;
const CLUB_ATTACK_BOX_HALF_WIDTH = CLUB_ATTACK_BOX_WIDTH / 2;
const CLUB_ATTACK_BOX_HALF_HEIGHT = CLUB_ATTACK_BOX_HEIGHT / 2;
const CLUB_TURRET_STUN_DURATION = 50;
const CLUB_ENEMY_KNOCKBACK_MULTIPLIER = 8;
const CLUB_BOX_KNOCKBACK_MULTIPLIER = 3.5;
const CLUB_HIT_SHAKE_INTENSITY = 4;
const CLUB_PLAYER_KNOCKBACK = 4; // Player recoil when hitting enemies/boxes
const CLUB_DRAW_TIP_PADDING = 14;
const CLUB_SPRITE_ROTATION_OFFSET = Math.PI / 4;
const CLUB_FALLBACK_STROKE_BASE = 4;
const CLUB_FALLBACK_STROKE_VARIATION = 5;
const CLUB_DAMAGE = 0.5;
const DEBUG_HITBOX_LINE_WIDTH = 1;
const DEBUG_HITBOX_FILL_ALPHA = 0.14;
const DEBUG_HITBOX_ENTITY_COLOR = "rgba(255,0,0,0.7)";
const DEBUG_HITBOX_PLAYER_COLOR = "rgba(0,255,0,0.9)";
const DEBUG_HITBOX_SWORD_COLOR = "rgba(120,200,255,0.95)";
const DEBUG_HITBOX_CLUB_COLOR = "rgba(255,220,120,0.95)";
const SWORD_DRAW_ALPHA = 0.95;
const SWORD_DRAW_SHADOW_COLOR = "rgba(45, 121, 255, 0.5)";
const SWORD_DRAW_SHADOW_BLUR = 12;
// Sword attack animation canvases for rendering (right & left directions + extended reach)
let swordAnimFrames = {
right: [], // frames 0-6 (original right-facing sprites)
left: [], // frames 0-6 (flipped left-facing sprites)
rightExtended: [], // frames 0-6 (right with extended reach pixels)
leftExtended: [] // frames 0-6 (left with extended reach pixels)
};
// Initialize sword animation frames from the sprite sheet
function initSwordAnimFrames() {
swordAnimFrames.right.length = 0;
swordAnimFrames.left.length = 0;
swordAnimFrames.rightExtended.length = 0;
swordAnimFrames.leftExtended.length = 0;
if (!swordAttackSheet.complete || !swordAttackSheet.naturalWidth) return;
for (let frameIdx = 0; frameIdx < SWORD_FRAME_COUNT; frameIdx++) {
// Extract frame from sprite sheet
const sourceX = frameIdx * SWORD_FRAME_WIDTH;
const sourceY = 0;
// Create canvas for right-facing frame
const rightCanvas = document.createElement('canvas');
rightCanvas.width = SWORD_FRAME_WIDTH;
rightCanvas.height = SWORD_FRAME_HEIGHT;
const rightCtx = rightCanvas.getContext('2d');
rightCtx.drawImage(swordAttackSheet, sourceX, sourceY, SWORD_FRAME_WIDTH, SWORD_FRAME_HEIGHT, 0, 0, SWORD_FRAME_WIDTH, SWORD_FRAME_HEIGHT);
swordAnimFrames.right[frameIdx] = rightCanvas;
// Create canvas for left-facing frame (horizontally flipped)
const leftCanvas = document.createElement('canvas');
leftCanvas.width = SWORD_FRAME_WIDTH;
leftCanvas.height = SWORD_FRAME_HEIGHT;
const leftCtx = leftCanvas.getContext('2d');
leftCtx.scale(-1, 1);
leftCtx.drawImage(swordAttackSheet, sourceX, sourceY, SWORD_FRAME_WIDTH, SWORD_FRAME_HEIGHT, -SWORD_FRAME_WIDTH, 0, SWORD_FRAME_WIDTH, SWORD_FRAME_HEIGHT);
swordAnimFrames.left[frameIdx] = leftCanvas;
// Create extended reach frames (stretch sprite for reach extension)
// Right extended - stretched
const rightExtCanvas = document.createElement('canvas');
rightExtCanvas.width = SWORD_FRAME_WIDTH + SWORD_ATTACK_EXTENSION_WIDTH;
rightExtCanvas.height = SWORD_FRAME_HEIGHT;
const rightExtCtx = rightExtCanvas.getContext('2d');
rightExtCtx.drawImage(swordAttackSheet, sourceX, sourceY, SWORD_FRAME_WIDTH, SWORD_FRAME_HEIGHT, 0, 0, SWORD_FRAME_WIDTH + SWORD_ATTACK_EXTENSION_WIDTH, SWORD_FRAME_HEIGHT);
swordAnimFrames.rightExtended[frameIdx] = rightExtCanvas;
// Left extended - stretched and mirrored
const leftExtCanvas = document.createElement('canvas');
leftExtCanvas.width = SWORD_FRAME_WIDTH + SWORD_ATTACK_EXTENSION_WIDTH;
leftExtCanvas.height = SWORD_FRAME_HEIGHT;
const leftExtCtx = leftExtCanvas.getContext('2d');
leftExtCtx.scale(-1, 1);
leftExtCtx.drawImage(swordAttackSheet, sourceX, sourceY, SWORD_FRAME_WIDTH, SWORD_FRAME_HEIGHT, -(SWORD_FRAME_WIDTH + SWORD_ATTACK_EXTENSION_WIDTH), 0, SWORD_FRAME_WIDTH + SWORD_ATTACK_EXTENSION_WIDTH, SWORD_FRAME_HEIGHT);
swordAnimFrames.leftExtended[frameIdx] = leftExtCanvas;
}
}
// Listen for sprite sheet load
swordAttackSheet.addEventListener('load', initSwordAnimFrames);
// Try to init if already loaded
setTimeout(initSwordAnimFrames, 100);
// --- PLAYER ANIMATION FRAMES ---
const playerWalkSheet = new Image();
playerWalkSheet.src = "Assets/Sprites/MACHO.png"; // your sheet
const WALK_FRAME_WIDTH = 70;
const WALK_FRAME_HEIGHT = 90;
const WALK_FRAME_COUNT = 8;
const PLAYER_SPRITE_H = 64;
const PLAYER_SPRITE_W = Math.round(PLAYER_SPRITE_H * (WALK_FRAME_WIDTH / WALK_FRAME_HEIGHT)); // ~50
const PLAYER_SPRITE_OX = Math.round((PLAYER_SPRITE_W - 32) / 2); // horizontal overflow offset (~9px)
const playerIdleImg = new Image();
playerIdleImg.src = "Assets/Sprites/Player_idle.png"; // or keep using Player.png
/*const playerJumpImg = new Image();
playerJumpImg.src = "Assets/Sprites/Player_jump.png"; // optional, for later*/
const playerImg = new Image();
playerImg.src = "Assets/Sprites/Player.png";
const snailFrames = [];
const snail1 = new Image();
snail1.src = "Assets/Sprites/Snail 1.png";
const snail2 = new Image();
snail2.src = "Assets/Sprites/Snail 2.png";
const snail3 = new Image();
snail3.src = "Assets/Sprites/Snail 3.png";
snailFrames.push(snail1, snail2, snail3, snail2);
const chairFrames = [];
for (let i = 0; i < 5; i++) {
const img = new Image();
img.src = `Assets/Sprites/pixil-frame-${i}.png`;
chairFrames.push(img);
}
const settings = {
// Graphics
graphicsQuality: 50,
lighting: true,
particles: true,
decor: true,
enemyHealthBars: true,
screenShake: true,
offScreenIndicators: true,
// Audio
sfxEnabled: true,
musicVolume: 0.5,
sfxVolume: 0.5,
// Gameplay
invincible: false,
infiniteLives: false,
infiniteDash: false,
showHitboxes: false,
speedMultiplier: 1,
// Controls
swapJumpDash: false,
vibration: true,
joystickDeadzone: 0.2,
// UI
showFPS: false,
showCoords: false,
showMinimap: true,