-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
739 lines (619 loc) · 20.6 KB
/
Copy pathscript.js
File metadata and controls
739 lines (619 loc) · 20.6 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
const APP_VERSION = "ver1.0-1103261912_beta";
let activeMenu = null;
const BGM_LOOPS = {
"assets/WELCOME.ogg": { start: 8.365, end: 45.293 },
"assets/ABOUT.ogg": { start: 0.000, end: 94.834 },
"assets/OPTIONS.ogg": { start: 3.918, end: 25.387 },
"assets/MAINMENU.ogg": { start: 3.502, end: 27.507 }
};
let currentBgmTrack = "assets/MAINMENU.ogg";
const speechScripts = {
aboutIntro: [
{
text: "<span class='redText'>Brain Training: Web Edition</span> is a browser-based adaptation of the Nintendo DS <span class='redText'>Brain Age</span> series...",
head: "assets/HEAD1.webp"
},
{
text: "Characters, names & likenesses from the Nintendo DS series are © <span class='redText'>Nintendo</span>.",
head: "assets/HEAD1.webp"
},
{
text: "All original Nintendo DS IP belongs to <span class='redText'>Nintendo.</span>",
head: "assets/HEAD1.webp",
rightImage: "assets/Nintendo.webp"
},
{
text: "This fan-made edition is non-commercial and intended for educational & entertainment purposes.",
head: "assets/HEAD1.webp"
},
{
text: `<span class='redText'>Brain Training: Web Edition</span> <span class='redText'>${APP_VERSION}</span> <br/><span class='redText'>by unredacted! Studios<br/></span>All rights reserved.`,
head: "assets/HEAD2.webp",
}
],
introduction: [
{
text: "Hello! My name is Ryuta Kawashima, and I'm a doctor of neuroscience.",
head: "assets/HEAD2.webp"
},
{
text: "Why don't we try doing some <span class='redText'>brain training</span> together?",
head: "assets/HEAD1.webp"
},
{
text: "Let's get started!",
head: "assets/HEAD.webp"
},
{
text: "If you don't use your brain, it will age quickly.",
head: "assets/HEAD1.webp"
},
{
text: "Your brain will not be as strong as one with the ideal brain age of <span class='redText'>20</span>.",
head: "assets/HEADSAD.webp"
},
{
text: "It's just like not exercising. A healthy body must work out regularly, you know!",
head: "assets/HEAD1.webp"
},
{
text: "But there is a solution: you can <span class='redText'>train your brain</span> like you train your body!",
head: "assets/HEAD1.webp"
},
{
text: "I've found that quick, simple exercises in reading, writing, and arithmetic can help.",
head: "assets/HEAD1.webp"
},
{
text: "Take a look at these brain scans.",
head: "assets/HEAD1.webp",
rightImage: "assets/BRAINSCAN_1.webp"
},
{
text: "The yellow and red patches show areas of rapid blood flow.",
head: "assets/HEAD1.webp",
rightImage: "assets/BRAINSCAN_1.webp"
},
{
text: "Those areas of your brain are doing <span class='redText'>a lot of work</span>!",
head: "assets/HEAD.webp",
rightImage: "assets/BRAINSCAN_1.webp"
},
{
text: "Image A shows a brain in idle thought. Image B shows a brain watching TV.",
head: "assets/HEAD1.webp",
rightImage: "assets/BRAINSCAN_1.webp"
},
{
text: "These brains are hardly working at all! Lazy brains!",
head: "assets/HEADSAD.webp",
rightImage: "assets/BRAINSCAN_1.webp"
},
{
text: "Now, look at the following images.",
head: "assets/HEAD1.webp",
rightImage: "assets/BRAINSCAN_2.webp"
},
{
text: "Image C shows a brain that is solving simple math problems rapidly,",
head: "assets/HEAD1.webp",
rightImage: "assets/BRAINSCAN_2.webp"
},
{
text: "while image D shows a brain whose owner is reading out loud.",
head: "assets/HEAD1.webp",
rightImage: "assets/BRAINSCAN_2.webp"
},
{
text: "See how <span class='redText'>larger portions of these brains are being put to work</span>?",
head: "assets/HEAD.webp",
rightImage: "assets/BRAINSCAN_2.webp"
},
{
text: "Solving simple calculations and reading out loud activate the brain!",
head: "assets/HEAD2.webp",
rightImage: "assets/BRAINSCAN_2.webp"
},
{
text: "These activities are included in this software,",
head: "assets/HEAD2.webp",
rightImage: "assets/BRAINSCAN_2.webp"
},
{
text: "as well as many other <span class='redText'>training programs</span> for your brain.",
head: "assets/HEAD2.webp",
rightImage: "assets/BRAINSCAN_2.webp"
},
{
text: "I designed this software to include only exercises which aid your brain.",
head: "assets/HEAD2.webp",
rightImage: "assets/BRAINSCAN_2.webp"
},
{
text: "Modern imaging tools suggest that they are all quite effective!",
head: "assets/HEAD2.webp",
rightImage: "assets/BRAINSCAN_2.webp"
},
{
text: "And now, it's time to start your training!",
head: "assets/HEAD2.webp",
rightImage: "assets/BRAINSCAN_2.webp",
nextPage: "training.html" // optional: points to another HTML page
}
]
};
let menuTransitionInProgress = false;
let moreState = "normal"; // "normal" | "grey" | "blink"
function getActiveHead() {
return activeMenu?.querySelector(".head");
}
let currentSpeechList = [];
let currentSpeechIndex = 0;
let speechInProgress = false;
let blinkInterval = null;
function clearMenuTopButtons() {
const back = activeMenu?.querySelector(".leftBtn");
if (back) back.replaceWith(back.cloneNode(true));
const more = activeMenu?.querySelector(".moreBtn");
if (more) more.replaceWith(more.cloneNode(true));
}
function playSfx(src, volume = 1) {
const s = new Audio(src);
s.volume = volume;
s.play().catch(() => {});
}
function handleBackButton() {
const backBtn = activeMenu?.querySelector(".leftBtn");
if (!backBtn) return;
// replace node to remove previous listeners (keeps your pattern)
backBtn.replaceWith(backBtn.cloneNode(true));
const newBack = activeMenu.querySelector(".leftBtn");
// PRESS
newBack.addEventListener("mousedown", () => {
newBack.src = "assets/BACK_PRESSED.webp";
});
// RELEASE
function restoreBackVisual() {
newBack.src = "assets/BACK.webp";
}
newBack.addEventListener("mouseup", restoreBackVisual);
newBack.addEventListener("mouseleave", restoreBackVisual);
newBack.addEventListener("click", () => {
speechInProgress = false;
if (blinkInterval) {
clearInterval(blinkInterval);
blinkInterval = null;
}
setMoreState("normal");
transitionWithOverlay("mainMenu", "assets/MAINMENU.ogg");
});
}
function handleMoreButton() {
const moreBtn = activeMenu?.querySelector(".moreBtn");
if (!moreBtn) return;
moreBtn.replaceWith(moreBtn.cloneNode(true));
const newMore = activeMenu.querySelector(".moreBtn");
// PRESS
newMore.addEventListener("mousedown", () => {
if (moreState === "grey") return; // stay greyed
newMore.src = "assets/MORE_PRESSED.webp";
});
// RELEASE
function restoreMoreVisual() {
if (moreState === "grey") {
newMore.src = "assets/MORE_GREYED.webp";
} else {
setMoreState(moreState); // restore normal or blink
}
}
newMore.addEventListener("mouseup", restoreMoreVisual);
newMore.addEventListener("mouseleave", restoreMoreVisual);
// CLICK ACTION
newMore.addEventListener("click", () => {
if (speechInProgress || moreState === "grey") return;
playSfx("assets/NEXT_PRESSED.ogg", 1);
currentSpeechIndex++;
if (currentSpeechIndex < currentSpeechList.length) {
// Next speech line
typeSpeechLine(
currentSpeechList[currentSpeechIndex],
"speechText",
"assets/SPEAK.ogg"
);
} else {
// Last line reached
const lastLine = currentSpeechList[currentSpeechList.length - 1];
const nextPage = lastLine.nextPage;
if (nextPage) {
// Fade white overlay, then navigate to new page
const overlay = document.getElementById("whiteOverlay");
overlay.style.pointerEvents = "auto";
overlay.style.opacity = "1";
setTimeout(() => {
window.location.href = nextPage;
}, 500); // give the overlay 0.5s to fade in
} else {
// Default: go back to main menu
transitionWithOverlay("mainMenu", "assets/MAINMENU.ogg");
}
setMoreState("normal");
}
});
}
const moreBtn = document.getElementById("moreBtn");
function getActiveMoreBtn() {
return activeMenu?.querySelector(".moreBtn");
}
function setMoreState(state) {
const moreBtn = getActiveMoreBtn();
if (!moreBtn) return;
moreState = state; // track state globally
if (blinkInterval) {
clearInterval(blinkInterval);
blinkInterval = null;
}
if (state === "grey") {
moreBtn.src = "assets/MORE_GREYED.webp";
}
if (state === "blink") {
let on = false;
blinkInterval = setInterval(() => {
moreBtn.src = on ? "assets/MORE.webp" : "assets/MORE_BLINK.webp";
on = !on;
}, 250);
}
if (state === "normal") {
moreBtn.src = "assets/MORE.webp";
}
}
const esrb = document.getElementById("esrb");
const splash = document.getElementById("splash");
const boot = document.getElementById("boot");
const mainMenu = document.getElementById("mainMenu");
const bgm = document.getElementById("bgm");
bgm.loop = false; // IMPORTANT: disable normal full-file looping
function loopBGM() {
const loop = BGM_LOOPS[currentBgmTrack];
if (
loop &&
!bgm.paused &&
bgm.readyState >= 2 &&
bgm.currentTime >= loop.end
) {
bgm.currentTime = loop.start;
}
requestAnimationFrame(loopBGM);
}
loopBGM();
const isFastBoot = window.location.hash === "#fastboot";
window.addEventListener("load", initApp);
const buttons = document.querySelectorAll(".button");
const menuScreens = document.querySelectorAll(".menuScreen");
function fadeAudio(audio, targetVolume = 0, duration = 500) {
const startVolume = audio.volume;
const startTime = performance.now();
return new Promise(resolve => {
function step(now) {
const elapsed = now - startTime;
const progress = Math.min(elapsed / duration, 1);
audio.volume = startVolume + (targetVolume - startVolume) * progress;
if (progress < 1) requestAnimationFrame(step);
else resolve();
}
requestAnimationFrame(step);
});
}
function switchMenu(targetMenuId, newBGMsrc) {
if (menuTransitionInProgress) return;
menuTransitionInProgress = true;
const currentMenu =
activeMenu || document.querySelector(".menuScreen:not([style*='display:none'])");
const targetMenu = document.getElementById(targetMenuId);
if (!targetMenu) {
menuTransitionInProgress = false; // FAILSAFE UNLOCK
return;
}
fadeAudio(bgm, 0, 500).then(() => {
bgm.pause();
if (currentMenu) {
currentMenu.style.display = "none";
currentMenu.classList.remove("fading");
}
targetMenu.style.display = "block";
if (targetMenuId === "mainMenu") {
targetMenu.classList.remove("fading");
} else {
targetMenu.classList.add("fading");
}
activeMenu = targetMenu;
updateVersionLabel();
currentBgmTrack = newBGMsrc;
bgm.src = currentBgmTrack;
bgm.loop = false;
bgm.currentTime = 0;
bgm.volume = 1;
bgm.play().catch(() => {});
if (targetMenuId === "aboutMenu") startSpeech("aboutIntro");
if (targetMenuId === "homeMenu") startSpeech("introduction");
clearMenuTopButtons();
handleBackButton();
handleMoreButton();
menuTransitionInProgress = false; // unlock after transition
});
}
buttons.forEach(button => {
button.addEventListener("click", () => {
if (menuTransitionInProgress) return;
// Play OK.ogg sound immediately
const okSound = new Audio("assets/OK.ogg");
okSound.volume = 1;
okSound.play().catch(() => {});
button.style.pointerEvents = "none";
// Get button's current position
const rect = button.getBoundingClientRect();
// Fix it in place at its current coordinates
button.style.position = "fixed";
button.style.top = `${rect.top}px`;
button.style.left = `${rect.left}px`;
button.style.transform = "none";
button.style.zIndex = 999;
// Shift clicked button down/right by 5px
button.style.transition = "transform 0.1s ease-in-out";
button.style.transform = "translate(5px, 5px)";
// Hide only other buttons inside the same menu screen
const currentScreen = button.closest(".menuScreen");
if (currentScreen) {
currentScreen.querySelectorAll(".button").forEach(b => {
if (b !== button) b.style.display = "none";
});
}
// Delay a tiny bit to see the click shift
setTimeout(() => {
const text = button.textContent;
if (text === "Introduction") transitionWithOverlay("homeMenu", "assets/WELCOME.ogg");
else if (text === "My Brain Training") {
const overlay = document.getElementById("whiteOverlay");
overlay.style.pointerEvents = "auto";
overlay.style.opacity = "1";
setTimeout(() => {
window.location.href = "training.html";
}, 500); // 0.5s fade
}
else if (text === "Options") transitionWithOverlay("optionsMenu", "assets/OPTIONS.ogg");
else if (text === "About") transitionWithOverlay("aboutMenu", "assets/ABOUT.ogg");
}, 150);
});
});
function typeSpeech(text, containerId, speakSrc, interval = 50) {
const container = document.getElementById(containerId);
container.textContent = ""; // clear previous text
let index = 0;
function addLetter() {
if (index < text.length) {
container.textContent += text[index];
// Play SPEAK.ogg with overlap
const speak = new Audio(speakSrc);
speak.volume = 1;
speak.play().catch(err => console.warn("SPEAK.ogg blocked:", err));
index++;
setTimeout(addLetter, interval);
}
}
addLetter();
}
function typeSpeechLine(line, containerId, speakSrc, interval = 30, onComplete) {
// find container inside active menu first; fallback to document
const container = (activeMenu && activeMenu.querySelector(`#${containerId}`)) || document.getElementById(containerId);
if (!container) {
console.error("speech container not found:", containerId, "activeMenu:", activeMenu);
return;
}
container.innerHTML = "";
speechInProgress = true;
setMoreState("grey");
// Set head image
const headImg = getActiveHead();
if (line.head && headImg) headImg.src = line.head;
// Set right image
const rightImg = getActiveRightImage();
if (line.rightImage && rightImg) {
rightImg.src = line.rightImage;
rightImg.style.opacity = "1";
rightImg.classList.add("floating");
} else if (rightImg) {
rightImg.style.opacity = "0";
rightImg.classList.remove("floating");
}
const text = line.text;
let i = 0;
let htmlBuffer = "";
function addLetter() {
if (i >= text.length) {
container.innerHTML = htmlBuffer;
speechInProgress = false;
setMoreState("blink");
if (onComplete) onComplete();
return;
}
if (text[i] === "<") {
const tagEnd = text.indexOf(">", i);
if (tagEnd !== -1) {
htmlBuffer += text.slice(i, tagEnd + 1);
i = tagEnd + 1;
} else {
htmlBuffer += text[i];
i++;
}
} else {
htmlBuffer += text[i];
i++;
}
// update visible HTML each step
container.innerHTML = htmlBuffer;
// play voice tick
const speak = new Audio(speakSrc);
speak.volume = 1;
speak.play().catch(() => {});
setTimeout(addLetter, interval);
}
addLetter();
}
function startSpeech(scriptName) {
currentSpeechList = speechScripts[scriptName];
currentSpeechIndex = 0;
typeSpeechLine(
currentSpeechList[currentSpeechIndex],
"speechText",
"assets/SPEAK.ogg"
);
}
function getActiveRightImage() {
return activeMenu?.querySelector(".rightImage");
}
function resetMainMenuButtons() {
// Restore inline styles applied when a button was clicked
const mainButtons = document.querySelectorAll("#mainMenu .button");
mainButtons.forEach(b => {
// Remove any inline positioning/transform/pointer/etc applied during click
b.style.position = "";
b.style.top = "";
b.style.left = "";
b.style.transform = "";
b.style.zIndex = "";
b.style.transition = "";
b.style.display = ""; // allow CSS to decide (was hidden by inline style previously)
b.style.pointerEvents = ""; // re-enable pointer events
b.classList.remove("clicked"); // if you used that class
});
// Also ensure any top-bar buttons inside mainMenu are visible / clickable if present
const topBarBtns = document.querySelectorAll("#mainMenu .topBarBtn, #mainMenu .moreBtn, #mainMenu .leftBtn, #mainMenu .rightBtn");
topBarBtns.forEach(btn => {
btn.style.display = "";
btn.style.pointerEvents = "";
});
}
function updateVersionLabel() {
const label = document.getElementById("versionLabel");
if (!label) return;
if (activeMenu?.id === "mainMenu") {
label.textContent = APP_VERSION;
label.style.display = "block";
} else {
label.style.display = "none";
}
}
function transitionWithOverlay(targetMenuId, newBGMsrc) {
const overlay = document.getElementById("whiteOverlay");
if (!overlay) {
switchMenu(targetMenuId, newBGMsrc);
return;
}
overlay.style.pointerEvents = "auto";
overlay.style.opacity = "1";
setTimeout(() => {
resetMainMenuButtons();
switchMenu(targetMenuId, newBGMsrc);
const onPlaying = () => {
requestAnimationFrame(() => requestAnimationFrame(() => {
overlay.style.opacity = "0";
}));
};
bgm.addEventListener("playing", onPlaying, { once: true });
const fallbackTimeout = setTimeout(() => {
if (overlay.style.opacity === "1") overlay.style.opacity = "0";
}, 2000);
overlay.addEventListener("transitionend", function handler(e) {
if (e.propertyName !== "opacity") return;
if (overlay.style.opacity === "0") {
overlay.style.pointerEvents = "none";
clearTimeout(fallbackTimeout);
overlay.removeEventListener("transitionend", handler);
}
});
}, 500); // wait 0.5s for fade-in
}
function animateMainMenuButtons() {
const buttons = document.querySelectorAll("#buttonStack .button, #buttonStack2 .button");
buttons.forEach((btn, index) => {
btn.style.animation = `slideInFromRight 0.6s forwards`;
btn.style.animationDelay = `${index * 0.1}s`; // stagger each button
});
}
function initApp() {
const overlay = document.getElementById("whiteOverlay");
if (isFastBoot) {
if (boot) boot.style.display = "none";
mainMenu.style.display = "block";
activeMenu = mainMenu;
updateVersionLabel();
if (overlay) {
overlay.style.transition = "none";
overlay.style.opacity = "1";
overlay.style.pointerEvents = "auto";
overlay.getBoundingClientRect(); // force white to render first
overlay.style.transition = "opacity 0.5s ease";
}
bgm.currentTime = 0;
currentBgmTrack = "assets/MAINMENU.ogg";
bgm.src = currentBgmTrack;
bgm.loop = false;
bgm.currentTime = 0;
bgm.play().catch(err => {
console.warn("BGM autoplay was blocked:", err);
});
requestAnimationFrame(() => {
requestAnimationFrame(() => {
animateMainMenuButtons();
if (overlay) overlay.style.opacity = "0";
});
});
if (overlay) {
overlay.addEventListener("transitionend", function handler(e) {
if (e.propertyName !== "opacity") return;
if (overlay.style.opacity === "0") {
overlay.style.pointerEvents = "none";
overlay.removeEventListener("transitionend", handler);
history.replaceState(
null,
"",
window.location.pathname + window.location.search
);
}
});
} else {
history.replaceState(
null,
"",
window.location.pathname + window.location.search
);
}
return;
}
showSequence();
}
// Call this after main menu is displayed
function showSequence() {
// ESRB fade in
setTimeout(() => esrb.classList.add("visible"), 0);
// ESRB fade out
setTimeout(() => esrb.classList.remove("visible"), 1000);
// SPLASH fade in
setTimeout(() => splash.classList.add("visible"), 1500);
// SPLASH fade out
setTimeout(() => splash.classList.remove("visible"), 2500);
// End boot, show menu and play BGM
setTimeout(() => {
boot.style.display = "none";
mainMenu.style.display = "block";
activeMenu = mainMenu;
updateVersionLabel();
animateMainMenuButtons();
currentBgmTrack = "assets/MAINMENU.ogg";
bgm.src = currentBgmTrack;
bgm.loop = false;
bgm.currentTime = 0;
bgm.play().catch(err => {
console.warn("BGM autoplay was blocked:", err);
});
}, 3000);
}