ludic/selfhost/backend/stdlib/emit_ease.ludic
Orkuncakilkaya e4d1e95dcb
All checks were successful
bootstrap / cfree-fixpoint (push) Successful in 16s
ci / build-and-test (push) Successful in 1m9s
commit-lint / conventional-commits (push) Successful in 3s
docs / build-and-deploy (push) Successful in 18s
feat(stdlib): add Anim.* + Tween.* — deterministic 2D animation & tweening (#5)
Two ECS-native, deterministic namespaces for 2D motion, driven off the fixed
frame clock so replays and lockstep netcode reproduce every frame and every
eased value exactly. Both are pure computed-inline Q16.16 / integer math (no new
runtime, no heap) — the game stores a timer on a component and calls these each
frame, exactly the way Collision.* / Grid.* are used.

Anim.* — spritesheet frame animation:
  - Anim.frame(timer,fps,count) -> int      looping frame index
  - Anim.once(timer,fps,count) -> int       one-shot, clamps on the last frame
  - Anim.pingpong(timer,fps,count) -> int   bounce 0..count-1..0
  - Anim.finished(timer,fps,count) -> bool   has a one-shot run past its end?
  - Anim.duration(fps,count) -> fixed        seconds for one cycle
  - Anim.cell_x/cell_y(frame,cols,cell) -> int  source rect on a grid sheet

Tween.* — value interpolation over a timeline:
  - Tween.progress/loop/yoyo(timer,duration) -> fixed  normalized amount
  - Tween.done(timer,duration) -> bool
  - Tween.ease(t, mode) -> fixed             shape by a literal curve 0..6,
                                             the same curves as Ease.* (now
                                             factored into a shared ease_eval)
  - Tween.number/round/point/tint(from,to,t) blend a fixed / int / Vector / color

The typed blends reuse the existing fixed / Vector / color helpers, and
Tween.ease shares Ease.*'s exact formulas via the new ease_eval(mode,t) — one
source of truth for every easing curve in the engine.

examples/library/anim.ludic asserts 34 cases (frame math, clamping, ping-pong,
cell geometry, timeline clamp/loop/yoyo, rounding, color/vector blends, and
Ease.in == Tween.ease(.,1)); wired into x test (now 62 passed). Docs: Anim +
Tween sections with 16 per-symbol pages, inventory/coverage green. Seed
reseeded; the C-free bootstrap fixpoint holds.

The stateful sugar the proposal sketches (named clips, Anim.play, fluent
Tween.chain/parallel handles, and an auto-injected advance system) is deliberately
left as a follow-up — this lands the deterministic math core both halves stand on.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-31 13:05:06 +03:00

87 lines
4.2 KiB
Text

# emit_ease.ludic — the Ease.* namespace: tween curves over a normalized amount
# t in 0.0..1.0, returning an eased fixed. All pure Q16.16, deterministic. The
# "juice" layer that makes motion feel good (Robert Penner's easings).
#
# The curve math is factored into ease_eval(mode, t) so the Tween.* namespace
# (emit_anim.ludic) can pick a curve by a small integer mode and reuse the exact
# same formulas — one source of truth for every easing in the engine.
# 0 linear 1 in 2 out 3 in_out 4 back 5 elastic 6 bounce
function is_ease_ns(meth: pointer) -> bool {
if (meth == "in") or (meth == "out") or (meth == "in_out") { return true }
if (meth == "back") or (meth == "bounce") or (meth == "elastic") { return true }
return false
}
# n1 * u * u (u a fixed code) -> code of a fixed i32
function ease_bounce_seg(u: pointer) -> pointer {
let uu = fx_mul_code(u, u)
return fx_mul_code(uu, "495616") # 7.5625 * u*u
}
# ease-out bounce: four parabolic segments, selected by t (all computed, then
# picked branch-free). Shifts/offsets are the standard 2.75-denominator set.
function ease_bounce_code(t: pointer) -> pointer {
let sA = ease_bounce_seg(t)
let uB = emit_bind(`sub i32 {t}, 35747`); let sB0 = ease_bounce_seg(uB); let sB = emit_bind(`add i32 {sB0}, 49152`)
let uC = emit_bind(`sub i32 {t}, 53620`); let sC0 = ease_bounce_seg(uC); let sC = emit_bind(`add i32 {sC0}, 61440`)
let uD = emit_bind(`sub i32 {t}, 62557`); let sD0 = ease_bounce_seg(uD); let sD = emit_bind(`add i32 {sD0}, 64512`)
let cCD = emit_bind(`icmp slt i32 {t}, 59578`)
let rCD = emit_bind(`select i1 {cCD}, i32 {sC}, i32 {sD}`)
let cB = emit_bind(`icmp slt i32 {t}, 47663`)
let rB = emit_bind(`select i1 {cB}, i32 {sB}, i32 {rCD}`)
let cA = emit_bind(`icmp slt i32 {t}, 23831`)
return emit_bind(`select i1 {cA}, i32 {sA}, i32 {rB}`)
}
# evaluate easing `mode` at normalized amount `t` (a fixed code) -> fixed code.
# The single source of truth for the engine's easing curves.
function ease_eval(mode: int, t: pointer) -> pointer {
if (mode == 0) { # linear: t
return t
}
if (mode == 1) { # ease-in quad: t*t
return fx_mul_code(t, t)
}
if (mode == 2) { # ease-out quad: t*(2 - t)
let inv = emit_bind(`sub i32 131072, {t}`)
return fx_mul_code(t, inv)
}
if (mode == 3) { # smooth ease-in-out: 3t^2 - 2t^3
let t2 = fx_mul_code(t, t)
let t3 = fx_mul_code(t2, t)
let three = emit_bind(`mul i32 {t2}, 3`)
let two = emit_bind(`mul i32 {t3}, 2`)
return emit_bind(`sub i32 {three}, {two}`)
}
if (mode == 4) { # ease-in-back (overshoots below 0)
let t2 = fx_mul_code(t, t)
let t3 = fx_mul_code(t2, t)
let a = fx_mul_code(t3, "177051") # 2.70158 * t^3
let b = fx_mul_code(t2, "111515") # 1.70158 * t^2
return emit_bind(`sub i32 {a}, {b}`)
}
if (mode == 5) { # ease-out elastic: springy overshoot that settles
g_uses_mathrt = true # 2^(-10t) * sin((10t - 0.75) * 2pi/3) + 1
let tt = emit_bind(`mul i32 {t}, 10`) # 10t
let ntt = emit_bind(`sub i32 0, {tt}`) # -10t (exp2 exponent, Q16.16)
let decay = emit_bind(`call i32 @fn_fx_exp2(i32 {ntt})`)
let ph = emit_bind(`sub i32 {tt}, 49152`) # 10t - 0.75
let ang = fx_mul_code(ph, "137258") # * (2pi/3), 2pi/3 = 137258 fixed
let s = emit_bind(`call i32 @fn_fx_sin(i32 {ang})`)
let osc = fx_mul_code(decay, s)
return emit_bind(`add i32 {osc}, 65536`)
}
# mode == 6 — ease-out bounce
return ease_bounce_code(t)
}
function emit_ease_ns(meth: pointer, e: Node) -> Val {
let t = emit_expr(e.kids[0])
if (meth == "in") { return val(ease_eval(1, t.code), "fixed") }
if (meth == "out") { return val(ease_eval(2, t.code), "fixed") }
if (meth == "in_out") { return val(ease_eval(3, t.code), "fixed") }
if (meth == "back") { return val(ease_eval(4, t.code), "fixed") }
if (meth == "elastic"){ return val(ease_eval(5, t.code), "fixed") }
return val(ease_eval(6, t.code), "fixed") # bounce
}