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...
This commit is contained in:
@@ -19,14 +19,26 @@ impl FuelGauge {
|
||||
},
|
||||
BackgroundColor(GREEN.into()),
|
||||
Text::new("Fuel: "),
|
||||
children![TextSpan::new("<n>"),],
|
||||
)
|
||||
}
|
||||
|
||||
pub fn detect_fuel_change(event: Trigger<FuelChanged>) {
|
||||
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<FuelChanged>,
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user