tween

package
v0.0.0-...-94fff03 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: MIT Imports: 6 Imported by: 1 module, 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 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

Constants

This section is empty.

Variables

View Source
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

func KillLens[C, V any](tw *Tweens, e ecs.Entity, lens *Lens[C, V])

KillLens kills every tween of lens on e (and the rest of their sequences), additive ones included.

func LerpColor

func LerpColor(a, b gfx.Color, t float32) gfx.Color

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 LerpFloat

func LerpFloat(a, b, t float32) float32

LerpFloat interpolates a float32.

func LerpFloat4

func LerpFloat4(a, b [4]float32, t float32) [4]float32

LerpFloat4 interpolates each element of a [4]float32, such as gfx.Sprite.Params.

func LerpVec2

func LerpVec2(a, b gfx.Vec2, t float32) gfx.Vec2

LerpVec2 interpolates each coordinate of a gfx.Vec2.

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

func From[C, V any](tw *Tweens, e ecs.Entity, lens *Lens[C, V], from V, seconds float32) Builder

From is To in reverse: it tweens from from to the field's current value, read when the tween starts.

func To

func To[C, V any](tw *Tweens, e ecs.Entity, lens *Lens[C, V], to V, seconds float32) Builder

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

func (b Builder) Additive() Builder

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

func (b Builder) Delay(seconds float32) Builder

Delay waits seconds (scaled by Speed) before the tween starts and reads its start value. A negative delay counts as 0.

func (Builder) DespawnOnDone

func (b Builder) DespawnOnDone() Builder

DespawnOnDone deletes the entity (through system.Commands) when the tween finishes by itself or through Complete. A killed tween despawns nothing.

func (Builder) Ease

func (b Builder) Ease(e Ease) Builder

Ease sets the easing curve; the default is Linear.

func (Builder) EaseFunc

func (b Builder) EaseFunc(f func(t float32) float32) Builder

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

func (b Builder) ID() ID

ID returns the tween's handle, for Tweens.Kill, Sequence and the other control functions.

func (Builder) OnDone

func (b Builder) OnDone(f func()) Builder

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

func (b Builder) Relative() Builder

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

func (b Builder) Repeat(n int) Builder

Repeat runs the cycle n more times; a negative n repeats forever. A tween that repeats forever never finishes by itself.

func (Builder) Speed

func (b Builder) Speed(f float32) Builder

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

func (b Builder) Then(next ID) Builder

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

func (b Builder) Unscaled() Builder

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.

func (Builder) Yoyo

func (b Builder) Yoyo() Builder

Yoyo runs every second cycle backwards, from the end value to the start value, with the ease mirrored. Use it with Repeat: Repeat(1).Yoyo() goes there and back once.

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.

func (Ease) At

func (e Ease) At(t float32) float32

At returns the eased fraction for t. t is clamped to [0, 1]; an invalid Ease behaves as Linear.

func (Ease) String

func (e Ease) String() string

String returns the constant's name, such as "OutBack".

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

func Sequence(tw *Tweens, ids ...ID) ID

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.

func (ID) IsZero

func (id ID) IsZero() bool

IsZero reports whether id is the zero ID.

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

func NewLens[C, V any](get func(*C) *V, lerp func(a, b V, t float32) V) *Lens[C, V]

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.

func Param

func Param(i int) *Lens[gfx.Sprite, float32]

Param returns the lens for one shader parameter, gfx.Sprite.Params[i]. Tweens on different parameters of one sprite do not overwrite each other.

Panics with "tween: Param index out of range: <i>" unless 0 <= i < 4.

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.

func NewSystem

func NewSystem(tw *Tweens) *System

NewSystem returns the system that advances tw.

func (*System) Run

func (s *System) Run(ctx *system.Context)

Run implements system.Runner.

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

func New(w *ecs.World) *Tweens

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

func (tw *Tweens) Complete(id ID)

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

func (tw *Tweens) Kill(id ID)

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

func (tw *Tweens) KillEntity(e ecs.Entity)

KillEntity kills every tween on e (and the rest of their sequences).

func (*Tweens) Len

func (tw *Tweens) Len() int

Len returns the number of running tweens, for tests and diagnostics.

func (*Tweens) Pause

func (tw *Tweens) Pause(id ID, on bool)

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

func (tw *Tweens) Running(id ID) bool

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

func (tw *Tweens) Update(dt, unscaled float32)

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).

Jump to

Keyboard shortcuts

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