ludic/examples/library/anim_sugar.ludic
Orkuncakilkaya 1f5e3c1c1a
All checks were successful
bootstrap / cfree-fixpoint (push) Successful in 18s
ci / build-and-test (push) Successful in 1m21s
commit-lint / conventional-commits (push) Successful in 5s
docs / build-and-deploy (push) Successful in 20s
feat(anim): animation ergonomics — named clips, Anim.play/Motion.to, frame events, fluent Tween handles (#48)
The ergonomic layer over the engine-owned SpriteAnim/Motion systems (#43):

- Named clips: Anim.clip("run", frames, fps, mode) registers a clip by name and
  Anim.play(entity, "run") plays it; Anim.play(entity, fps, frames, mode) sets
  the clip directly. A name-keyed registry in systems.ludic.
- Frame events: Anim.on_frame(entity, frame) arms optional SpriteAnim
  event_frame/event_fired fields; the engine flags the tick the clip first lands
  on that frame, and Anim.fired(entity) reads it — the game reacts, so it stays
  inside the no-runtime-dispatch event model.
- Motion.to(entity, from, to, dur, ease) starts a value tween over the Motion
  component in one call (reflection-ABI writes, resetting the timer).
- Fluent Tween handles (runtime/native/tween.ludic): Tween.to / Tween.chain /
  Tween.delay build a sequenced, disposable handle advanced by a new engine-owned
  system (esys_tween, run each Update tick); Tween.value / Tween.done /
  Tween.parallel / Tween.stop read and control it. The 1-arg Tween.done(handle)
  is disambiguated from the 2-arg pure Tween.done(timer, dur).

Splicing: g_uses_anim_rt pulls in systems.ludic; g_uses_tween_rt pulls in
tween.ludic and inserts esys_tween into the Update phase. All integer and
deterministic, so animation and motion reproduce exactly under replay/lockstep.

Worked example + regression: examples/library/anim_sugar.ludic
(4 8 2 1 0 100 100 0 0 1 20 20 30 0 1). Twelve new docs pages (Anim, the new
Motion namespace, Tween handles). Full suite 77 passed, self-host C-free fixpoint
intact, no golden drift.

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

84 lines
3.7 KiB
Text

# anim_sugar.ludic — the animation ergonomics from #48, layered over the
# engine-owned SpriteAnim / Motion systems (#43) and the fixed frame clock:
#
# * Anim.clip / Anim.play — named spritesheet clips ("run") and one-call
# (re)start, direct or by name
# * Anim.on_frame / fired — a frame event the engine flags, gameplay reacts to
# * Motion.to — start a value tween over the Motion component
# * Tween.to/chain/delay — a fluent, engine-advanced tween handle
# * Tween.parallel/done — completion queries over handles
#
# Everything is integer + deterministic (the clock ticks 60/s), so a full run
# prints: 4 8 2 1 0 100 100 0 0 1 20 20 30 0 1
program AnimSugar {
property SpriteAnim { ticks: int = 0, fps: int = 0, frames: int = 0, mode: int = 0, frame: int = 0, event_frame: int = 0, event_fired: int = 0 }
property Motion { ticks: int = 0, dur: int = 0, from: int = 0, to: int = 0, ease: int = 0, value: int = 0, done: int = 0 }
model Sprite { SpriteAnim }
model Mover { Motion }
function tick_n(n: int) -> void { var i = 0; while i < n { tick_fixed(); i = i + 1 } }
function bi(b: bool) -> int { if b { return 1 }; return 0 }
entry {
Anim.clip("run", 4, 12, 0) # register a 4-frame, 12fps loop
let sa = World.prop_id("SpriteAnim")
let saf = World.field_id(sa, "frame")
let sfr = World.field_id(sa, "frames")
spawn Sprite { SpriteAnim { } }
let sprite = World.query_next(sa, 0)
Anim.play(sprite, "run") # named clip -> frames = 4
print(World.get(sprite, sa, sfr)) # 4
Anim.play(sprite, 6, 8, 1) # direct: fps 6, frames 8, once
print(World.get(sprite, sa, sfr)) # 8
# frame event: arm frame 2 on the looping "run" clip, then tick onto it
Anim.play(sprite, "run")
Anim.on_frame(sprite, 2)
tick_n(10) # frame = (10*12/60) % 4 = 2
print(World.get(sprite, sa, saf)) # 2
print(bi(Anim.fired(sprite))) # 1 — landed on the armed frame
tick_n(1)
print(bi(Anim.fired(sprite))) # 0 — no new landing
# Motion.to: one call starts a tween over the Motion component
spawn Mover { Motion { } }
let mo = World.prop_id("Motion")
let mov = World.field_id(mo, "value")
let mover = World.query_next(mo, 0)
Motion.to(mover, 0, 100, 10, 0) # linear 0..100 over 10 ticks
tick_n(10)
print(World.get(mover, mo, mov)) # 100
# Tween fluent handle: 0->100, then 100->0
var h = Tween.to(0, 100, 10, 0)
h = Tween.chain(h, 0, 10, 0)
tick_n(10)
print(Tween.value(h)) # 100 — end of first segment
print(bi(Tween.done(h))) # 0 — chained segment pending
tick_n(10)
print(Tween.value(h)) # 0 — end of chained segment
print(bi(Tween.done(h))) # 1
# Tween with a delay in the middle, and a parallel completion query
var g = Tween.to(10, 20, 10, 0)
g = Tween.delay(g, 5)
g = Tween.chain(g, 30, 10, 0)
tick_n(10)
print(Tween.value(g)) # 20 — first segment done
tick_n(5)
print(Tween.value(g)) # 20 — held through the delay
tick_n(10)
print(Tween.value(g)) # 30 — chained segment done
let p = Tween.to(0, 5, 4, 0)
let q = Tween.to(0, 9, 8, 0)
tick_n(4)
print(bi(Tween.parallel(p, q))) # 0 — q still running
tick_n(4)
print(bi(Tween.parallel(p, q))) # 1 — both done
quit()
}
}