115 lines
2.9 KiB
JavaScript
115 lines
2.9 KiB
JavaScript
import jQuery from 'jquery'
|
|
const inkjs = require('inkjs').Story;
|
|
const entryPoint = 'fogg.ink.json';
|
|
|
|
let clearProgress, continueToNextChoice, displayText, loadGame, saveChoice, s;
|
|
|
|
saveChoice = function(index) {
|
|
window.progress.push(index);
|
|
return localStorage.setItem("progress", JSON.stringify(window.progress));
|
|
};
|
|
|
|
displayText = function(s, interactive = true) {
|
|
let block, delay, html, i, paragraphs, results;
|
|
results = [];
|
|
while (s.canContinue) {
|
|
paragraphs = s.Continue().split("\n");
|
|
if (interactive) {
|
|
delay = 1000;
|
|
}
|
|
results.push((function() {
|
|
let j, len, results1;
|
|
results1 = [];
|
|
for (j = 0, len = paragraphs.length; j < len; j++) {
|
|
i = paragraphs[j];
|
|
if (i !== "") {
|
|
html = jQuery.parseHTML(i);
|
|
block = jQuery('<p>').html(html);
|
|
if (interactive) {
|
|
block.hide();
|
|
}
|
|
jQuery("#content").append(block);
|
|
if (interactive) {
|
|
block.fadeIn(delay);
|
|
results1.push(delay += 500);
|
|
} else {
|
|
results1.push(void 0);
|
|
}
|
|
} else {
|
|
results1.push(void 0);
|
|
}
|
|
}
|
|
return results1;
|
|
})());
|
|
}
|
|
return results;
|
|
};
|
|
|
|
continueToNextChoice = function(s) {
|
|
let choice, j, len, ref, scrollTo;
|
|
displayText(s, true);
|
|
scrollTo = jQuery('#options').offset().top;
|
|
if (s.currentChoices.length > 0) {
|
|
jQuery("#options").html("").hide();
|
|
ref = s.currentChoices;
|
|
for (j = 0, len = ref.length; j < len; j++) {
|
|
choice = ref[j];
|
|
jQuery("#options").append(`<li><a href='#' id='choice-${choice.index}' data-index=${choice.index}>${choice.text}</a></li>`);
|
|
}
|
|
jQuery("#options").fadeIn(800);
|
|
} else {
|
|
jQuery("#content").append("<p>THE END</p>");
|
|
jQuery("#options").html("");
|
|
}
|
|
return jQuery('html, body').animate({
|
|
scrollTop: scrollTo
|
|
}, 800);
|
|
};
|
|
|
|
loadGame = function(s) {
|
|
let index, j, len, ref, results;
|
|
ref = window.progress;
|
|
results = [];
|
|
for (j = 0, len = ref.length; j < len; j++) {
|
|
index = ref[j];
|
|
displayText(s, false);
|
|
results.push(s.ChooseChoiceIndex(index));
|
|
}
|
|
return results;
|
|
};
|
|
|
|
clearProgress = function() {
|
|
return window.progress = [];
|
|
};
|
|
|
|
jQuery.ajax({
|
|
url: entryPoint,
|
|
dataType: 'text',
|
|
success: function(data) {
|
|
let progress;
|
|
progress = localStorage.getItem("progress");
|
|
if (progress != null) {
|
|
window.progress = JSON.parse(progress);
|
|
} else {
|
|
window.progress = [];
|
|
}
|
|
s = new inkjs(data);
|
|
if (window.progress !== []) {
|
|
loadGame(s);
|
|
}
|
|
return continueToNextChoice(s);
|
|
}
|
|
});
|
|
|
|
jQuery(document).on('click', "#restart", function() {
|
|
localStorage.setItem("progress", '[]');
|
|
window.location.reload();
|
|
});
|
|
|
|
jQuery(document).on('click', "#options li a", function() {
|
|
s.ChooseChoiceIndex(jQuery(this).data("index"));
|
|
saveChoice(jQuery(this).data("index"));
|
|
continueToNextChoice(s);
|
|
return false;
|
|
});
|