ink 是一种用于创建交互式文字叙述小说的脚本语言。它可以使用编辑器 Inky 来进行编写, inkjs 是一个 NPM 模块,用于调用ink引擎。
现在整理ink Story的API如下:
名称 | 类型 | 描述 |
canContinue | Property | 返回boolean故事是否可以继续 |
Continue() | Function | 返回下一条故事文本,和选项 |
currentChoices | Property | 当前故事的可选项(数组) |
ChooseChoiceIndex() | Function | 用户选择后执行,用于改变故事走向 |
currentTags | Property. | 当前文本的标签(数组) |
globalTags | Property | 全局标签(数组) |
使用:
一、创建一个json故事文件
使用 inky 编辑器编写故事[如何编写?] ,然后保存,导出json文件
二、安装 inkjs
npm install inkjs
三、使用流程
import storyContent from "./storys/some_story.json"
import {Story} from "inkjs"
let story = new Story(storyContent)
// 获取故事全局标签
let globalTags = story.globalTags
console.log(globalTags)
while(story.canContinue) {
// 故事段落
let paragraphText = story.Continue()
console.log(paragraphText)
// 段落标签
let tags = story.currentTags
console.log(tags)
// 当前段落可选项
if(Array.isArray(story.currentChoices)){
story.currentChoices.forEach(function(e){
console.log(e.index + e.text)
})
}
// 触发选项,index为当前段落的选项键
let index = story.currentChoices[0].index
story.ChooseChoiceIndex(index)
}
// 重启故事
story.ResetState()
注:
如何编写故事?这里有网友热心翻译版(语雀):https://www.yuque.com/inky/pcke0w/gix3ga