# rmp::behavior

> rmp/behavior.h — the catalogue.

Source: https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-behavior.html

namespace `#include <rmp/behavior.h>`

rmp/behavior.h — the catalogue.

```cpp
ball.add<rmp::behavior::Ball>({ .speed = 320 });
player.add<rmp::behavior::TopDown>({ .speed = 220 });
```

A behavior is any struct with `void _update(rmp::Object &, float)`. The model is explained above [`Object::add`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Object.html#add) in rmp/object.h, and the part worth repeating here is that every one of these is a struct exactly like one of yours: there is no extension API, because there is nothing to extend.

THE FIELDS ARE THE CONFIGURATION, and they are hot:

```cpp
player.get<rmp::behavior::TopDown>()->speed = 400;   // a power-up
```

NONE OF THEM CHOOSES A DIRECTION FOR YOU. A behavior contributes behaviour, not intention: which way the Pong ball is served is a rule of the game, and picking it would be the framework deciding something that is not its to decide. So a Ball starts still, a Projectile travels the way you pushed it, a Follow chases the handle you gave it, and a TopDown reads the actions you named.

WHAT THEY WRITE IS A NUMBER. A behavior writes into its object — the position, the velocity, the scale — only finite numbers: a field that is NaN or an infinity, or one so large the arithmetic overflows, leaves the object as it was, and the behavior says so once (a field that is NaN is named). A hook handed a delta that is not a number of seconds, 0 or more, counts it as 0 and says so once; the framework never hands one.

PAUSED, NOT REMOVED. `object.enable<B>(false)` stops a behavior's hooks and keeps its state; a Chase pauses the Patrol it interrupts that way, and a StateMachine's states say which behaviors run while they are on.

FROM THE EDITOR. The behaviors a level designer places — Patrol, Chase, Orbit, Shooter, Hitbox, Collectible — have `B::from(const MapObject &)`, which reads the entity's fields by the names written above each of them, and leaves every field the entity does not have at its default:

```cpp
map.on_object("Bat", [](rmp::Scene &s, const rmp::MapObject &o) {
    auto &bat = s.spawn({ .position = o.position });
    bat.add(rmp::behavior::Patrol::from(o));
});
```

WHAT IS NOT HERE, and why, in short: `tetromino`, `formation` and `paddle` are three games with a different name on them.

## On pages of their own

- [`rmp::behavior::TopDown`](https://omardev29.github.io/rmp-docs/reference/behaviors/TopDown.html) — Eight directions, and the sprite chosen from the one you are facing.
- [`rmp::behavior::Platformer`](https://omardev29.github.io/rmp-docs/reference/behaviors/Platformer.html) — Gravity, jumping, ground, coyote time and a buffered jump.
- [`rmp::behavior::Runner`](https://omardev29.github.io/rmp-docs/reference/behaviors/Runner.html) — Constant forward motion that SPEEDS UP over time, with a jump and a duck.
- [`rmp::behavior::Ball`](https://omardev29.github.io/rmp-docs/reference/behaviors/Ball.html) — Bounces, keeps its pace, and leaves a paddle at an angle that depends on where it hit.
- [`rmp::behavior::Projectile`](https://omardev29.github.io/rmp-docs/reference/behaviors/Projectile.html) — Travels the way you pushed it and is discarded when it leaves or hits.
- [`rmp::behavior::Follow`](https://omardev29.github.io/rmp-docs/reference/behaviors/Follow.html) — Chases a handle, with a speed and a distance it stops at.
- [`rmp::behavior::Tween`](https://omardev29.github.io/rmp-docs/reference/behaviors/Tween.html) — Moves one property of its object from `from` to `to` over `seconds`, along an Ease.
- [`rmp::behavior::GridSnap`](https://omardev29.github.io/rmp-docs/reference/behaviors/GridSnap.html) — Squares the position with a grid, AFTER everything that moves it.
- [`rmp::behavior::Parallax`](https://omardev29.github.io/rmp-docs/reference/behaviors/Parallax.html) — A background layer that scrolls at its own factor and repeats itself with no seam.
- [`rmp::behavior::Spawner`](https://omardev29.github.io/rmp-docs/reference/behaviors/Spawner.html) — Produces objects every N seconds, or every N units TRAVELLED.
- [`rmp::behavior::Timer`](https://omardev29.github.io/rmp-docs/reference/behaviors/Timer.html) — Calls you every N seconds.
- [`rmp::behavior::Lifespan`](https://omardev29.github.io/rmp-docs/reference/behaviors/Lifespan.html) — Discarded after N seconds.
- [`rmp::behavior::Health`](https://omardev29.github.io/rmp-docs/reference/behaviors/Health.html) — Hit points, a window of invulnerability with a blink, and death.
- [`rmp::behavior::Area`](https://omardev29.github.io/rmp-docs/reference/behaviors/Area.html) — A region that says who comes and goes: a trigger, a door's sensor, a pit, a shop's counter.
- [`rmp::behavior::Hitbox`](https://omardev29.github.io/rmp-docs/reference/behaviors/Hitbox.html) — What hurts: a sword's swing, a spike, a bullet, an explosion.
- [`rmp::behavior::Talker`](https://omardev29.github.io/rmp-docs/reference/behaviors/Talker.html) — Someone to talk to.
- [`rmp::behavior::Collectible`](https://omardev29.github.io/rmp-docs/reference/behaviors/Collectible.html) — A coin, a key, a heart.
- [`rmp::behavior::Shooter`](https://omardev29.github.io/rmp-docs/reference/behaviors/Shooter.html) — Fires.
- [`rmp::behavior::Patrol`](https://omardev29.github.io/rmp-docs/reference/behaviors/Patrol.html) — Goes from waypoint to waypoint — round in a loop, or back and forth with `ping_pong` — waiting `wait` seconds at each.
- [`rmp::behavior::Pathfind`](https://omardev29.github.io/rmp-docs/reference/behaviors/Pathfind.html) — Walks to a target, or to a point, around the map's walls: `rmp::find_path()`, asked again every `repath_every` seconds, and followed at `speed`.
- [`rmp::behavior::Chase`](https://omardev29.github.io/rmp-docs/reference/behaviors/Chase.html) — Sees a target and goes after it.
- [`rmp::behavior::Orbit`](https://omardev29.github.io/rmp-docs/reference/behaviors/Orbit.html) — Circles a handle or a point: a shield of blades, a moon, a fairy.
- [`rmp::behavior::Animator`](https://omardev29.github.io/rmp-docs/reference/behaviors/Animator.html) — Plays the tag that fits what the object is doing: `die` once its Health is dead, `hurt` while it is invulnerable after a hit, `jump` and `fall` in the air (with gravity), `run` while it moves and `idle` while it does not — and mirrors it with flip_x by the way it moves.
- [`rmp::behavior::Draggable`](https://omardev29.github.io/rmp-docs/reference/behaviors/Draggable.html) — Picked up with the pointer and put down somewhere.
- [`rmp::behavior::StateMachine`](https://omardev29.github.io/rmp-docs/reference/behaviors/StateMachine.html) — Named states and the moves between them: an enemy that idles, patrols, chases and flees; a boss's phases.

## Tween: animate one property, with easing, looping and ping-pong

### `Ease`

enum class Ease

enum [include/rmp/behavior.h:284](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/behavior.h#L284)

The curve a Tween's value follows from `from` to `to` over its `seconds`. LINEAR moves at a constant rate; IN starts slow and speeds up; OUT starts fast and slows into `to`; IN_OUT is slow at both ends and fastest in the middle. The last three are quadratic, and a ping_pong's way back runs the same curve in reverse.

| Value |  | Means |
|---|---|---|
| `LINEAR` |  |  |
| `IN` |  |  |
| `OUT` |  |  |
| `IN_OUT` |  |  |

### `Property`

enum class Property

enum [include/rmp/behavior.h:288](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/behavior.h#L288-L295)

Which field of its object a Tween writes, on every update it runs. ALPHA takes 0 to 255 and clamps to that range.

| Value |  | Means |
|---|---|---|
| `POSITION_X` |  | Position.x, the centre |
| `POSITION_Y` |  | Position.y, the centre |
| `SCALE_X` |  | Scale.x |
| `SCALE_Y` |  | Scale.y |
| `ROTATION` |  | Rotation, in degrees |
| `ALPHA` |  | The shape's colour, or the sprite's tint |

## World and production

### `MAX_SPAWNED`

inline constexpr int MAX_SPAWNED = 64

constant [include/rmp/behavior.h:380](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/behavior.h#L380)

How many of its own objects one Spawner keeps track of, which is the largest `max_alive` it can enforce. A fixed array and not a std::vector because <vector> costs 92 ms in every translation unit that includes a behavior, and a spawner that needs hundreds of things alive at once is a pool, not a spawner. A `max_alive` above this warns once and is treated as this.

## Moving on its own

### `Face`

enum class Face

enum [include/rmp/behavior.h:906](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/behavior.h#L906-L910)

Which way an Orbit turns its object.

| Value |  | Means |
|---|---|---|
| `NONE` |  | Its rotation is left alone |
| `CENTRE` |  | It looks at what it circles |
| `OUTWARD` |  | It looks away from it |

## The sprite, the pointer and the brain

### `AnimationRule`

struct AnimationRule

struct [include/rmp/behavior.h:940](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/behavior.h#L940-L943)

A rule of an Animator: play `tag` while `when` says so. The rules are asked in order, before anything the Animator works out itself.

| Field | Type | Default | What it is |
|---|---|---|---|
| `tag` | `std::string` |  | The tag played while `when` says so |
| `when` | `Condition<Object &>` |  | Asked with the object, every update |

### `State`

struct State

struct [include/rmp/behavior.h:1022](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/behavior.h#L1022-L1030)

One state of a StateMachine: what it does on the way in, every frame, on the way out, and which behaviors run while it is on.

| Field | Type | Default | What it is |
|---|---|---|---|
| `name` | `std::string` |  | What transitions and `go()` call it |
| `on_enter` | `Callback<Object &>` |  | Once, as the machine moves into it |
| `on_update` | `Callback<Object &, float>` |  | (the object, delta) |
| `on_exit` | `Callback<Object &>` |  | Once, as the machine moves out of it |
| `enables` | `std::vector<int>` |  | The behaviors that run in this state: [`rmp::behavior::kinds`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-behavior.html#kinds)<Patrol>(). Every behavior some state names is paused in the states that do not. |

### `Transition`

struct Transition

struct [include/rmp/behavior.h:1033](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/behavior.h#L1033-L1037)

A move from one state to another, taken when `when` says so.

| Field | Type | Default | What it is |
|---|---|---|---|
| `from` | `std::string` |  | "" = from any state |
| `to` | `std::string` |  | The state it moves to |
| `when` | `Condition<Object &>` |  | Asked with the object every update; yes takes the move |

### `kinds`

template <class... B> std::vector<int> kinds()

function [include/rmp/behavior.h:1040](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/behavior.h#L1040-L1042)

The behaviors a State turns on, by type: `.enables = kinds<Patrol, Animator>()`.
