API 레퍼런스는 소스 코드의 Go 문서 주석에서 추출되며 영어로 작성되어 있습니다.
package vn
import "github.com/nsgo-dev/game/pkg/vn"개요
Package vn presents dialogue scripts as a visual novel: character portraits with expressions standing in stage slots, a text box with name plates and a typewriter, choices, a backlog, auto and skip modes, and voice lines.
A Presenter plays a flow.DialogueScript (Yarn Spinner) with a Cast read from a YAML cast file. Lines steer the stage with tags (#expr:smile, #at:left, #voice:aiko_001, #offstage) and scripts with the commands <<show>>, <<hide>> and <<expr>>. Update moves it on by a game.Loop's fixed step and Draw draws it into a Canvas, so a scene plays alike every time and under nsgotest.
색인
- 상수
- func EncodeCast(c *Cast) ([]byte, error)
- type BacklogEntry
- type Cast
- func ParseCast(data []byte) (*Cast, error)
- func (c *Cast) Character(id string) (*Character, bool)
- func (c *Cast) CheckFrames(atlas *sprite.Atlas) error
- func (c *Cast) EffectiveSlots() []Slot
- func (c *Cast) Slot(name string) (Slot, bool)
- func (c *Cast) Speaker(speaker string) (*Character, bool)
- func (c *Cast) Validate() error
- type Character
- type EffectVoices
- type Expression
- type Layout
- type Line
- type Options
- type Portrait
- type Presenter
- func New(script *flow.DialogueScript, cast *Cast, opts Options) *Presenter
- func (p *Presenter) Advance() error
- func (p *Presenter) Auto() bool
- func (p *Presenter) Backlog() []BacklogEntry
- func (p *Presenter) Cast() *Cast
- func (p *Presenter) ChoiceAt(size core.Size, st Style, pt core.Point) (int, bool)
- func (p *Presenter) Choices() []flow.DialogueChoice
- func (p *Presenter) Choose(index int) error
- func (p *Presenter) Draw(d *core.Drawing, size core.Size, st Style)
- func (p *Presenter) DrawBacklog(d *core.Drawing, size core.Size, st Style, scroll int) int
- func (p *Presenter) Ended() bool
- func (p *Presenter) Line() (Line, bool)
- func (p *Presenter) MarkRead(keys ...string)
- func (p *Presenter) Portraits() []Portrait
- func (p *Presenter) ReadKeys() []string
- func (p *Presenter) ReplayVoice(i int) bool
- func (p *Presenter) Runner() *flow.DialogueRunner
- func (p *Presenter) SetAuto(on bool)
- func (p *Presenter) SetSkip(on bool)
- func (p *Presenter) Skipping() bool
- func (p *Presenter) Start(node string) error
- func (p *Presenter) Typewriter() *text.Typewriter
- func (p *Presenter) Update(dt time.Duration) error
- type Slot
- type Style
- type VoiceClip
- type VoicePlayer
상수
const (
// DefaultCharsPerSecond is how fast a line's text appears when Options.CharsPerSecond is 0.
DefaultCharsPerSecond = 40
// DefaultAutoDelay is how long auto mode waits after a line's text and voice end when Options.AutoDelay is 0.
DefaultAutoDelay = 1500 * time.Millisecond
// DefaultBacklogSize is how many entries the backlog keeps when Options.BacklogSize is 0.
DefaultBacklogSize = 200
)const (
// TagExpression sets the speaker's expression: #expr:smile.
TagExpression = "expr:"
// TagSlot moves the speaker's portrait to a slot: #at:left.
TagSlot = "at:"
// TagVoice names the line's voice line: #voice:aiko_001.
TagVoice = "voice:"
// TagOffstage speaks the line without bringing the speaker's portrait on the stage: #offstage.
TagOffstage = "offstage"
)Line tags a Presenter reads off a dialogue line, written after the line in the Yarn script, such as "Aiko: Hello! #expr:smile #at:left #voice:aiko_001".
const (
// CommandShow shows a character's portrait: <<show aiko>>, <<show aiko left>>, <<show aiko smile>> or <<show aiko left smile>>, the slot and expression in either order.
CommandShow = "show"
// CommandHide hides a character's portrait, <<hide aiko>>, or every portrait, <<hide>>.
CommandHide = "hide"
// CommandExpression changes a character's expression, showing the portrait if it is hidden: <<expr aiko smile>>.
CommandExpression = "expr"
)Commands a Presenter runs itself rather than passing to DialogueOptions.OnCommand. Each still reaches DialogueOptions.Bus.
함수
func EncodeCast(c *Cast) ([]byte, error)EncodeCast validates c and writes it as deterministic 2-space YAML, fields in schema order and lists in their order, so that ParseCast reads back the same cast.
타입
type BacklogEntry struct {
// Character is the speaking cast member's ID, empty for none.
Character string
// Name is the name plate's text, empty for narration and for choices.
Name string
// Color is the cast member's name plate color, empty for the style's.
Color string
// Text is the line's text with its rich text markup.
Text string
// LineID is the line's #line: id, empty for none.
LineID string
// Voice is the line's voice line id, empty for none.
Voice string
// Choice reports that the entry is an option the player chose rather than a line.
Choice bool
}BacklogEntry is one line or chosen option in the backlog.
type Cast struct {
// Version is the schema version; it must equal CastVersion (1).
Version int `yaml:"version"`
// Slots are the named places on the stage a portrait stands in, in document order. Empty uses DefaultSlots.
Slots []Slot `yaml:"slots,omitempty"`
// Characters are the cast members, in document order.
Characters []Character `yaml:"characters"`
}Cast is a visual novel's cast file: the stage slots portraits stand in and the characters, each with a name plate and the portraits of its expressions. It is the YAML document ParseCast reads and EncodeCast writes, such as cast.yaml.
func ParseCast(data []byte) (*Cast, error)ParseCast decodes a YAML cast file and validates it. Unknown fields and trailing YAML documents are refused.
func (c *Cast) Character(id string) (*Character, bool)Character returns the character whose ID is id, and whether there is one.
func (c *Cast) CheckFrames(atlas *sprite.Atlas) errorCheckFrames reports the first expression whose frame atlas lacks, so a game finds a missing portrait as it loads rather than as a scene shows it.
func (c *Cast) EffectiveSlots() []SlotEffectiveSlots returns the cast's slots, or DefaultSlots when it declares none.
func (c *Cast) Slot(name string) (Slot, bool)Slot returns the slot named name and whether there is one.
func (c *Cast) Speaker(speaker string) (*Character, bool)Speaker returns the character a line's speaker names: the one whose ID is speaker, or else the first whose Name is.
func (c *Cast) Validate() errorValidate checks c against the version 1 schema: unique, non-empty slot names with X from 0 to 1; unique character IDs without whitespace; parseable colors; slots and default expressions that exist; and at least one expression per character, each with a frame and a unique name that no slot has, so that <<show>> reads its arguments one way.
type Character struct {
// ID identifies the character in commands and is matched against a line's speaker before Name. It holds no whitespace.
ID string `yaml:"id"`
// Name is the text of the character's name plate and is also matched against a line's speaker; empty uses ID.
Name string `yaml:"name,omitempty"`
// Color is the name plate's color as a CSS color string (core.ParseColor); empty uses the style's NamePlateColor.
Color string `yaml:"color,omitempty"`
// Slot is the slot the character's portrait enters when a line or command names none; empty uses the cast's middle slot.
Slot string `yaml:"slot,omitempty"`
// DefaultExpression is the expression the portrait enters with; empty uses the first expression.
DefaultExpression string `yaml:"defaultExpression,omitempty"`
// Expressions are the character's portraits, each a frame of the portrait atlas.
Expressions []Expression `yaml:"expressions"`
}Character is one cast member.
func (ch *Character) DisplayName() stringDisplayName returns the text of the character's name plate: Name, or ID when Name is empty.
func (ch *Character) EffectiveExpression() stringEffectiveExpression returns the expression the character's portrait enters with: DefaultExpression, or the first expression when it is empty.
func (ch *Character) Expression(name string) (Expression, bool)Expression returns the expression named name, and whether there is one.
type EffectVoices struct {
// Clips are the voice lines by id.
Clips map[string]VoiceClip
// contains filtered or unexported fields
}EffectVoices is a VoicePlayer of mixer effects by voice id. It plays one line at a time, stopping the line before it.
func (v *EffectVoices) PlayVoice(id string) (time.Duration, bool)PlayVoice stops the line playing and starts the clip id, returning its Length; ok is false when Clips has no such clip or its effect played nothing.
func (v *EffectVoices) StopVoice()StopVoice stops the line playing, if any.
type Expression struct {
// Name identifies the expression in line tags (#expr:smile) and commands (<<expr aiko smile>>).
Name string `yaml:"name"`
// Frame names the portrait's frame in the atlas a Style draws portraits from.
Frame string `yaml:"frame"`
}Expression is one portrait of a character.
type Layout struct {
// Box is the text box.
Box core.Bounds
// Text is the area of the box the line's text wraps in.
Text core.Bounds
// Choices are the choice buttons, one for each option in the order Choices returns them.
Choices []core.Bounds
}Layout is where Draw puts a presenter's text box and choices on a stage, for a game to hit test taps against.
type Line struct {
// DialogueLine is the line as the runner produced it, tags included.
flow.DialogueLine
// Character is the speaking cast member's ID, empty for a speaker not in the cast and for narration.
Character string
// Name is the name plate's text: the cast member's display name, the speaker as the script writes it for a speaker not in the cast, or empty for narration.
Name string
// Color is the cast member's name plate color, empty for the style's.
Color string
// Voice is the line's voice line id, empty for none.
Voice string
// Read reports whether the player had read the line before it was shown this time.
Read bool
}Line is the dialogue line a Presenter shows.
type Options struct {
// Dialogue configures the presenter's flow.DialogueRunner. Its OnCommand receives every command but CommandShow, CommandHide and CommandExpression.
Dialogue flow.DialogueOptions
// Voices plays voice lines; nil plays none.
Voices VoicePlayer
// VoiceFromLineID plays a line's #line: id as its voice line when it has no #voice: tag.
VoiceFromLineID bool
// CharsPerSecond is how fast a line's text appears; 0 uses DefaultCharsPerSecond and a negative rate shows the whole line at once.
CharsPerSecond float32
// AutoDelay is how long auto mode waits after a line's text has appeared and its voice ended before it goes on; 0 uses DefaultAutoDelay and a negative delay waits none.
AutoDelay time.Duration
// AutoDelayPerRune adds this much to AutoDelay for each character of the line, so a long line stays longer.
AutoDelayPerRune time.Duration
// SkipInterval is how long skip mode shows each line; 0 shows one line per Update.
SkipInterval time.Duration
// SkipUnread lets skip mode go past lines not read before; when false, skip mode stops on the first line the player has not read.
SkipUnread bool
// BacklogSize is how many entries the backlog keeps, dropping the oldest; 0 uses DefaultBacklogSize and a negative size keeps none.
BacklogSize int
}Options configures a Presenter.
type Portrait struct {
// Character is the character's ID.
Character string
// Expression is the expression shown.
Expression string
// Frame is the expression's atlas frame.
Frame string
// Slot is the slot the portrait stands in.
Slot string
// X is the slot's X, the portrait's center as a fraction of the stage's width.
X float32
// Speaking reports whether the character speaks the line shown.
Speaking bool
}Portrait is a character's portrait on the stage.
type Presenter struct {
// contains filtered or unexported fields
}Presenter plays a dialogue script as a visual novel: it runs the script with a flow.DialogueRunner and keeps the portraits on the stage, the line in the text box with its name plate, the typewriter revealing it, the choices, the backlog and the lines read, and plays voice lines. Auto mode goes on by itself after each line, and skip mode runs through lines quickly.
It reads no clock: Update moves it on by the fixed step a game.Loop passes, so a scene plays alike every time and under nsgotest. Its methods are called on one goroutine.
func New(script *flow.DialogueScript, cast *Cast, opts Options) *PresenterNew returns a presenter that plays script with cast. Call Start to begin a node.
func (p *Presenter) Advance() errorAdvance is the player's tap or key to go on: it stops skip mode, shows the rest of a line still appearing, or goes on to the next line. It fails with flow.ErrChoiceRequired while choices wait and does nothing once the script has ended.
func (p *Presenter) Backlog() []BacklogEntryBacklog returns the lines shown and options chosen, oldest first.
func (p *Presenter) ChoiceAt(size core.Size, st Style, pt core.Point) (int, bool)ChoiceAt returns the Index of the choice button at pt on a stage of size, for Choose, and whether pt is on an available one.
func (p *Presenter) Choices() []flow.DialogueChoiceChoices returns the options waiting for Choose, nil when none wait.
func (p *Presenter) Choose(index int) errorChoose picks the option whose Index is index, adds it to the backlog and goes on.
func (p *Presenter) Draw(d *core.Drawing, size core.Size, st Style)Draw draws the presenter on a stage of size: the portraits, dimming those not speaking; the text box with the line's name plate and the text the typewriter has revealed; and the choice buttons while choices wait.
func (p *Presenter) DrawBacklog(d *core.Drawing, size core.Size, st Style, scroll int) intDrawBacklog draws the backlog over a stage of size: each entry's name in its plate color above its text, the newest at the bottom, skipping the newest scroll entries so a game can scroll back through it. An entry taller than the stage is drawn alone from the top, cut off at the bottom, so every entry can be read. It returns how many entries it drew.
func (p *Presenter) Ended() boolEnded reports whether the script has ended or not started.
func (p *Presenter) Line() (Line, bool)Line returns the line shown and whether there is one. The line stays shown while its choices wait.
func (p *Presenter) MarkRead(keys ...string)MarkRead marks the lines with keys, from ReadKeys, as read.
func (p *Presenter) Portraits() []PortraitPortraits returns the portraits on the stage in the order they entered, which is the order to draw them in.
func (p *Presenter) ReadKeys() []stringReadKeys returns the keys of the lines the player has read, sorted, for a save to keep and MarkRead to restore. A line's key is its #line: id, or for an untagged line its node, speaker and text as shown, after string table lookup and {$var} interpolation; give lines #line: ids so that skip mode knows them across languages and changing variables.
func (p *Presenter) ReplayVoice(i int) boolReplayVoice plays the voice line of backlog entry i again and reports whether it played.
func (p *Presenter) Runner() *flow.DialogueRunnerRunner returns the presenter's dialogue runner, for its variables and save state. Advance it through the presenter, not directly.
func (p *Presenter) SetAuto(on bool)SetAuto turns auto mode on or off. In auto mode the presenter goes on by itself once a line's text has appeared, its voice ended and Options.AutoDelay passed; it waits at choices.
func (p *Presenter) SetSkip(on bool)SetSkip turns skip mode on or off. In skip mode each Update shows the next line at once, without its voice, one every Options.SkipInterval; it stops at choices, at the end, on Advance, and, unless Options.SkipUnread, on a line the player had not read before, which shows as usual, so skip mode never passes a line the player has not seen.
func (p *Presenter) Skipping() boolSkipping reports whether skip mode is on.
func (p *Presenter) Start(node string) errorStart clears the stage and the line shown and begins the script at node. The backlog and the lines read stay. A node the script lacks fails with flow.ErrUnknownNode and leaves the presenter as it was.
func (p *Presenter) Typewriter() *text.TypewriterTypewriter returns the typewriter revealing the line shown, nil for none. Draw it, or read how much of the line has appeared.
func (p *Presenter) Update(dt time.Duration) errorUpdate moves the presenter on by dt: it reveals the line's text and keeps its rich text effects animating, counts down its voice, and in auto mode goes on once the line has stayed its delay, or in skip mode goes on to the next line. An error is the script's or a stage command's.
type Slot struct {
// Name identifies the slot in line tags (#at:left) and commands (<<show aiko left>>).
Name string `yaml:"name"`
// X is where the portrait's center stands, as a fraction of the stage's width from its left edge, from 0 to 1.
X float32 `yaml:"x"`
}Slot is a named place on the stage where a portrait stands.
func DefaultSlots() []SlotDefaultSlots returns the slots a cast without any uses: left at 0.2, center at 0.5 and right at 0.8 of the stage's width.
type Style struct {
// Atlas holds the portraits' frames, which Cast.CheckFrames checks; nil draws no portraits.
Atlas *sprite.Atlas
// PortraitHeight scales each portrait to this height in points, keeping its aspect; 0 draws frames at their own size.
PortraitHeight float32
// PortraitBottom is how far above the stage's bottom edge the portraits stand.
PortraitBottom float32
// InactiveAlpha is the opacity of portraits whose character does not speak the line shown, while one on the stage does; 0 draws them opaque.
InactiveAlpha float32
// Box draws the text box; nil fills a rounded rectangle in BoxColor.
Box *sprite.NineSlice
// BoxColor fills the text box when Box is nil; 0 uses a translucent dark blue.
BoxColor core.Color
// BoxHeight is the text box's height; 0 uses 28% of the stage's height.
BoxHeight float32
// Margin is the space between the text box and the stage's edges; 0 uses 16.
Margin float32
// Padding is the space between the text box's edges and its text; 0 uses 16.
Padding float32
// Font draws the line's text as a bitmap font; nil draws platform text in TextFont.
Font *text.Font
// TextFont is the line's platform font; a zero size uses 20.
TextFont core.Font
// TextColor is the line's color; 0 uses white.
TextColor core.Color
// NameFont is the name plate's platform font; a zero size uses 18, bold.
NameFont core.Font
// NameColor is the name plate's text color; 0 uses white.
NameColor core.Color
// NamePlateColor fills the name plate of a speaker without a cast color; 0 uses a slate blue.
NamePlateColor core.Color
// ChoiceWidth is each choice button's width; 0 uses 60% of the stage's width.
ChoiceWidth float32
// ChoiceHeight is each choice button's height; 0 uses 44.
ChoiceHeight float32
// ChoiceGap is the space between choice buttons; 0 uses 12.
ChoiceGap float32
// ChoiceColor fills the choice buttons; 0 uses the box color.
ChoiceColor core.Color
// BacklogColor fills the backlog behind its entries; 0 uses a near-opaque dark blue.
BacklogColor core.Color
}Style sets how Draw and DrawBacklog draw a presenter. Its zero value draws portraits at their frames' size and a translucent dark text box with platform text; a zero field uses the default its comment gives.
func (st Style) Layout(size core.Size, n int) LayoutLayout returns where Draw puts the text box and n choice buttons on a stage of size.
type VoiceClip struct {
// Effect plays the line, in the mixer's voice group, such as mixer.Effect(snd, "voice", 1).
Effect *sound.Effect
// Length is how long the line plays, such as the loaded audio.Sound's Duration; auto mode waits this long before it counts its delay.
Length time.Duration
}VoiceClip is one voice line played through a sound.Mixer.
type VoicePlayer interface {
// PlayVoice starts the voice line id and returns how long it plays; ok is false when there is no such line or nothing played, such as while the voice volume is 0.
PlayVoice(id string) (length time.Duration, ok bool)
// StopVoice stops the voice line playing, if any.
StopVoice()
}VoicePlayer plays a scene's voice lines. A Presenter calls it on the engine's goroutine, as audio's methods are.