rpg-dialogue
Install
$ npm install --save-dev rpg-dialogue
Description
An es6 class for handling dialogue trees, useful for RPG games.
Features
- Simple interface:
dialogue.interact(id);
- Dialogue tree defined in HTML
- Conditional branches
- Branch actions
Usage
Code:
;; let dialogue = ;dialogue; //interact with a specific dialogue branch
Dialogue tree:
d#0 Hello world d(next=1) A good day to you too, sir and/or madam! d(next=2) Bye. d#1 Thanks! d(next=2) d#2 Bye
API
Instantiate a new dialogue with a template dialogue tree
let dialogue = template;
.interact(id, [conditions, actions])
Interact with the dialogue tree
let response = dialogue;
This will return a JSON
object containing a response text and subsequent dialogue options
You would then have the option to continue with dialogue.interact(1)
or dialogue.interact(2)
.
Templates
Templates are entirely defined as HTML.
To make life easier it is recommended to combine this with your favourite templating language. For the purpose of this documentation we will use Pug but you are free to pick whichever one you prefer, even native html.
d#0 Hello world d(next=1) A good day to you too, sir! d(next=2) Bye. d#1 Thanks! d(next=2) d#2 Bye
;; let dialogue = ;dialogue;
ID
Use an ID to define a point in your dialogue that can be interacted with.
d#0 Hello world
Next
Use the next
attribute to have dialogue options lead to a different point within the dialogue.
d#0 Hello world d(next=1) Thanks d#1 Bye!
Linear text
ID's can be omitted for non-branching dialogue trees
d#0 Stay a while and listen d I have an amazing story to tell you d But time is running short d(next=1) Ok... d#1 Bye!
Conditions
Conditions can be defined to control which branches can be accessed by the dialogue.
This lets your application have control over when a specific dialogue option is available or not.
Conditions can be negated with !
.
d#0 Are you alive? d(next=1, if='IS_ALIVE') Yes! d(next=1, if='!IS_ALIVE') Nope! d(next=2) Maybe... d#1 Cool!d#2 Hmmmm...
Then, in your code:
const CONDITIONS = playerhp > 0 IS_PLAYER: true let response = dialogue;
The conditions must map to a truthy value or a function that returns a truthy value
Actions
Actions can be defined to trigger when a certain branch is reached in the dialogue. This lets the dialogue trigger functions within your application.
d#0 Do you feel lucky? Punk. d(next=1) Yes! d(next=2) Nope... d#1 Okay then. d#2(then='KILL_PLAYER') Wrong answer!
const ACTIONS = player let response = dialogue;
Any action that is not a valid function will be ignored