Documentation
¶
Overview ¶
Package cues is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.
Package cues maps named gameplay cue events to presentation handlers.
The package contains only the deterministic data and event boundary. A game supplies Hooks to connect handlers to particles, sound, camera and sprites. This keeps cue data usable by headless simulations and dedicated servers.
Index ¶
Examples ¶
Constants ¶
const CurrentVersion = 1
CurrentVersion is the cue mapping file version this package reads and writes.
const MaxCues = 4096
MaxCues bounds the cues in one set.
const SchemaURL = "https://nsgo.games/schemas/cues.v1.json"
SchemaURL is the published JSON Schema URL for version 1 cue mapping files.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Active ¶
type Active struct{ Tag, Target string }
Active identifies a while-active cue present in a gameplay state.
type Cue ¶
type Cue struct {
// Tag is the unique dotted gameplay tag for this cue, such as cue.hit.fire or cue.status.burning.
Tag string `yaml:"tag"`
// Once enables one-shot playback when EventExecuted is dispatched.
Once bool `yaml:"once,omitempty"`
// WhileActive enables continuous playback between EventAdded and EventRemoved.
WhileActive bool `yaml:"whileActive,omitempty"`
// Particles spawns a particle emitter or effect file on the target.
Particles *Particles `yaml:"particles,omitempty"`
// Sound plays a one-shot or looping sound file.
Sound *Sound `yaml:"sound,omitempty"`
// Shake triggers a camera shake.
Shake *Shake `yaml:"shake,omitempty"`
// Flash briefly tints the target sprite.
Flash *Flash `yaml:"flash,omitempty"`
// Animation names an animation state to enter on the target.
Animation string `yaml:"animation,omitempty"`
}
Cue maps one gameplay tag to presentation handlers. Once makes the cue react to EventExecuted (a one-shot such as a hit spark), WhileActive makes it react to EventAdded and EventRemoved (a loop such as burning); a cue may set both.
type Duration ¶
Duration is a Go duration string in YAML, such as 250ms or 1.5s.
func (Duration) MarshalYAML ¶
MarshalYAML writes d as a Go duration string.
type Event ¶
Event is a deterministic cue notification. Target identifies the cue's owner, and Instance distinguishes repeated executions of the same tag on one target in one step.
type Flash ¶
type Flash struct {
// Color is the tint color name or hex string such as #ff8800.
Color string `yaml:"color"`
// Duration is how long the tint lasts as a Go duration string such as 50ms.
Duration Duration `yaml:"duration"`
}
Flash tints the target sprite with Color, a name or #rrggbb string the game resolves, for Duration.
type Hooks ¶
type Hooks struct {
Particles func(Particles, Event)
Sound func(Sound, Event)
Shake func(Shake, Event)
Flash func(Flash, Event)
Animation func(string, Event)
// Stop is called once when an active cue is removed, and receives the removal event.
Stop func(Cue, Event)
}
Hooks connects cue data to client presentation. Nil hooks are skipped.
type Offset ¶
type Offset struct {
// X is the horizontal offset.
X float64 `yaml:"x,omitempty"`
// Y is the vertical offset.
Y float64 `yaml:"y,omitempty"`
// Z is the depth offset.
Z float64 `yaml:"z,omitempty"`
}
Offset is a position relative to the particle attach point.
type Particles ¶
type Particles struct {
// File is the project-relative path to a particle emitter (.particles.yaml) or effect (.effect.yaml) file.
File string `yaml:"file"`
// Attach names a target attach point or socket, such as chest or weapon.
Attach string `yaml:"attach,omitempty"`
// Offset shifts the emitter from the attach point in world units.
Offset *Offset `yaml:"offset,omitempty"`
}
Particles names a particle effect file to spawn, optionally on an attach point of the target and at an offset from it.
type Player ¶
type Player struct {
// contains filtered or unexported fields
}
Player dispatches cues on a client. It has no graphics or audio dependency, so a headless server can keep cue data loaded without constructing one.
func (*Player) Active ¶
Active returns the currently active while-active cues ordered by tag and then target.
func (*Player) Dispatch ¶
Dispatch delivers one event. An EventExecuted starts a Once cue's handlers unless the same tag, target, instance and step already fired, as when rollback re-simulation replays it. An EventAdded starts a WhileActive cue's handlers unless the cue is already active on the target, and an EventRemoved stops an active cue. Events for unknown tags, and events a cue does not react to, are ignored.
Example ¶
A Player fires a one-shot cue once per tag, target, instance and step, so a replayed event does nothing, and starts a while-active cue only once until it is removed.
package main
import (
"fmt"
"time"
"github.com/nsgo-dev/game/pkg/cues"
)
// combat maps a one-shot hit cue and a looping burning cue to their presentation handlers.
const combat = `version: 1
cues:
- tag: cue.hit.fire
once: true
sound:
file: sfx/hit.ogg
shake:
amplitude: 0.5
duration: 100ms
- tag: cue.status.burning
whileActive: true
particles:
file: fx/burning.yaml
animation: burning
`
// printHooks prints each handler call instead of spawning particles or playing sounds.
func printHooks() cues.Hooks {
return cues.Hooks{
Particles: func(p cues.Particles, e cues.Event) { fmt.Println("particles", p.File, e.Target) },
Sound: func(s cues.Sound, e cues.Event) { fmt.Println("sound", s.File, e.Target) },
Shake: func(s cues.Shake, e cues.Event) { fmt.Println("shake", s.Amplitude, time.Duration(s.Duration)) },
Animation: func(a string, e cues.Event) { fmt.Println("animation", a, e.Target) },
Stop: func(c cues.Cue, e cues.Event) { fmt.Println("stop", c.Tag, e.Target) },
}
}
func main() {
set, _ := cues.Parse([]byte(combat))
p, err := cues.NewPlayer(set, printHooks())
if err != nil {
fmt.Println(err)
return
}
hit := cues.Event{Tag: "cue.hit.fire", Target: "hero", Instance: "cast-1", Kind: cues.EventExecuted, Step: 1}
p.Dispatch(hit)
p.Dispatch(hit)
burning := cues.Event{Tag: "cue.status.burning", Target: "hero", Kind: cues.EventAdded, Step: 2}
p.Dispatch(burning)
p.Dispatch(burning)
p.Dispatch(cues.Event{Tag: "cue.status.burning", Target: "hero", Kind: cues.EventRemoved, Step: 3})
}
Output: sound sfx/hit.ogg hero shake 0.5 100ms particles fx/burning.yaml hero animation burning hero stop cue.status.burning hero
func (*Player) DispatchAll ¶
DispatchAll delivers each event in events in order.
func (*Player) Forget ¶
Forget drops the one-shot records of steps before step. Call it with the oldest step a rollback can still return to, such as the last confirmed step, so the records do not grow for the whole session; a one-shot replayed from a forgotten step would fire again.
func (*Player) IsActive ¶
IsActive reports whether the while-active cue tag is currently active on target.
func (*Player) Reconcile ¶
Reconcile makes the active while-active cues match states, the cues active in the gameplay state at step. Call it after a rollback, once re-simulation has reached the present step, with that step's state: cues missing from states stop first, ordered by tag and then target, and newly present cues start in input order, so a cue active both before and after the rollback keeps playing without a restart.
Example ¶
After a rollback, Reconcile stops the cues the present state no longer holds and starts the new ones, leaving the rest playing.
package main
import (
"fmt"
"time"
"github.com/nsgo-dev/game/pkg/cues"
)
// combat maps a one-shot hit cue and a looping burning cue to their presentation handlers.
const combat = `version: 1
cues:
- tag: cue.hit.fire
once: true
sound:
file: sfx/hit.ogg
shake:
amplitude: 0.5
duration: 100ms
- tag: cue.status.burning
whileActive: true
particles:
file: fx/burning.yaml
animation: burning
`
// printHooks prints each handler call instead of spawning particles or playing sounds.
func printHooks() cues.Hooks {
return cues.Hooks{
Particles: func(p cues.Particles, e cues.Event) { fmt.Println("particles", p.File, e.Target) },
Sound: func(s cues.Sound, e cues.Event) { fmt.Println("sound", s.File, e.Target) },
Shake: func(s cues.Shake, e cues.Event) { fmt.Println("shake", s.Amplitude, time.Duration(s.Duration)) },
Animation: func(a string, e cues.Event) { fmt.Println("animation", a, e.Target) },
Stop: func(c cues.Cue, e cues.Event) { fmt.Println("stop", c.Tag, e.Target) },
}
}
func main() {
set, _ := cues.Parse([]byte(combat))
p, _ := cues.NewPlayer(set, printHooks())
p.Dispatch(cues.Event{Tag: "cue.status.burning", Target: "hero", Kind: cues.EventAdded, Step: 5})
fmt.Println("rollback")
p.Reconcile([]cues.Active{{Tag: "cue.status.burning", Target: "goblin"}}, 9)
}
Output: particles fx/burning.yaml hero animation burning hero rollback stop cue.status.burning hero particles fx/burning.yaml goblin animation burning goblin
type Set ¶
type Set struct {
// Version is the schema version, always CurrentVersion (1).
Version int `yaml:"version"`
// Cues maps unique gameplay cue tags to their client presentation handlers.
Cues []Cue `yaml:"cues"`
}
Set is a versioned cue mapping file.
func Parse ¶
Parse decodes and validates one cue mapping file. Unknown fields, duplicate keys and trailing YAML documents are refused.
Example ¶
Parse reads a cue set and Encode writes it back in a form Parse reads to the same bytes again.
package main
import (
"bytes"
"fmt"
"github.com/nsgo-dev/game/pkg/cues"
)
// combat maps a one-shot hit cue and a looping burning cue to their presentation handlers.
const combat = `version: 1
cues:
- tag: cue.hit.fire
once: true
sound:
file: sfx/hit.ogg
shake:
amplitude: 0.5
duration: 100ms
- tag: cue.status.burning
whileActive: true
particles:
file: fx/burning.yaml
animation: burning
`
func main() {
set, err := cues.Parse([]byte(combat))
if err != nil {
fmt.Println(err)
return
}
first, _ := cues.Encode(set)
again, _ := cues.Parse(first)
second, _ := cues.Encode(again)
fmt.Println(len(set.Cues), set.Cues[0].Tag, set.Cues[1].WhileActive)
fmt.Println("stable:", bytes.Equal(first, second))
_, err = cues.Parse([]byte("version: 2\ncues: []\n"))
fmt.Println("version 2 refused:", err != nil)
}
Output: 2 cue.hit.fire true stable: true version 2 refused: true
type Shake ¶
type Shake struct {
// Amplitude is the shake intensity in world units (finite and non-negative).
Amplitude float64 `yaml:"amplitude"`
// Duration is how long the shake lasts as a Go duration string such as 100ms.
Duration Duration `yaml:"duration"`
}
Shake is a camera shake of Amplitude world units over Duration.
type Sound ¶
type Sound struct {
// File is the project-relative path to a sound definition (.sound.yaml) or audio clip.
File string `yaml:"file"`
// Loop repeats the sound until the cue stops.
Loop bool `yaml:"loop,omitempty"`
}
Sound names a sound file to play; Loop keeps it playing while the cue is active.