# Cascades

> Winning symbols burst, the rest fall into the gaps and new ones drop in, until nothing else wins.

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

A cascade round has two different moments. Keeping them apart makes
[`.tumble()`](/reference/api-builder/#tumble) and
[`refill()`](/reference/api-reelset/#cascade-tumble) easier to reason about.

## The two moments

Moment A is the player pressing spin. Everything falls off the bottom, the
reels sit empty while the server thinks, and the new symbols drop in when the
answer arrives.

Moment B is a cascade refill. You clear the winning cells. Survivors above a
hole slide down into it, and new symbols drop in from off-window to fill the
gap at the top, as many per reel as that reel lost. The reels never re-spin.

The same animation engine runs both. The split matters because A is one beat
(fall, wait, drop) with the same shape as `spin()` followed by `setResult()`,
while B is as many beats as there are cascade levels, one per refill until
nothing wins, each starting on whatever the last one landed.

## The three phases

The engine runs both moments with three `ReelPhase` subclasses:

| Phase | Fires on | What it does |
|---|---|---|
| `cascade:fall` | Moment A, the `spin()` call | Tweens every visible symbol off the bottom of the viewport, then waits at speed 0. |
| `cascade:place` | Both moments | Swaps the symbol identities for the new grid and snaps every view to its grid position. Survivors become visible at once. New arrivals stay at alpha 0 until `cascade:dropIn` moves them above the viewport. |
| `cascade:dropIn` | Both moments | Tweens each moving view from its origin (above the viewport for new symbols, its old cell for survivors) down to its grid position. Survivors that did not move skip the tween. |

The per-symbol events (`cascade:fall:symbol`, `cascade:dropIn:symbol`) fire
before the library's own tween, so a tween you start in the listener finishes
in step with it.

The chain and destroy events sit on the same bus:

| Event | When | What it is for |
|---|---|---|
| `cascade:chain:start` | A chain stage opens, after `detectWinners` and before `destroySymbols` | A per-chain sound cue, a chain counter, freezing the spin button. |
| `cascade:destroy:start` / `cascade:destroy:end` | Around every `destroySymbols(...)` batch, direct calls included | A shatter sound, a viewport dim independent of `destroyOptions.dim`. |
| `cascade:chain:end` | A chain stage closes, after the refill drop-in settled | Per-chain teardown, queueing the next decoration. |

`runCascade` emits the chain events. A loop you write yourself with bare
`refill()` calls only gets the per-phase ones.

You can override any phase by registering a subclass under its name:

```ts
builder
  .tumble({ /* ... */ })
  .phases((f) => f.register('cascade:fall', MyCometFallPhase));
```

The other two keep their defaults. Override a phase only when events and
config cannot do what you want. A per-symbol listener is usually enough.

## Survivor order

The grid you pass to a refill follows one rule per reel. The first cells,
as many as that reel had winners, are new symbols. The rest are the
survivors in their original order. A server-side gravity simulation already
produces this.

Take the reel `['A','B','C','D','E']` with winners at cells 1 and 3:

A grid that follows the rule goes straight into `refill({ winners, grid })`.
One that does not needs a transform on the client first.

`computeDropOffsets` is exported. It is the algorithm the engine uses, so
you can check your server output against it offline.

## The three calls

A cascade game needs three calls:

```ts
// 1. Fall, wait, drop. Symmetric to a normal spin.
const spinDone = reelSet.spin();                  // or spin({ mode: 'cascade' }) if you also use standard mode
reelSet.setResult(await server.spin());
await spinDone;

// 2. Clear winning cells. The library defers to each symbol's playDestroy().
const winners = detectWinners(reelSet.getVisibleGrid());
await reelSet.destroySymbols(winners);

// 3. Refill survivors + new symbols.
//    grid is ColumnTarget[]. For the common per-reel visible case, wrap
//    your server's string[][] with .map(visible => ({ visible })). For
//    big-symbol anchors that land into the buffer, use the full
//    ColumnTarget shape with bufferStart / bufferEnd.
const next: string[][] = await server.cascade(winners);
await reelSet.refill({ winners, grid: next.map((visible) => ({ visible })) });
```

That is one win and one refill. For a chain, loop it:

```ts
while (true) {
  const winners = detectWinners(reelSet.getVisibleGrid());
  if (winners.length === 0) break;
  await reelSet.destroySymbols(winners);
  await wait(PAUSE_AFTER_REMOVAL_MS);
  const next = await server.cascade(winners);
  await reelSet.refill({ winners, grid: next.map((visible) => ({ visible })) });
}
```

Or call `runCascade` with your two callbacks. It runs the same loop, fires
the per-stage chain events, and resolves with a `RunCascadeResult` summary.
Both callbacks may be async. `nextGrid` is where you await the server.

```ts
await reelSet.runCascade({ detectWinners, nextGrid });
```

## Per-cascade options

The options a tumble game usually sets:

| Option | Default | When to set |
|---|---|---|
| `pauseAfterDestroyMs` | `250` | Lower it to 150 for a turbo mode, raise it to 500 for a slower round. |
| `maxChain` | `32` | A safety cap. Raise it only if your game can legitimately exceed 32 cascades. |
| `onCascade` | none | Runs once per cascade. Update a multiplier or play a sound here. Return a promise to delay the next refill. |
| `destroyOptions` | `{}` | Forwarded to `destroySymbols(...)`: per-cell stagger, lifted z-index, viewport dim. |
| `signal` | none | An `AbortSignal` that cancels the chain. See [cancelling a chain](#cancelling-a-chain-via-abortsignal). |

```ts
await reelSet.runCascade({
  detectWinners,
  nextGrid,
  pauseAfterDestroyMs: 180,        // snappy
  destroyOptions: {
    delay: (cell, i) => i * 0.03,  // left-to-right disintegration
    dim:   true,                   // dim the viewport while pops play
  },
  onCascade: async ({ chain }) => {
    bumpMultiplier(chain);
    await playWinSound(chain);     // delays the next refill until the sound resolves
  },
});
```

`DestroySymbolsOptions` has four fields: `delay`, `zIndex`, `dim` and
`signal`. The delay can be a function of the cell:

```ts
destroyOptions: {
  delay:  (cell, i) => i * 0.02,   // first-to-last stagger
  zIndex: 1500,                    // lift above the default 1000
  dim:    0.5,                     // custom viewport dim alpha
}
```

## Cancelling a chain

`skipSpin()` slams the phase in flight, but it returns early when the engine
is idle, which it is between two refills and during the pause. A tap in that
window does nothing and the chain keeps going. Use an `AbortController` for
the chain:

```ts
const controller = new AbortController();
skipButton.addEventListener('click', () => controller.abort());

await reelSet.runCascade({
  detectWinners,
  nextGrid,
  signal: controller.signal,
});
// summary.wasSkipped === true after an abort
```

When the signal aborts, `runCascade` sets its skipped flag, calls
`slamStop()` if a refill is in flight so the await unblocks at once, exits at
the next await boundary, and resolves with `wasSkipped: true` in the result.

Wire both to the skip button: `skipSpin()` cuts the phase that is moving,
and the abort ends the chain.

## `refill()` or `runCascade()`?

Start with `runCascade`. It is a short wrapper around `refill` and handles
the skip path for you. Use bare `refill` when the chain needs something the
options do not cover, such as different pauses per level, branching per
level, or a game loop that already owns the chain. Writing it yourself means the loop
above, plus the chain events and the abort handling if you need them.

## Other features in a cascade

The rest of the reel set API keeps working during a cascade:

- `setDropOrder('all')` makes every reel drop together. Set it once before
  `runCascade` and it holds for every refill in the chain.
- `setStopDelays([...])` sets per-reel delays, for a V-shape or an
  outside-in reveal.
- `speed.set('turbo')` changes the strip-spin speed on standard rounds.
  Cascade phase durations are static (see [`TumbleConfig`](/reference/api-builder/#tumble)),
  so for fast cascades you swap the `.tumble({})` config or override the
  phase.
- `pin(...)` persists a cell across cascades. A pinned wild stays put through
  every refill in the round.
- `reelSet.viewport.showDim(alpha)` dims the whole board. Pair it with
  `destroySymbols(..., { dim: true })` to draw the eye to the winners.

## The empty wait

Between `cascade:fall:end` and `cascade:dropIn:start` the board is empty and
the server has not answered. Show a spinner on fall end and hide it on the
first drop-in, or show nothing. A server reply under 100 ms is not noticed.
If the gap still shows, shorten `fall.duration`.

## The pause matters

Leave a pause between the winners fading out and the refill starting.
Without it the refill begins on the frame the winners reach alpha 0, and the
symbols look like they teleported. With 150 to 500 ms between them the
player sees two events, the wins clearing and then the new symbols arriving.
The default is 250 ms. Around 120 ms feels fast, around 350 ms feels
relaxed. Set it with `pauseAfterDestroyMs` in `runCascade()`, or with your
own wait in your own loop.

## Override a phase, or listen to events?

To run something alongside the library's tween, listen to
`cascade:fall:symbol` or `cascade:dropIn:symbol`. They fire first, so your
tween stays in step with theirs.

To decorate symbols after they land, listen to `cascade:place:end`. Its
`isInitial` and `winnerCells` fields let you skip survivors.

To hold a beat between fall and drop-in, override `cascade:place` and put the
beat in its `onEnter`. A listener cannot gate a phase.

To make symbols fly sideways instead of falling, override `cascade:fall`.

Override only when a listener cannot do it. A phase of your own has to
implement the whole `ReelPhase` contract, which is `onEnter`, `update`,
`onSkip` and `_complete`.

## Recipes by purpose

| If you want | Look at |
|---|---|
| The smallest working cascade game | [Basic cascade](/mechanics/cascade/#cascade-6x5) |
| Tumble motion compared | [Tumble motion](/mechanics/cascade/#tumble-feels) |
| Refill orders compared | [Refill order](/mechanics/cascade/#refill-orders) |
| The delay between the press and the fall | [Delay before the fall](/mechanics/cascade/#fall-delays) |
| Cascades after a strip spin | [Opening the round with a spin](/mechanics/cascade/#spin-then-cascade) |
| Cluster wins and cascades on MultiWays | [MultiWays cascade](/mechanics/big-symbols/#multiways-cascade) |
| Win presentation with `WinPresenter` | [Removing winners](/mechanics/cascade/#cascade-destroy-and-present) |
| The longer write-up | [`docs/recipes/tumble-cascade.md`](https://github.com/schmooky/pixi-reels/blob/main/docs/recipes/tumble-cascade.md) |

## Related references

- [`ReelSetBuilder.tumble()`](/reference/api-builder/#tumble), the config fields and their defaults
- [`ReelSet.refill`, `destroySymbols`, `runCascade`](/reference/api-reelset/#cascade-tumble), the signatures
- [Cascade events](/reference/api-events/#cascade-tumble), the payloads

## Demos

A cascade round removes the winning symbols, lets the survivors fall into the holes and drops new symbols in, then checks for wins again. The first demo is that loop with nothing changed. The ones after it each change one part of it.

## Basic cascade

A 6x4 board where `runCascade` runs the loop. You pass `detectWinners` and `nextGrid`, and it destroys, refills and asks again until `detectWinners` returns no cells. The second canvas is the same recipe with Thunderkick Spine symbols; only the symbol registration differs.

Live demo (cascade-6x5): https://pixi-reels.schmooky.dev/mechanics/cascade/#cascade-6x4-tumble
Live demo (cascade-6x5-spine): https://pixi-reels.schmooky.dev/mechanics/cascade/#cascade-6x4-tumble

## Removing winners

Each cascade step runs in a fixed order: present the win, destroy the winning symbols, refill the holes. Work out the winners from your match logic. Diffing the grid before and after does not work, because survivors slide past the cleared cells and a diff reads them as changed.

### Present the win first

Two winning combinations share reel 2. `runCascade` hands them to a `WinPresenter` through its `presentWinners` hook, which shows each one in turn before the winners explode and the refill drops.

Live demo (destroy-winpresenter): https://pixi-reels.schmooky.dev/mechanics/cascade/#cascade-wins-present-destroy-refill

### Default destroy

No `out` animation is registered, so `destroySymbols` plays the engine's scale and fade implode. Use this when you have no destruction art.

Live demo (destroy-fade): https://pixi-reels.schmooky.dev/mechanics/cascade/#cascade-wins-present-destroy-refill

### Spine explosion

The same board with `outAnimation: 'explode'` set at registration. `destroySymbols` plays the skeleton's explosion instead of the implode.

Live demo (destroy-explode): https://pixi-reels.schmooky.dev/mechanics/cascade/#cascade-wins-present-destroy-refill

## Timing and motion

These demos run the same spin and the same win. Only the timing or the motion of the fall and the refill changes.

### Delay before the fall

The pause between the spin press and the first frame of the fall: 0, 150, 350 and 700 ms. A longer pause adds suspense before the board clears.

Live demo (fall-delay-instant): https://pixi-reels.schmooky.dev/mechanics/cascade/#lead-in-delay-before-the-fall
Live demo (fall-delay-subtle): https://pixi-reels.schmooky.dev/mechanics/cascade/#lead-in-delay-before-the-fall
Live demo (fall-delay-anticipation): https://pixi-reels.schmooky.dev/mechanics/cascade/#lead-in-delay-before-the-fall
Live demo (fall-delay-suspense): https://pixi-reels.schmooky.dev/mechanics/cascade/#lead-in-delay-before-the-fall

### Refill order

The order the new symbols arrive in after a win. All reels at once with `setDropOrder('all')`, a left-to-right wave, and bottom-up, where the bottom cell lands first (`cellOrder: 'endFirst'`).

Live demo (refill-simultaneous): https://pixi-reels.schmooky.dev/mechanics/cascade/#cascade-refill-orders
Live demo (refill-ltr-wave): https://pixi-reels.schmooky.dev/mechanics/cascade/#cascade-refill-orders
Live demo (refill-bottom-up): https://pixi-reels.schmooky.dev/mechanics/cascade/#cascade-refill-orders

### Tumble motion

Five `.tumble({ fall, dropIn })` configs on the same round: classic, slow drop, slam, rain column and wave. They differ in durations, eases and per-cell stagger.

Live demo (tumble-classic): https://pixi-reels.schmooky.dev/mechanics/cascade/#tumble-feels-every-preset
Live demo (tumble-cartoon-bounce): https://pixi-reels.schmooky.dev/mechanics/cascade/#tumble-feels-every-preset
Live demo (tumble-slam): https://pixi-reels.schmooky.dev/mechanics/cascade/#tumble-feels-every-preset
Live demo (tumble-rain-column): https://pixi-reels.schmooky.dev/mechanics/cascade/#tumble-feels-every-preset
Live demo (tumble-wave): https://pixi-reels.schmooky.dev/mechanics/cascade/#tumble-feels-every-preset

### Two-stage refill

`refill({ mode: 'gravity-then-drop' })` splits the refill in two. Survivors slide down first, the engine holds for `gravityHoldMs`, then the new symbols drop in. Put a multiplier or a mascot beat in the hold. The second canvas drops the reels one after another instead of in a wave, with a longer hold.

Live demo (tumble-anticipation): https://pixi-reels.schmooky.dev/mechanics/cascade/#cascade-anticipation-refill
Live demo (tumble-anticipation-stagger): https://pixi-reels.schmooky.dev/mechanics/cascade/#cascade-anticipation-refill

## Opening the round with a spin

Many cascade games open each round with an ordinary strip spin and only cascade the follow-ups. The first canvas does that with `spin({ mode: 'standard' })`. The second opens with a cascade drop instead, so the old board falls out and the new one drops in. The third is the spin opener tuned for turbo: the strip spins on cached textures and the cascade uses a shorter drop-in and a faster explode.

Live demo (spin-then-cascade): https://pixi-reels.schmooky.dev/mechanics/cascade/#spin-first-cascade-after
Live demo (cascade-open-drop): https://pixi-reels.schmooky.dev/mechanics/cascade/#spin-first-cascade-after
Live demo (spin-then-cascade-turbo): https://pixi-reels.schmooky.dev/mechanics/cascade/#spin-first-cascade-after

## Gravity and orientation

By default a reel drains toward the edge it travels toward and refills from the other one. Gravity also decides the server contract. Survivors must be packed against the edge they drain through, and the engine animates the grid you send as it is. It never reorders it.

### Sideways

The same tumble on `.orientation('horizontal')`. Reels are screen rows, the strip travels along X, and `tumble({ gravity })` stays on its default `'auto'`. Under forward gravity, `nextGrid` returns `[...fresh, ...survivors]`.

Live demo (cascade-horizontal-tumble): https://pixi-reels.schmooky.dev/mechanics/cascade/#cascade-sideways

### Upward

A `.direction('reverse')` set with no `gravity` key. Gravity follows travel, so the top cell falls and lands first and the column empties upward. The survivors now pack against the top, so `nextGrid` returns `[...survivors, ...fresh]`. If you reverse the reels and keep the old grid order, the board looks scrambled.

Live demo (cascade-drain-upward): https://pixi-reels.schmooky.dev/mechanics/cascade/#drain-upward-gravity-follows-travel

### Spin up, settle down

Reels built `.direction('reverse')` roll upward, and `tumble({ gravity: 'forward' })` makes the tumble settle downward anyway. Set `gravity` only when you want travel and settling to disagree.

The cell stagger follows gravity too. `cellOrder` defaults to `'auto'`, which starts at the end symbols leave through. Here the bottom cell still goes first; without the `gravity` override the top cell would lead. Pass `'endFirst'` or `'startFirst'` to fix the starting end whatever the gravity.

Live demo (cascade-reverse-gravity): https://pixi-reels.schmooky.dev/mechanics/cascade/#spin-up-drop-down-explicit-gravity

## Teaser in the buffer

A strip spin with no cascade. A symbol placed in the `bufferStart` slot sits above the window and scrolls through it at the start of the next spin, so the player sees it briefly before the random symbols take over.

Live demo (peek-from-above): https://pixi-reels.schmooky.dev/mechanics/cascade/#peek-symbol-from-buffer-start
