npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rpg-dialogue

v1.0.5

Published

An es6 class for handling dialogue trees, useful for RPG games

Downloads

3

Readme

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:

import RPGDialogue from 'rpg-dialogue';
import template from 'demo.pug';

let dialogue = new RPGDialogue(template());
dialogue.interact(id);                     //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 = new RPGDialogue(template);

.interact(id, [conditions, actions])

Interact with the dialogue tree

let response = dialogue.interact(0);

This will return a JSON object containing a response text and subsequent dialogue options

{
  "text": "Hello world",
  "responses": [
    {
      "id": 1,
      "text": "A good day to you too, sir!"
    },
    {
      "id": 2,
      "text": "Bye."
    }
  ]
}

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
import helloWorld from 'hello-world.pug';
import RPGDialogue from 'rpg-dialogue';

let dialogue = new RPGDialogue(helloWorld());
dialogue.interact(0);

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 = {
  IS_ALIVE: () => player.hp > 0,
  IS_PLAYER: true
}

let response = dialogue.interact(0, CONDITIONS);

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 = {
  KILL_PLAYER: () => player.kill()
}

let response = dialogue.interact(0, null, ACTIONS);

Any action that is not a valid function will be ignored