Compare commits

3 Commits

Author SHA1 Message Date
ed2e1e75ef 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...
2025-08-28 15:33:21 -05:00
21c00d4a02 Pass target ID through the cutter's button handler
Use the machine entity ID in the button handler prototype. Now to see
about getting the FuelGauge to show the value after a button press.
2025-08-28 12:27:53 -05:00
5f1f283500 Change trait SpawnUi to require a target entity
The UIs aren't *any* UIs. They are specifically *machine* UIs. They need
to be able to fetch data from components on those machine entities,
which means they need some way to find the machine. Passing an entity ID
is the easiest way, IMO.
2025-08-28 12:27:45 -05:00
4 changed files with 30 additions and 27 deletions

View File

@@ -86,5 +86,5 @@ fn spawn_machine_ui<M: SpawnUi>(
) {
let _entity = event.target;
// TODO: Hook up the Fuel component (and spawn one so that I can)
M::spawn_ui(commands, theme);
M::spawn_ui(commands, theme, event.target());
}

View File

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

View File

@@ -7,7 +7,7 @@ use crate::{
};
impl SpawnUi for CuttingMachine {
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) {
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>, machine_id: Entity) {
let base_entity = machine_ui_base(&mut commands, "Cutting Machine", &theme);
commands.entity(base_entity).with_children(|commands| {
// Left panel. For fuel or machine stats or whatever.
@@ -37,19 +37,10 @@ impl SpawnUi for CuttingMachine {
))
.with_children(|cmds| {
let mut button_cmds = cmds.spawn(BigRedButton::bundle("CUT"));
button_cmds.observe(
|trigger: Trigger<Pointer<Click>>,
mut com: Commands,
q: Single<(Entity, &CuttingMachine)>| {
dbg!("Cut button pressed. Triggering a FuelChanged event");
// Feed through the target machine, once the SpawnUi trait is updated
// to require it gets here in the first place.
let cutting_machine = q.0;
com.trigger_targets(FuelChanged, cutting_machine);
},
);
button_cmds.observe(move |trigger: Trigger<Pointer<Click>>, mut com: Commands| {
dbg!("Cut button pressed. Triggering a FuelChanged event");
com.trigger_targets(FuelChanged, machine_id);
});
// TODO: Attach on-press observer so this machine can do something
// in response to that button being pressed
});
@@ -58,7 +49,7 @@ impl SpawnUi for CuttingMachine {
}
impl SpawnUi for RotatingMachine {
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) {
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>, machine_id: Entity) {
let base_entity = machine_ui_base(&mut commands, "Rotating Machine", &theme);
commands.entity(base_entity).with_children(|commands| {
commands.spawn(FuelGauge::bundle(Entity::PLACEHOLDER));
@@ -93,7 +84,7 @@ impl SpawnUi for RotatingMachine {
}
impl SpawnUi for FlippingMachine {
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) {
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>, machine_id: Entity) {
let base_entity = machine_ui_base(&mut commands, "Flipping Machine", &theme);
commands.entity(base_entity).with_children(|commands| {
commands.spawn(FuelGauge::bundle(Entity::PLACEHOLDER));
@@ -128,7 +119,7 @@ impl SpawnUi for FlippingMachine {
}
impl SpawnUi for TransposingMachine {
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) {
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>, machine_id: Entity) {
let base_entity = machine_ui_base(&mut commands, "Transposing Machine", &theme);
commands.entity(base_entity).with_children(|commands| {
commands.spawn(FuelGauge::bundle(Entity::PLACEHOLDER));

View File

@@ -30,12 +30,12 @@ impl Plugin for GameUiPlugin {
}
}
/// Anything that can spawn a themed UI should impl this trait.
/// Machines spawn their UI through this trait.
///
/// This exists mainly so that I can write generic functions and have the *compiler*
/// This exists mainly so that I can write generic functions and have the compiler
/// expand the code for me, instead of doing it by hand.
pub trait SpawnUi {
fn spawn_ui(commands: Commands, theme: Res<UiTheme>);
fn spawn_ui(commands: Commands, theme: Res<UiTheme>, machine_id: Entity);
}
/// The base panel for the machines that manipulate the room cards.