@a-morphous/frontispiece-ink-processor
v4.2.3
Published
API wrapper around ink to show multiple lines, give ink a JSON visual state that can be saved and loaded, and connect to state.
Downloads
32
Readme
Ink Processor
A generic ink story processor that wraps InkJS
and gives you an api for advancing through the story, saving visual state of lines you've seen before, and parsing lines and producing generic commands.
Why?
Ink's parser is very stateful; at any point, the ink story only lets you see the current line, and holds a lot of variables within the core class that cannot easily be serialized. This means that ink doesn't play nice with a lot of Javascript's immutable, data-first frontend stores, which this library attempts to rectify.
Note that this doesn't handle all of Ink's features, and is framework agnostic. More likely for projects, you'll be using one of the other adapters that take the results of ink-processor
and hooks it up with a state management system... such as ink-hookstate
.
Features
- Creates an engine wrapper that includes separate
visual
state for an Ink story, which can be fed into React or other frontend frameworks to display stories (and properly keeps the correct objects mutable / immutable for performance reasons) - Exposes a Command engine that lets you create your own logic for lines that are marked as commands, by default any line that begins with
$
.
Usage
Creating a Story
import { Story } from 'inkjs'
import storyJSON from 'assets/story.json'
const story = new Story(storyJSON)
const config = {
commandIdentifier: '$' // can be overridden to use a different prefix for identifying commands than $
lineParser: undefined // can be replaced by a function to produce a different set of tokens than the default.
}
const engine: InkProcessorEngine = new InkProcessorEngine(story, config)
// now you can manipulate the story
engine.advance()
engine.hideAllVisible()
engine.makeChoice(1)
// ...and see the raw Ink state
console.log(engine.inkStory)
// or the visual state
console.log(engine.getVisibleLines())
Creating Commands
Your game can create custom commands that will be parsed from statements beginning with $
(or whatever prefix is chosen).
A command is a function that takes in the ink-processor
engine, the parameters parsed from the command string, and optionally the visualState of the current story. It can then do whatever it needs to the story, and returns a CommandReturn
object, which determines whether the story should automatically advance after parsing the command.
For example:
import { Command } from '@a-morphous/frontispiece-ink-processor/src/data/command'
export const Pause: Command = (engine, params: string[] = []) => {
const time = params.length ? parseFloat(params[0]) : 1
if (time > 0) {
setTimeout(() => {
engine.advance()
}, time * 1000)
}
return {
interruptStoryFlow: true,
}
}
After creating a command, you register it to make it operate.
engine.registerCommand('pause', Pause)
// now you can run the following lines in the Ink story:
// $pause // pause for 1 second (default)
// $pause 2 // pause for 2 seconds