# Animation and posing

Make things move without writing a line of script for each step: clips that play frames of a sheet,
tweens that take a number somewhere over a time, specks from an emitter, and in 3D the
poses of a model's own clips. All of it is world state, so a recording, a seek and a
checksum see it. Every field is in the [animation reference](../reference/animation.md).

Animation runs after the scripts in each step. What a script decides is on the frame
drawn after that step, and an event the animation sends (a clip's footstep, a tween's
`done`) is read by scripts in the next step.

## The sample

The clips sample walks a sprite across and back with a
tween, plays its walk clip, puffs dust at each footstep the clip sends, and fades a
banner in halfway:

```toml
# example: clips/game.toml
# The animation guide's game: a clip that plays a walk from a sheet and sends an event on
# each footfall, tweens that carry a thing and fade a banner in, and specks from an
# Emitter on each footfall. The script decides when; the animation stage moves things.
#
#   frost dev game.toml
#   frost run --headless --steps 180 --state game.toml
scene = "scene.toml"
scripts = ["walk.luau"]
seed = 1
replay = { steps = 180, checksum = "fdc8b0822819e33f", engine = "0.0.1 r280" }
# The run's checksum on the way there, written by `frost test --write`: a test that
# fails says between which two of these steps the game first went another way.
checkpoints = []

# Four frames of 16 by 16 pixels from the left of hero.png, ten a second. Frames 1 and
# 3 send `footstep` about the thing as they come up.
[clips]
walk = { image = "hero.png", frame = [16, 16], count = 4, fps = 10, events = [{ frame = 1, name = "footstep" }, { frame = 3, name = "footstep" }] }

[window]
size = [320, 180]
background = 0x1b2030
```

```toml
# example: clips/scene.toml
[hero]
Transform = { x = 40, y = 40 }
Sprite = { image = "hero.png", w = 32, h = 32, layer = 2 }
Clip = { name = "walk" }

# Dust where the feet fall: a small puff that rises and fades.
[dust]
Transform = { x = 40, y = 26 }
Emitter = { life = 0.5, speed = 25, spread = 60, gravity = -20, size = 3, size_end = 0, color = 0xc8b89a, color_end = 0x1b2030, add = 0, layer = 1, seed = 4 }

# Opacity must be in the scene for a tween to fade it later.
[banner]
Transform = { x = 160, y = 140 }
Text = { text = "ACROSS", size = 2, color = 0xf5c542 }
Opacity = { alpha = 0 }
```

```lua
-- example: clips/walk.luau
-- A tween carries the hero across and back for ever; its clip walks by itself. Each
-- footstep the clip sends (read a step later: animation runs after the scripts) puffs
-- dust under the hero and is counted. Halfway there, the banner fades in.
state.steps = state.steps or 0

function on_scene(name)
    tween(find("hero"), "Transform", "x", 280, 2.5, { ease = "in_out_quad", loop = "pingpong" })
end

function step(dt)
    local hero = find("hero")
    for _, footstep in ipairs(events("footstep")) do
        state.steps += 1
        local dust = find("dust")
        set(dust, "Transform", "x", get(hero, "Transform", "x"))
        emit(dust, 4)
    end
    local banner = find("banner")
    if get(hero, "Transform", "x") > 160 and get(banner, "Opacity", "alpha") == 0 then
        tween(banner, "Opacity", "alpha", 1, 0.5, { ease = "out_cubic" })
    end
end
```

## Clips

A clip is a run of equal frames on one picture, left to right, declared once under
`[clips]`: `frame` is a frame's size in pixels, `at` where the first one is, `count` how
many and `fps` how fast. A thing with a `Sprite` and `Clip = { name = "walk" }` plays it:
the engine writes the sprite's `src_*` (and its `image`, when the clip names one) every
step.

- A clip loops unless it says `loop = false`; then it holds its last frame, and `end =
  "landed"` sends that event once when it is over.
- `events = [{ frame = 1, name = "footstep" }]` sends an event about the thing each time
  that frame comes up.
- To change clip, set `name` and set `time` to 0. Setting only `name` carries on from the
  same moment, which keeps a run and a run-with-sword in step.
- `speed` 0 holds the frame, 2 plays twice as fast. `frame` is the engine's to write: read
  it, do not write it.

## Tweens

`tween(entity, "Transform", "x", 280, 2.5, { ease = "in_out_quad" })` takes one number
field from what it is to 280 over two and a half seconds. Options: `ease` (one of
`linear`, `in_quad`, `out_quad`, `in_out_quad`, `in_cubic`, `out_cubic`, `in_out_cubic`,
`out_back`, `out_bounce`), `from`, `loop` (`"restart"` or `"pingpong"`, for ever) and
`done`, an event sent when it ends. A tween is a thing of its own: `tween` returns it and
`despawn` stops it. When it ends it writes `to` exactly and is gone. Of two tweens on one
field, the later one wins.

A tween cannot move the `Transform` of an interface node the engine places (`E1306`):
tween its `Anchor.dx`, `Scale` or `Opacity` instead.

## Specks

An `Emitter` describes specks: how long they live, how fast and how widely they leave,
their gravity, size and colour from start to end, whether they add light or cover.
`emit(entity, n)` lets `n` of them go and returns a `Burst`, the one thing that holds
them all, so a thousand specks are not a thousand things. The `seed` picks directions and
speeds, so equal seeds emit equal patterns and a run replays the same dust. At most 4,096
are alive at once (`E0560`). They are untextured squares.

## Spinning

`Spin = { rate = 90 }` turns a thing at a steady rate with no script: in 2D its
`Rotation.angle`, in 3D about its own axis (`axis_x`, `axis_y`, `axis_z`).

## 3D models: poses

A glTF model's own clips play with `Pose = { clip = "walk" }` beside its `Mesh`: set
`clip` to play another, `time` to 0 to start again, `speed` 0 to hold, and `fade` to mix
the pose it was in into the new one over that many seconds. `[poses]` in the project file
declares events a model's clip sends at moments of it, read by scripts in the next step.

`Gait` picks the clip for a walking thing by how fast it goes over the ground: `idle`
below `walk_at` metres a second, `walk` from there, `run` from `run_at`, each played at
the speed it was made for, with a fade between.

A script can turn one joint of a posed model itself: a `Bone = { of = "hero", joint =
"head", qx, qy, qz, qw }` thing overrides that node after the clip, and `bone(thing,
"hand")` reads where a joint is in the world. `Parent = { of = "hero", joint = "hand" }`
holds a sword in the hand. The engine's own `pose` example has an arm,
a held box and a light at its tip.

Not yet: sharing one clip between models that have one rig, and hiding a model's node.
Bending posed models on the graphics device is [coming next](../soon/gpu-skinning.md).

## Checks

```sh
frost check game.toml
frost run --headless --steps 180 --state game.toml   # state: {"walk.luau":{"steps":15}}
frost test game.toml
```

`--state` prints what the script counted: fifteen footsteps in three seconds of a clip
that sends two every four frames at ten frames a second, back and forth.
