Die API-Referenz stammt aus den Go-Doc-Kommentaren im Quellcode und ist auf Englisch verfasst.
package cues
import "github.com/nsgo-dev/game/pkg/cues"Übersicht
Package cues is experimental: its API and documented formats may change in a minor release. See the API stability policy. 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
Konstanten
const CurrentVersion = 1CurrentVersion is the cue mapping file version this package reads and writes.
Funktionen
func Encode(set *Set) ([]byte, error)Encode validates set and writes it as deterministic two-space YAML, cues in set order. Parse reads the result back to an equal Set.
func Validate(set *Set) errorValidate checks cue tags, uniqueness, handler fields and limits.
Typen
type Active struct{ Tag, Target string }Active identifies a while-active cue present in a gameplay state.
type Cue struct {
Tag string `yaml:"tag"`
Once bool `yaml:"once,omitempty"`
WhileActive bool `yaml:"whileActive,omitempty"`
Particles *Particles `yaml:"particles,omitempty"`
Sound *Sound `yaml:"sound,omitempty"`
Shake *Shake `yaml:"shake,omitempty"`
Flash *Flash `yaml:"flash,omitempty"`
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 time.DurationDuration is a Go duration string in YAML, such as 250ms or 1.5s.
func (d Duration) MarshalYAML() (any, error)MarshalYAML writes d as a Go duration string.
func (d *Duration) UnmarshalYAML(n *yaml.Node) errorUnmarshalYAML reads a non-negative Go duration string.
type Event struct {
Tag, Target, Instance string
Kind EventKind
Step uint64
}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 EventKind uint8EventKind identifies a cue lifecycle event.
const (
// EventExecuted fires a one-shot cue, one whose Once is set.
EventExecuted EventKind = iota + 1
// EventAdded starts a while-active cue, one whose WhileActive is set, on a target.
EventAdded
// EventRemoved stops a while-active cue on a target.
EventRemoved
)type Flash struct {
Color string `yaml:"color"`
Duration Duration `yaml:"duration"`
}Flash tints the target sprite with Color, a name or #rrggbb string the game resolves, for Duration.
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 struct {
X float64 `yaml:"x,omitempty"`
Y float64 `yaml:"y,omitempty"`
Z float64 `yaml:"z,omitempty"`
}Offset is a position relative to the particle attach point.
type Particles struct {
File string `yaml:"file"`
Attach string `yaml:"attach,omitempty"`
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 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 NewPlayer(set *Set, hooks Hooks) (*Player, error)NewPlayer returns a player for set. Hooks may be zero-valued.
func (p *Player) Dispatch(e Event)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.
func (p *Player) Forget(before uint64)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 (p *Player) Reconcile(states []Active, step uint64)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.
func (p *Player) Reset(step uint64)Reset stops every active cue, ordered by tag and then target, and clears the one-shot records.
type Set struct {
Version int `yaml:"version"`
Cues []Cue `yaml:"cues"`
}Set is a versioned cue mapping file.
func Parse(data []byte) (*Set, error)Parse decodes and validates one cue mapping file. Unknown fields, duplicate keys and trailing YAML documents are refused.
type Shake struct {
Amplitude float64 `yaml:"amplitude"`
Duration Duration `yaml:"duration"`
}Shake is a camera shake of Amplitude world units over Duration.
type Sound struct {
File string `yaml:"file"`
Loop bool `yaml:"loop,omitempty"`
}Sound names a sound file to play; Loop keeps it playing while the cue is active.