Documentation
¶
Overview ¶
Package tween animates component fields over time: "move this enemy to (120, 40) with OutBack in 0.4 seconds", "pulse this coin forever", "ease the HP bar to the new value".
Tweens are not components. A Tweens resource owns every running tween, in one typed pool per Lens, and hands out an ID to control it. A Lens names the field a tween writes (Pos of gfx.Transform, Tint of gfx.Sprite, or a field of the game's own component) with typed functions, so there are no strings, no reflection and no unsafe, and the value type is checked at compile time. Any number of tweens can run on one entity.
tw := ecs.GetResource[tween.Tweens](ctx.World)
tween.To(tw, e, tween.Scale, gfx.Vec2{X: 1.3, Y: 1.3}, 0.1).Yoyo().Repeat(1)
System advances the tweens once per tick; the game.Engine runs it at the start of its camera stage and publishes the Tweens as a resource. The package makes no Ebiten calls, uses no randomness and no wall clock, and allocates nothing per tick once its pools have grown.
The guide is doc/guide/engine/tweens.md.
Index ¶
- Variables
- func KillLens[C, V any](tw *Tweens, e ecs.Entity, lens *Lens[C, V])
- func LerpColor(a, b gfx.Color, t float32) gfx.Color
- func LerpFloat(a, b, t float32) float32
- func LerpFloat4(a, b [4]float32, t float32) [4]float32
- func LerpVec2(a, b gfx.Vec2, t float32) gfx.Vec2
- type Builder
- func (b Builder) Additive() Builder
- func (b Builder) Delay(seconds float32) Builder
- func (b Builder) DespawnOnDone() Builder
- func (b Builder) Ease(e Ease) Builder
- func (b Builder) EaseFunc(f func(t float32) float32) Builder
- func (b Builder) ID() ID
- func (b Builder) OnDone(f func()) Builder
- func (b Builder) Relative() Builder
- func (b Builder) Repeat(n int) Builder
- func (b Builder) Speed(f float32) Builder
- func (b Builder) Then(next ID) Builder
- func (b Builder) Unscaled() Builder
- func (b Builder) Yoyo() Builder
- type Ease
- type ID
- type Lens
- type System
- type Tweens
Constants ¶
This section is empty.
Variables ¶
var ( // Pos is gfx.Transform.Pos. Pos = NewLens(func(t *gfx.Transform) *gfx.Vec2 { return &t.Pos }, LerpVec2) // Scale is gfx.Transform.Scale. A zero Scale counts as (1, 1), as when // drawing, so tweening an unscaled entity starts from its real size. Scale = withNorm(NewLens(func(t *gfx.Transform) *gfx.Vec2 { return &t.Scale }, LerpVec2), normScale) // Rot is gfx.Transform.Rot, in radians. Rot = NewLens(func(t *gfx.Transform) *float32 { return &t.Rot }, LerpFloat) // Tint is gfx.Sprite.Tint. Fade a sprite out by tweening it to a // color with zero alpha, such as gfx.RGBA(255, 255, 255, 0). Tint = NewLens(func(s *gfx.Sprite) *gfx.Color { return &s.Tint }, LerpColor) // Params is gfx.Sprite.Params, the shader's Params uniform. Params = NewLens(func(s *gfx.Sprite) *[4]float32 { return &s.Params }, LerpFloat4) )
Built-in lenses for the engine's components.
Functions ¶
func KillLens ¶
KillLens kills every tween of lens on e (and the rest of their sequences), additive ones included.
func LerpColor ¶
LerpColor interpolates each channel of a gfx.Color in 0..1 RGBA, non-premultiplied, and clamps the result to the valid range. The zero Color counts as opaque white, as everywhere in gfx. It returns a and b themselves at t = 0 and t = 1.
func LerpFloat4 ¶
LerpFloat4 interpolates each element of a [4]float32, such as gfx.Sprite.Params.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder sets the options of a tween just started with To or From. It is a small value (the Tweens and the ID), so chaining allocates nothing. Each option writes straight into the tween, so their order does not matter. On a tween that has finished or was killed every option is a no-op.
Relative and Additive are read when the tween starts, so set them right after To or From.
func From ¶
From is To in reverse: it tweens from from to the field's current value, read when the tween starts.
func To ¶
To starts a tween of the field lens selects on e, from its current value to to, over seconds (one cycle). The start value is read when the tween actually starts, after its delay and after the previous tween of its sequence. Chain options on the returned Builder:
tween.To(tw, e, tween.Pos, target, 0.4).Ease(tween.OutBack)
Overwrite: when the tween starts, it kills a running tween on the same entity and lens (unless either is Builder.Additive), so a new "move to" never fights an old one.
To is a function, not a method, because Go methods cannot have type parameters. A zero or negative seconds jumps to the end value when the tween starts. A dead entity, or one without C, never panics: the tween is dropped in the next tick. Once the lens's pool has grown, To allocates nothing.
Panics with "tween: nil lens" if lens is nil.
func (Builder) Additive ¶
Additive makes the tween write its change each tick instead of its value, and exempts it from the overwrite rule. Additive tweens on one field stack with each other and with game code that moves the field (a shake on top of a move or of physics). A non-additive tween on the same field still writes absolute values and wins while it runs. Meant for numeric values.
func (Builder) Delay ¶
Delay waits seconds (scaled by Speed) before the tween starts and reads its start value. A negative delay counts as 0.
func (Builder) DespawnOnDone ¶
DespawnOnDone deletes the entity (through system.Commands) when the tween finishes by itself or through Complete. A killed tween despawns nothing.
func (Builder) EaseFunc ¶
EaseFunc sets a custom easing curve, used instead of Ease. f maps the time fraction in (0, 1) to the progress; it should return 0 at 0 and 1 at 1, but the last cycle writes the exact end value either way. Storing f costs nothing per tick; a closure that captures variables allocates once, when it is created.
func (Builder) ID ¶
ID returns the tween's handle, for Tweens.Kill, Sequence and the other control functions.
func (Builder) OnDone ¶
OnDone sets a callback that runs when the tween finishes by itself or through Complete, after every pool has run for the tick, so it may start and kill tweens. It does not run for killed tweens or dead targets. A closure that captures variables allocates once, when it is created.
func (Builder) Relative ¶
Relative makes the To (or From) value an offset from the start value: To(tw, e, Pos, gfx.Vec2{X: 16}, 0.2).Relative() moves 16 to the right. Meant for numeric values (float32, gfx.Vec2, [4]float32), not colors.
func (Builder) Repeat ¶
Repeat runs the cycle n more times; a negative n repeats forever. A tween that repeats forever never finishes by itself.
func (Builder) Speed ¶
Speed scales the tween's clock, delay included: 2 runs twice as fast, 0 freezes it.
Panics with "tween: negative speed: <f>" if f < 0.
func (Builder) Then ¶
Then holds next until this tween finishes, then lets it start; killing this tween kills next. Sequence chains several. A stale next, or next equal to this tween, is ignored.
func (Builder) Unscaled ¶
Unscaled runs the tween on real time: it advances by the unscaled tick (gfx.Time.Unscaled, see Tweens.Update) while the game clock is slowed down or stopped, for example the pause menu's own animations. Speed, Delay and the ease apply as usual.
type Ease ¶
type Ease uint8
Ease shapes the progress of a tween: it maps the linear time fraction t (0 at the start of a cycle, 1 at its end) to the fraction of the way from the start value to the end value. The zero Ease is Linear.
Every Ease returns exactly 0 at 0 and exactly 1 at 1. The Back and Elastic curves overshoot in between (values below 0 or above 1), so lerp functions must extrapolate.
const ( Linear Ease = iota InQuad OutQuad InOutQuad InCubic OutCubic InOutCubic InSine OutSine InOutSine InExpo OutExpo InOutExpo InBack OutBack InOutBack InElastic OutElastic InBounce OutBounce // Step stays at 0 until the end of the cycle, then jumps to 1. Step )
The easing curves. In eases start slowly, Out eases end slowly, InOut eases do both. The formulas are Robert Penner's.
type ID ¶
type ID struct {
// contains filtered or unexported fields
}
ID is the handle of one tween. It stays valid while the tween runs (including its delay, and while paused or waiting in a sequence); once the tween finishes or is killed, every call with the ID is a no-op, so gameplay code never has to track whether a tween still runs. The zero ID is never valid.
func Sequence ¶
Sequence runs the tweens one after another: each one after the first is held until the previous one finishes (see Builder.Then). It returns the first ID. To run tweens in parallel, just start them.
type Lens ¶
type Lens[C, V any] struct { // contains filtered or unexported fields }
Lens selects the field a tween writes: a value of type V inside a component C. Create lenses once, as package-level variables, with NewLens; each lens gets its own pool in every Tweens, and the overwrite rule (see To) is per entity and lens.
A game's own field is one line:
var HPBar = tween.NewLens(func(h *Health) *float32 { return &h.Shown }, tween.LerpFloat)
func NewLens ¶
NewLens returns the lens for the field get points to. get must return a pointer into the component it is given (never nil). lerp returns the value t of the way from a to b; it must give a at 0 and b at 1 and extrapolate for t outside [0, 1] (overshooting eases, and Builder.Relative and Builder.Additive, rely on it). The Lerp functions of this package do.
Panics with "tween: nil lens function" if get or lerp is nil.
type System ¶
type System struct {
// contains filtered or unexported fields
}
System advances a Tweens by the tick's ctx.Time.Delta, the scaled tick of the game clock, and the Builder.Unscaled tweens by gfx.Time.Unscaled (the context's delta in a World without the gfx.Time resource). The game.Engine runs it first in its camera stage: tweens started by game systems run in the same tick, the follow camera sees the tweened position, and the stage is paused with the rest of the game while a Tool pauses it.
type Tweens ¶
type Tweens struct {
// contains filtered or unexported fields
}
Tweens owns every running tween of a World, in one pool per Lens. The game.Engine creates one and publishes it as a resource:
tw := ecs.GetResource[tween.Tweens](ctx.World)
Start tweens with To and From; System advances them. Like the World, a Tweens is single-goroutine.
func New ¶
New returns an empty Tweens for w. DespawnOnDone queues on w's system.Commands, the buffer the schedule applies after every stage.
func (*Tweens) Clear ¶
func (tw *Tweens) Clear()
Clear drops every tween without writing values or running callbacks. The editor calls it when Play stops. Every ID becomes stale.
func (*Tweens) Complete ¶
Complete writes the tween's end value and finishes it now: its OnDone runs, the next tween of its sequence is released and DespawnOnDone is queued. A tween that has not started yet reads its start value first. The end value is the one the tween would rest on when finishing by itself; for one that repeats forever it is the To value. A stale id is a no-op.
func (*Tweens) Kill ¶
Kill stops the tween where it is, without writing its end value or running OnDone. The tweens after it in a sequence are killed too. A stale id is a no-op.
func (*Tweens) KillEntity ¶
KillEntity kills every tween on e (and the rest of their sequences).
func (*Tweens) Pause ¶
Pause pauses (on) or resumes the tween. A paused tween keeps its place and its delay. A stale id is a no-op.
func (*Tweens) Running ¶
Running reports whether id is a tween that has not finished or been killed. A tween in its delay, paused or waiting in a sequence is running.
func (*Tweens) Update ¶
Update advances every tween by dt seconds (the scaled tick of the game clock), the tweens started with Builder.Unscaled by unscaled seconds (the real tick), then runs the OnDone callbacks of the tweens that finished. System calls it once per tick; call it yourself only when driving a Tweens without the System, with the same value twice if there is no time scale. 0 allocations once the pools have grown (callbacks aside).