# Game modes and pawns

> Describe the flow of a game as phases with rules that move between them, and let the player possess and control other objects as pawns.

Source: https://feather-engine.com/docs/gameplay/game-modes/

A game mode describes the flow of your game as **phases**: a title screen, a countdown, the round
itself, a boss fight, won and lost. Each phase can last a set time, show a game screen, pause the
game or end it. **Rules** move between phases when a script event arrives, when an attribute such
as the player's health drops below a value, or when a state value compares true. Scripts read the
phase and can change it at any time.

**Pawns** are objects the player can take control of instead of the player character: a drone, a
rolling ball, a creature, a turret. The player walks up and presses **Interact**, or a script
possesses one, and the camera follows it until the player steps out.

Both are project data: the game mode through `game_mode.*` commands, and pawns as a component on
any object.

## What's included

- **Phases** with an optional time limit and the phase that follows, a game screen shown while the
  phase lasts, and switches to pause the game and to mark it as over.
- **Rules** that fire on a script event, an attribute (see
  [Abilities, attributes and effects](/docs/gameplay/abilities/)) or a state value.
- A **survival preset**: an intro, the round, and won and lost endings.
- Script functions and a `game_phase` event, and a `phase` state value that screens can show.
- **Pawns** that walk, fly or roll, driven by the player or by a script.

## Set up a game mode

1. Open **Window → Game logic** and select the **Game mode** tab. The tab is locked during Play.
2. Turn on **Game mode on**, or click **Use survival preset** to start from a working flow.
3. Under **PHASES**, click **+ Phase** for each phase and fill in its row: **Id**, **Label**,
   **Seconds**, **Then** (the next phase), **Screen**, **Paused** and **Ends game**.
4. Set **Starts in** to the first phase, or leave it on **first phase**.
5. Under **RULES**, click **+ Rule**. Pick the phase it applies **In** (or **any phase**), the phase
   to **go to**, and **when**: an **event** name, an **attribute** (with its owner, `player` when
   empty) or a **state** key, a comparison (`<`, `>` or `=`) and a value. Add a **reason**;
   scripts receive it.
6. Click **Apply**. Problems, such as a rule that names a missing phase, show above the button.
7. Press **Play**.

### Phases

| Setting | Field | Range | Default |
| --- | --- | --- | --- |
| Id | `id` | Lowercase letters, digits and `_`, up to 48 characters | — |
| Label | `label` | Up to 80 characters | Empty |
| Seconds | `seconds` | 0–86,400 s; 0 lasts until a rule or script ends it | 0 |
| Then | `next` | The phase that follows when the time runs out | None |
| Screen | `screen` | A game screen shown during the phase and hidden after it | None |
| Paused | `paused` | Pauses the game while in the phase | Off |
| Ends game | `ends_game` | The game is over in this phase | Off |
| Description | `description` | Up to 400 characters | Empty |

Time doesn't count in a phase with **Paused** on, so give it a rule or end it from a script. When the game mode is on, Play starts in the
**Starts in** phase (`start`), or the first phase.

### Rules

| Setting | Field | Description |
| --- | --- | --- |
| In | `from` | The phase the rule applies in. Empty applies in any phase that doesn't end the game |
| Go to | `to` | The phase it moves to |
| Event | `on_event` | Fires when a script event with this name is sent, for example `emit("escaped", #{})` |
| Attribute | `attribute`, `owner` | Compares the owner's attribute value; the owner is `player` when empty |
| State | `state` | Compares a state value set with `set(key, value)`; `true` counts as 1 |
| Compare | `compare` | `Below`, `Above` or `Equal` |
| Value | `value` | The number to compare with |
| Reason | `reason` | Text passed to scripts with the change; up to 200 characters |

Each rule has exactly one trigger: an event, an attribute or a state key. When several rules match
at once, the first in the list wins. A phase's time limit is checked before the attribute and state
rules.

The survival preset (`game_mode.set` `preset` `survival`) starts in `intro`, which moves to
`playing` after 3 s. From `playing`, the `escaped` event leads to `won`, and the player's `health`
dropping below 0.001 leads to `lost`. Both endings pause and end the game.

## Example: a 60-second survival round

You'll give **Ocean Voyage** a three-second countdown, a 60-second round the player wins by
surviving, and a lost phase when their health runs out.

1. Open **Window → World & terrain → Abilities**, select **Attributes**, type `health` and click
   **+ Add**. Leave it at **Base** 100.
2. Open **Window → Game logic → Game mode** and turn on **Game mode on**.
3. Add four phases:

   | Id | Label | Seconds | Then | Paused | Ends game |
   | --- | --- | --- | --- | --- | --- |
   | `countdown` | Get ready | 3 | `playing` | Off | Off |
   | `playing` | Survive | 60 | `won` | Off | Off |
   | `won` | You made it | 0 | — | On | On |
   | `lost` | Game over | 0 | — | On | On |

4. Set **Starts in** to `countdown`.
5. Add a rule: in `playing`, go to `lost` when **attribute** `health` `<` `0.001`, with the reason
   `Out of health`.
6. Click **Apply** and press **Play**.

Do the same from the command line, on a copy of the example:

```bash
cp -R examples/ocean-voyage /tmp/demo

feather-cli --workspace /tmp/demo --project project.feather.json command ability.upsert \
  --json '{"kind":"attribute","value":{"id":"health","base":100,"max":100}}' --save project.feather.json

feather-cli --workspace /tmp/demo --project project.feather.json command game_mode.set --json '{"patch":{
  "enabled": true, "start": "countdown",
  "phases": [
    {"id":"countdown","label":"Get ready","seconds":3,"next":"playing"},
    {"id":"playing","label":"Survive","seconds":60,"next":"won"},
    {"id":"won","label":"You made it","paused":true,"ends_game":true},
    {"id":"lost","label":"Game over","paused":true,"ends_game":true}],
  "transitions": [
    {"from":"playing","to":"lost","attribute":"health","compare":"Below","value":0.001,"reason":"Out of health"}]
}}' --save project.feather.json
```

**Check it worked.** Run four seconds headlessly:

```bash
feather-cli --workspace /tmp/demo --project project.feather.json command game.simulate --json '{"frames":240}'
```

In `data.report`, `game_mode.phase` is `playing`, and `game_mode.history` lists the change to
`countdown` (reason `start`) and, at about 3 s, to `playing` (reason `countdown ended`).
`world.state.phase` is `playing` too.

To show a screen in each phase, create the screens in [Game UI](/docs/presentation/game-ui/) and
pick them in the **Screen** column, or bind a label to `{{phase}}`.

## Pawns

1. Select the object and click **Add Component → Pawn** in the Details panel. The component isn't
   offered on objects with a vehicle or boat.
2. Choose a **Mode** and set its speeds.
3. Leave **Player can enter** on to let the player take control by walking up and pressing
   **Interact** (E), and type a **Prompt** if you like.
4. Give the object a **Dynamic** body to make it collide and be pushed. Pawns without one move
   their transform directly.
5. Press **Play**, walk up to the object and press **E**. Press **E** again to step out beside it.

While possessed, the camera follows the pawn and the player character is hidden. **W** and **S**
move it forward and back along the way it faces, and **A** and **D** turn it. A Flyer climbs while
**Jump** is held and descends while **Crouch** is held. Walkers and Rollers with a Dynamic body jump
with **Jump** when they aren't already moving up or down.

| Setting | Field | Range | Default |
| --- | --- | --- | --- |
| Mode | `mode` | `Walker` (keeps its gravity), `Flyer`, `Roller` (a ball that rolls where it faces) | Walker |
| Speed | `speed` | 0–200 m/s | 6 m/s |
| Acceleration | `acceleration` | 0.1–500 m/s² | 20 m/s² |
| Turn rate | `turn_rate` | 0–1440°/s | 150°/s |
| Jump / climb | `jump` | 0–60 m/s | 5 m/s |
| Camera distance | `camera_distance` | 0.5–60 m | 5 m |
| Player can enter | `enterable` | On/off | On |
| Prompt | `prompt` | Up to 80 characters | Empty |

The Details panel's sliders cover a smaller part of some ranges; type a value or use `scene.update`
for the rest. The player can enter a pawn from about 2 m beyond half its largest size.

To check possession headlessly, give a fresh copy of **Ocean Voyage** a drone 3 m ahead of the
player start, then walk to it, press **E**, fly forward and up, and press **E** again:

```bash
feather-cli --workspace /tmp/demo --project project.feather.json command scene.add \
  --json '{"kind":"Cube","name":"Drone","position":[0,17,49]}' --save project.feather.json

feather-cli --workspace /tmp/demo --project project.feather.json command scene.update \
  --json '{"id":36,"patch":{"scale":[0.6,0.3,0.6],"physics":{"body":"Dynamic","gravity_scale":0},"pawn":{"mode":"Flyer","speed":8,"jump":3}}}' \
  --save project.feather.json

feather-cli --workspace /tmp/demo --project project.feather.json command game.simulate --json '{
  "frames": 240,
  "inputs": [
    {"frame": 10, "key": "W", "pressed": true}, {"frame": 30, "key": "W", "pressed": false},
    {"frame": 50, "key": "E", "pressed": true}, {"frame": 52, "key": "E", "pressed": false},
    {"frame": 60, "key": "W", "pressed": true}, {"frame": 60, "key": "Space", "pressed": true},
    {"frame": 150, "key": "W", "pressed": false}, {"frame": 150, "key": "Space", "pressed": false},
    {"frame": 200, "key": "E", "pressed": true}, {"frame": 202, "key": "E", "pressed": false}
  ]}'
```

The report's `pawn` section lists `possessions: [36]`, `possessed: null` (the player stepped out),
about 2.5 `seconds` in control and a `distance` of several metres. The drone's entry in `objects`
ends more than 3 m higher than it started.

## Extend it

| Function | Description |
| --- | --- |
| `game_phase()` | The current phase ID; `""` when the game mode is off |
| `set_game_phase(id[, reason])` | Moves to a phase now; the reason defaults to `script` |
| `phase_time()` | Seconds in the current phase |
| `game_over()` | `true` while the phase ends the game |
| `possess(id)` | Takes control of an object with a Pawn component |
| `unpossess()` | Steps out of the possessed pawn |
| `possessed()` | The possessed pawn's ID, or `""` |
| `pawn_input(id, forward, turn, up)` | Drives a pawn nobody possesses for this frame, each value −1 to 1; call it every update. Use it for AI and cutscenes |

Every phase change sends the `game_phase` event with `from`, `to` and `reason`, and sets the state
value `phase`. `pause_game(on)` and `game_paused()` use the same pause as phases.

```rust
// A world script: show every phase change, and move to a final_wave phase (which you add)
// after two minutes of playing.
fn start() {
    on("game_phase", |d| {
        toast(d.from + " → " + d.to + " (" + d.reason + ")");
    });
}

fn update(dt) {
    if game_phase() == "playing" && phase_time() > 120.0 {
        set_game_phase("final_wave", "two minutes survived");
    }
}
```

## Limits

- Up to 64 phases and 128 rules. An enabled game mode needs at least one phase.
- Event names are up to 64 characters.
- Phase changes are kept in a history of the last 256.
- A rule that names an attribute the project doesn't have never fires.
- `set_game_phase` fails while the game mode is off or when the phase doesn't exist.
- Edits made during Play apply at once; the current phase stays if it still exists.

## Automate it

```bash
# Start from the survival flow
feather-cli command game_mode.set --json '{"preset":"survival"}'

# Win when a script sets the state value "keys" above 2
feather-cli command game_mode.set \
  --json '{"patch":{"transitions":[{"from":"playing","to":"won","state":"keys","compare":"Above","value":2,"reason":"All keys found"}]}}'

# Make object 12 a rolling ball the player can take over
feather-cli command scene.update \
  --json '{"id":12,"patch":{"physics":{"body":"Dynamic"},"pawn":{"mode":"Roller","speed":10,"prompt":"Roll"}}}'
```

`game_mode.set` patches replace arrays, so the second command keeps only that rule. `game_mode.get`
returns the game mode and the presets. `game.simulate` reports the phase, its time and the history
in `game_mode`, and possession in `pawn`. See [Simulation](/docs/agents/simulation/).
