cuefx

package
v0.4.0-rc.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cuefx is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.

Package cuefx runs client-side presentation handlers for pkg/cues: subscribing to ability.Caster and stats.Sheet events, pooling particles.Emitter instances, tracking active looping sounds, camera shakes, target sprite flashes and animation states, and reconciling active cues across rollback without restarting loops.

Keep cuefx out of headless server and simulation packages; dedicated servers import only pkg/cues (or neither) so no particle, sprite or audio code is linked.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseColor

func ParseColor(s string) (core.Color, bool)

ParseColor parses a cue flash color string: #rgb, #rrggbb, #rrggbbaa, or a common color name.

Types

type Config

type Config struct {
	// FS loads particle emitter (.particles.yaml) and particle effect (.effect.yaml) files referenced by cue particles.file entries.
	FS fs.FS
	// Atlas is bound to pooled particle emitters so particles.Emitter.Draw renders their frames.
	Atlas *sprite.Atlas
	// LoadParticles overrides FS loading for particles.file entries when non-nil.
	LoadParticles func(file string) ([]ParticleSpec, error)
	// PlaySound plays a cue's sound and optionally returns a stop callback that Runner calls once when a whileActive cue stops.
	PlaySound func(s cues.Sound, ev cues.Event) (stop func())
	// ResolveAttach maps a target ID and attach point name to 2D world coordinates. Called on spawn and on every Update so active emitters follow moving targets.
	ResolveAttach func(target, attach string) (x, y float32, ok bool)
	// OnShake is called whenever a shake cue fires, in addition to Runner's built-in deterministic ShakeOffset state.
	OnShake func(s cues.Shake, ev cues.Event)
	// OnFlash is called whenever a flash cue fires with the parsed color, in addition to Runner's built-in FlashState tracker.
	OnFlash func(f cues.Flash, color core.Color, ev cues.Event)
	// OnAnimation is called when an animation cue starts.
	OnAnimation func(state string, ev cues.Event)
	// OnAnimationStop is called once when a whileActive cue with an animation stops.
	OnAnimationStop func(state string, ev cues.Event)
	// OnError receives asset-loading errors (such as a missing particle file).
	OnError func(ev cues.Event, err error)
	// MaxPooledEmitters caps idle emitters retained in the pool (default 64 when <= 0).
	MaxPooledEmitters int
}

Config configures a client-side cue Runner. Every field is optional.

type ParticleSpec

type ParticleSpec struct {
	Config  particles.Config
	OffsetX float32
	OffsetY float32
}

ParticleSpec is one emitter configuration and local offset produced by loading a particle emitter (.particles.yaml) or multi-emitter effect (.effect.yaml) file.

type PoolStats

type PoolStats struct {
	// Active is the number of emitters currently running for whileActive cues.
	Active int
	// OneShots is the number of emitters currently running for once cues.
	OneShots int
	// Draining is the number of stopped emitters finishing their remaining live particles before returning to the pool.
	Draining int
	// Pooled is the number of idle emitters ready for reuse.
	Pooled int
	// Created is the total number of particles.Emitter instances allocated.
	Created int
	// Reused is the total number of times an emitter was acquired from the pool instead of allocated.
	Reused int
}

PoolStats reports particle emitter pool utilization on a Runner.

type Runner

type Runner struct {
	// contains filtered or unexported fields
}

Runner executes client-side presentation handlers for a cues.Set: subscribing to ability.Caster and stats.Sheet events, pooling particle emitters, tracking looping sounds, camera shakes, target flashes and animations, and reconciling active cues across rollback.

func NewRunner

func NewRunner(set *cues.Set, cfg Config) (*Runner, error)

NewRunner validates set and constructs a client-side cue Runner.

Example
package main

import (
	"fmt"
	"testing/fstest"

	"github.com/nsgo-dev/game/pkg/cues"
	"github.com/nsgo-dev/game/pkg/cues/cuefx"
	"github.com/nsgo-dev/game/pkg/stats"
)

func main() {
	cueSet, _ := cues.Parse([]byte(`version: 1
cues:
  - tag: cue.status.burning
    whileActive: true
    particles:
      file: fx/burning.particles.yaml
    sound:
      file: sfx/burning.sound.yaml
      loop: true
`))
	fxFS := fstest.MapFS{
		"fx/burning.particles.yaml": {Data: []byte("version: 1\nrate: 15\nmaxParticles: 16\nlifetimeMin: 100ms\nlifetimeMax: 100ms\n")},
	}
	runner, _ := cuefx.NewRunner(cueSet, cuefx.Config{
		FS: fxFS,
		PlaySound: func(s cues.Sound, ev cues.Event) func() {
			fmt.Println("start", s.File, ev.Target)
			return func() { fmt.Println("stop", s.File, ev.Target) }
		},
	})

	sf, _ := stats.Parse([]byte(`version: 2
stats:
  - name: health
    base: 100
effects:
  - name: burn
    kind: timed
    steps: 2
    cue: cue.status.burning
`))
	sheet, _ := sf.NewSheet()
	unwatch := runner.WatchSheet(sheet, "hero")
	defer unwatch()

	burn, _ := sf.Effect("burn")
	_, _ = sheet.Apply("trap", burn)
	fmt.Println("active emitters:", runner.PoolStats().Active)
	sheet.Step()
	sheet.Step()
	fmt.Println("active emitters:", runner.PoolStats().Active)

}
Output:
start sfx/burning.sound.yaml hero
active emitters: 1
stop sfx/burning.sound.yaml hero
active emitters: 0

func (*Runner) Dispatch

func (r *Runner) Dispatch(ev cues.Event)

Dispatch delivers one cue event directly to the underlying player.

func (*Runner) DispatchAll

func (r *Runner) DispatchAll(events []cues.Event)

DispatchAll delivers each cue event in order to the underlying player.

func (*Runner) Draw

func (r *Runner) Draw(d *core.Drawing)

Draw draws every active, one-shot and draining particle emitter onto d in deterministic order.

func (*Runner) FlashState

func (r *Runner) FlashState(target string) (color core.Color, alpha float32, ok bool)

FlashState reports whether target currently has an active sprite flash, returning its color and linear remaining alpha in (0, 1].

func (*Runner) ForEachEmitter

func (r *Runner) ForEachEmitter(fn func(*particles.Emitter))

ForEachEmitter calls fn for every live emitter (active while-active cues in tag/target order, then one-shots, then draining emitters).

func (*Runner) Forget

func (r *Runner) Forget(before uint64)

Forget drops one-shot deduplication records strictly older than before.

func (*Runner) Player

func (r *Runner) Player() *cues.Player

Player returns the underlying deterministic cues.Player.

func (*Runner) PoolStats

func (r *Runner) PoolStats() PoolStats

PoolStats returns current particle emitter counts and pool allocation statistics.

func (*Runner) Reconcile

func (r *Runner) Reconcile(step uint64, extra ...cues.Active)

Reconcile gathers the active while-active cues from every watched Caster and Sheet (plus any extra active cues) and reconciles the player at step without restarting cues that remain active.

func (*Runner) Reset

func (r *Runner) Reset(step uint64)

Reset stops every active cue, releases all active/one-shot/draining emitters back to the pool, clears shakes and flashes, and clears one-shot records.

func (*Runner) ShakeOffset

func (r *Runner) ShakeOffset() (x, y float32)

ShakeOffset returns the combined deterministic 2D camera shake offset at the current time, or (0, 0) when no shake is active.

func (*Runner) Update

func (r *Runner) Update(dt time.Duration)

Update advances all active, one-shot and draining particle emitters, camera shakes, and sprite flashes by dt. Non-positive dt does nothing.

func (*Runner) WatchCaster

func (r *Runner) WatchCaster(caster *ability.Caster, target string) func()

WatchCaster subscribes r to caster's ability events for target and registers caster for Reconcile. Any existing caster.OnEvent callback is preserved and called before cue dispatch. Call the returned function to stop watching caster.

func (*Runner) WatchSheet

func (r *Runner) WatchSheet(sheet *stats.Sheet, target string) func()

WatchSheet subscribes r to sheet's effect lifecycle events for target and registers sheet for Reconcile. Any existing sheet.OnEffect callback is preserved and called before cue dispatch. Call the returned function to stop watching sheet.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL