API 参考取自源码中的 Go 文档注释,以英文书写。
package tween
import "github.com/nsgo-dev/game/pkg/tween"概述
Package tween animates values, timelines, and tilemap cameras deterministically on fixed game.Loop steps using NSGo easing curves.
索引
- 常量
- type CatmullRomCurve
- func CatmullRom(points []core.Point, closed bool, alpha float32) *CatmullRomCurve
- func CatmullRomFromTiledObject(obj tilemap.Object) *CatmullRomCurve
- func NewCatmullRom(points []core.Point, closed bool) *CatmullRomCurve
- func (c *CatmullRomCurve) Closed() bool
- func (c *CatmullRomCurve) Length() float32
- func (c *CatmullRomCurve) Point(t float32) core.Point
- func (c *CatmullRomCurve) PointAtDistance(dist float32) core.Point
- func (c *CatmullRomCurve) Tangent(t float32) core.Point
- func (c *CatmullRomCurve) TangentAtDistance(dist float32) core.Point
- type CubicBezierCurve
- func CubicBezier(p0, p1, p2, p3 core.Point) *CubicBezierCurve
- func (c *CubicBezierCurve) Closed() bool
- func (c *CubicBezierCurve) Length() float32
- func (c *CubicBezierCurve) Point(t float32) core.Point
- func (c *CubicBezierCurve) PointAtDistance(dist float32) core.Point
- func (c *CubicBezierCurve) Tangent(t float32) core.Point
- func (c *CubicBezierCurve) TangentAtDistance(dist float32) core.Point
- type Curve
- type EaseFunc
- type FollowerOptions
- type Group
- type LoopMode
- type PathFollower
- func NewPathFollower(curve Curve, opts FollowerOptions) *PathFollower
- func (f *PathFollower) Angle() float32
- func (f *PathFollower) Distance() float32
- func (f *PathFollower) Done() bool
- func (f *PathFollower) Position() core.Point
- func (f *PathFollower) Progress() float32
- func (f *PathFollower) Reset()
- func (f *PathFollower) Seek(progress float32)
- func (f *PathFollower) Step(dt float32)
- func (f *PathFollower) Tangent() core.Point
- func (f *PathFollower) Tween() *Tween
- func (f *PathFollower) Update(dt time.Duration)
- type PolylineCurve
- func Polyline(points []core.Point, closed bool) *PolylineCurve
- func PolylineFromTiledObject(obj tilemap.Object) *PolylineCurve
- func (c *PolylineCurve) Closed() bool
- func (c *PolylineCurve) Length() float32
- func (c *PolylineCurve) Point(t float32) core.Point
- func (c *PolylineCurve) PointAtDistance(dist float32) core.Point
- func (c *PolylineCurve) Tangent(t float32) core.Point
- func (c *PolylineCurve) TangentAtDistance(dist float32) core.Point
- type QuadraticBezierCurve
- func QuadraticBezier(p0, p1, p2 core.Point) *QuadraticBezierCurve
- func (c *QuadraticBezierCurve) Closed() bool
- func (c *QuadraticBezierCurve) Length() float32
- func (c *QuadraticBezierCurve) Point(t float32) core.Point
- func (c *QuadraticBezierCurve) PointAtDistance(dist float32) core.Point
- func (c *QuadraticBezierCurve) Tangent(t float32) core.Point
- func (c *QuadraticBezierCurve) TangentAtDistance(dist float32) core.Point
- type Timeline
- func NewTimeline() *Timeline
- func (tl *Timeline) Add(at time.Duration, tw *Tween) *Timeline
- func (tl *Timeline) Append(tw *Tween) *Timeline
- func (tl *Timeline) Call(at time.Duration, fn func()) *Timeline
- func (tl *Timeline) Done() bool
- func (tl *Timeline) Duration() time.Duration
- func (tl *Timeline) Pause()
- func (tl *Timeline) Play()
- func (tl *Timeline) Playing() bool
- func (tl *Timeline) Restart()
- func (tl *Timeline) Reverse(reverse bool)
- func (tl *Timeline) Seek(at time.Duration)
- func (tl *Timeline) SetLoop(loop bool)
- func (tl *Timeline) Time() time.Duration
- func (tl *Timeline) Update(dt time.Duration)
- type Tween
- func CameraFollowTransition(cam *tilemap.Camera, target func() core.Point, d time.Duration, curve core.Curve) *Tween
- func CameraMove(cam *tilemap.Camera, toX, toY, toZoom, toRot float32, d time.Duration, curve core.Curve) *Tween
- func CameraPan(cam *tilemap.Camera, toX, toY float32, d time.Duration, curve core.Curve) *Tween
- func CameraRotate(cam *tilemap.Camera, toRot float32, d time.Duration, curve core.Curve) *Tween
- func CameraZoom(cam *tilemap.Camera, toZoom float32, d time.Duration, curve core.Curve) *Tween
- func Color(from, to core.Color, d time.Duration, curve core.Curve, set func(core.Color)) *Tween
- func Float32(from, to float32, d time.Duration, curve core.Curve, set func(float32)) *Tween
- func Float64(from, to float64, d time.Duration, curve core.Curve, set func(float64)) *Tween
- func Follow(curve Curve, d time.Duration, ease core.Curve, set func(pos core.Point, angle float32)) *Tween
- func New[T any](from, to T, d time.Duration, curve core.Curve, lerp func(a, b T, t float64) T, set func(T)) *Tween
- func Point(from, to core.Point, d time.Duration, curve core.Curve, set func(core.Point)) *Tween
- func (t *Tween) Delay(d time.Duration) *Tween
- func (t *Tween) Done() bool
- func (t *Tween) Duration() time.Duration
- func (t *Tween) OnComplete(fn func()) *Tween
- func (t *Tween) Repeat(count int, yoyo bool) *Tween
- func (t *Tween) Reset()
- func (t *Tween) Seek(at time.Duration)
- func (t *Tween) Update(dt time.Duration) bool
常量
类型
type CatmullRomCurve struct {
// contains filtered or unexported fields
}CatmullRomCurve is a C1-continuous Catmull-Rom spline passing through all control points.
func CatmullRom(points []core.Point, closed bool, alpha float32) *CatmullRomCurveCatmullRom returns a Catmull-Rom spline through points with parameterization exponent alpha (0 = uniform, 0.5 = centripetal, 1 = chordal). Negative alpha is clamped to 0.
func CatmullRomFromTiledObject(obj tilemap.Object) *CatmullRomCurveCatmullRomFromTiledObject converts a Tiled polyline (open) or polygon (closed) object translated by (obj.X, obj.Y) into a centripetal CatmullRomCurve.
func NewCatmullRom(points []core.Point, closed bool) *CatmullRomCurveNewCatmullRom returns a centripetal Catmull-Rom spline (alpha = 0.5) passing through points.
func (c *CatmullRomCurve) Closed() boolClosed reports whether the Catmull-Rom spline forms a closed loop.
func (c *CatmullRomCurve) Length() float32Length returns the total arc length of the Catmull-Rom spline.
func (c *CatmullRomCurve) Point(t float32) core.PointPoint evaluates the Catmull-Rom spline at parameter t in [0, 1].
func (c *CatmullRomCurve) PointAtDistance(dist float32) core.PointPointAtDistance returns the point at arc-length distance dist along the spline.
func (c *CatmullRomCurve) Tangent(t float32) core.PointTangent returns the unit-length tangent vector at parameter t in [0, 1].
func (c *CatmullRomCurve) TangentAtDistance(dist float32) core.PointTangentAtDistance returns the unit-length tangent vector at arc-length distance dist along the spline.
type CubicBezierCurve struct {
// contains filtered or unexported fields
}CubicBezierCurve is a cubic Bézier curve defined by four control points.
func CubicBezier(p0, p1, p2, p3 core.Point) *CubicBezierCurveCubicBezier returns a cubic Bézier curve from p0 to p3 with control points p1 and p2.
func (c *CubicBezierCurve) Closed() boolClosed reports false because a cubic Bézier curve is open.
func (c *CubicBezierCurve) Length() float32Length returns the total arc length of the cubic Bézier curve.
func (c *CubicBezierCurve) Point(t float32) core.PointPoint evaluates the cubic Bézier curve at parameter t in [0, 1].
func (c *CubicBezierCurve) PointAtDistance(dist float32) core.PointPointAtDistance returns the point at arc-length distance dist along the curve.
func (c *CubicBezierCurve) Tangent(t float32) core.PointTangent returns the unit-length tangent vector at parameter t in [0, 1].
func (c *CubicBezierCurve) TangentAtDistance(dist float32) core.PointTangentAtDistance returns the unit-length tangent vector at arc-length distance dist along the curve.
type Curve interface {
// Point evaluates the curve at parameter t in [0, 1] (clamped).
Point(t float32) core.Point
// Tangent returns the unit-length tangent vector at parameter t in [0, 1] (clamped),
// or the zero point if the curve is degenerate (zero length).
Tangent(t float32) core.Point
// Length returns the total arc length of the curve.
Length() float32
// PointAtDistance returns the point at arc-length distance dist along the curve (clamped to [0, Length()]).
PointAtDistance(dist float32) core.Point
// TangentAtDistance returns the unit-length tangent vector at arc-length distance dist along the curve (clamped to [0, Length()]).
TangentAtDistance(dist float32) core.Point
// Closed reports whether the curve forms a closed loop.
Closed() bool
}Curve is a 2D parametric curve with arc-length parameterization.
func CurveFromTiledObject(obj tilemap.Object, smooth bool) CurveCurveFromTiledObject converts a Tiled polyline (open) or polygon (closed) object translated by (obj.X, obj.Y) into a Curve: a centripetal Catmull-Rom spline when smooth is true, or a Polyline otherwise.
type EaseFunc func(t float32) float32EaseFunc maps normalized pass progress t in [0, 1] to an eased progress value.
func EaseFromCurve(c core.Curve) EaseFuncEaseFromCurve adapts an NSGo core.Curve easing curve into an EaseFunc.
type FollowerOptions struct {
// Speed is the constant travel speed in points per second along the curve's arc length.
// Used when Duration <= 0.
Speed float32
// Duration is the total travel time for one pass along the curve.
// When Duration > 0, it takes precedence over Speed.
Duration time.Duration
// Ease maps normalized pass progress [0, 1] to eased arc-length fraction.
// When nil and Curve is zero, motion proceeds linearly by arc length.
Ease EaseFunc
// Curve is an optional NSGo easing curve used when Ease is nil.
Curve core.Curve
// Loop controls playback behavior on reaching the end of the curve.
Loop LoopMode
// OrientToTangent computes Angle() from the curve tangent (atan2(ty, tx)) plus RotationOffset.
// When false, Angle() returns RotationOffset.
OrientToTangent bool
// RotationOffset is added in radians to the heading angle.
RotationOffset float32
// OnUpdate is invoked whenever the follower moves or seeks with the current position and angle (radians).
OnUpdate func(pos core.Point, angle float32)
// OnComplete is invoked once when a LoopNone follower finishes the curve.
OnComplete func()
}FollowerOptions configures a PathFollower.
type Group struct {
// contains filtered or unexported fields
}Group steps a collection of active tweens together and removes each once it finishes. Its zero value is ready to use.
func (g *Group) Add(tweens ...*Tween)Add registers one or more non-nil tweens to be stepped by Update.
func (g *Group) Len() intLen returns the number of active tweens currently in the group.
func (g *Group) Update(dt time.Duration)Update advances every active tween in the group by dt and removes any that finish.
type LoopMode intLoopMode controls what a PathFollower does on reaching the end of a curve.
const (
// LoopNone stops at the end of the curve and fires OnComplete once.
LoopNone LoopMode = iota
// LoopRepeat wraps back to the beginning of the curve and continues indefinitely.
LoopRepeat
// LoopPingPong reverses direction at each end of the curve and oscillates indefinitely.
LoopPingPong
)type PathFollower struct {
// contains filtered or unexported fields
}PathFollower advances deterministically along a Curve by arc length, maintaining constant speed or easing cleanly over a fixed Duration.
func NewPathFollower(curve Curve, opts FollowerOptions) *PathFollowerNewPathFollower returns a PathFollower positioned at the start of curve.
func (f *PathFollower) Angle() float32Angle returns the follower's current heading angle in radians.
func (f *PathFollower) Distance() float32Distance returns the follower's current arc-length distance from the start of the curve.
func (f *PathFollower) Done() boolDone reports whether a non-looping follower has reached the end of the curve.
func (f *PathFollower) Position() core.PointPosition returns the follower's current position along the curve.
func (f *PathFollower) Progress() float32Progress returns the follower's normalized progress in [0, 1] along the curve.
func (f *PathFollower) Reset()Reset rewinds the follower back to the start of the curve so it can run again.
func (f *PathFollower) Seek(progress float32)Seek moves the follower directly to normalized progress in [0, 1] without firing OnComplete.
func (f *PathFollower) Step(dt float32)Step advances the follower by dt seconds.
func (f *PathFollower) Tangent() core.PointTangent returns the follower's current unit-length tangent vector.
func (f *PathFollower) Tween() *TweenTween returns a *Tween backed by this follower so it integrates directly with Group and Timeline. It uses the follower's Duration (or Length/Speed when Duration <= 0), Loop mode, easing, and callbacks.
func (f *PathFollower) Update(dt time.Duration)Update advances the follower by dt.
type PolylineCurve struct {
// contains filtered or unexported fields
}PolylineCurve is a piecewise-linear curve through a sequence of vertices with exact arc-length lookup.
func Polyline(points []core.Point, closed bool) *PolylineCurvePolyline returns a piecewise-linear curve through points. When closed is true and the last point differs from the first, an explicit closing segment back to points[0] is added.
func PolylineFromTiledObject(obj tilemap.Object) *PolylineCurvePolylineFromTiledObject converts a Tiled polyline (open) or polygon (closed) object translated by (obj.X, obj.Y) into a PolylineCurve.
func (c *PolylineCurve) Closed() boolClosed reports whether the polyline forms a closed loop.
func (c *PolylineCurve) Length() float32Length returns the exact total length of the polyline.
func (c *PolylineCurve) Point(t float32) core.PointPoint evaluates the polyline at arc-length fraction t in [0, 1].
func (c *PolylineCurve) PointAtDistance(dist float32) core.PointPointAtDistance returns the exact point at arc-length distance dist along the polyline.
func (c *PolylineCurve) Tangent(t float32) core.PointTangent returns the unit-length tangent vector at arc-length fraction t in [0, 1].
func (c *PolylineCurve) TangentAtDistance(dist float32) core.PointTangentAtDistance returns the unit-length tangent vector at arc-length distance dist along the polyline.
type QuadraticBezierCurve struct {
// contains filtered or unexported fields
}QuadraticBezierCurve is a quadratic Bézier curve defined by three control points.
func QuadraticBezier(p0, p1, p2 core.Point) *QuadraticBezierCurveQuadraticBezier returns a quadratic Bézier curve from p0 to p2 controlled by p1.
func (c *QuadraticBezierCurve) Closed() boolClosed reports false because a quadratic Bézier curve is open.
func (c *QuadraticBezierCurve) Length() float32Length returns the total arc length of the quadratic Bézier curve.
func (c *QuadraticBezierCurve) Point(t float32) core.PointPoint evaluates the quadratic Bézier curve at parameter t in [0, 1].
func (c *QuadraticBezierCurve) PointAtDistance(dist float32) core.PointPointAtDistance returns the point at arc-length distance dist along the curve.
func (c *QuadraticBezierCurve) Tangent(t float32) core.PointTangent returns the unit-length tangent vector at parameter t in [0, 1].
func (c *QuadraticBezierCurve) TangentAtDistance(dist float32) core.PointTangentAtDistance returns the unit-length tangent vector at arc-length distance dist along the curve.
type Timeline struct {
// contains filtered or unexported fields
}Timeline coordinates multiple tweens and timed callback cues along a single deterministic clock, supporting seeking, forward and reverse playback, and looping. Its zero value is ready to use.
func NewTimeline() *TimelineNewTimeline returns an empty Timeline ready to add tracks and cues to.
func (tl *Timeline) Add(at time.Duration, tw *Tween) *TimelineAdd places tw on the timeline starting at offset at. Nil tweens are ignored; negative offsets are clamped to 0.
func (tl *Timeline) Append(tw *Tween) *TimelineAppend places tw on the timeline starting at its current Duration. Nil tweens are ignored.
func (tl *Timeline) Call(at time.Duration, fn func()) *TimelineCall schedules fn to run when playback crosses timestamp at. Nil callbacks are ignored; negative offsets are clamped to 0.
func (tl *Timeline) Done() boolDone reports whether a non-looping timeline has reached the end of its playback direction.
func (tl *Timeline) Duration() time.DurationDuration returns the total span of the timeline across all tracks and cues.
func (tl *Timeline) Pause()Pause suspends playback at the current timestamp.
func (tl *Timeline) Play()Play starts or resumes playback in the current direction.
func (tl *Timeline) Playing() boolPlaying reports whether the timeline is currently playing.
func (tl *Timeline) Restart()Restart jumps to the start of the current playback direction (0 forward, Duration in reverse), applies all tracks, and starts playing.
func (tl *Timeline) Reverse(reverse bool)Reverse sets whether playback runs backward (from Duration down to 0).
func (tl *Timeline) Seek(at time.Duration)Seek moves the timeline to timestamp at (clamped to [0, Duration()]) and puts every track — before, during, and after its window — at the exact value it holds at that time without firing cues.
func (tl *Timeline) SetLoop(loop bool)SetLoop sets whether playback wraps around on reaching the end.
func (tl *Timeline) Time() time.DurationTime returns the timeline's current playback timestamp.
func (tl *Timeline) Update(dt time.Duration)Update advances the timeline by dt when playing, updating tracks and firing any cues crossed during the step.
type Tween struct {
// contains filtered or unexported fields
}Tween interpolates a value over a fixed duration using a core.Curve easing function, advancing deterministically via Update(dt) on game.Loop steps.
func CameraFollowTransition(cam *tilemap.Camera, target func() core.Point, d time.Duration, curve core.Curve) *TweenCameraFollowTransition returns a tween that smoothly moves cam from its position when the tween starts onto a moving target position sampled from target each step, ending directly on target() (clamped to cam.Bounds if set).
func CameraMove(cam *tilemap.Camera, toX, toY, toZoom, toRot float32, d time.Duration, curve core.Curve) *TweenCameraMove returns a tween that moves cam simultaneously to (toX, toY), toZoom, and toRot over d with curve, clamping to cam.Bounds if set.
func CameraPan(cam *tilemap.Camera, toX, toY float32, d time.Duration, curve core.Curve) *TweenCameraPan returns a tween that pans cam from its position when the tween starts to (toX, toY) over d with curve, clamping to cam.Bounds if set.
func CameraRotate(cam *tilemap.Camera, toRot float32, d time.Duration, curve core.Curve) *TweenCameraRotate returns a tween that rotates cam from its rotation when the tween starts to toRot (in radians) over d with curve, clamping to cam.Bounds if set.
func CameraZoom(cam *tilemap.Camera, toZoom float32, d time.Duration, curve core.Curve) *TweenCameraZoom returns a tween that zooms cam from its zoom when the tween starts (treating <= 0 as 1) to toZoom over d with curve, clamping to cam.Bounds if set.
func Color(from, to core.Color, d time.Duration, curve core.Curve, set func(core.Color)) *TweenColor returns a tween over a core.Color from from to to over d with curve, interpolating sRGB and alpha channels.
func Float32(from, to float32, d time.Duration, curve core.Curve, set func(float32)) *TweenFloat32 returns a tween over a float32 value from from to to over d with curve.
func Float64(from, to float64, d time.Duration, curve core.Curve, set func(float64)) *TweenFloat64 returns a tween over a float64 value from from to to over d with curve.
func Follow(curve Curve, d time.Duration, ease core.Curve, set func(pos core.Point, angle float32)) *TweenFollow returns a Tween that moves along curve by arc length over d with ease, calling set with the current position and tangent angle (in radians) on each step. A zero ease uses core.Linear. Negative d is treated as 0.
func New[T any](from, to T, d time.Duration, curve core.Curve, lerp func(a, b T, t float64) T, set func(T)) *TweenNew returns a tween that interpolates from from to to over d with curve, using lerp to compute each intermediate value and set to apply it.
A zero curve uses core.Linear. A negative d is treated as 0.
func Point(from, to core.Point, d time.Duration, curve core.Curve, set func(core.Point)) *TweenPoint returns a tween over a core.Point from from to to over d with curve.
func (t *Tween) Delay(d time.Duration) *TweenDelay waits d before the tween begins moving. Negative durations are treated as 0.
func (t *Tween) Duration() time.DurationDuration returns the tween's total duration including delay and repeats. For a tween that repeats forever, Duration returns one full cycle (delay plus one pass, or two passes when yoyo is enabled).
func (t *Tween) OnComplete(fn func()) *TweenOnComplete registers fn to run once when Update finishes the tween. Multiple callbacks run in the order registered.
func (t *Tween) Repeat(count int, yoyo bool) *TweenRepeat repeats the tween count additional times after its first pass (0 plays once; RepeatForever or any negative value repeats forever). When yoyo is true, alternating passes play back from to to from.
func (t *Tween) Reset()Reset rewinds the tween to its start so it can play again from elapsed 0.
func (t *Tween) Seek(at time.Duration)Seek moves the tween to timestamp at and applies the value it holds there without firing OnComplete callbacks.
func (t *Tween) Update(dt time.Duration) boolUpdate advances the tween by dt, applies the value at the new time, and reports whether the tween has finished.