fix lethal damage
kill an entity at less than one health
set an entity's new_health when it is damaged to exactly zero
@@ -271,25 +271,14 @@ impl Game {
return;
}
- let (should_kill, old_health, new_health) =
- if let Some(mut health) = world.try_get_mut::<Health>(entity) {
+ let (old_health, new_health) = if let Some(mut health) = world.try_get_mut::<Health>(entity)
+ {
let old_health = health.0;
- let should_kill = match health.0.checked_sub(damage) {
- Some(0) => true,
- None => {
- // below 0
- health.0 = 0;
- true
- }
- Some(new_health) => {
+ let new_health = health.0.saturating_sub(damage);
health.0 = new_health;
- false
- }
- };
- let new_health = health.0;
- (should_kill, Some(old_health), new_health)
+ (Some(old_health), new_health)
} else {
- (false, None, 0)
+ (None, 0)
};
if let Some(old_health) = old_health {
@@ -301,12 +290,12 @@ impl Game {
entity,
},
);
- }
- if should_kill {
+ if new_health == 0 {
self.kill(entity, world);
}
}
+ }
/// Kills an entity.
pub fn kill(&mut self, entity: Entity, world: &mut World) {
