On this page
  1. Fields
    1. damage, heal
    2. invulnerable
    3. dead

rmp::behavior::Health

behavior #include <rmp/behavior.h> include/rmp/behavior.h:478
struct Health

Hit points, a window of invulnerability with a blink, and death.

Added to an object with add<rmp::behavior::Health>(); the framework calls its _ready, _update, _collision, _end with the object it is on.

Fields

FieldTypeDefaultWhat it is
hpint3

Hit points left, and the number it starts with

max_hpint0

0 = whatever hp starts at, so { .hp = 5 } needs no second number

invulnerable_forfloat0.6f

0 = no window

destroy_on_deathbooltrue

Destroy the object when it dies, after on_death

on_deathCallback<Object &>

Once, when damage() takes hp to 0

on_damageCallback<Object &, int>

The amount that actually landed

hurt_byunsigned0

WHICH LAYERS HURT. 0, the default, is "nothing does": contact costs nothing until a game says which contact costs something, and then it says it once here instead of writing the same if at the top of every _collision.

player.add<rmp::behavior::Health>({ .hp = 3, .hurt_by = layer::ENEMY });

It is the OTHER object's collision_layer that is tested, the same numbers rmp::Object::collision_mask uses — so a spike, an enemy and a bullet are one layer each and a player is hurt by whichever it names. The invulnerable window applies, which is what stops standing inside a fire costing sixty hearts a second.

damage_on_hitint1

What one touch costs

oursstruct { … }

What it is keeping track of. Readable, because sometimes you want it; not yours to write. It is one PUBLIC member and not a private block because a struct with a private member is not an aggregate in C++20, and then { .speed = 320 } stops compiling — which is the whole design. The name is the fence.

damage, heal

bool damage(Object &self, int amount = 1)
void heal(int amount = 1)

Returns whether it landed. Damage during the invulnerable window does not, which is what stops one spike costing three hearts in three frames.

invulnerable

bool invulnerable() const

Whether the window after a hit is still open, during which damage() lands nothing.

dead

bool dead() const

Whether hp is 0 or less. A death through damage() is final: heal() does nothing after it.