conversation

Talking to someone, as data and a clock. A script is a set of nodes: each says some lines and may offer the player replies. A reply can depend on what the player carries or has done, and can do things: give or take an item, open a trade, set a flag, go to another node, end the talk. The playback machine plays the lines in time, offers the replies before the last line has quite finished, and lets a pick cut a line short, which is what keeps a conversation from being a wait, then a read, then a pick (see CONVERSATION-DESIGN.md).

import { startConversation } from 'tosijs-3d'

const talk = startConversation(script, {
  has: (kind, qty) => countOf(kind) >= qty,
  give: (kind, qty) => putInBag(kind, qty), // null, or why not
  take: (kind, qty) => takeFromBag(kind, qty),
  trade: (done) => openShop(done),
  handleEvent: (e) => stage(e), // lines, replies, cuts, effects, the end
})
// each frame, in scene time:
talk.tick(dt)
// when the player picks:
talk.pick('potion')

A script

const script = {
  start: 'hello',
  nodes: {
    hello: {
      lines: [{ speaker: 'shopkeeper', text: 'Back again?', gesture: 'wave' }],
      replies: [
        { id: 'shop', label: 'What do you sell?', then: [{ trade: true }, { goto: 'after' }] },
        {
          id: 'potion', label: 'Your potion.', line: "Here's that potion I promised you.",
          when: { has: 'Potion' },
          then: [{ take: 'Potion' }, { give: 'gold', qty: 20 }, { set: 'potion-delivered' }, { goto: 'thanks' }],
        },
        { id: 'bye', label: 'Bye.', then: [{ end: true }] },
      ],
    },
    thanks: { lines: [{ speaker: 'shopkeeper', text: 'You remembered!' }], then: [{ goto: 'hello' }] },
    after: { lines: [{ speaker: 'shopkeeper', text: 'Anything else?' }], then: [{ goto: 'hello' }] },
  },
}

The replies appear once the last line is "somewhat complete": at 75% of it, or 0.7 s before its end if that is later. Every line said goes in transcript. Pure: no DOM, no Babylon, no clock but tick(dt).