-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
866 lines (774 loc) · 25.9 KB
/
index.html
File metadata and controls
866 lines (774 loc) · 25.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>The Last Elevator</title>
<style>
:root{
--bg:#0b0e14;
--card:#0f1420;
--ink:#e9eefc;
--muted:#9aa6c3;
--line:#22304a;
--good:#9bf6ff;
--warn:#ffd6a5;
--bad:#ffadad;
--accent:#bdb2ff;
--shadow: 0 20px 60px rgba(0,0,0,.45);
--radius: 18px;
--max: 720px;
}
*{ box-sizing:border-box; }
html,body{ height:100%; }
body{
margin:0;
font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji","Segoe UI Emoji";
background:
radial-gradient(900px 600px at 30% 15%, rgba(189,178,255,.12), transparent 60%),
radial-gradient(800px 500px at 70% 80%, rgba(155,246,255,.10), transparent 55%),
var(--bg);
color:var(--ink);
display:grid;
place-items:center;
padding:28px 16px;
}
.wrap{ width:min(var(--max), 100%); }
header{
display:flex;
justify-content:space-between;
align-items:flex-end;
gap:12px;
margin:0 0 14px;
}
.brand{
display:flex;
flex-direction:column;
gap:6px;
}
.title{
font-size:18px;
letter-spacing:.2px;
margin:0;
font-weight:650;
}
.subtitle{
margin:0;
color:var(--muted);
font-size:13px;
line-height:1.3;
}
.hud{
display:flex;
align-items:center;
gap:10px;
flex-wrap:wrap;
justify-content:flex-end;
}
.pill{
border:1px solid var(--line);
background: rgba(255,255,255,.02);
border-radius:999px;
padding:8px 10px;
color:var(--muted);
font-size:12px;
display:flex;
align-items:center;
gap:8px;
user-select:none;
}
.dot{
width:8px;height:8px;border-radius:99px;background:var(--accent);
box-shadow:0 0 0 3px rgba(189,178,255,.08);
}
.meter{
width:160px;
height:8px;
background: rgba(255,255,255,.05);
border:1px solid var(--line);
border-radius:999px;
overflow:hidden;
}
.meter > i{
display:block;
height:100%;
width:50%;
background: linear-gradient(90deg, rgba(155,246,255,.95), rgba(189,178,255,.95));
border-radius:999px;
transition: width .35s ease;
}
.card{
background: linear-gradient(180deg, rgba(255,255,255,.03), rgba(255,255,255,.015));
border:1px solid var(--line);
border-radius: var(--radius);
box-shadow: var(--shadow);
overflow:hidden;
}
.card-inner{
padding:22px 20px 16px;
}
.scene-title{
margin:0 0 10px;
font-size:18px;
font-weight:650;
letter-spacing:.2px;
}
.scene-text{
margin:0;
color:rgba(233,238,252,.92);
line-height:1.55;
font-size:14.5px;
white-space:pre-wrap;
}
.divider{
height:1px;
background: linear-gradient(90deg, transparent, rgba(34,48,74,.9), transparent);
margin:16px 0;
}
.choices{
display:grid;
gap:10px;
margin-top:6px;
}
button.choice{
width:100%;
text-align:left;
padding:12px 12px;
border-radius:14px;
border:1px solid var(--line);
background: rgba(255,255,255,.02);
color:var(--ink);
cursor:pointer;
transition: transform .12s ease, border-color .12s ease, background .12s ease;
display:flex;
align-items:flex-start;
gap:10px;
line-height:1.25;
}
button.choice:hover{
transform: translateY(-1px);
border-color: rgba(189,178,255,.45);
background: rgba(189,178,255,.07);
}
button.choice:active{ transform: translateY(0px); }
button.choice[disabled]{
cursor:not-allowed;
opacity:.55;
filter:saturate(.7);
}
.kbd{
flex:0 0 auto;
font-size:12px;
color:var(--muted);
border:1px solid rgba(34,48,74,.8);
background: rgba(0,0,0,.18);
border-radius:10px;
padding:6px 8px;
min-width:34px;
text-align:center;
}
.choice-main{
display:flex;
flex-direction:column;
gap:4px;
}
.choice-label{
font-weight:600;
font-size:14px;
}
.choice-hint{
font-size:12px;
color:var(--muted);
}
footer{
display:flex;
justify-content:space-between;
align-items:center;
gap:10px;
padding:14px 16px 16px;
border-top:1px solid rgba(34,48,74,.75);
background: rgba(0,0,0,.12);
}
.micro{
color:var(--muted);
font-size:12px;
line-height:1.2;
}
.actions{
display:flex;
gap:8px;
flex-wrap:wrap;
justify-content:flex-end;
}
.ghost{
border:1px solid var(--line);
background: rgba(255,255,255,.02);
color:var(--ink);
border-radius:12px;
padding:10px 11px;
cursor:pointer;
font-size:13px;
}
.ghost:hover{ border-color: rgba(155,246,255,.45); background: rgba(155,246,255,.06); }
.toast{
margin-top:12px;
color:var(--muted);
font-size:12px;
text-align:center;
min-height:16px;
}
.fade{
animation: fadeIn .22s ease both;
}
@keyframes fadeIn{
from{ opacity:0; transform: translateY(4px); }
to{ opacity:1; transform: translateY(0); }
}
</style>
</head>
<body>
<div class="wrap">
<header>
<div class="brand">
<h1 class="title">The Last Elevator</h1>
<p class="subtitle">A simple decision tree. Choose carefully. (Keys: 1–4, R to restart, U to undo.)</p>
</div>
<div class="hud" aria-label="Game status">
<div class="pill" title="Scene progress">
<span class="dot" aria-hidden="true"></span>
<span id="progress">Scene 1 / 14</span>
</div>
<div class="pill" title="Time remaining">
<span style="font-weight:650;color:var(--ink)">Time</span>
<span id="timeLabel">7</span>
<div class="meter" aria-hidden="true"><i id="timeBar"></i></div>
</div>
</div>
</header>
<main class="card" role="main">
<div class="card-inner fade" id="sceneRoot">
<h2 class="scene-title" id="sceneTitle">Loading…</h2>
<p class="scene-text" id="sceneText"></p>
<div class="divider"></div>
<div class="choices" id="choices"></div>
</div>
<footer>
<div class="micro" id="microHint">Tip: some choices cost time; running out ends the run.</div>
<div class="actions">
<button class="ghost" id="undoBtn" type="button" title="Undo (U)">Undo</button>
<button class="ghost" id="restartBtn" type="button" title="Restart (R)">Restart</button>
</div>
</footer>
</main>
<div class="toast" id="toast" aria-live="polite"></div>
</div>
<script>
(() => {
// --------- Data model (decision tree) ----------
// One visible meter: TIME. Starts at 7, ends at 0.
// Also uses a couple of quiet flags to make outcomes feel richer (still "simple").
const START_TIME = 7;
/** @type {Record<string, any>} */
const NODES = {
start: {
title: "Lobby — Emergency Lights",
text:
`The building hums like a sleeping engine.
Emergency lights stutter. Somewhere above, a faint alarm complains and then stops.
The elevator doors are open. Inside: a dead panel, a quiet mirror, and a single button labeled "SERVICE."`,
choices: [
{ label: "Step into the elevator.", to: "inside", cost: 0, hint: "Commit." },
{ label: "Search the front desk for anything useful.", to: "desk", cost: 1, hint: "Costs 1 time." },
{ label: "Check the stairwell door.", to: "stairs", cost: 1, hint: "Costs 1 time." }
]
},
desk: {
title: "Front Desk — Dusty Drawer",
text:
`A drawer sticks. You yank it open.
A ring of keys slides out with a small, bright sound.`,
effects: (s) => { s.flags.hasKeys = true; },
choices: [
{ label: "Take the keys and return to the elevator.", to: "inside", cost: 1, hint: "Costs 1 time." },
{ label: "Leave them. Return to the elevator.", to: "inside", cost: 0, hint: "No cost." }
]
},
stairs: {
title: "Stairwell — The Breath of Concrete",
text:
`The stairwell is colder than it should be.
A sign reads: "DO NOT USE DURING POWER FAILURE."
Above you: darkness. Below you: more darkness.`,
choices: [
{ label: "Go up a few floors anyway.", to: "stairsUp", cost: 2, hint: "Costs 2 time." },
{ label: "Back to the elevator.", to: "inside", cost: 1, hint: "Costs 1 time." }
]
},
stairsUp: {
title: "Stairwell — Landing",
text:
`After a few flights, you stop.
You can hear something… like a slow scraping behind a service door.`,
choices: [
{ label: "Open the service door.", to: "serviceDoor", cost: 1, hint: "Costs 1 time." },
{ label: "Ignore it. Return to the lobby.", to: "inside", cost: 2, hint: "Costs 2 time." }
]
},
serviceDoor: {
title: "Service Door — Cable Room",
text:
`The room smells of metal and old lubricant.
A backup battery pack blinks weakly beside a control box.`,
effects: (s) => { s.flags.sawBattery = true; },
choices: [
{ label: "Pocket the battery pack.", to: "inside", cost: 1, hint: "Costs 1 time." , effect: (s)=>{s.flags.hasBattery=true;} },
{ label: "Don't risk it. Return.", to: "inside", cost: 1, hint: "Costs 1 time." }
]
},
inside: {
title: "Inside the Elevator",
text:
`The mirror shows you too clearly.
The panel is dark except for the "SERVICE" button, which feels warm—wrongly warm.
A tiny speaker crackles: "…limited operation… choose destination…"`
,
choices: (s) => {
const out = [
{ label: "Press SERVICE.", to: "service", cost: 0, hint: "No cost." },
{ label: "Wait and listen for more instructions.", to: "listen", cost: 1, hint: "Costs 1 time." }
];
if (s.flags.hasKeys) out.unshift({ label: "Try the keys on the maintenance panel.", to: "panel", cost: 1, hint: "Costs 1 time." });
return out;
}
},
panel: {
title: "Maintenance Panel",
text:
`One key fits.
A small access hatch clicks open, revealing a manual selector: B, 1, 2… up to 20.
A note is taped inside: "If you go too high, don't stop. If you stop, don't look."`,
effects: (s) => { s.flags.unlockedPanel = true; },
choices: [
{ label: "Select Floor 7.", to: "f7", cost: 1, hint: "Costs 1 time." },
{ label: "Select Floor 13.", to: "f13", cost: 1, hint: "Costs 1 time." },
{ label: "Select Floor 20.", to: "f20", cost: 1, hint: "Costs 1 time." }
]
},
listen: {
title: "Static Instructions",
text:
`The speaker tries again:
"…If you are inside… do not use stairs… service override available…"
Then, softer: "Don’t open the doors unless you're sure."`,
effects: (s) => { s.flags.heardWarning = true; },
choices: [
{ label: "Press SERVICE.", to: "service", cost: 0, hint: "No cost." },
{ label: "Step back out to the lobby.", to: "start", cost: 1, hint: "Costs 1 time." }
]
},
service: {
title: "SERVICE Mode",
text:
`The elevator shudders—alive.
The display shows: "SELECT: 7 / 13 / 20"
A thin blue light runs along the door seam like a locked smile.`,
choices: (s) => ([
{ label: "Go to Floor 7.", to: "f7", cost: 1, hint: "Costs 1 time." },
{ label: "Go to Floor 13.", to: "f13", cost: 1, hint: "Costs 1 time." },
{ label: "Go to Floor 20.", to: "f20", cost: 1, hint: "Costs 1 time." },
])
},
f7: {
title: "Floor 7 — Offices",
text:
`The doors part on a dim corridor.
Exit signs glow like distant embers. The air is normal here—almost.
A security door stands ajar.`,
choices: (s) => {
const out = [
{ label: "Walk the corridor toward the glow.", to: "officeGlow", cost: 1, hint: "Costs 1 time." },
{ label: "Return to the elevator immediately.", to: "inside", cost: 1, hint: "Costs 1 time." },
];
if (s.flags.hasBattery) out.unshift({ label: "Use the battery pack to power your phone.", to: "phone", cost: 0, hint: "No cost." });
return out;
}
},
phone: {
title: "A Single Bar of Signal",
text:
`Your phone wakes. One bar. A text draft appears as if it was already there:
"IF YOU ARE READING THIS, DO NOT GO TO 13."`,
effects: (s) => { s.flags.warned13 = true; },
choices: [
{ label: "Continue into the corridor.", to: "officeGlow", cost: 1, hint: "Costs 1 time." },
{ label: "Return to the elevator.", to: "inside", cost: 1, hint: "Costs 1 time." }
]
},
officeGlow: {
title: "Floor 7 — The Glow",
text:
`In a conference room, a laptop screen is still on.
On it: an evacuation plan. A highlighted route leads to a rooftop access on Floor 20.
A second highlight circles Floor 13—scribbled over, angry.`,
effects: (s) => { s.flags.learned20 = true; },
choices: (s) => ([
{ label: "Go back to the elevator and head for Floor 20.", to: "f20", cost: 1, hint: "Costs 1 time." },
{ label: "Ignore the plan. Check Floor 13 anyway.", to: "f13", cost: 1, hint: "Costs 1 time." }
])
},
f13: {
title: "Floor 13 — The Unlabeled Floor",
text:
`The elevator stops too softly.
The doors open on a hallway that looks like the lobby, but… wrong.
The carpet pattern repeats in a way that makes your eyes want to slide away.`,
choices: (s) => {
const warned = s.flags.warned13 || s.flags.heardWarning;
return [
{ label: "Step out and follow the hallway.", to: "thirteenHall", cost: 1, hint: "Costs 1 time." },
{ label: warned ? "Do NOT step out. Slam CLOSE." : "Hesitate. Then step back.", to: "inside", cost: 1, hint: "Costs 1 time." },
{ label: "Look into the mirror.", to: "mirror", cost: 0, hint: "No cost." }
];
}
},
mirror: {
title: "Mirror — A Fraction Late",
text:
`Your reflection moves just a fraction after you do.
It smiles like it practiced.`,
effects: (s)=>{ s.flags.mirrorSeen = true; },
choices: [
{ label: "Back away. Close the doors.", to: "inside", cost: 1, hint: "Costs 1 time." },
{ label: "Step out anyway.", to: "thirteenHall", cost: 1, hint: "Costs 1 time." }
]
},
thirteenHall: {
title: "Floor 13 — The Long Hall",
text:
`The exit sign is ahead. Always ahead.
Your footsteps sound normal until they don't.
Something behind you exhales.`,
choices: (s) => [
{ label: "Run for the exit sign.", to: "thirteenRun", cost: 2, hint: "Costs 2 time." },
{ label: "Turn around slowly.", to: "thirteenTurn", cost: 0, hint: "No cost." },
{ label: "Close your eyes and walk backward.", to: "thirteenBack", cost: 1, hint: "Costs 1 time." }
]
},
thirteenRun: {
title: "Floor 13 — Too Fast",
text:
`You sprint. The sign flickers.
The hallway stretches like it’s being pulled.
Your lungs burn with borrowed air.`,
choices: [
{ label: "Keep running.", to: "endingBad13", cost: 1, hint: "Costs 1 time." },
{ label: "Stop. Don't look behind you.", to: "endingWeird13", cost: 0, hint: "No cost." }
]
},
thirteenTurn: {
title: "Floor 13 — Facing It",
text:
`You turn.
There is nothing there—until your mind insists there is.
A shape made of absence stands where the hallway should be.`,
choices: [
{ label: "Say: “No.”", to: "endingWeird13", cost: 0, hint: "No cost." },
{ label: "Apologize without knowing why.", to: "endingBad13", cost: 0, hint: "No cost." }
]
},
thirteenBack: {
title: "Floor 13 — Backward Steps",
text:
`You walk backward with your eyes closed.
The air warms.
A door clicks behind you: the elevator.`,
choices: [
{ label: "Fall into the elevator. Mash SERVICE.", to: "service", cost: 1, hint: "Costs 1 time." },
{ label: "Open your eyes first.", to: "endingBad13", cost: 0, hint: "No cost." }
]
},
f20: {
title: "Floor 20 — Rooftop Access",
text:
`The elevator climbs like it’s reluctant.
Floor 20 is quiet—too quiet.
A heavy door marked ROOF is chained, but the chain looks old.`,
choices: (s) => {
const out = [];
if (s.flags.hasKeys) out.push({ label: "Use the keys to try the rooftop lock.", to: "roof", cost: 1, hint: "Costs 1 time." });
out.push({ label: "Pull the chain until it gives.", to: "roof", cost: 2, hint: "Costs 2 time." });
out.push({ label: "Return to the elevator and go back down.", to: "inside", cost: 1, hint: "Costs 1 time." });
return out;
}
},
roof: {
title: "Rooftop — Wind and Citylight",
text:
`Cold wind hits your face like a verdict.
The city is a field of quiet stars. Somewhere far below, a siren finally completes its song.
A maintenance ladder leads to a blinking antenna beacon.`,
choices: (s) => [
{ label: "Climb to the beacon and signal for help.", to: "endingGood", cost: 1, hint: "Costs 1 time." },
{ label: "Stay low. Wait. Listen.", to: "endingNeutral", cost: 1, hint: "Costs 1 time." }
]
},
endingGood: {
title: "Ending — The Beacon Answers",
ending: true,
text:
`You climb. Your hands are numb, but steady.
You flash the beacon, over and over—simple, stubborn light.
Minutes later (or hours), you see it: a helicopter sweep, a searchlight, a slow turn toward you.
Somewhere deep in the building, the elevator dings once—like it’s disappointed.`,
choices: [
{ label: "Restart.", to: "start", cost: 0, hint: "Try a different path." }
]
},
endingNeutral: {
title: "Ending — Safe, For Now",
ending: true,
text:
`You don’t climb.
You keep your body small against the rooftop wall and let the wind pass over you.
You survive the night.
In the morning, the building is still without power—and the elevator doors are closed.
When you leave, you never look up at the 13th-floor windows.`,
choices: [
{ label: "Restart.", to: "start", cost: 0, hint: "Try a different path." }
]
},
endingBad13: {
title: "Ending — The Hall Keeps Going",
ending: true,
text:
`You make one more choice.
The hallway makes a thousand more for you.
The exit sign stays ahead. Always ahead.
And behind you, something follows with your exact footsteps—perfectly on time.`,
choices: [
{ label: "Restart.", to: "start", cost: 0, hint: "Avoid Floor 13." }
]
},
endingWeird13: {
title: "Ending — You Are Released",
ending: true,
text:
`You stop moving.
You refuse to play along.
The air loosens. The carpet pattern becomes normal again.
A door appears where there wasn’t one before: the stairwell, lit and ordinary.
You descend and reach the lobby with time missing from your watch.`,
choices: [
{ label: "Restart.", to: "start", cost: 0, hint: "Try reaching the roof." }
]
},
timeout: {
title: "Ending — Time Runs Out",
ending: true,
text:
`The last emergency light flickers and dies.
The building becomes a shape made of dark.
Somewhere close, the elevator makes a sound like a sigh.
Then nothing.`,
choices: [
{ label: "Restart.", to: "start", cost: 0, hint: "Move faster." }
]
}
};
// --------- State ----------
const state = {
nodeId: "start",
time: START_TIME,
flags: {},
seenCount: 0,
history: [] // stack of snapshots for undo
};
// --------- UI ----------
const el = {
title: document.getElementById("sceneTitle"),
text: document.getElementById("sceneText"),
choices: document.getElementById("choices"),
toast: document.getElementById("toast"),
progress: document.getElementById("progress"),
timeLabel: document.getElementById("timeLabel"),
timeBar: document.getElementById("timeBar"),
undoBtn: document.getElementById("undoBtn"),
restartBtn: document.getElementById("restartBtn"),
sceneRoot: document.getElementById("sceneRoot")
};
function clamp(n, a, b){ return Math.max(a, Math.min(b, n)); }
function snapshot(){
// Deep-ish clone flags
state.history.push({
nodeId: state.nodeId,
time: state.time,
flags: JSON.parse(JSON.stringify(state.flags)),
seenCount: state.seenCount
});
// Keep history short and tidy
if (state.history.length > 40) state.history.shift();
}
function restore(){
const snap = state.history.pop();
if (!snap) return false;
state.nodeId = snap.nodeId;
state.time = snap.time;
state.flags = snap.flags;
state.seenCount = snap.seenCount;
return true;
}
function showToast(msg){
el.toast.textContent = msg || "";
if (msg) setTimeout(() => { if (el.toast.textContent === msg) el.toast.textContent=""; }, 1800);
}
function applyCost(cost){
if (!cost) return;
state.time = clamp(state.time - cost, 0, START_TIME);
}
function goto(nodeId, meta = {}){
const node = NODES[nodeId];
if (!node) throw new Error("Unknown node: " + nodeId);
// Save for undo unless we're restarting
if (!meta.noUndo) snapshot();
// Apply time cost already deducted outside
state.nodeId = nodeId;
// Per-node effects
if (typeof node.effects === "function") node.effects(state);
// Count scenes (excluding endings/timeout)
if (!node.ending && nodeId !== "timeout") state.seenCount++;
// If time hit zero and node isn't ending, force timeout
if (state.time <= 0 && !node.ending && nodeId !== "timeout") {
state.nodeId = "timeout";
}
render();
}
function getChoices(node){
const raw = (typeof node.choices === "function") ? node.choices(state) : node.choices;
// Normalize with optional per-choice effect
return raw.map((c) => ({
label: c.label,
to: c.to,
cost: c.cost || 0,
hint: c.hint || "",
effect: c.effect || null,
disabled: !!c.disabled
}));
}
function totalScenesEstimate(){
// Elegant, not exact. Fixed label keeps UI simple.
return 14;
}
function render(){
const node = NODES[state.nodeId];
// Animate card content lightly
el.sceneRoot.classList.remove("fade");
void el.sceneRoot.offsetWidth; // reflow
el.sceneRoot.classList.add("fade");
el.title.textContent = node.title;
el.text.textContent = node.text;
// HUD
el.timeLabel.textContent = String(state.time);
el.timeBar.style.width = `${(state.time / START_TIME) * 100}%`;
el.progress.textContent = `Scene ${clamp(state.seenCount || 1, 1, totalScenesEstimate())} / ${totalScenesEstimate()}`;
// Undo availability
el.undoBtn.disabled = state.history.length === 0;
// Choices
el.choices.innerHTML = "";
const choices = getChoices(node);
// If ending, don't show hint about costs
if (node.ending) showToast("Press R to restart. (Or click Restart.)");
choices.slice(0, 4).forEach((c, idx) => {
const btn = document.createElement("button");
btn.type = "button";
btn.className = "choice";
btn.disabled = c.disabled;
const k = document.createElement("div");
k.className = "kbd";
k.textContent = String(idx + 1);
const main = document.createElement("div");
main.className = "choice-main";
const label = document.createElement("div");
label.className = "choice-label";
label.textContent = c.label;
const hint = document.createElement("div");
hint.className = "choice-hint";
hint.textContent = c.hint || (c.cost ? `Costs ${c.cost} time.` : " ");
main.appendChild(label);
main.appendChild(hint);
btn.appendChild(k);
btn.appendChild(main);
btn.addEventListener("click", () => choose(idx));
el.choices.appendChild(btn);
});
// In super-rare case: no choices (shouldn't happen)
if (choices.length === 0){
const p = document.createElement("p");
p.style.color = "var(--muted)";
p.style.margin = "0";
p.textContent = "No choices available.";
el.choices.appendChild(p);
}
}
function choose(index){
const node = NODES[state.nodeId];
const choices = getChoices(node);
const c = choices[index];
if (!c) return;
// Endings can restart without cost
if (node.ending && c.to === "start") {
restart();
return;
}
snapshot(); // snapshot before applying choice effects/cost
// Apply choice-specific effect
if (typeof c.effect === "function") c.effect(state);
// Apply time cost
applyCost(c.cost);
// If time is out, go to timeout
if (state.time <= 0 && c.to !== "timeout" && !NODES[c.to]?.ending) {
state.nodeId = "timeout";
render();
return;
}
// Move to next node (without adding an extra undo snapshot)
const to = c.to;
// We already snapshot() above, so we don't need goto() to snapshot again.
state.history.pop(); // remove the snapshot from choose(), then let goto snapshot once
// (this avoids double-undo steps)
goto(to);
}
function restart(){
state.history = [];
state.nodeId = "start";
state.time = START_TIME;
state.flags = {};
state.seenCount = 0;
showToast("Restarted.");
render();
}
function undo(){
if (!restore()) {
showToast("Nothing to undo.");
return;
}
showToast("Undone.");
render();
}
// --------- Events ----------
el.restartBtn.addEventListener("click", restart);
el.undoBtn.addEventListener("click", undo);
window.addEventListener("keydown", (e) => {
if (e.key >= "1" && e.key <= "4") {
const idx = Number(e.key) - 1;
choose(idx);
return;
}
if (e.key === "r" || e.key === "R") restart();
if (e.key === "u" || e.key === "U") undo();
});
// --------- Start ----------
restart();
})();
</script>
</body>
</html>