-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
533 lines (458 loc) · 20.9 KB
/
main.js
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
var re = /\((.*)\)/;
var audioFlag = 0;
var myAudio = document.getElementById("game-audio");
var timeFlag = true;
function togglePlay(){
return myAudio.paused ? myAudio.play() : myAudio.pause();
};
function theme_check(change=false){
if(!change){
page_color = window.getComputedStyle( document.body ,null).getPropertyValue('background-color');
if (page_color.includes("255")){
document.getElementById("time_remaining").style.color = 'black';
document.getElementById("hours").style.color = 'black';
document.getElementById("hours_text").style.color = 'black';
document.getElementById("mins").style.color = 'black';
document.getElementById("mins_text").style.color = 'black';
var list= document.getElementsByTagName('p');
for (var i = 0; i < document.getElementsByTagName('p').length; i++) {
document.getElementsByTagName('p')[i].style.color = "black";
}
}
if (page_color.includes("0")){
document.getElementById("time_remaining").style.color = 'white';
document.getElementById("hours").style.color = 'white';
document.getElementById("hours_text").style.color = 'white';
document.getElementById("mins").style.color = 'white';
document.getElementById("mins_text").style.color = 'white';
for (var i = 0; i < document.getElementsByTagName('p').length; i++) {
document.getElementsByTagName('p')[i].style.color = "white";
}
}
}
else{
if(document.getElementById("time_remaining").style.color == 'black'){
document.getElementById("time_remaining").style.color = 'white';
document.getElementById("hours").style.color = 'white';
document.getElementById("hours_text").style.color = 'white';
document.getElementById("mins").style.color = 'white';
document.getElementById("mins_text").style.color = 'white';
for (var i = 0; i < document.getElementsByTagName('p').length; i++) {
document.getElementsByTagName('p')[i].style.color = "white";
}
}
else{
document.getElementById("time_remaining").style.color = 'black';
document.getElementById("hours").style.color = 'black';
document.getElementById("hours_text").style.color = 'black';
document.getElementById("mins").style.color = 'black';
document.getElementById("mins_text").style.color = 'black';
for (var i = 0; i < document.getElementsByTagName('p').length; i++) {
document.getElementsByTagName('p')[i].style.color = "black";
}
}
}
}
(function(storyContent) {
// Create ink story from the content using inkjs
var story = new inkjs.Story(storyContent);
// console.log(story);
var savePoint = "";
let savedTheme;
let globalTagTheme;
// Global tags - those at the top of the ink file
// We support:
// # theme: dark
// # author: Your Name
var globalTags = story.globalTags;
if( globalTags ) {
for(var i=0; i<story.globalTags.length; i++) {
var globalTag = story.globalTags[i];
var splitTag = splitPropertyTag(globalTag);
// THEME: dark
if( splitTag && splitTag.property == "theme" ) {
globalTagTheme = splitTag.val;
}
// author: Your Name
else if( splitTag && splitTag.property == "author" ) {
var byline = document.querySelector('.byline');
byline.innerHTML = "by "+splitTag.val;
}
}
}
var storyContainer = document.querySelector('#story');
var outerScrollContainer = document.querySelector('.outerContainer');
// page features setup
setupTheme(globalTagTheme);
var hasSave = loadSavePoint();
setupButtons(hasSave);
// Set initial save point
savePoint = story.state.toJson();
// Kick off the start of the story!
continueStory(true);
// Main story processing function. Each time this is called it generates
// all the next content up as far as the next set of choices.
function continueStory(firstTime) {
var paragraphIndex = 0;
var delay = 0.0;
// Don't over-scroll past new content
var previousBottomEdge = firstTime ? 0 : contentBottomEdgeY();
// Generate story text - loop through available content
while(story.canContinue) {
// console.log("while");
// Get ink to generate the next paragraph
var paragraphText = story.Continue();
var tags = story.currentTags;
// Any special tags included with this line
var customClasses = [];
for(var i=0; i<tags.length; i++) {
var tag = tags[i];
// Detect tags of the form "X: Y". Currently used for IMAGE and CLASS but could be
// customised to be used for other things too.
var splitTag = splitPropertyTag(tag);
// AUDIO: src
if( splitTag && splitTag.property == "AUDIO" ) {
if('audio' in this) {
this.audio.pause();
this.audio.removeAttribute('src');
this.audio.load();
}
this.audio = new Audio(splitTag.val);
this.audio.play();
}
// AUDIOLOOP: src
else if( splitTag && splitTag.property == "AUDIOLOOP" ) {
if('audioLoop' in this) {
this.audioLoop.pause();
this.audioLoop.removeAttribute('src');
this.audioLoop.load();
}
this.audioLoop = new Audio(splitTag.val);
this.audioLoop.play();
this.audioLoop.loop = true;
}
// IMAGE: src
if( splitTag && splitTag.property == "IMAGE" ) {
var imageElement = document.createElement('img');
imageElement.src = splitTag.val;
storyContainer.appendChild(imageElement);
showAfter(delay, imageElement);
delay += 200.0;
}
// LINK: url
else if( splitTag && splitTag.property == "LINK" ) {
window.location.href = splitTag.val;
}
// LINKOPEN: url
else if( splitTag && splitTag.property == "LINKOPEN" ) {
window.open(splitTag.val);
}
// BACKGROUND: src
else if( splitTag && splitTag.property == "BACKGROUND" ) {
outerScrollContainer.style.backgroundImage = 'url('+splitTag.val+')';
}
// CLASS: className
else if( splitTag && splitTag.property == "CLASS" ) {
customClasses.push(splitTag.val);
}
// CLEAR - removes all existing content.
// RESTART - clears everything and restarts the story from the beginning
else if( tag == "CLEAR" || tag == "RESTART" ) {
removeAll("p");
removeAll("img");
// Comment out this line if you want to leave the header visible when clearing
setVisible(".header", false);
if( tag == "RESTART" ) {
restart();
return;
}
}
}
// Create paragraph element (initially hidden)
var paragraphElement = document.createElement('p');
paragraphElement.innerHTML = paragraphText;
storyContainer.appendChild(paragraphElement);
// Add any custom classes derived from ink tags
for(var i=0; i<customClasses.length; i++)
paragraphElement.classList.add(customClasses[i]);
// Fade in paragraph after a short delay
showAfter(delay, paragraphElement);
delay += 200.0;
}
// console.log(story.currentChoices);
// Create HTML choices from ink choices
story.currentChoices.forEach(function(choice) {
// Create paragraph with anchor element
var choiceParagraphElement = document.createElement('p');
choiceParagraphElement.classList.add("choice");
choiceParagraphElement.innerHTML = `<a href='#'>${choice.text}</a>`
// console.log(choice.text);
storyContainer.appendChild(choiceParagraphElement);
// Fade choice in after a short delay
showAfter(delay, choiceParagraphElement);
delay += 200.0;
// Click on choice
var choiceAnchorEl = choiceParagraphElement.querySelectorAll("a")[0];
choiceAnchorEl.addEventListener("click", function(event) {
// console.log("Inside addevenlistener");
// console.log(choiceAnchorEl.textContent);
a_text = choiceAnchorEl.textContent;
if(a_text.includes("Continue reading") && audioFlag==0){
togglePlay();
audioFlag=1;
}
if(a_text.includes("hours") || a_text.includes("minutes")){
timeDeduction=a_text.match(re)[1];
if(timeDeduction.includes("hours")){
const hrs = document.getElementById("hours");
hrs_int = parseInt(hrs.textContent);
hrsDeduction = parseInt(timeDeduction.replace("hours",""));
hrsRemaining = hrs_int - hrsDeduction;
if(hrsRemaining>=0){
hrs.textContent = ("0" + hrsRemaining.toString()).slice(-2);
}
else{
timeFlag = false;
hrs.textContent = "00";
const mins = document.getElementById("mins");
mins.textContent = "00";
}
}
else if(timeDeduction.includes("minutes")){
const mins = document.getElementById("mins");
mins_int = parseInt(mins.textContent);
minsDeduction = parseInt(timeDeduction.replace("minutes",""));
if(mins_int >= minsDeduction){
minsRemaining = mins_int - minsDeduction;
mins.textContent = ("0" + minsRemaining.toString()).slice(-2);
}
else{
const hrs = document.getElementById("hours");
hrs_int = parseInt(hrs.textContent);
hrsRemaining = hrs_int - 1;
if(hrsRemaining>=0){
hrs.textContent = ("0" + hrsRemaining.toString()).slice(-2);
minsDeduction = minsDeduction - mins_int;
minsRemaining = 60 - minsDeduction;
mins.textContent = ("0" + minsRemaining.toString()).slice(-2);
}
else{
timeFlag = false;
hrs.textContent = "00";
mins.textContent = "00";
}
}
}
// console.log(timeDeduction);
}
// if(a_text.includes("restart")){
// const hrs = document.getElementById("hours");
// hrs.textContent = "06"
// const mins = document.getElementById("mins");
// mins.textContent = "00"
// }
// Don't follow <a> link
event.preventDefault();
// Remove all existing choices
removeAll(".choice");
if(timeFlag==true){
// Tell the story where to go next
story.ChooseChoiceIndex(choice.index);
// This is where the save button will save from
savePoint = story.state.toJson();
// Aaand loop
continueStory();
}
else{
paragraphText = "Uh oh! You are out of time! You did not make wise choices and now Rob goes free."
var paragraphElement = document.createElement('p');
paragraphElement.innerHTML = paragraphText;
paragraphElement.style.fontWeight = "bold";
storyContainer.appendChild(paragraphElement);
// Fade in paragraph after a short delay
showAfter(delay, paragraphElement);
storyContainer.style.height = contentBottomEdgeY()+"px";
scrollDown(previousBottomEdge);
paragraphText = "Rob sues the department and Jake retires in shame knowing that the first and only case he lost was his last.";
var paragraphElement = document.createElement('p');
paragraphElement.innerHTML = paragraphText;
storyContainer.appendChild(paragraphElement);
// Fade in paragraph after a short delay
showAfter(delay, paragraphElement);
storyContainer.style.height = contentBottomEdgeY()+"px";
scrollDown(previousBottomEdge);
paragraphText = "The end.";
var paragraphElement = document.createElement('p');
paragraphElement.innerHTML = paragraphText;
storyContainer.appendChild(paragraphElement);
// Fade in paragraph after a short delay
showAfter(delay, paragraphElement);
storyContainer.style.height = contentBottomEdgeY()+"px";
scrollDown(previousBottomEdge);
}
});
});
// Extend height to fit
// We do this manually so that removing elements and creating new ones doesn't
// cause the height (and therefore scroll) to jump backwards temporarily.
storyContainer.style.height = contentBottomEdgeY()+"px";
if( !firstTime )
scrollDown(previousBottomEdge);
}
function restart() {
story.ResetState();
setVisible(".header", true);
// set save point to here
savePoint = story.state.toJson();
continueStory(true);
outerScrollContainer.scrollTo(0, 0);
const hrs = document.getElementById("hours");
hrs.textContent = "06";
const mins = document.getElementById("mins");
mins.textContent = "00";
timeFlag = true;
}
// -----------------------------------
// Various Helper functions
// -----------------------------------
// Fades in an element after a specified delay
function showAfter(delay, el) {
el.classList.add("hide");
setTimeout(function() { el.classList.remove("hide") }, delay);
}
// Scrolls the page down, but no further than the bottom edge of what you could
// see previously, so it doesn't go too far.
function scrollDown(previousBottomEdge) {
// Line up top of screen with the bottom of where the previous content ended
var target = previousBottomEdge;
// Can't go further than the very bottom of the page
var limit = outerScrollContainer.scrollHeight - outerScrollContainer.clientHeight;
if( target > limit ) target = limit;
var start = outerScrollContainer.scrollTop;
var dist = target - start;
var duration = 300 + 300*dist/100;
var startTime = null;
function step(time) {
if( startTime == null ) startTime = time;
var t = (time-startTime) / duration;
var lerp = 3*t*t - 2*t*t*t; // ease in/out
outerScrollContainer.scrollTo(0, (1.0-lerp)*start + lerp*target);
if( t < 1 ) requestAnimationFrame(step);
}
requestAnimationFrame(step);
}
// The Y coordinate of the bottom end of all the story content, used
// for growing the container, and deciding how far to scroll.
function contentBottomEdgeY() {
var bottomElement = storyContainer.lastElementChild;
return bottomElement ? bottomElement.offsetTop + bottomElement.offsetHeight : 0;
}
// Remove all elements that match the given selector. Used for removing choices after
// you've picked one, as well as for the CLEAR and RESTART tags.
function removeAll(selector)
{
var allElements = storyContainer.querySelectorAll(selector);
for(var i=0; i<allElements.length; i++) {
var el = allElements[i];
el.parentNode.removeChild(el);
}
}
// Used for hiding and showing the header when you CLEAR or RESTART the story respectively.
function setVisible(selector, visible)
{
var allElements = storyContainer.querySelectorAll(selector);
for(var i=0; i<allElements.length; i++) {
var el = allElements[i];
if( !visible )
el.classList.add("invisible");
else
el.classList.remove("invisible");
}
}
// Helper for parsing out tags of the form:
// # PROPERTY: value
// e.g. IMAGE: source path
function splitPropertyTag(tag) {
var propertySplitIdx = tag.indexOf(":");
if( propertySplitIdx != null ) {
var property = tag.substr(0, propertySplitIdx).trim();
var val = tag.substr(propertySplitIdx+1).trim();
return {
property: property,
val: val
};
}
return null;
}
// Loads save state if exists in the browser memory
function loadSavePoint() {
try {
let savedState = window.localStorage.getItem('save-state');
if (savedState) {
story.state.LoadJson(savedState);
return true;
}
} catch (e) {
console.debug("Couldn't load save state");
}
return false;
}
// Detects which theme (light or dark) to use
function setupTheme(globalTagTheme) {
// load theme from browser memory
var savedTheme;
try {
savedTheme = window.localStorage.getItem('theme');
} catch (e) {
console.debug("Couldn't load saved theme");
}
// Check whether the OS/browser is configured for dark mode
var browserDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
if (savedTheme === "dark"
|| (savedTheme == undefined && globalTagTheme === "dark")
|| (savedTheme == undefined && globalTagTheme == undefined && browserDark))
document.body.classList.add("dark");
}
// Used to hook up the functionality for global functionality buttons
function setupButtons(hasSave) {
let rewindEl = document.getElementById("rewind");
if (rewindEl) rewindEl.addEventListener("click", function(event) {
removeAll("p");
removeAll("img");
setVisible(".header", false);
restart();
});
let saveEl = document.getElementById("save");
if (saveEl) saveEl.addEventListener("click", function(event) {
try {
window.localStorage.setItem('save-state', savePoint);
document.getElementById("reload").removeAttribute("disabled");
window.localStorage.setItem('theme', document.body.classList.contains("dark") ? "dark" : "");
} catch (e) {
console.warn("Couldn't save state");
}
});
let reloadEl = document.getElementById("reload");
if (!hasSave) {
reloadEl.setAttribute("disabled", "disabled");
}
reloadEl.addEventListener("click", function(event) {
if (reloadEl.getAttribute("disabled"))
return;
removeAll("p");
removeAll("img");
try {
let savedState = window.localStorage.getItem('save-state');
if (savedState) story.state.LoadJson(savedState);
} catch (e) {
console.debug("Couldn't load save state");
}
continueStory(true);
});
let themeSwitchEl = document.getElementById("theme-switch");
if (themeSwitchEl) themeSwitchEl.addEventListener("click", function(event) {
document.body.classList.add("switched");
document.body.classList.toggle("dark");
});
}
})(storyContent);