# Spin feel

> Tune how a spin starts and stops: the stagger between reels, a wind-up, the kick on landing.

Source: https://pixi-reels.schmooky.dev/mechanics/feel/

A speed profile holds the numbers that decide how a spin feels, such as the
acceleration, the spin speed, the stagger between stops and the bounce.

## The three defaults

```ts
import { SpeedPresets } from 'pixi-reels';

// SpeedPresets.NORMAL        relaxed feel
// SpeedPresets.TURBO         faster stop, short acceleration
// SpeedPresets.SUPER_TURBO   almost instant
```

A builder with no `.speed()` call gets `NORMAL` under the name `'normal'`.

## Switch at runtime

```ts
reelSet.speed.addProfile('turbo', SpeedPresets.TURBO);
reelSet.setSpeed('turbo');
reelSet.events.on('speed:changed', (p, prev) => console.log(p.name, prev.name));
```

## Custom profile

```ts
const CINEMATIC: SpeedProfile = {
  name: 'cinematic',
  spinDelay: 280,
  spinSpeed: 38,
  stopDelay: 180,
  anticipationDelay: 1200,
  bounceDistance: 14,
  bounceDuration: 260,
  accelerationEase: 'power3.in',
  bounceEase: 'elastic.out(1, 0.5)',
  accelerationDuration: 420,
  minimumSpinTime: 1400,
};

builder
  .speed('cinematic', CINEMATIC)
  .initialSpeed('cinematic');
```

The engine fires events for each moment of a round, and your audio engine
plays sounds. `attachReelSounds` connects the two with a table that says
which sound plays on which event, when a loop starts and when it fades out.

## The cue table

```ts
import { createEngine } from '@schmooky/zvuk';
import { attachReelSounds, zvukPort } from 'pixi-reels';

const engine = createEngine({ buses: { reels: {}, anticipation: {}, wins: {}, ui: {} } });
await engine.unlock(); // from a gesture
await engine.loadSound('spin', ['/sfx/spin.webm', '/sfx/spin.m4a'], { bus: 'reels' });
await engine.loadVariants('reel-stop', [
  ['/sfx/reel-stop.1.webm', '/sfx/reel-stop.1.m4a'],
  ['/sfx/reel-stop.2.webm', '/sfx/reel-stop.2.m4a'],
], { bus: 'reels' });
// ... anticipation, skip, win, pop

const sounds = attachReelSounds(reelSet, zvukPort(engine), {
  spin: { play: 'spin' },
  reelStop: { play: 'reel-stop', pitch: { jitter: 0.04 },
              delay: ({ profile }) => profile.bounceDuration / 2000 },
  anticipation: { loop: 'anticipation', fadeIn: 0.12, fadeOut: 0.25,
                  pitch: ({ order }) => 1 + 0.08 * order },
  skip: { play: 'skip' },
  win: { play: 'win' },
  cascadeDestroy: { play: 'pop' },
});

sounds.destroy(); // when the reel set goes
```

The demos on this site run a table like this one. `reelSounds(reelSet)` from
`@pixi-reels/kit/sound` attaches it to a set, using one audio engine for the
whole page, and `boardSounds(board)` does the same for a Hold & Win board.
The Sound button under a demo mutes it. The files load on the first press.

The spin cue plays once per spin. A loop under every spin
(`spin: { loop: 'spin-loop', fadeIn: 0.05, fadeOut: 0.08 }`) is supported and
fades out on the last landing, but on a page with many demos it gets
tiring. The reel stop sounds are enough to mark the rhythm.

## The beats

| Cue | Starts on | A loop ends on | Context |
|---|---|---|---|
| `spin` | `spin:start` | `spin:allLanded` | |
| `reelStop` | `spin:reelLanding`, on every landing path, cascade refills included | | `reelIndex` |
| `anticipation` | `anticipation:reel`, one per teasing reel | that reel's `anticipation:reelEnd` | `order`, `total`, `reelIndex` |
| `skip` | `skip:requested` | | |
| `win` | `spotlight:start`, and `win:group` per presented win | | |
| `cascadeDestroy` | `cascade:destroy:start` | | |
| `on[event]` | the named event | its `until` event | |

Every context also carries `speed` (the active profile's name), `profile`
(the profile itself) and `mode` (`'standard'`, or `'cascade'` for a tumble
and its refills).

The default table the demos run uses `on` for events outside the spin
lifecycle. It plays a shove on `nudge:start`, a tick on `column:set` when a
symbol splits or a column changes, a rising sweep when a set gains cells or
reels (`expand:stepAdded`, `reels:added`, `rows:added`), and a soft pop on
`pin:placed`.

## A Hold & Win board

`attachBoardSounds(board, port, cues)` is the same table over a board's
beats: `featureEnter`, `respinStart` (a loop that runs until `respin:end`),
`cellLanded` (with `reelIndex`, `cell` and `hit`), `coinLocked`, `boardFull`,
`featureEnd`, and `on` for any other board event. The demos play a quiet
kick per respin, a thud when a coin lands, a ring a moment after it locks,
and the win sound once when the feature ends. Empty cells land without a
sound.

A cue is `{ play: name }` for a one-shot or `{ loop: name, fadeIn, fadeOut }`
for a bed. `volume` and `pitch` take a number, a `{ base, jitter }` spread so
repeated thuds do not sound cloned, or a function of the context. The tease
cue above climbs about a semitone per reel, and a spin cue can read `speed`
to pitch up in turbo. `delay` is the time in seconds between the event and the
sound. The demos delay the thud by half the profile's `bounceDuration`, which
is when the reel reaches the bottom of its bounce. Without the delay it would
play on the frame the reel snaps. `when(ctx)` keeps a cue quiet. On a tumble, where the
symbols drop in one by one, the demos play the kick and the thuds at half
level with no delay.

Loops are tracked per event, so there is one tease loop per reel. They fade out
when their beat ends and stop at once on `destroy()` or when the reel set is
destroyed.

## Any engine

`attachReelSounds` only needs a `ReelAudioPort`: a `play(name, { bus, volume,
pitch })` method and a `loop(name, { ..., fadeIn })` method that returns
`{ stop({ fade }) }`. `zvukPort` is that port over a zvuk engine. A variants
set plays its next take, and a missing name is skipped with one warning. Wrap
Howler or raw Web Audio the same way.

## Demos

These demos change how a spin starts and stops. The first two react to a reel landing. The rest edit the step lists the built-in phases run.

## Landing

### Landing events

`spin:reelLanding` fires on the frame a reel reaches its result: the strip has snapped, every symbol has been told it landed, and the bounce is about to start. `spin:reelLanded` fires once the bounce has settled, a whole `bounceDuration` later, or on the same tick after a slam. Hang anything tied to the landing itself on the first one. The demo grows a takeover plate over the copy reels on `spin:reelLanding` and prints the gap between the two events.

Live demo (landing-signal): https://pixi-reels.schmooky.dev/mechanics/feel/#landing-the-frame-and-the-beat

### Spine landing animation

Each symbol hears the same moment through `onReelLanded(ctx)`, with the reel and cell it landed in. A Spine symbol's `autoPlayLanding` can be a function of that context. It returns `true` for the configured landing animation, `false` for none, or an animation name to play something else. Here the copy reels land with no animation, so nothing competes with the takeover, and the last two reels play a reveal as their landing.

Live demo (spine-landing-rule): https://pixi-reels.schmooky.dev/mechanics/feel/#spine-landing-rule

## Editing the step lists

The built-in phases run named step lists. `StartPhase` runs `delay, launch, pull, accelerate, announce`, `StopPhase` runs `delay, spinOut, land, bounce`, and `AnticipationPhase` runs `tease`. Registering a phase with `f.register('stop', StopPhase, { steps })` passes you the list before it runs. Each demo below changes it with one `insertBefore`, `insertAfter` or `replaceStep` call, or builds the list by hand. Slam and quicken keep working because the waits stay marked `cut`. The step API is on the [Phases page](/reference/api-phases/#steps).

### Landing kick

One step after `land` and before `bounce`. The reel set kicks and a puff of dust spreads under the reel, harder on the last one. The step returns nothing, so the bounce starts on the same frame. If it returned the tween, the phase would wait for it before bouncing. That return value is what decides whether a step is an effect or a beat.

A slam lands the reel through `onSkip`, not through the list, so a step after `land` does not run on a slam. Effects that must also play on a slam belong on `spin:reelLanded`. A quicken keeps the list.

Live demo (steps-land-impact): https://pixi-reels.schmooky.dev/mechanics/feel/#a-landing-kick-with-dust

### Stagger curves

The flat stagger arrives as `ctx.config.delay`, and the `delay` step is the only place it is used. So a curve is one `replaceStep` per phase. Here the start ripples out from the middle reel, and the gaps between stops grow, so reels 1 to 3 land quickly and reel 5 hangs. The profile still sets the unit. The second line of the readout shows measured landings, not the schedule.

Keep `{ cut: true }` on the replacement. It is a wait, and a quicken press has to cut it the same way it cuts the flat one.

Live demo (steps-stagger-curve): https://pixi-reels.schmooky.dev/mechanics/feel/#steps-stagger-curve

### Wind-up launch

The built-in `pull` moves the reel back two pixels. This demo replaces it with a pull of most of a cell, inserts a `hold` after it, and leaves `accelerate` as it is, so the reel accelerates from wherever the pull left it. Tap the readout to turn the wind-up off and compare.

Handle both motion models the way the built-in step does. Under the drive model, set a `targetSpeed` and wait. Under the tween model, tween `reel.speed`. `reel.hasDrive` tells you which one is active.

Live demo (steps-windup-start): https://pixi-reels.schmooky.dev/mechanics/feel/#a-wind-up-launch

### Extra wait on the last reel

Every reel runs the same list, so a beat for one reel is a step that does nothing on the others. `suspense` waits 700 ms before reel 5's spin-out and returns at once on the other reels. The backlight pulses while it waits.

A step that starts something has to stop it on every way out. `ctx.wait` resolves early when the step is cut, and `ctx.signal` fires when it is cancelled, so one `stop` call covers the natural end, a quicken and a slam.

Live demo (steps-last-reel-suspense): https://pixi-reels.schmooky.dev/mechanics/feel/#one-extra-beat-on-the-last-reel

### Spotlight around the tease

`AnticipationPhase` runs one step and is created only on the teased reel. To shade the other reels while the tease runs, build the list by hand around the built-in step: `[dim, ...steps, lift]`. Both added steps are awaited, 150 ms each, so the shade is on before the reel starts to crawl and off before the stop phase takes over.

A slam cancels the list in flight and `lift` never runs, so also clear the shades on `spin:complete`. A quicken cuts the `tease` step and `lift` still runs.

Live demo (steps-anticipation-spotlight): https://pixi-reels.schmooky.dev/mechanics/feel/#a-spotlight-around-the-tease

## Steps that read the speed profile

### Per speed mode

`ctx.profile` is the profile the game registered under the active speed name, custom fields included. A `breath` step before the spin-out and a `glow` step after `land` both read it, so normal gets both and turbo gets neither, and the glow's fade follows `bounceDuration`. Tap the readout to switch speeds. The skip button here calls `requestSkip({ mode: 'quicken', speed: 'turbo' })`, which switches the profile the same way.

Do not check for turbo inside a step. Put the decision on the profile, and every speed mode you add later carries its own setting.

Live demo (steps-fx-per-profile): https://pixi-reels.schmooky.dev/mechanics/feel/#beats-and-fx-that-follow-the-speed-mode

### Profile sections

The demo above puts a step's settings on the profile as flat fields. That works, but every phase sees them and nothing says which phase they belong to. A section does. Both steps here are inserted into `StopPhase`, so their settings live under `stop`, keyed by step name. The `whenAnticipated` half is merged on top for a reel that teased, so reels 4 and 5 land with a deeper bounce and a heavier impact, and the same step draws them amber based on `ctx.anticipated`. Nothing checks the speed name.

```ts
stop: {
  steps: { breath: { duration: 140 }, impact: { lift: 8, duration: 420 } },
  whenAnticipated: { bounceDistance: 30, steps: { impact: { lift: 24 } } },
}
```

A step reads its own entry as `ctx.step`, in segment order. One object in the profile arrives as one entry; a list is for a step written to read several segments. Declare the two names once by merging them into `StopSteps`, and `stop.steps` is type-checked and autocompleted like the built-in steps.

`whenAnticipated` applies only to a tease that ran to the end. Press skip mid-tease and that reel uses the section's base settings (blue puff, small lift), because the press cut the tease the variant is there to follow.

Live demo (steps-profile-sections): https://pixi-reels.schmooky.dev/mechanics/feel/#a-step-you-added-tuned-from-the-profile
