pub struct Damage<'a> { /* private fields */ }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<'_>
impl Damage<'_>
Sourcepub const fn cause(&self) -> DamageCause
pub const fn cause(&self) -> DamageCause
Sourcepub const fn target(&self) -> Entity
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();
}
})
}Sourcepub const fn source(&self) -> Option<Entity>
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();
}
})
}Sourcepub const fn attacker(&self) -> Option<Entity>
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);
}
})
}Sourcepub const fn amount(&self, ty: DamageTypeNew) -> f32
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));
})
}Sourcepub const fn set(&mut self, ty: DamageTypeNew, amount: f32)
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.
Sourcepub fn total(&self) -> f32
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);
}
})
}Sourcepub fn scale_type(&mut self, ty: DamageTypeNew, factor: f32)
pub fn scale_type(&mut self, ty: DamageTypeNew, factor: f32)
Sourcepub fn cancel(&mut self)
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.