# Nudge

> Push a reel that has already stopped up or down a few places to pull a symbol into view.

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

A nudge moves one reel a few cells after it has landed, and brings in symbols
you choose. The classic use is a near-miss: the reels land one symbol short,
then one reel nudges by one cell to reveal the symbol that was missing.

```ts
await reelSet.spin();

await reelSet.nudge(2, {
  distance: 1,
  direction: 'forward',
  incoming: ['wild'],
});
```

Reel 2 moves one cell. Everything on it shifts along, the last symbol leaves
the window, and the wild arrives at the front.

Live demo (nudge): https://pixi-reels.schmooky.dev/mechanics/nudge/

## The contract

```ts
interface NudgeOptions {
  /**
   * Number of full positions to shift. Positive integer, strictly less than
   * the reel's total strip capacity (bufferStart + visibleCells + bufferEnd).
   */
  distance: number;
  /**
   *  - 'forward': the way this reel travels; new symbols enter at its
   *    feed edge.
   *  - 'reverse': the other way; new symbols enter at the opposite edge.
   *
   * Relative to the reel, not the screen. On a reel built with
   * direction('reverse'), 'forward' nudges upward.
   */
  direction: 'forward' | 'reverse';
  /**
   * Symbol ids in top-down order of their final on-strip position,
   * including any overflow into the off-screen buffer. Length must equal
   * `distance` exactly.
   */
  incoming: string[];
  /** Tween duration in ms. Default: `200 * distance`. */
  duration?: number;
  /** GSAP ease name. Default: `'power2.out'` (smooth deceleration, no overshoot). */
  ease?: string;
  /** Delay before the tween begins (ms). Sugar for staggered Promise.all. */
  startDelay?: number;
  /** Abort the nudge mid-flight. Strip still snaps to landed position. */
  signal?: AbortSignal;
}

reelSet.nudge(reel: number, options: NudgeOptions): Promise<{ symbols: string[] }>;
reelSet.skipNudge(reel?: number): void;
```

The promise resolves with the reel's new visible cells, in order, which you
can feed straight into win detection. It rejects with an `AbortError` if the
signal aborts or the set is destroyed mid-tween.

## How `incoming` lays out

For a 3-cell reel landed on `['A', 'B', 'C']` (first, middle, last):

| Call | New visible (first to last) |
| --- | --- |
| `nudge(r, { distance: 1, direction: 'forward', incoming: ['X'] })` | `['X', 'A', 'B']` |
| `nudge(r, { distance: 2, direction: 'forward', incoming: ['X', 'Y'] })` | `['X', 'Y', 'A']` |
| `nudge(r, { distance: 1, direction: 'reverse', incoming: ['X'] })` | `['B', 'C', 'X']` |
| `nudge(r, { distance: 2, direction: 'reverse', incoming: ['X', 'Y'] })` | `['C', 'X', 'Y']` |

The first entry always ends up nearest the start of the reel and the last
entry nearest the end.

A nudge further than the visible cell count pushes the overflow into the
buffer on the matching side. With `'forward'`, the trailing entries spill into
the buffer at the exit edge. With `'reverse'`, the leading entries spill into
the other one. Nothing is thrown away. Every entry stays on the strip, ready
for a follow-up nudge or the next spin's opening frame.

A distance equal to or larger than the strip capacity throws, because a full
rotation would silently overwrite a pre-placed buffer entry.

## Sequential or parallel

One nudge moves one reel. When several reels nudge, you choose how to start
them. The engine output is the same either way; only the pacing differs.

### Sequential

Live demo (nudge-sequential): https://pixi-reels.schmooky.dev/mechanics/nudge/#sequential-three-separate-beats

```ts
for (const reel of [1, 2, 3]) {
  await reelSet.nudge(reel, { distance: 1, direction: 'forward', incoming: ['wild'], duration: 480 });
}
```

Each `await` waits for that reel to land, so the player sees reel 1's wild
before reel 2 starts. The total time is the number of reels times the
duration.

### Parallel

Live demo (nudge-parallel): https://pixi-reels.schmooky.dev/mechanics/nudge/#parallel-one-synchronised-beat

```ts
await Promise.all(
  [1, 2, 3].map((reel) =>
    reelSet.nudge(reel, { distance: 1, direction: 'forward', incoming: ['wild'], duration: 480 }),
  ),
);
```

All the tweens start on the same frame and the whole line arrives together.
The total time is one duration, however many reels.

### Stagger

The `startDelay` option gives you a wave without a loop:

```ts
await Promise.all(
  [1, 2, 3].map((reel, i) =>
    reelSet.nudge(reel, { ...opts, startDelay: i * 80 }),
  ),
);
```

The tweens start at 0, 80 and 160 ms and run concurrently. Validation still
throws synchronously at the call site. Only the movement is delayed.

## Big symbols on the reel

A tall block moves as one piece, as long as the whole block is still on the
strip afterwards:

| Direction | Survival condition |
| --- | --- |
| `'forward'` | `anchorStripIdx + h - 1 + distance < total` |
| `'reverse'`   | `anchorStripIdx - distance >= 0` |

Here `total` is the strip capacity (buffer start plus visible cells plus
buffer end) and `anchorStripIdx` counts from 0 at the outermost start-buffer
cell, so the index of visible cell 0 equals the buffer start count.

A block may sit partly in a buffer and still render correctly. The engine
sizes the anchor to span the whole block wherever it lives, and the mask clips
the rest. This is how a tail reveal works: land a tall wild with its anchor in the
buffer, nudge, and the rest of it moves into view.

A block wider than one reel always throws. Nudging one reel would tear the
block off its cells on the next one.

```ts
// A 1x2 wild sits at visible cell 1 with its tail in bufferEnd.
// Nudge back by 1 and the whole block lands at cells 0 and 1.
await reelSet.nudge(2, { distance: 1, direction: 'reverse', incoming: ['filler'] });
```

## Skip and abort

There are two ways to cut a nudge short.

```ts
// 1. Skip: fast-forward to landed. The promise resolves normally.
const p = reelSet.nudge(2, { ...opts });
button.onclick = () => reelSet.skipNudge(2);
const { symbols } = await p;
```

```ts
// 2. Abort: reject with AbortError. The strip still snaps to landed.
const controller = new AbortController();
const p = reelSet.nudge(2, { ...opts, signal: controller.signal });
button.onclick = () => controller.abort();
try { await p; }
catch (e) { if (e.name !== 'AbortError') throw e; }
```

The difference is what happens to the promise. `skipNudge` resolves it, so
your success path runs. An abort rejects it and fires `nudge:cancelled`, so
your error path runs. Skip when the win plays next, and abort when you are
tearing the feature down.

Either way the strip lands where it was going to land. A nudge never stops
half-tweened.

## Events

| Event | Payload | When |
| --- | --- | --- |
| `nudge:start` | `({ reelIndex, distance, direction })` | Pre-placement is done and the tween is about to begin. |
| `nudge:complete` | `({ reelIndex, distance, direction, symbols })` | The strip snapped to the post-nudge grid. `symbols` is the new visible column. |
| `nudge:cancelled` | `({ reelIndex, distance, direction, reason })` | The signal aborted or the reel was destroyed mid-tween. Never fires together with `nudge:complete`; the promise rejected instead. |
| `phase:enter` (per reel) | `('nudge')` | Mirror of `nudge:start`, on the affected reel's bus. |
| `phase:exit` (per reel) | `('nudge')` | Mirror of `nudge:complete`. |

The per-reel `landed` event does not fire on a nudge. It belongs to the spin
pipeline. Listen to `nudge:complete` instead.

```ts
reelSet.events.on('nudge:complete', ({ reelIndex, symbols }) => {
  console.log(`reel ${reelIndex} now reads`, symbols);
  // Re-run win detection on reelSet.getVisibleGrid() here.
});
```

## When `nudge()` throws or rejects

Bad arguments throw at the call. Cancellation rejects later.

It throws synchronously when:

- The reel set is spinning, refilling, or in a cascade.
- `reel` is out of range.
- The target reel has an active pin. Call `unpin` first.
- `distance` is below 1, not an integer, or at least the strip capacity.
- `direction` is neither `'forward'` nor `'reverse'`.
- The length of `incoming` differs from `distance`.
- An incoming id is not registered, or is a big symbol.
- A block on the target reel would not survive the rotation.
- A cell on the target reel belongs to a block wider than one reel.

It rejects with an `AbortError` when:

- The signal aborts before or during the tween.
- `reelSet.destroy()` is called mid-tween.

A nudge touches one reel. The other reels, their pins and any spotlight are
left alone.

## Recipes

- [Single nudge](/mechanics/nudge/), the smallest demo.
- [Spotlight after a nudge](/mechanics/nudge/#nudge-spotlight), a nudge that completes a line, then the win.
- [Big symbols](/mechanics/nudge/#nudge-big-symbol), the block survival rule and the tail reveal.
- [Land a big symbol partially in buffer](/mechanics/big-symbols/#big-symbol-partial-land), land with the tail showing, then nudge to reveal it.
- [Nudge a big symbol in, then hold it across a respin](/mechanics/big-symbols/#big-symbol-held-respin), reveal a buffer-anchored wild, then hold the reel while the others respin.

## Demos

Each demo shifts a reel that has already landed by a few positions and brings in symbols you choose. The first one is a single nudge. The rest cover nudging several reels, presenting the win, big symbols, other reel directions and cutting a nudge short.

## Single nudge

After the reels land, reel 1 nudges `'forward'` by one and a wild enters at the top, then reel 3 nudges `'reverse'` by one and a wild enters at the bottom. `incoming` lists the symbols in their final order on the strip, top to bottom on a vertical set.

Live demo (nudge): https://pixi-reels.schmooky.dev/mechanics/nudge/#nudge-a-reel

## Several reels

The same row of wilds nudged into reels 1 to 3 in three ways. The engine output is the same each time; only the pacing changes.

### One after another

Each `await` waits for the previous reel to land, so the player sees three separate reveals. The total time is the number of reels times the duration.

Live demo (nudge-sequential): https://pixi-reels.schmooky.dev/mechanics/nudge/#nudge-several-reels

### All at once

`Promise.all` starts every tween on the same frame and the wilds arrive together. The total time is one duration.

Live demo (nudge-parallel): https://pixi-reels.schmooky.dev/mechanics/nudge/#nudge-several-reels

### Staggered

`Promise.all` with a per-reel `startDelay`. The tweens run at the same time but start one after another, so the wilds arrive in a wave. Bad arguments still throw at the call; only the movement waits.

Live demo (nudge-stagger): https://pixi-reels.schmooky.dev/mechanics/nudge/#staggered-nudge-wave-reveal

## Spotlight after a nudge

The reels land one wild short of a line. Three reels nudge in parallel to complete it, then `SymbolSpotlight` presents the new winning cells.

Live demo (nudge-spotlight): https://pixi-reels.schmooky.dev/mechanics/nudge/#nudge-spotlight

## Big symbols

A 1xH block on the target reel moves as one piece. In the first demo a 1x2 wild nudges down by two, so its lower half moves into the buffer below the window, then up by one into full view. In the second the block nudges up so only its lower half shows, then back down into full view.

Only blocks one reel wide (`size.reels === 1`) can be nudged. A block that spans reels throws.

Live demo (nudge-big-symbol): https://pixi-reels.schmooky.dev/mechanics/nudge/#nudge-through-a-big-symbol
Live demo (nudge-tail-reveal): https://pixi-reels.schmooky.dev/mechanics/nudge/#nudge-through-a-big-symbol

## Other reel directions

`direction` is relative to the reel's own axis, so the nudge code is the same on any set. What changes is where on screen the new symbol enters.

### Horizontal set

On `.orientation('horizontal')` a reel is a screen row, so a nudge shifts it sideways. One press nudges row 0 `'forward'`, where a wild enters at the left edge, and row 2 `'reverse'`, where a wild enters at the right. `incoming` reads left to right here.

Live demo (nudge-horizontal): https://pixi-reels.schmooky.dev/mechanics/nudge/#nudge-a-horizontal-set

### Reels that spin up

Two wilds land on the middle line and the third stops one cell below it. On a `.direction('reverse')` reel a `'forward'` nudge travels upward, so the incoming symbol enters at the bottom cell and the wild rolls onto the line, which then lights up.

Live demo (nudge-reverse-rollup): https://pixi-reels.schmooky.dev/mechanics/nudge/#nudge-on-a-roll-up-set

## Cutting a nudge short

Both ways end with the strip where the nudge was going. They differ in what the promise does.

### Skip

`reelSet.skipNudge(reel)` jumps a running nudge to its end. `nudge()` resolves as usual, so the success path still runs. Use it when the win plays next.

Live demo (nudge-skip): https://pixi-reels.schmooky.dev/mechanics/nudge/#skip-an-in-flight-nudge

### Abort

Abort through `NudgeOptions.signal`. The strip still snaps to its landed position, but `nudge()` rejects with `AbortError` and `nudge:cancelled` fires. Use it when you are tearing the feature down.

Live demo (nudge-abort): https://pixi-reels.schmooky.dev/mechanics/nudge/#abort-a-nudge
