ludic/runtime/native/tween.ludic

210 lines
8.2 KiB
Text

# ============================================================================
# tween.ludic — fluent, stateful tween handles the engine advances (#48).
#
# The pure Tween.* namespace (emit_anim.ludic) interpolates a single value from a
# game-managed timer. This adds the *sequenced* layer the follow-up asked for: a
# disposable handle that chains several segments — tween, delay, tween — and that
# the engine advances one tick per frame, so a game fires a multi-step motion once
# and just reads the current value:
#
# var h = Tween.to(0, 100, 30, 2) # ease-out 0 -> 100 over 30 ticks
# h = Tween.delay(h, 15) # hold 15 ticks
# h = Tween.chain(h, 0, 30, 1) # then ease-in 100 -> 0 over 30 ticks
# # ...each frame the engine advances it...
# sprite.x = Tween.value(h)
# if Tween.done(h) { ... }
#
# Everything is integer + Q16.16-free (the ease curves run in 0..1024 fixed-point,
# like Motion), so a sequence plays identically on every run and headless — free
# replays and lockstep. The handle pool is fixed-size; Tween.to reuses a finished
# slot, so a game that starts and finishes tweens forever never runs out.
# ============================================================================
const TW_MAX: int = 64 # concurrent handles
const TW_SEGS: int = 8 # segments per handle (chain depth)
# per-handle state
export state RtTweenState {
tw_used: words = null # slot in use (1) or free (0)
tw_seg: words = null # index of the segment currently playing
tw_nseg: words = null # number of segments queued
tw_tick: words = null # ticks elapsed inside the current segment
tw_value: words = null # OUTPUT: the value this frame
tw_done: words = null # OUTPUT: 1 once every segment has finished
tw_kind: words = null # 0 = tween (from->to), 1 = delay (hold)
tw_from: words = null
tw_to: words = null
tw_dur: words = null # segment length in ticks
tw_ease: words = null # 0 linear, 1 in, 2 out, 3 in-out (Motion's curves)
}
# per-segment state (flat: handle * TW_SEGS + seg)
function tw_init(rt_tween_st: mut RtTweenState) -> void {
if rt_tween_st.tw_used == null {
rt_tween_st.tw_used = words(TW_MAX)
rt_tween_st.tw_seg = words(TW_MAX)
rt_tween_st.tw_nseg = words(TW_MAX)
rt_tween_st.tw_tick = words(TW_MAX)
rt_tween_st.tw_value = words(TW_MAX)
rt_tween_st.tw_done = words(TW_MAX)
rt_tween_st.tw_kind = words(TW_MAX * TW_SEGS)
rt_tween_st.tw_from = words(TW_MAX * TW_SEGS)
rt_tween_st.tw_to = words(TW_MAX * TW_SEGS)
rt_tween_st.tw_dur = words(TW_MAX * TW_SEGS)
rt_tween_st.tw_ease = words(TW_MAX * TW_SEGS)
}
}
# claim a free handle (a finished or never-used slot). -1 if the pool is full.
function tw_alloc(rt_tween_st: mut RtTweenState) -> int {
tw_init(rt_tween_st)
var i = 0
while i < TW_MAX {
if rt_tween_st.tw_used[i] == 0 { return i }
i += 1
}
# none free: reuse the first finished handle so long-lived games don't leak.
i = 0
while i < TW_MAX {
if rt_tween_st.tw_done[i] != 0 { return i }
i += 1
}
return -1
}
# append one segment to a handle (internal). Silently ignored past TW_SEGS.
function tw_push(rt_tween_st: mut RtTweenState, h: int, kind: int, from: int, to: int, dur: int, ease: int) -> void {
if (h < 0) or (h >= TW_MAX) { return }
let n = rt_tween_st.tw_nseg[h]
if n >= TW_SEGS { return }
let s = h * TW_SEGS + n
rt_tween_st.tw_kind[s] = kind
rt_tween_st.tw_from[s] = from
rt_tween_st.tw_to[s] = to
rt_tween_st.tw_dur[s] = dur
rt_tween_st.tw_ease[s] = ease
rt_tween_st.tw_nseg[h] = n + 1
}
# start a new tween handle: from -> to over `dur` ticks with easing `ease`.
function tween_to(rt_tween_st: mut RtTweenState, from: int, to: int, dur: int, ease: int) -> int {
let h = tw_alloc(rt_tween_st)
if h < 0 { return -1 }
rt_tween_st.tw_used[h] = 1
rt_tween_st.tw_seg[h] = 0
rt_tween_st.tw_nseg[h] = 0
rt_tween_st.tw_tick[h] = 0
rt_tween_st.tw_value[h] = from
rt_tween_st.tw_done[h] = 0
tw_push(rt_tween_st, h, 0, from, to, dur, ease)
return h
}
# chain a tween segment after the handle's current queue: continues from where the
# previous segment ends, so the game only names the new target. Returns the handle
# for fluent chaining.
function tween_chain(rt_tween_st: mut RtTweenState, h: int, to: int, dur: int, ease: int) -> int {
if (h < 0) or (h >= TW_MAX) { return h }
var from = rt_tween_st.tw_value[h]
let n = rt_tween_st.tw_nseg[h]
if n > 0 {
let last = h * TW_SEGS + (n - 1)
if rt_tween_st.tw_kind[last] == 0 { from = rt_tween_st.tw_to[last] } # continue from the previous tween's end
}
tw_push(rt_tween_st, h, 0, from, from + (to - from), dur, ease) # `to` is the absolute target
return h
}
# chain a pause of `ticks` ticks (the value holds). Returns the handle.
function tween_delay(rt_tween_st: mut RtTweenState, h: int, ticks: int) -> int {
if (h < 0) or (h >= TW_MAX) { return h }
tw_push(rt_tween_st, h, 1, 0, 0, ticks, 0)
return h
}
# the value of a handle this frame.
function tween_value(rt_tween_st: RtTweenState, h: int) -> int {
if (h < 0) or (h >= TW_MAX) { return 0 }
return rt_tween_st.tw_value[h]
}
# has every segment of the handle finished?
function tween_done(rt_tween_st: RtTweenState, h: int) -> bool {
if (h < 0) or (h >= TW_MAX) { return true }
return rt_tween_st.tw_done[h] != 0
}
# are two handles both finished? — the completion of a parallel pair. Independent
# handles advance together each frame, so running several at once *is* parallel;
# this is the "all done" query over a pair.
function tween_parallel(rt_tween_st: RtTweenState, a: int, b: int) -> bool {
return tween_done(rt_tween_st, a) and tween_done(rt_tween_st, b)
}
# free a handle immediately (stop and dispose). Its value is frozen where it was.
function tween_stop(rt_tween_st: mut RtTweenState, h: int) -> void {
if (h < 0) or (h >= TW_MAX) { return }
rt_tween_st.tw_used[h] = 0
rt_tween_st.tw_done[h] = 1
}
# floor of a/b for non-negative a (the tick counter only ever rises).
function tw_div(a: int, b: int) -> int {
if b <= 0 { return 0 }
return a / b
}
# the same integer easing curves Motion uses (progress carried in 0..1024).
function tween_ease(t: int, ease: int) -> int {
if ease == 1 { return t * t / 1024 }
if ease == 2 { let u = 1024 - t; return 1024 - (u * u / 1024) }
if ease == 3 {
if t < 512 { return (t * t / 1024) * 2 }
let u = 1024 - t
return 1024 - (u * u / 1024) * 2
}
return t
}
# advance one active handle by a tick: interpolate within the current segment,
# roll over to the next when it ends, latch done past the last.
function tw_advance_one(rt_tween_st: mut RtTweenState, h: int) -> void {
if rt_tween_st.tw_done[h] != 0 { return }
let n = rt_tween_st.tw_nseg[h]
if n == 0 { rt_tween_st.tw_done[h] = 1; return }
var seg = rt_tween_st.tw_seg[h]
if seg >= n { rt_tween_st.tw_done[h] = 1; return }
var tick = rt_tween_st.tw_tick[h] + 1
let s = h * TW_SEGS + seg
let dur = rt_tween_st.tw_dur[s]
let kind = rt_tween_st.tw_kind[s]
if kind == 0 { # a tween segment
var t = 1024
if dur > 0 { t = tw_div(tick * 1024, dur) }
if t > 1024 { t = 1024 }
let te = tween_ease(t, rt_tween_st.tw_ease[s])
rt_tween_st.tw_value[h] = rt_tween_st.tw_from[s] + (rt_tween_st.tw_to[s] - rt_tween_st.tw_from[s]) * te / 1024
}
# (a delay segment holds tw_value unchanged.)
if tick >= dur { # this segment finished: advance
if kind == 0 { rt_tween_st.tw_value[h] = rt_tween_st.tw_to[s] } # rest exactly on the target
seg += 1
rt_tween_st.tw_seg[h] = seg
rt_tween_st.tw_tick[h] = 0
if seg >= n { rt_tween_st.tw_done[h] = 1 }
} else {
rt_tween_st.tw_tick[h] = tick
}
}
# the engine-owned system: advance every active tween handle one tick. Inserted
# into the Update phase when a game uses Tween.to (emit_game.ludic).
function esys_tween(rt_tween_st: mut RtTweenState) -> void {
if rt_tween_st.tw_used == null { return }
var i = 0
while i < TW_MAX {
if (rt_tween_st.tw_used[i] != 0) and (rt_tween_st.tw_done[i] == 0) { tw_advance_one(rt_tween_st, i) }
i += 1
}
}