tween
game/tween 使用 NSGo 的 core.Curve 緩動曲線與具名緩動函式,在 game.Loop 固定步長上確定性地驅動數值、座標、顏色、自定義型別、時間軸以及 tilemap.Camera 運鏡補間。
import "github.com/nsgo-dev/game/pkg/tween"
建立與推進 Tween
使用 Float32、Float64、Point、Color 或泛型 New[T] 建立 *Tween,可鏈式配置 Delay、Repeat 和 OnComplete,再通過 Update(dt) 或 Group 推進:
var group tween.Group
popup := tween.Float32(0, 1, 300*time.Millisecond, core.EaseOut, func(v float32) {
banner.ScaleX, banner.ScaleY = v, v
}).Delay(100 * time.Millisecond).
OnComplete(func() { bannerReady = true })
bob := tween.Float32(0, -6, 600*time.Millisecond, core.EaseInOut, func(y float32) {
coin.Y = baseY + y
}).Repeat(tween.RepeatForever, true) // 無限往返(yoyo)
group.Add(popup, bob)
// 在 game.Loop.Update 中:
group.Update(dt) // 完成的補間會自動移除
- 緩動曲線:零值
core.Curve預設為core.Linear。 Repeat(count, yoyo):在首輪之後額外重複count次(0表示只播一次;tween.RepeatForever或任意負數表示無限重複)。yoyo為true時,奇數輪反向從to插值回from。- 跳轉與重置:
tw.Seek(at)跳轉到指定時間點並立即應用該時刻的值(不觸發OnComplete);tw.Reset()倒回起點並清除延遲捕獲的起始狀態(使To、By、FromCurrent與相機補間在下一次執行時重新讀取當前值,而Seek(0)會保留已捕獲的起始值以保證拖拽回放的確定性)。可通過tw.Done()與tw.Duration()查詢狀態。 - 自定義型別:用
tween.New[T](from, to, d, curve, lerp, set)對任意結構體或向量做補間。
具名緩動與延遲讀取起始值
tween.Ease 是普通的緩動函式型別,提供完整的具名集合:sine、quad、cubic、quart、quint、expo、circ、back、elastic、bounce、spring 十一個族,每族都有 In、Out、InOut、OutIn 四個變體(tween.BackOut、tween.ElasticOut 等)。名稱與資源曲線關鍵幀的 interp 值一致,動畫剪輯與程式碼共享同一套緩動。
To、By、FromCurrent 在步驟真正執行時(而不是構建時)延遲讀取起始值,因此每一步總是從前一步留下的結果繼續:
// 先彈出放大,再向上漂移 40 點——無需手寫起始值。
coin := tween.Sequence().
Then(tween.To(&coin.Scale, 1.4, 80*time.Millisecond, tween.BackOut)).
With(tween.By(&coin.Y, -40, 300*time.Millisecond, tween.QuadOut, nil, nil)).
Build()
tween.To(&v, target, d, ease)將*v動畫到target。tween.By(&v, delta, d, ease, add, lerp)將*v動畫到步驟開始時的值加上delta;內建型別(float32、float64、core.Point、core.Color)傳nil, nil即可,自定義型別需要提供add和lerp。tween.FromCurrent(get, set, target, d, ease)通過get讀取起始值,適用於不是普通欄位的值。To和FromCurrent接受可選的lerp引數用於自定義型別;省略時保持起始值並在結束時跳到目標值,與New一致。- 迴繞時間軸(或對補間呼叫
Reset)會清除已捕獲的起始值,下一次執行重新讀取。
用 Sequence 串聯步驟
Sequence 無需手工計算起始時間即可構建 Timeline:Then 把步驟接到前一塊之後,With 讓步驟與最後一步並行,Wait 插入間隔,Call 在游標處安排回撥,Loops 設定迴圈次數:
seq := tween.Sequence().
Then(tween.To(&coin.Scale, 1.4, 80*time.Millisecond, tween.BackOut)).
With(tween.By(&coin.Y, -40, 300*time.Millisecond, tween.QuadOut, nil, nil)). // 與最後一步並行
Wait(100 * time.Millisecond).
Then(tween.FromCurrent(getGlow, setGlow, 0, 200*time.Millisecond, tween.ElasticOut)).
Call(func() { score.Add(1) }).
Loops(3)
var group tween.Group
group.AddTimeline(seq.Build())
group.SetSpeed(0.25) // 慢動作
group.SetPaused(true)
// 在 game.Loop.Update 中:
group.Update(dt)
With與最後一個Then的步驟同時開始;游標推進到並行步驟中較長的那個的結束點。Loops(n):n <= 0無限迴圈,n == 1播放一次,更大的n額外迴繞n-1次。每次迴圈都完全重播所有步驟,保持確定性。Timeline.SetLoops(n)在任意時間軸上同樣有效,Timeline.SetSpeed(s)縮放其步長;Group.SetSpeed與Group.SetPaused統一縮放或凍結組內所有補間與時間軸(用於慢動作或隨遊戲暫停)。Group.Bind(cx)在作用域結束時清空補間組。
使用 Timeline 編排多軌動畫
Timeline 在同一條確定性時鐘上協調多段補間與定時回撥,支援播放、暫停、倒放、迴圈和精確拖拽(Seek):
tl := tween.NewTimeline().
Add(0, tween.Float32(0, 1, 400*time.Millisecond, core.EaseOut, setFade)).
Append(tween.Point(startPos, endPos, 800*time.Millisecond, core.EaseInOut, setPos)).
Call(600*time.Millisecond, func() { playSfx("whoosh") })
tl.Play()
// 在 game.Loop.Update 中:
tl.Update(dt)
Add(at, tw)將tw放到偏移量at處;Append(tw)將tw追加到時間軸當前的Duration()末尾。Call(at, fn)在播放跨越時間戳at時觸發fn(呼叫Seek時不觸發)。- 通過
Play()、Pause()、Restart()、Reverse(bool)、SetLoop(bool)或SetLoops(n)(按次數迴圈)、SetSpeed(s)和Seek(at)控制回放;Seek會把每條軌道(無論處於區間前、中、後)精確設到at時刻對應的狀態。
相機運鏡輔助函式
內建相機補間直接操作 *tilemap.Camera,並在設定了 cam.Bounds 時自動限位:
// 平移到 Boss 區域、拉近鏡頭,最後平滑切回跟隨玩家
intro := tween.NewTimeline().
Add(0, tween.CameraPan(cam, boss.X, boss.Y, 900*time.Millisecond, core.EaseInOut)).
Add(200*time.Millisecond, tween.CameraZoom(cam, 1.5, 700*time.Millisecond, core.EaseOut)).
Append(tween.CameraZoom(cam, 1.0, 500*time.Millisecond, core.EaseInOut)).
Append(tween.CameraFollowTransition(cam, func() core.Point {
return player.Pos
}, 600*time.Millisecond, core.EaseInOut))
intro.Play()
可用的相機補間建構函式:
| 輔助函式 | 動畫屬性 |
|---|---|
CameraPan(cam, toX, toY, d, curve) |
將 cam.X、cam.Y 從起始位置平移至 (toX, toY)。 |
CameraZoom(cam, toZoom, d, curve) |
將 cam.Zoom(<= 0 視為 1)縮放至 toZoom。 |
CameraRotate(cam, toRot, d, curve) |
將 cam.Rotation(弧度)旋轉至 toRot。 |
CameraMove(cam, toX, toY, toZoom, toRot, d, curve) |
同時驅動位置、縮放與旋轉。 |
CameraFollowTransition(cam, targetFn, d, curve) |
從相機起始位置平滑過渡到持續移動的 targetFn(),結束時精確落在該目標上。 |
每個相機補間在首次執行時捕獲相機當前的位置、縮放或旋轉。呼叫 Reset() 會清除已捕獲的起始狀態,使重播時從相機當前狀態出發;而 Seek(0) 會保留已捕獲的起始狀態,使拖拽與時間軸重啟保持確定性。