scratchy/js/script.js

174 lines
4.3 KiB
JavaScript
Raw Normal View History

2016-06-08 17:28:38 +03:00
import jQuery from 'jquery'
const inkjs = require('inkjs').Story;
2021-11-19 19:29:25 +02:00
/**
* Path to your game's compiled JSON.
* It is relative to index.html file.
*/
2021-08-14 15:18:12 +03:00
const entryPoint = 'game/game.ink.json';
2016-06-08 17:28:38 +03:00
2021-11-19 19:29:25 +02:00
/**
* You can change this function.
* It's an easy way to define your own tags and text transformations.
*/
function transform (text) {
text = text.replace('<st>', '<span class="subtitle">');
text = text.replace('</st>', '</span>');
text = text.replace('<ell>', '<span class="ellipsis">⏸️&nbsp;</span>');
return text;
}
2016-06-08 17:28:38 +03:00
2021-11-19 19:29:25 +02:00
/**
* You don't need to change anything past this point.
*/
function saveChoice(index) {
2016-06-08 17:28:38 +03:00
window.progress.push(index);
return localStorage.setItem("progress", JSON.stringify(window.progress));
};
2021-11-19 19:29:25 +02:00
function displayText(interactive = true) {
2016-06-08 17:28:38 +03:00
let block, delay, html, i, paragraphs, results;
results = [];
2021-11-19 19:29:25 +02:00
while (window.s.canContinue) {
paragraphs = window.s.Continue().split("\n");
2016-06-08 17:28:38 +03:00
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-11-19 19:29:25 +02:00
i = transform(i);
2016-06-08 17:28:38 +03:00
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;
};
2021-11-19 19:29:25 +02:00
function continueToNextChoice () {
2016-06-08 17:28:38 +03:00
let choice, j, len, ref, scrollTo;
2021-11-19 19:29:25 +02:00
displayText(true);
2016-06-08 17:28:38 +03:00
scrollTo = jQuery('#options').offset().top;
if (s.currentChoices.length > 0) {
jQuery("#options").html("").hide();
2021-11-19 19:29:25 +02:00
ref = window.s.currentChoices;
2016-06-08 17:28:38 +03:00
for (j = 0, len = ref.length; j < len; j++) {
choice = ref[j];
2021-11-19 19:29:25 +02:00
let text = transform(choice.text)
2016-06-08 17:28:38 +03:00
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("");
}
2021-11-19 19:29:25 +02:00
if (window.progress.length > 0) {
return jQuery('html, body').animate({
scrollTop: scrollTo
}, 800);
}
2016-06-08 17:28:38 +03:00
};
2021-11-19 19:29:25 +02:00
function loadGame () {
let index, j, len, ref, results;
document.getElementById("content").innerHTML = ""
document.getElementById("options").innerHTML = ""
2016-06-08 17:28:38 +03:00
ref = window.progress;
results = [];
2021-11-19 19:29:25 +02:00
if (ref.length > 0) {
for (j = 0, len = ref.length; j < len; j++) {
index = ref[j];
displayText(false);
results.push(window.s.ChooseChoiceIndex(index));
2021-08-14 15:18:12 +03:00
}
2021-11-19 19:29:25 +02:00
} else {
continueToNextChoice(window.s);
2016-06-08 17:28:38 +03:00
}
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 = [];
}
2021-11-19 19:29:25 +02:00
window.s = new inkjs(data)
2016-06-08 17:28:38 +03:00
if (window.progress !== []) {
2021-11-19 19:29:25 +02:00
loadGame(window.s);
2016-06-08 17:28:38 +03:00
}
2021-11-19 19:29:25 +02:00
return continueToNextChoice(window.s);
2016-06-08 17:28:38 +03:00
}
});
jQuery(document).on('click', "#restart", function() {
localStorage.setItem("progress", '[]');
window.location.reload();
});
2021-11-19 19:29:25 +02:00
jQuery(document).on('click', "#undo", function() {
jQuery("#undo").hide();
window.progress.pop()
localStorage.setItem("progress", JSON.stringify(window.progress));
window.location.reload();
});
function choose(index) {
window.s.ChooseChoiceIndex(index);
jQuery("#undo").show()
saveChoice(index);
2016-06-08 17:28:38 +03:00
continueToNextChoice(s);
2021-11-19 19:29:25 +02:00
}
jQuery(document).on('click', "#options li a", function() {
choose(jQuery(this).data("index"));
2016-06-08 17:28:38 +03:00
return false;
});
jQuery(document).on('click', "#options li", function() {
2021-11-19 19:29:25 +02:00
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;
}
2016-06-08 17:28:38 +03:00
return false;
});