inkjs-linux/js/script.js

161 lines
3.9 KiB
JavaScript
Raw Normal View History

import jQuery from 'jquery'
const inkjs = require('inkjs').Story;
2021-08-25 19:18:58 +03:00
/**
* Path to your game's compiled JSON.
* It is relative to index.html file.
*/
2021-08-14 10:21:51 +03:00
const entryPoint = 'game/fogg.ink.json';
2021-08-14 09:58:24 +03:00
let continueToNextChoice, displayText, loadGame, saveChoice, s;
2021-08-25 19:18:58 +03:00
/**
* You can change this function.
* It's an easy way to define your own tags and text transformations.
*/
transform = function(text) {
text = text.replace('<st>', '<span class="subtitle">');
text = text.replace('</st>', '</span>');
return text;
}
/**
* You don't need to change anything past this point.
*/
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 !== "") {
2021-08-25 19:18:58 +03:00
i = transform(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];
2021-08-25 19:18:58 +03:00
let text = transform(choice.text)
2020-11-07 12:16:38 +02:00
jQuery("#options").append(`<li><a href='#' id='choice-${choice.index}' data-index=${choice.index}>${text}</a></li>`);
}
2020-11-07 12:16:38 +02:00
jQuery("#options").fadeIn(500);
} else {
jQuery("#content").append("<p>THE END</p>");
jQuery("#options").html("");
}
2021-08-24 12:07:55 +03:00
if (window.progress.length > 0) {
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;
};
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();
});
function choose(index) {
s.ChooseChoiceIndex(index);
saveChoice(index);
continueToNextChoice(s);
}
jQuery(document).on('click', "#options li a", function() {
choose(jQuery(this).data("index"));
return false;
});
2021-08-14 10:06:06 +03:00
jQuery(document).on('click', "#options li", function() {
choose(jQuery(this).find('a').data("index"));
return false;
});
jQuery(document).on('keydown', function(key) {
key = key.key
switch (key) {
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
jQuery(`#options li:nth-child(${key})`).first().trigger("click")
break;
case '0':
jQuery(`#options li:nth-child(10)`).first().trigger("click")
break;
}
2021-08-14 10:06:06 +03:00
return false;
});