From e5ef0f9202ef0d71ac6b8cf71c3184902653b7bd Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Tue, 19 Mar 2024 22:18:29 -0700 Subject: [PATCH] Minor refactoring --- src/healthbar.rs | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/healthbar.rs b/src/healthbar.rs index d1bc13a..f6a7eec 100644 --- a/src/healthbar.rs +++ b/src/healthbar.rs @@ -48,7 +48,7 @@ pub fn spawn(mut commands: Commands, query: Query<(Entity, &HealthBar), Added>, mut bg_query: Query<&mut Sprite, (With, Without)>, - health_query: Query< - (&HealthBar, &HitPoints, &Children), - (Changed, Without), - >, + health_query: Query<(&HealthBar, &HitPoints, &Children), Changed>, ) { for (healthbar, hp, children) in health_query.iter() { - let mut frac = hp.current as f32 / hp.max as f32; - frac = frac.clamp(0.0, 1.0); + let frac = (hp.current as f32 / hp.max as f32).clamp(0.0, 1.0); - for child in children.iter() { + let invisible = (hp.current >= hp.max && !healthbar.show_full) + || (hp.current == 0 && !healthbar.show_empty); + + for child in children { // Update the bar itself if let Ok((mut transform, mut sprite)) = bar_query.get_mut(*child) { - if (hp.current == hp.max && !healthbar.show_full) - || (hp.current == 0 && !healthbar.show_empty) - { + if invisible { sprite.color = HEALTHBAR_INVISIBLE; } else if frac < 0.25 { sprite.color = HEALTHBAR_CRITICAL; @@ -111,17 +108,16 @@ fn update( sprite.color = HEALTHBAR_HEALTHY; }; - let w = frac * healthbar.size.x; - transform.scale.x = w; - transform.translation.x = (healthbar.size.x - transform.scale.x) / -2.0; + let current_width = frac * healthbar.size.x; + + transform.translation.x = (healthbar.size.x - current_width) / -2.0; + transform.scale.x = current_width; } // Update the bar background if let Ok(mut sprite) = bg_query.get_mut(*child) { - if (hp.current == hp.max && !healthbar.show_full) - || (hp.current == 0 && !healthbar.show_empty) - { + if invisible { sprite.color = HEALTHBAR_INVISIBLE; } else { sprite.color = HEALTHBAR_BACKGROUND;