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); } }