Skip to main content

Damage

Struct Damage 

Source
pub struct Damage<'a> { /* private fields */ }
Available on crate feature unstable only.
Expand description

Damage about to reach an entity’s health, for a filter registered with Load::on_damage to see and change.

Unstable: needs the unstable feature and may change in any release.

§Examples

See the module.

Implementations§

Source§

impl Damage<'_>

Source

pub const fn cause(&self) -> DamageCause

What deals it.

§Examples

See the module.

Source

pub const fn target(&self) -> Entity

The entity that takes it.

§Examples
fn init(load: &mut enshwick::Load<'_>) -> Result<(), enshwick::Off> {
    // Entity 2 takes no damage at all.
    load.on_damage(|damage| {
        if damage.target().0 == 2 {
            damage.cancel();
        }
    })
}
Source

pub const fn source(&self) -> Option<Entity>

What dealt a hit: the attacker, or its projectile; None for other causes.

§Examples
fn init(load: &mut enshwick::Load<'_>) -> Result<(), enshwick::Off> {
    // Nothing hurts itself.
    load.on_damage(|damage| {
        if damage.source() == Some(damage.target()) {
            damage.cancel();
        }
    })
}
Source

pub const fn attacker(&self) -> Option<Entity>

Who dealt a hit: the attacker, also for its arrows and spells; None for other causes.

§Examples
fn init(load: &mut enshwick::Load<'_>) -> Result<(), enshwick::Off> {
    // Entity 7 deals double damage, by hand or by bow.
    load.on_damage(|damage| {
        if damage.attacker().is_some_and(|a| a.0 == 7) {
            damage.scale(2.0);
        }
    })
}
Source

pub const fn amount(&self, ty: DamageTypeNew) -> f32

A hit’s damage of type ty; 0 for other causes.

§Examples
use enshwick::damage::DamageTypeNew;

fn init(load: &mut enshwick::Load<'_>) -> Result<(), enshwick::Off> {
    // Fire never deals more than 50 at once.
    load.on_damage(|damage| {
        let fire = damage.amount(DamageTypeNew::MagicalFire);
        damage.set(DamageTypeNew::MagicalFire, fire.min(50.0));
    })
}
Source

pub const fn set(&mut self, ty: DamageTypeNew, amount: f32)

Sets a hit’s damage of type ty; does nothing for other causes. A negative or not finite amount counts as none.

§Examples

See Damage::amount.

Source

pub fn total(&self) -> f32

The damage the health takes: for a hit, the sum of its types but healing, before the target’s armour.

§Examples
fn init(load: &mut enshwick::Load<'_>) -> Result<(), enshwick::Off> {
    // No single blow deals more than 200.
    load.on_damage(|damage| {
        let total = damage.total();
        if total > 200.0 {
            damage.scale(200.0 / total);
        }
    })
}
Source

pub fn scale(&mut self, factor: f32)

Scales the damage by factor: every type of a hit but healing, or the amount of another cause.

§Examples

See the module.

Source

pub fn scale_type(&mut self, ty: DamageTypeNew, factor: f32)

Scales a hit’s damage of type ty by factor; does nothing for other causes.

§Examples

See the module.

Source

pub fn cancel(&mut self)

Takes all damage away: the health stays as it is. A hit still lands, with no damage, as against an immune target.

§Examples

See Damage::target.

Trait Implementations§

Source§

impl<'a> Debug for Damage<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !UnwindSafe for Damage<'a>

§

impl<'a> Freeze for Damage<'a>

§

impl<'a> RefUnwindSafe for Damage<'a>

§

impl<'a> Send for Damage<'a>

§

impl<'a> Sync for Damage<'a>

§

impl<'a> Unpin for Damage<'a>

§

impl<'a> UnsafeUnpin for Damage<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.