Adding linkToNextStorylet to Readme and fixing a bug

This commit is contained in:
David Masad
2021-02-28 10:38:24 -05:00
parent a30931691e
commit f3bafb8d35
7 changed files with 79 additions and 41 deletions

View File

@ -124,7 +124,7 @@ StoryManager.getAllStorylets = function(tag=null) {
}
StoryManager.getStorylets =
function(n=null, tag=null, selection="weighted", respect_interrupt=true) {
function(n=null, tag=null, selection="ordered", respect_interrupt=true) {
/*
Get n storylets, prioritizing the highest-priority ones first.
@ -138,10 +138,14 @@ StoryManager.getStorylets =
// Check for interruptions
// TODO: Handle more than one interruption
if (respect_interrupt) {
for (let i in allStorylets)
if (allStorylets[i].interrupt) return [allStorylets[i]];
let interruptions = [];
for (let storylet of allStorylets)
if (storylet.interrupt) interruptions.push(storylet);
if (interruptions.length > 0) {
interruptions.sort((a, b) => b.priority - a.priority);
return [interruptions[0]];
}
}
// Return n or the max
@ -188,7 +192,7 @@ StoryManager.weightedRandom = function(allStorylets, n) {
counter = 0;
rand = Math.random() * sum;
for (let i=0; i<allStorylets.length; i++) {
if (counter + allStorylets[i].priority) {
if (counter + allStorylets[i].priority > rand) {
selectedStorylets.push(allStorylets.splice(i, 1)[0]);
break;
}
@ -204,19 +208,27 @@ StoryManager.weightedRandom = function(allStorylets, n) {
// ---------------------------------------------------------------
Macro.add("getStoryletLinks", {
handler: function() {
let n, tag;
[n=null, tag=null] = this.args;
State.temporary.nextStorylets = StoryManager.getStorylets(n, tag);
$(this.output).wiki(`
let n, tag, selection;
[n=null, tag=null, selection="ordered"] = this.args;
State.temporary.nextStorylets = StoryManager.getStorylets(n, tag, selection);
$(this.output).wiki(`\
<<for _storylet range _nextStorylets>> \
<<capture _storylet>> \
[[_storylet.description|_storylet.passage][$currentStorylet=_storylet]]<br>
<</capture>> \
<</for>> \
`);
`)
}
})
});
Macro.add("linkToNextStorylet", {
handler: function() {
let text, tag, selection;
[text, tag=null, selection="weighted"] = this.args;
State.temporary.nextStorylet = StoryManager.getStorylets(1, tag, selection)[0];
$(this.output).wiki(`<<capture _nextStorylet>>[[${text}|_nextStorylet.passage][$currentStorylet=_nextStorylet]]<</capture>>`);
}
});
window.SM = StoryManager;
/* twine-user-script #2: "Story JavaScript" */