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" */
@ -239,8 +251,9 @@ StoryManager.storylets["Go somewhere"] = {
yield storylet;
}
}
}</script><tw-passagedata pid="1" name="Start" tags="" position="100,100" size="100,100">You find yourself in $currentLocation. You should probably [[jump | Jump]].</tw-passagedata><tw-passagedata pid="2" name="Jump" tags="" position="225,100" size="100,100">You prep your ship to jump.
&lt;&lt;getStoryletLinks&gt;&gt;</tw-passagedata><tw-passagedata pid="3" name="Orbit" tags="" position="350,100" size="100,100">&lt;&lt;set $currentLocation = $currentStorylet.planet&gt;&gt;
}</script><tw-passagedata pid="1" name="Start" tags="" position="100,100" size="100,100">You find yourself in $currentLocation. You should probably [[jump | Jump]].</tw-passagedata><tw-passagedata pid="2" name="Jump" tags="" position="225,100" size="100,100">You prep your ship to jump. You can choose a destination:&lt;br&gt;
&lt;&lt;getStoryletLinks&gt;&gt;
Or you can &lt;&lt;linkToNextStorylet &quot;jump blind&quot;&gt;&gt;.</tw-passagedata><tw-passagedata pid="3" name="Orbit" tags="" position="350,100" size="100,100">&lt;&lt;set $currentLocation = $currentStorylet.planet&gt;&gt;
You orbit around $currentLocation.
[[Explore the surface]] or [[Jump]] somewhere else.</tw-passagedata><tw-passagedata pid="4" name="Explore the surface" tags="" position="475,100" size="100,100">You take your shuttle down to the surface of $currentLocation.