-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.js
More file actions
2297 lines (2123 loc) · 81.9 KB
/
engine.js
File metadata and controls
2297 lines (2123 loc) · 81.9 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
// =====================================================================
// CODEREGON TRAIL — Shared Game Engine
// =====================================================================
// Game HTML files define these globals before loading this script:
// window.TRAIL_DATA — trail JSON (stops, events, partyMembers, deathMessages)
// window.TRAIL_CONFIG — { mountainColors: { far, near } } (optional)
// window.TRAIL_FLAVORS — array of travel flavor strings (optional)
// window.drawCustomEventOverlay(time) — per-game event animations (optional)
// =====================================================================
const TRAIL_DATA = window.TRAIL_DATA;
const _TRAIL_CONFIG = window.TRAIL_CONFIG || {};
const _MC = _TRAIL_CONFIG.mountainColors || { far: '#AA00AA', near: '#FF55FF' };
// Mobile detection
var isMobile = /Android|iPhone|iPad|iPod|webOS|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) || ('ontouchstart' in window && window.innerWidth < 900);
// Inject viewport meta for mobile
(function injectViewport() {
if (!document.querySelector('meta[name="viewport"]')) {
var m = document.createElement('meta');
m.name = 'viewport';
m.content = 'width=device-width, initial-scale=1.0';
document.head.appendChild(m);
}
})();
// Google Analytics (GA4) — loaded dynamically so every game page is covered via engine.js
(function injectGA() {
var GA_ID = 'G-LKPR91782L';
if (window.gtag) return;
var s = document.createElement('script');
s.async = true;
s.src = 'https://www.googletagmanager.com/gtag/js?id=' + GA_ID;
document.head.appendChild(s);
window.dataLayer = window.dataLayer || [];
window.gtag = function() { window.dataLayer.push(arguments); };
window.gtag('js', new Date());
window.gtag('config', GA_ID);
})();
function trackEvent(name, params) {
if (typeof window.gtag !== 'function') return;
try { window.gtag('event', name, params || {}); } catch (e) {}
}
function getGameSlug() {
var path = window.location.pathname.replace(/\/index\.html$/, '').replace(/\/$/, '');
var parts = path.split('/');
return parts[parts.length - 1] || (TRAIL_DATA && TRAIL_DATA.framework) || 'unknown';
}
// Inject CSS
(function injectStyles() {
var s = document.createElement('style');
s.textContent = `
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
html, body {
height: 100vh;
overflow: hidden;
background: #000;
font-family: 'Courier New', monospace;
color: #AAAAAA;
}
#game-container {
display: flex;
flex-direction: column;
height: 100vh;
width: 100vw;
overflow: hidden;
position: relative;
}
#canvas-area {
height: 52vh;
min-height: 0;
position: relative;
background: #000;
}
#game-canvas {
width: 100%;
height: 100%;
image-rendering: pixelated;
image-rendering: -moz-crisp-edges;
image-rendering: crisp-edges;
display: block;
}
#text-panel {
flex: 1;
min-height: 0;
background: #000;
border-top: 2px solid #555555;
overflow-y: auto;
padding: 8px 16px;
font-size: 14px;
line-height: 1.4;
color: #FFFFFF;
}
#text-panel > div { max-width: 720px; margin: 0 auto; }
#text-panel.code-expanded {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 62px;
z-index: 10;
}
#text-panel::-webkit-scrollbar { width: 8px; }
#text-panel::-webkit-scrollbar-track { background: #000; }
#text-panel::-webkit-scrollbar-thumb { background: #555555; }
#status-bar {
height: 60px;
min-height: 60px;
background: #000;
border-top: 2px solid #555555;
display: flex;
align-items: center;
padding: 0 16px;
font-size: 15px;
color: #AAAAAA;
white-space: nowrap;
overflow: hidden;
}
.blink {
animation: blink-anim 1s step-end infinite;
}
@keyframes blink-anim {
0%, 100% { opacity: 1; }
50% { opacity: 0; }
}
@keyframes shake {
0%, 100% { transform: translateX(0); }
10%, 30%, 50%, 70%, 90% { transform: translateX(-4px); }
20%, 40%, 60%, 80% { transform: translateX(4px); }
}
.shake { animation: shake 0.5s ease-in-out; }
.code-box {
background: #000;
border: 1px solid #555555;
padding: 6px 8px;
margin: 6px 0;
max-height: 130px;
overflow-y: auto;
overflow-x: auto;
font-size: 12px;
line-height: 1.3;
white-space: pre;
color: #55FF55;
cursor: pointer;
transition: max-height 0.2s ease;
}
.code-box.expanded { max-height: none; }
.code-box.shiki-rendered { padding: 0; background: transparent; }
.code-box.shiki-rendered pre.shiki { margin: 0; padding: 6px 8px; font-size: 12px; line-height: 1.3; overflow: visible; }
.code-box.shiki-rendered code { font-family: 'Courier New', monospace; font-size: inherit; line-height: inherit; }
.code-box::-webkit-scrollbar { width: 6px; height: 6px; }
.code-box::-webkit-scrollbar-track { background: #000; }
.code-box::-webkit-scrollbar-thumb { background: #555555; }
.choice-item { cursor: pointer; padding: 2px 4px; }
.choice-item:hover { color: #FFFF55; }
.choice-selected { color: #FFFF55; background: #333300; }
.stop-opt { color: #55FFFF; cursor: pointer; padding: 2px 4px; }
.stop-opt:hover { color: #FFFF55; }
.choice-correct { color: #55FF55; }
.choice-wrong { color: #FF5555; }
.choice-dimmed { color: #555555; }
.health-green { color: #00AA00; }
.health-yellow { color: #FFFF55; }
.health-red { color: #FF5555; }
.health-dead { color: #555555; text-decoration: line-through; }
.text-cyan { color: #55FFFF; }
.text-yellow { color: #FFFF55; }
.text-green { color: #55FF55; }
.text-red { color: #FF5555; }
.text-magenta { color: #FF55FF; }
.text-gray { color: #555555; }
.text-white { color: #FFFFFF; }
.text-bright { color: #FFFFFF; font-weight: bold; }
#music-indicator {
position: absolute;
top: 4px;
right: 8px;
font-size: 11px;
color: #555555;
z-index: 10;
pointer-events: none;
}
#mobile-controls {
display: none;
position: absolute;
bottom: 66px;
right: 8px;
z-index: 20;
gap: 6px;
}
#mobile-controls button {
background: #111;
border: 1px solid #555;
color: #AAAAAA;
font-family: 'Courier New', monospace;
font-size: 18px;
width: 44px;
height: 44px;
cursor: pointer;
border-radius: 4px;
-webkit-tap-highlight-color: transparent;
}
#mobile-controls button:active {
background: #333;
color: #FFFF55;
}
@media (max-width: 600px), (hover: none) and (pointer: coarse) {
#text-panel { font-size: 15px; padding: 10px 12px; }
.choice-item { padding: 10px 8px !important; margin: 6px 0 !important; font-size: 15px; min-height: 44px; display: flex; align-items: center; }
.stop-opt { padding: 10px 16px !important; font-size: 15px; display: inline-block; min-height: 44px; }
#status-bar { font-size: 12px; height: 52px; min-height: 52px; padding: 0 8px; }
#canvas-area { height: 35vh; }
#text-panel > div { max-width: 100%; }
.code-box { font-size: 11px; max-height: 100px; }
#mobile-controls { display: flex; }
}
`;
document.head.appendChild(s);
})();
// =====================================================================
// GAME STATE
// =====================================================================
const STATES = { TITLE: 'TITLE', SETUP: 'SETUP', TRAVEL: 'TRAVEL', STOP: 'STOP', EVENT: 'EVENT', RIVER: 'RIVER', DEATH: 'DEATH', WIN: 'WIN' };
const PROFESSIONS = [
{ name: 'Ralph Wiggum', desc: '"I\'m learnding!"', health: 999, supplies: 999, hintFree: true, forgiving: true, wrongDmg: 15, riverDmg: 20, drainInterval: 0, healOnCorrect: 10, partyMaxHp: 3 },
{ name: 'Vibe Coder', desc: '"It works on my machine"', health: 100, supplies: 5, hintFree: false, forgiving: false, wrongDmg: 15, riverDmg: 20, drainInterval: 3000, healOnCorrect: 10, partyMaxHp: 3 },
{ name: 'Engineer', desc: '"Let me check the docs"', health: 70, supplies: 2, hintFree: false, forgiving: false, wrongDmg: 20, riverDmg: 25, drainInterval: 2500, healOnCorrect: 5, partyMaxHp: 2 },
{ name: 'Staff Architect', desc: '"I designed this system"', health: 50, supplies: 0, hintFree: false, forgiving: false, wrongDmg: 25, riverDmg: 30, drainInterval: 2000, healOnCorrect: 0, partyMaxHp: 1 }
];
let gameState = STATES.TITLE;
let difficulty = 1;
let health = 100;
let maxHealthForGame = 100;
let supplies = 5;
let currentStop = 0;
let day = 1;
let score = 0;
let totalQuestions = 0;
let conceptScores = {};
let streak = 0;
let bestStreak = 0;
let hintsUsed = 0;
let scrollX = 0;
let travelTimer = null;
let animFrame = null;
let partyHealth = [];
let pendingEvents = [];
let currentEventIndex = 0;
let eventAnswered = false;
let eventHinted = false;
let dimmedChoice = -1;
let selectedEventChoice = -1;
let selectedStopChoice = 0;
let shuffledIndices = []; // maps display position -> original choice index
let selectedDifficulty = (function() {
try { var d = parseInt(localStorage.getItem('coderegon-trail:difficulty')); return (d >= 0 && d <= 3) ? d : 1; }
catch(e) { return 1; }
})();
let musicPlaying = false;
let audioCtx = null;
let musicInitialized = false;
let typewriterTimer = null;
let typewriterText = '';
let typewriterIndex = 0;
let showRiver = false;
let wagonWheelAngle = 0;
let travelFlavorIndex = 0;
let deathPending = false;
let currentEventType = ''; // event type for canvas overlay
let currentEventTitle = ''; // event title for canvas overlay
const travelFlavors = window.TRAIL_FLAVORS || [
"The wagon creaks along the trail...",
"Dust rises from the wheels as you press onward...",
"The landscape shifts as new territory opens up...",
"Fellow travelers share stories around the campfire...",
"The trail stretches endlessly toward the horizon...",
"Progress is steady. The destination draws closer..."
];
// =====================================================================
// CANVAS RENDERING (320x200 internal)
// =====================================================================
const canvas = document.getElementById('game-canvas');
const ctx = canvas.getContext('2d');
ctx.imageSmoothingEnabled = false;
const W = 320, H = 200;
// Seeded PRNG for consistent landscape
function seededRandom(seed) {
let s = seed;
return function() {
s = (s * 1664525 + 1013904223) & 0xFFFFFFFF;
return (s >>> 0) / 0xFFFFFFFF;
};
}
const rng = seededRandom(42);
const cloudPositions = [];
for (let i = 0; i < 5; i++) {
cloudPositions.push({ x: rng() * 400, y: 10 + rng() * 30, w: 25 + rng() * 30, h: 8 + rng() * 6 });
}
const farMountains = [];
for (let i = 0; i < 6; i++) {
farMountains.push({ x: i * 70 - 20 + rng() * 30, h: 40 + rng() * 35, w: 50 + rng() * 40 });
}
const nearMountains = [];
for (let i = 0; i < 8; i++) {
nearMountains.push({ x: i * 55 - 30 + rng() * 20, h: 25 + rng() * 25, w: 30 + rng() * 25, snow: rng() > 0.3 });
}
const treesData = [];
for (let i = 0; i < 25; i++) {
treesData.push({ x: rng() * 600, h: 12 + rng() * 16 });
}
function getSkyColors() {
const progress = currentStop / Math.max(TRAIL_DATA.stops.length, 1);
if (progress < 0.15) {
return { top: '#5500AA', bottom: '#FF5555' };
} else if (progress < 0.7) {
return { top: '#5555FF', bottom: '#55FFFF' };
} else {
return { top: '#5500AA', bottom: '#FFAA00' };
}
}
function drawPixelCloud(x, y, w, h) {
ctx.fillStyle = '#FFFFFF';
const ix = Math.floor(x), iy = Math.floor(y), iw = Math.floor(w), ih = Math.floor(h);
ctx.fillRect(ix, iy, iw, ih);
ctx.fillRect(ix + Math.floor(iw * 0.15), iy - Math.floor(ih * 0.5), Math.floor(iw * 0.3), Math.floor(ih * 0.6));
ctx.fillRect(ix + Math.floor(iw * 0.4), iy - Math.floor(ih * 0.7), Math.floor(iw * 0.35), Math.floor(ih * 0.8));
ctx.fillRect(ix + Math.floor(iw * 0.65), iy - Math.floor(ih * 0.3), Math.floor(iw * 0.2), Math.floor(ih * 0.4));
}
function drawMountain(x, h, w, color, snowCap) {
const baseY = 110;
ctx.fillStyle = color;
ctx.beginPath();
ctx.moveTo(Math.floor(x), baseY);
ctx.lineTo(Math.floor(x + w / 2), Math.floor(baseY - h));
ctx.lineTo(Math.floor(x + w), baseY);
ctx.closePath();
ctx.fill();
if (snowCap) {
ctx.fillStyle = '#FFFFFF';
const peakX = x + w / 2;
const peakY = baseY - h;
ctx.beginPath();
ctx.moveTo(Math.floor(peakX - w * 0.1), Math.floor(peakY + h * 0.18));
ctx.lineTo(Math.floor(peakX), Math.floor(peakY));
ctx.lineTo(Math.floor(peakX + w * 0.1), Math.floor(peakY + h * 0.18));
ctx.closePath();
ctx.fill();
}
}
function drawTree(x, baseY, h) {
const trunkW = 3, trunkH = Math.floor(h * 0.3);
ctx.fillStyle = '#AA5500';
ctx.fillRect(Math.floor(x - 1), Math.floor(baseY - trunkH), trunkW, trunkH);
for (let i = 0; i < 3; i++) {
const layerW = (h * 0.4) * (1 - i * 0.15);
const layerY = baseY - trunkH - (i * h * 0.2);
ctx.fillStyle = i === 1 ? '#00AA00' : '#005500';
ctx.beginPath();
ctx.moveTo(Math.floor(x - layerW / 2), Math.floor(layerY));
ctx.lineTo(Math.floor(x + 0.5), Math.floor(layerY - h * 0.25));
ctx.lineTo(Math.floor(x + layerW / 2), Math.floor(layerY));
ctx.closePath();
ctx.fill();
}
}
function drawWagon(wx, wy) {
// Wagon body (left/rear)
ctx.fillStyle = '#AA5500';
ctx.fillRect(Math.floor(wx - 26), Math.floor(wy - 8), 24, 10);
// Wagon sides darker
ctx.fillStyle = '#885500';
ctx.fillRect(Math.floor(wx - 26), Math.floor(wy - 8), 24, 1);
ctx.fillRect(Math.floor(wx - 26), Math.floor(wy + 1), 24, 1);
// Canvas cover (white curved top)
ctx.fillStyle = '#FFFFFF';
ctx.beginPath();
ctx.moveTo(Math.floor(wx - 25), Math.floor(wy - 8));
ctx.quadraticCurveTo(Math.floor(wx - 14), Math.floor(wy - 22), Math.floor(wx - 3), Math.floor(wy - 8));
ctx.closePath();
ctx.fill();
// Canvas cover outline
ctx.strokeStyle = '#AAAAAA';
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(Math.floor(wx - 25), Math.floor(wy - 8));
ctx.quadraticCurveTo(Math.floor(wx - 14), Math.floor(wy - 22), Math.floor(wx - 3), Math.floor(wy - 8));
ctx.stroke();
// Canvas ribs
ctx.strokeStyle = '#AAAAAA';
for (let ri = 0; ri < 3; ri++) {
const rx = wx - 20 + ri * 5;
ctx.beginPath();
ctx.moveTo(Math.floor(rx), Math.floor(wy - 8));
ctx.lineTo(Math.floor(rx), Math.floor(wy - 14 - ri % 2 * 2));
ctx.stroke();
}
// Yoke
ctx.fillStyle = '#AA5500';
ctx.fillRect(Math.floor(wx - 2), Math.floor(wy - 4), 6, 2);
// Oxen (right/front — leading the wagon)
ctx.fillStyle = '#AA5500';
ctx.fillRect(Math.floor(wx + 6), Math.floor(wy - 6), 8, 6);
ctx.fillRect(Math.floor(wx + 16), Math.floor(wy - 6), 8, 6);
// Oxen legs
ctx.fillStyle = '#553300';
ctx.fillRect(Math.floor(wx + 8), Math.floor(wy), 2, 4);
ctx.fillRect(Math.floor(wx + 12), Math.floor(wy), 2, 4);
ctx.fillRect(Math.floor(wx + 18), Math.floor(wy), 2, 4);
ctx.fillRect(Math.floor(wx + 22), Math.floor(wy), 2, 4);
// Oxen heads (facing right)
ctx.fillStyle = '#885500';
ctx.fillRect(Math.floor(wx + 23), Math.floor(wy - 7), 3, 3);
ctx.fillRect(Math.floor(wx + 13), Math.floor(wy - 7), 3, 3);
// Wheels
const wheelR = 5;
for (let wi = 0; wi < 2; wi++) {
const wcx = wx - 21 + wi * 14;
const wcy = wy + 4;
// Outer rim
ctx.fillStyle = '#553300';
ctx.beginPath();
ctx.arc(Math.floor(wcx), Math.floor(wcy), wheelR, 0, Math.PI * 2);
ctx.fill();
// Inner
ctx.fillStyle = '#AA5500';
ctx.beginPath();
ctx.arc(Math.floor(wcx), Math.floor(wcy), wheelR - 1.5, 0, Math.PI * 2);
ctx.fill();
// Spokes
ctx.strokeStyle = '#553300';
ctx.lineWidth = 1;
for (let si = 0; si < 6; si++) {
const angle = wagonWheelAngle + (si * Math.PI / 3);
ctx.beginPath();
ctx.moveTo(Math.floor(wcx), Math.floor(wcy));
ctx.lineTo(Math.floor(wcx + Math.cos(angle) * (wheelR - 1)), Math.floor(wcy + Math.sin(angle) * (wheelR - 1)));
ctx.stroke();
}
// Hub
ctx.fillStyle = '#553300';
ctx.fillRect(Math.floor(wcx - 1), Math.floor(wcy - 1), 2, 2);
}
// Purple flag (on wagon cover peak)
ctx.fillStyle = '#AA00AA';
ctx.fillRect(Math.floor(wx - 14), Math.floor(wy - 22), 1, -7);
ctx.fillStyle = '#FF55FF';
ctx.fillRect(Math.floor(wx - 13), Math.floor(wy - 29), 6, 4);
}
function drawLandmark(type, x, baseY) {
switch (type) {
case 'town':
ctx.fillStyle = '#AA5500';
ctx.fillRect(Math.floor(x), Math.floor(baseY - 18), 14, 18);
ctx.fillRect(Math.floor(x + 18), Math.floor(baseY - 14), 12, 14);
// Roofs
ctx.fillStyle = '#555555';
ctx.beginPath();
ctx.moveTo(Math.floor(x - 2), Math.floor(baseY - 18));
ctx.lineTo(Math.floor(x + 7), Math.floor(baseY - 25));
ctx.lineTo(Math.floor(x + 16), Math.floor(baseY - 18));
ctx.closePath();
ctx.fill();
ctx.beginPath();
ctx.moveTo(Math.floor(x + 16), Math.floor(baseY - 14));
ctx.lineTo(Math.floor(x + 24), Math.floor(baseY - 20));
ctx.lineTo(Math.floor(x + 32), Math.floor(baseY - 14));
ctx.closePath();
ctx.fill();
// Windows
ctx.fillStyle = '#FFFF55';
ctx.fillRect(Math.floor(x + 3), Math.floor(baseY - 13), 3, 3);
ctx.fillRect(Math.floor(x + 9), Math.floor(baseY - 13), 3, 3);
ctx.fillRect(Math.floor(x + 21), Math.floor(baseY - 10), 3, 3);
// Door
ctx.fillStyle = '#553300';
ctx.fillRect(Math.floor(x + 5), Math.floor(baseY - 7), 4, 7);
break;
case 'camp':
// Tent
ctx.fillStyle = '#AAAAAA';
ctx.beginPath();
ctx.moveTo(Math.floor(x - 6), Math.floor(baseY));
ctx.lineTo(Math.floor(x + 4), Math.floor(baseY - 14));
ctx.lineTo(Math.floor(x + 14), Math.floor(baseY));
ctx.closePath();
ctx.fill();
ctx.fillStyle = '#555555';
ctx.beginPath();
ctx.moveTo(Math.floor(x + 4), Math.floor(baseY));
ctx.lineTo(Math.floor(x + 4), Math.floor(baseY - 14));
ctx.lineTo(Math.floor(x + 14), Math.floor(baseY));
ctx.closePath();
ctx.fill();
// Campfire
ctx.fillStyle = '#FF5555';
ctx.fillRect(Math.floor(x + 20), Math.floor(baseY - 5), 4, 3);
ctx.fillStyle = '#FFFF55';
ctx.fillRect(Math.floor(x + 21), Math.floor(baseY - 8), 2, 3);
ctx.fillStyle = '#FFAA00';
ctx.fillRect(Math.floor(x + 18), Math.floor(baseY - 2), 8, 2);
break;
case 'mountain':
drawMountain(x - 20, 60, 50, _MC.far, true);
drawMountain(x + 10, 45, 40, _MC.near, true);
break;
case 'river':
ctx.fillStyle = '#AA5500';
ctx.fillRect(Math.floor(x), Math.floor(baseY - 20), 3, 20);
ctx.fillRect(Math.floor(x + 24), Math.floor(baseY - 20), 3, 20);
ctx.strokeStyle = '#AAAAAA';
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(Math.floor(x + 1), Math.floor(baseY - 18));
ctx.lineTo(Math.floor(x + 25), Math.floor(baseY - 18));
ctx.stroke();
// Sign
ctx.fillStyle = '#AA5500';
ctx.fillRect(Math.floor(x + 8), Math.floor(baseY - 25), 12, 8);
ctx.fillStyle = '#FFFFFF';
ctx.fillRect(Math.floor(x + 10), Math.floor(baseY - 23), 2, 1);
ctx.fillRect(Math.floor(x + 13), Math.floor(baseY - 23), 2, 1);
ctx.fillRect(Math.floor(x + 16), Math.floor(baseY - 23), 2, 1);
break;
case 'forest':
for (let i = 0; i < 6; i++) {
drawTree(x + i * 7, baseY, 14 + (i % 3) * 3);
}
break;
case 'desert':
ctx.fillStyle = '#FFAA00';
ctx.fillRect(Math.floor(x - 5), Math.floor(baseY - 3), 40, 3);
ctx.beginPath();
ctx.moveTo(Math.floor(x - 10), Math.floor(baseY));
ctx.quadraticCurveTo(Math.floor(x + 5), Math.floor(baseY - 8), Math.floor(x + 20), Math.floor(baseY));
ctx.fill();
ctx.fillStyle = '#00AA00';
ctx.fillRect(Math.floor(x + 25), Math.floor(baseY - 18), 3, 18);
ctx.fillRect(Math.floor(x + 22), Math.floor(baseY - 14), 3, 6);
ctx.fillRect(Math.floor(x + 28), Math.floor(baseY - 12), 3, 5);
ctx.fillRect(Math.floor(x + 22), Math.floor(baseY - 14), 1, 1);
ctx.fillRect(Math.floor(x + 30), Math.floor(baseY - 12), 1, 1);
break;
}
}
function drawRiver(time) {
const riverY = 155;
const riverH = 18;
ctx.fillStyle = '#0000AA';
ctx.fillRect(0, riverY, W, riverH);
ctx.fillStyle = '#00AAAA';
for (let wx = 0; wx < W; wx += 3) {
const waveY = riverY + 3 + Math.sin((wx + time * 0.05) * 0.08) * 3;
ctx.fillRect(wx, Math.floor(waveY), 2, 1);
const waveY2 = riverY + 10 + Math.sin((wx + time * 0.03 + 50) * 0.1) * 2;
ctx.fillRect(wx, Math.floor(waveY2), 2, 1);
}
// White foam
ctx.fillStyle = '#55FFFF';
for (let wx = 0; wx < W; wx += 12) {
const foamY = riverY + 1 + Math.sin((wx + time * 0.04) * 0.06) * 1;
ctx.fillRect(wx + 2, Math.floor(foamY), 3, 1);
}
}
// =====================================================================
// EVENT CANVAS OVERLAYS — unique visuals per quiz event
// =====================================================================
function drawEventOverlay(time) {
if (!currentEventType && !currentEventTitle) return;
if (typeof window.drawCustomEventOverlay === 'function') {
window.drawCustomEventOverlay(time);
return;
}
// Generic fallback overlays by event type
var t = (time || 0) * 0.001;
if (currentEventType === 'weather') {
ctx.fillStyle = 'rgba(0,0,50,0.45)';
ctx.fillRect(0, 0, W, 110);
var flashPhase = Math.sin(t * 1.7) + Math.sin(t * 3.1);
if (flashPhase > 1.6) {
ctx.fillStyle = 'rgba(255,255,255,0.4)';
ctx.fillRect(0, 0, W, H);
ctx.strokeStyle = '#FFFF55';
ctx.lineWidth = 2;
ctx.beginPath();
var lx = 80 + Math.sin(t * 2.3) * 60;
ctx.moveTo(lx, 10); ctx.lineTo(lx - 5, 35); ctx.lineTo(lx + 8, 35);
ctx.lineTo(lx - 3, 65); ctx.lineTo(lx + 10, 65); ctx.lineTo(lx + 2, 95);
ctx.stroke();
}
ctx.strokeStyle = '#5555FF'; ctx.lineWidth = 1;
for (var i = 0; i < 40; i++) {
var rx = (i * 8.3 + t * 80) % W, ry = (i * 13.7 + t * 120) % 110;
ctx.beginPath(); ctx.moveTo(Math.floor(rx), Math.floor(ry));
ctx.lineTo(Math.floor(rx - 1), Math.floor(ry + 5)); ctx.stroke();
}
} else if (currentEventType === 'river') {
ctx.fillStyle = '#55FFFF';
for (var i = 0; i < 20; i++) {
var sx = (i * 17 + t * 40) % W, sy = 157 + Math.sin(i * 3 + t * 5) * 3;
ctx.fillRect(Math.floor(sx), Math.floor(sy), 4, 2);
}
ctx.fillStyle = '#AA5500';
for (var i = 0; i < 3; i++) {
var logX = (i * 110 + t * 25) % W, logY = 159 + Math.sin(t * 2 + i) * 2;
ctx.fillRect(Math.floor(logX), Math.floor(logY), 8, 2);
}
} else if (currentEventType === 'encounter') {
var px = 200, py = 142;
ctx.fillStyle = '#AA5500'; ctx.fillRect(px, py - 12, 4, 8);
ctx.fillStyle = '#FFAA00'; ctx.fillRect(px, py - 15, 4, 3);
ctx.fillRect(px - 1, py - 4, 2, 5); ctx.fillRect(px + 3, py - 4, 2, 5);
ctx.fillStyle = '#555555'; ctx.fillRect(px - 1, py - 17, 6, 2); ctx.fillRect(px, py - 19, 4, 2);
var fx = 212, fy = 148;
ctx.fillStyle = '#AA5500'; ctx.fillRect(fx - 3, fy, 2, 3); ctx.fillRect(fx + 3, fy, 2, 3);
ctx.fillStyle = '#FF5555'; var flicker = Math.sin(t * 8) * 2;
ctx.fillRect(fx - 1, fy - 3 + Math.floor(flicker * 0.3), 4, 3);
ctx.fillStyle = '#FFFF55'; ctx.fillRect(fx, fy - 5 + Math.floor(flicker * 0.5), 2, 2);
} else if (currentEventType === 'misfortune') {
ctx.fillStyle = 'rgba(170,0,0,0.25)'; ctx.fillRect(0, 0, W, H);
ctx.fillStyle = '#555555';
for (var i = 0; i < 15; i++) {
var sx = 140 + Math.sin(i * 2.7 + t) * 30, sy = 130 - ((t * 20 + i * 15) % 90);
ctx.globalAlpha = 0.3 + Math.sin(t + i) * 0.15;
ctx.fillRect(Math.floor(sx), Math.floor(sy), 2 + (i % 3), 2 + (i % 3));
}
ctx.globalAlpha = 1.0;
ctx.fillStyle = '#FF5555';
for (var i = 0; i < 8; i++) {
var fx = 130 + i * 5, fh = 4 + Math.sin(t * 6 + i * 1.5) * 3;
ctx.fillRect(fx, Math.floor(140 - fh), 3, Math.floor(fh));
}
}
}
// =====================================================================
// DEATH SCREEN — Apple II green phosphor style
// =====================================================================
function drawDeathScene(time) {
var t = (time || 0) * 0.001;
var G = '#33FF33'; // Apple II green
var GD = '#1a8a1a'; // darker green for details
// Black background
ctx.fillStyle = '#000000';
ctx.fillRect(0, 0, W, H);
// Ground line
ctx.fillStyle = GD;
ctx.fillRect(0, 148, W, 1);
// --- WAGON (left side / rear) ---
var wx = 82, wy = 108;
// Wagon bed
ctx.fillStyle = G;
ctx.fillRect(wx, wy + 20, 50, 16);
// Bed slats (dark lines)
ctx.fillStyle = GD;
ctx.fillRect(wx, wy + 24, 50, 1);
ctx.fillRect(wx, wy + 28, 50, 1);
ctx.fillRect(wx, wy + 32, 50, 1);
// Side rails
ctx.fillStyle = G;
ctx.fillRect(wx, wy + 18, 2, 18);
ctx.fillRect(wx + 48, wy + 18, 2, 18);
// Canvas cover (the bonnet)
ctx.fillStyle = G;
// Left arch
ctx.fillRect(wx + 4, wy + 4, 2, 16);
// Right arch
ctx.fillRect(wx + 44, wy + 4, 2, 16);
// Top cover
ctx.fillRect(wx + 4, wy + 2, 42, 4);
// Cover fill
ctx.fillRect(wx + 6, wy + 6, 38, 12);
// Cover shading
ctx.fillStyle = GD;
ctx.fillRect(wx + 8, wy + 8, 34, 2);
ctx.fillRect(wx + 8, wy + 12, 34, 2);
ctx.fillRect(wx + 8, wy + 16, 34, 1);
// --- HITCH ---
ctx.fillStyle = G;
ctx.fillRect(wx + 50, wy + 28, 14, 2);
// --- OX (right side / front, facing right) ---
var ox = wx + 64, oy = 128;
// Body
ctx.fillStyle = G;
ctx.fillRect(ox, oy, 24, 14);
// Head (facing right)
ctx.fillRect(ox + 22, oy - 2, 12, 10);
// Horns
ctx.fillRect(ox + 25, oy - 6, 3, 5);
ctx.fillRect(ox + 33, oy - 6, 3, 5);
// Snout
ctx.fillStyle = GD;
ctx.fillRect(ox + 28, oy + 4, 6, 3);
// Eye
ctx.fillStyle = '#000';
ctx.fillRect(ox + 29, oy, 2, 2);
// Legs
ctx.fillStyle = G;
ctx.fillRect(ox + 4, oy + 14, 4, 8);
ctx.fillRect(ox + 10, oy + 14, 4, 8);
ctx.fillRect(ox + 18, oy + 14, 4, 8);
// Tail (on left/back)
ctx.fillRect(ox - 6, oy + 2, 6, 2);
ctx.fillRect(ox - 7, oy, 2, 4);
// Wheels
var wheelR = 8;
var wheels = [wx + 8, wx + 42];
var wa = -t * 0.5; // clockwise rotation for rightward travel
ctx.strokeStyle = G;
ctx.lineWidth = 1.5;
for (var wi = 0; wi < wheels.length; wi++) {
var cx = wheels[wi], cy = wy + 38;
// Rim
ctx.beginPath();
ctx.arc(cx, cy, wheelR, 0, Math.PI * 2);
ctx.stroke();
// Spokes
for (var sp = 0; sp < 6; sp++) {
var angle = wa + sp * Math.PI / 3;
ctx.beginPath();
ctx.moveTo(cx, cy);
ctx.lineTo(cx + Math.cos(angle) * wheelR, cy + Math.sin(angle) * wheelR);
ctx.stroke();
}
// Hub
ctx.fillStyle = G;
ctx.beginPath();
ctx.arc(cx, cy, 2, 0, Math.PI * 2);
ctx.fill();
}
ctx.lineWidth = 1;
// Subtle scanline effect
ctx.fillStyle = 'rgba(0,0,0,0.08)';
for (var sl = 0; sl < H; sl += 3) {
ctx.fillRect(0, sl, W, 1);
}
// Subtle green glow/vignette
var grd = ctx.createRadialGradient(W/2, H/2, 40, W/2, H/2, W * 0.7);
grd.addColorStop(0, 'rgba(0,0,0,0)');
grd.addColorStop(1, 'rgba(0,0,0,0.4)');
ctx.fillStyle = grd;
ctx.fillRect(0, 0, W, H);
}
function renderCanvas(time) {
// Death screen: green phosphor Apple II style
if (gameState === STATES.DEATH) {
drawDeathScene(time);
return;
}
const skyColors = getSkyColors();
// Sky gradient (banded for EGA feel)
const bands = 10;
const skyBottom = 110;
for (let i = 0; i < bands; i++) {
const t = i / (bands - 1);
const r1 = parseInt(skyColors.top.substr(1, 2), 16);
const g1 = parseInt(skyColors.top.substr(3, 2), 16);
const b1 = parseInt(skyColors.top.substr(5, 2), 16);
const r2 = parseInt(skyColors.bottom.substr(1, 2), 16);
const g2 = parseInt(skyColors.bottom.substr(3, 2), 16);
const b2 = parseInt(skyColors.bottom.substr(5, 2), 16);
const r = Math.floor(r1 + (r2 - r1) * t);
const g = Math.floor(g1 + (g2 - g1) * t);
const b = Math.floor(b1 + (b2 - b1) * t);
ctx.fillStyle = `rgb(${r},${g},${b})`;
const bandH = Math.ceil(skyBottom / bands) + 1;
ctx.fillRect(0, Math.floor(i * (skyBottom / bands)), W, bandH);
}
// Clouds (parallax 0.2x)
const cloudOffset = scrollX * 0.2;
for (const c of cloudPositions) {
let cx = ((c.x - cloudOffset) % (W + 80));
if (cx < -40) cx += W + 80;
drawPixelCloud(cx - 20, c.y, c.w, c.h);
}
// Far mountains (parallax 0.3x) - DARK MAGENTA signature Oregon Trail
const farOffset = scrollX * 0.3;
for (const m of farMountains) {
let mx = ((m.x - farOffset) % (W + 100));
if (mx < -50) mx += W + 100;
drawMountain(mx - 25, m.h, m.w, _MC.far, false);
}
// Near mountains (parallax 0.4x) - LIGHT MAGENTA with snow caps
const nearOffset = scrollX * 0.4;
for (const m of nearMountains) {
let mx = ((m.x - nearOffset) % (W + 80));
if (mx < -40) mx += W + 80;
drawMountain(mx - 20, m.h, m.w, _MC.near, m.snow);
}
// Grass
ctx.fillStyle = '#00AA00';
ctx.fillRect(0, 110, W, H - 110);
// Bright grass highlights
ctx.fillStyle = '#55FF55';
const grassOffset = scrollX * 1.0;
for (let i = 0; i < 50; i++) {
let gx = ((i * 11 + 5 - grassOffset) % (W + 20));
if (gx < -5) gx += W + 20;
const gy = 113 + (i * 7 + i * i * 3) % 55;
ctx.fillRect(Math.floor(gx), Math.floor(gy), 2, 2);
}
// Trees (parallax 0.6x)
const treeOffset = scrollX * 0.6;
const treeBaseY = 128;
for (const t of treesData) {
let tx = ((t.x - treeOffset) % (W + 60));
if (tx < -20) tx += W + 60;
drawTree(tx - 10, treeBaseY, t.h);
}
// Landmark for current stop (only at stops/events)
if (gameState === STATES.STOP || gameState === STATES.EVENT || gameState === STATES.RIVER) {
const stop = TRAIL_DATA.stops[currentStop];
if (stop) {
drawLandmark(stop.landmarkType, 240, 150);
}
}
// Trail/ground strip
if (!showRiver) {
ctx.fillStyle = '#AA5500';
ctx.fillRect(0, 152, W, 15);
// Trail texture dots
ctx.fillStyle = '#885500';
for (let i = 0; i < 35; i++) {
let dotX = ((i * 10 - scrollX * 1.0) % (W + 10));
if (dotX < -3) dotX += W + 10;
ctx.fillRect(Math.floor(dotX), 155 + (i % 4) * 2, 3, 1);
}
// Ruts
ctx.fillStyle = '#775500';
ctx.fillRect(0, 157, W, 1);
ctx.fillRect(0, 162, W, 1);
}
// River (when active)
if (showRiver) {
drawRiver(time || 0);
}
// Wagon
const wagonX = 65;
const wagonY = showRiver ? 148 : 149;
drawWagon(wagonX, wagonY);
// Event-specific visual overlay
if (gameState === STATES.EVENT || gameState === STATES.RIVER) {
drawEventOverlay(time);
}
// Ground below trail
ctx.fillStyle = '#885500';
ctx.fillRect(0, showRiver ? 173 : 167, W, 15);
ctx.fillStyle = '#553300';
ctx.fillRect(0, showRiver ? 188 : 182, W, H - 182);
}
// =====================================================================
// MUSIC SYSTEM (Web Audio API Chiptune)
// =====================================================================
const NOTE_FREQ = {
'C3': 130.81, 'D3': 146.83, 'G2': 98.00, 'A2': 110.00,
'C4': 261.63, 'D4': 293.66, 'E4': 329.63, 'G4': 392.00,
'A4': 440.00, 'C5': 523.25, 'REST': 0
};
const melody = [
{ note: 'E4', dur: 1 }, { note: 'G4', dur: 1 }, { note: 'A4', dur: 1 }, { note: 'G4', dur: 1 },
{ note: 'E4', dur: 1 }, { note: 'D4', dur: 1 }, { note: 'C4', dur: 1 }, { note: 'REST', dur: 1 },
{ note: 'E4', dur: 1 }, { note: 'G4', dur: 1 }, { note: 'A4', dur: 1 }, { note: 'C5', dur: 1 },
{ note: 'A4', dur: 2 }, { note: 'G4', dur: 2 },
{ note: 'C4', dur: 1 }, { note: 'D4', dur: 1 }, { note: 'E4', dur: 1 }, { note: 'G4', dur: 1 },
{ note: 'A4', dur: 1 }, { note: 'G4', dur: 1 }, { note: 'E4', dur: 1 }, { note: 'D4', dur: 1 },
{ note: 'C4', dur: 1 }, { note: 'E4', dur: 1 }, { note: 'G4', dur: 1 }, { note: 'E4', dur: 1 },
{ note: 'D4', dur: 1 }, { note: 'C4', dur: 3 }
];
const bassLine = [
{ note: 'C3', dur: 4 }, { note: 'A2', dur: 4 }, { note: 'C3', dur: 4 }, { note: 'A2', dur: 4 },
{ note: 'C3', dur: 4 }, { note: 'G2', dur: 4 }, { note: 'D3', dur: 4 }, { note: 'C3', dur: 4 }
];
let musicScheduleTimer = null;
let nextMelodyTime = 0;
let nextBassTime = 0;
var masterGain = null;
function initAudio() {
if (audioCtx) return;
try {
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
masterGain = audioCtx.createGain();
masterGain.gain.value = 1.0;
masterGain.connect(audioCtx.destination);
musicInitialized = true;
} catch (e) {
console.warn('Web Audio not available:', e);
}
}
function playNote(freq, startTime, duration, type, gainVal) {
if (!audioCtx || freq === 0) return;
const osc = audioCtx.createOscillator();
const gain = audioCtx.createGain();
osc.type = type;
osc.frequency.setValueAtTime(freq, startTime);
// Envelope: attack then decay
gain.gain.setValueAtTime(0.001, startTime);
gain.gain.linearRampToValueAtTime(gainVal, startTime + 0.02);
gain.gain.setValueAtTime(gainVal, startTime + duration * 0.75);
gain.gain.linearRampToValueAtTime(0.001, startTime + duration * 0.95);
osc.connect(gain);
gain.connect(masterGain || audioCtx.destination);
osc.start(startTime);
osc.stop(startTime + duration);
}
function scheduleLoop() {
if (!musicPlaying || !audioCtx) return;
const bpm = 110;
const quarterDur = 60 / bpm;
const now = audioCtx.currentTime;
// Schedule melody
let t = now + 0.05;
for (const note of melody) {
const dur = note.dur * quarterDur;
if (note.note !== 'REST') {
playNote(NOTE_FREQ[note.note], t, dur, 'square', 0.06);
}
t += dur;
}
// Schedule bass
let tb = now + 0.05;
for (const note of bassLine) {
const dur = note.dur * quarterDur;
playNote(NOTE_FREQ[note.note], tb, dur, 'triangle', 0.08);
tb += dur;
}
// Total loop duration in beats: melody has 32 quarter beats = 32 * quarterDur
const totalBeats = 32;
const loopDurationMs = totalBeats * quarterDur * 1000;
musicScheduleTimer = setTimeout(scheduleLoop, loopDurationMs - 200);
}
function startMusic() {
if (!musicInitialized) initAudio();
if (!audioCtx) return;
if (audioCtx.state === 'suspended') audioCtx.resume();
if (masterGain) masterGain.gain.setValueAtTime(1.0, audioCtx.currentTime);
musicPlaying = true;
scheduleLoop();
updateMusicIndicator();
}
function stopMusic() {
musicPlaying = false;
if (musicScheduleTimer) {
clearTimeout(musicScheduleTimer);
musicScheduleTimer = null;
}
if (masterGain) masterGain.gain.setValueAtTime(0, audioCtx.currentTime);