163 lines
4.2 KiB
JavaScript
163 lines
4.2 KiB
JavaScript
import jQuery from 'jquery'
|
||
const inkjs = require('inkjs').Story;
|
||
const entryPoint = 'game/game.ink.json';
|
||
|
||
let continueToNextChoice, displayText, loadGame, saveChoice, s;
|
||
|
||
saveChoice = function(index) {
|
||
window.progress.push(index);
|
||
return localStorage.setItem("progress", JSON.stringify(window.progress));
|
||
};
|
||
|
||
function listCharacters (paragraphs) {
|
||
const chars = [
|
||
{
|
||
match: "Я:",
|
||
title: "🙉",
|
||
},
|
||
{
|
||
match: "ПОЛ:",
|
||
title: "🙍♀️"
|
||
}
|
||
]
|
||
let output = "<p>На сцене:</p>"
|
||
for (let j = 0; j < chars.length; j++) {
|
||
const e = chars[j];
|
||
if (paragraphs.match(e.match)) {
|
||
output += "<p>"+e.title+"</p>"
|
||
}
|
||
}
|
||
return output.trim()
|
||
}
|
||
|
||
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 !== "") {
|
||
i = i.replace('<st>', '<span class="subtitle">');
|
||
i = i.replace('</st>', '</span>');
|
||
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) {
|
||
const characters = listCharacters(jQuery("#content").text());
|
||
if (characters.trim().length > 0) {
|
||
if (jQuery("#content .portraits").length === 0) {
|
||
jQuery("#content").prepend('<div class="portraits">'+characters.trim()+'</div>')
|
||
} else {
|
||
jQuery(".portraits").html(characters.trim())
|
||
}
|
||
}
|
||
jQuery("#options").html("").hide();
|
||
ref = s.currentChoices;
|
||
for (j = 0, len = ref.length; j < len; j++) {
|
||
choice = ref[j];
|
||
let text = choice.text.replace('<st>', '<span class="subtitle">');
|
||
text = text.replace('</st>', '</span>')
|
||
text = text.replace('<ell>', '<span class="ellipsis">⏸️ </span>');
|
||
jQuery("#options").append(`<li><a href='#' id='choice-${choice.index}' data-index=${choice.index}>${text}</a></li>`);
|
||
}
|
||
jQuery("#options").fadeIn(500);
|
||
} 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, ref, results;
|
||
ref = window.progress;
|
||
results = [];
|
||
const len = ref.length;
|
||
while(s.canContinue) {
|
||
displayText(s, false);
|
||
}
|
||
for (j = 0; j < len; j++) {
|
||
index = ref[j];
|
||
if (!index) {
|
||
continue
|
||
}
|
||
if (!s.currentChoices[index]) {
|
||
continue
|
||
}
|
||
s.ChooseChoiceIndex(index)
|
||
while(s.canContinue) {
|
||
displayText(s, false);
|
||
}
|
||
}
|
||
return results;
|
||
};
|
||
|
||
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;
|
||
});
|
||
jQuery(document).on('click', "#options li", function() {
|
||
s.ChooseChoiceIndex(jQuery(this).find('a').data("index"));
|
||
saveChoice(jQuery(this).find('a').data("index"));
|
||
continueToNextChoice(s);
|
||
return false;
|
||
});
|