From ed2e1e75efb09a0d3aa8a5d4a53dfa75662adf8e Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Thu, 28 Aug 2025 15:33:21 -0500 Subject: [PATCH] Impl the FuelGauge text update logic This does a linear search through all existing FuelGauges, but we're not expecting that to be a problem. There should only be zero or one of them on screen at any moment. It also doesn't work because the rest of the game state isn't correct... --- src/widgets/fuel_gauge.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/widgets/fuel_gauge.rs b/src/widgets/fuel_gauge.rs index f57cf3e..234a066 100644 --- a/src/widgets/fuel_gauge.rs +++ b/src/widgets/fuel_gauge.rs @@ -19,14 +19,26 @@ impl FuelGauge { }, BackgroundColor(GREEN.into()), Text::new("Fuel: "), - children![TextSpan::new(""),], ) } - pub fn detect_fuel_change(event: Trigger) { - dbg!(format!( - "Detected fuel change on entity ID: {}", - event.target() - )); + /// Observer system to update the [`FuelGauge`] widget when a machine emits a + /// [`FuelChanged`] event. + pub fn detect_fuel_change( + event: Trigger, + fuel_levels: Query<&Fuel>, + mut gauges: Query<(&FuelGauge, &mut Text)>, + ) { + // Find the FuelGauge that references the same Entity as the event target. + // That gauge's `Text` is the one to update. + let (_, mut label) = gauges + .iter_mut() + .find(|(gauge, _label)| gauge.0 == event.target()) + .expect("Couldn't find any fuel gauges"); + + let fuel = fuel_levels.get(event.target()).expect( + "Logic error: a `FuelChanged` event was targetted at an entity with no `Fuel` component.", + ); + label.0 = format!("Fuel: {}", fuel.0); } }