Proof-of-concept for fuel change event handling

Added a `FuelChanged` event which is to be used to trigger Observers
watching for a machine's fuel level change.

I've written a slapdash observer to emit these events when the cutting
machine's big red button is pressed. That observer *must* be replaced
because of how it feeds the machine ID into the UI (it only works when
there is exactly 1 CuttingMachine, which won't be the case).

The dummy machines are missing their machine components. I've added the
`CuttingMachine` component to the cutting machine just to test the event
passing.
This commit is contained in:
2025-08-28 12:01:30 -05:00
parent e169750923
commit 8f6dfc3e49
5 changed files with 62 additions and 22 deletions

View File

@@ -1,9 +1,9 @@
use bevy::{color::palettes::css::*, prelude::*, ui::Val::*};
use bevy::{color::palettes::css::*, prelude::*};
use crate::{
game::machines::*,
game::{consumables::FuelChanged, machines::*},
resources::UiTheme,
widgets::{BigRedButton, SpawnUi, machine_ui_base},
widgets::{BigRedButton, SpawnUi, fuel_gauge::FuelGauge, machine_ui_base},
};
impl SpawnUi for CuttingMachine {
@@ -11,7 +11,8 @@ impl SpawnUi for CuttingMachine {
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.
commands.spawn(fuel_gauge_placeholder());
// TODO: Pass along target machine, not the UI's root entity.
commands.spawn(FuelGauge::bundle(Entity::PLACEHOLDER));
// Center panel (placeholder for the Card view)
commands.spawn((
@@ -35,7 +36,20 @@ impl SpawnUi for CuttingMachine {
Pickable::default(),
))
.with_children(|cmds| {
let _button_cmds = cmds.spawn(BigRedButton::bundle("CUT"));
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);
},
);
// TODO: Attach on-press observer so this machine can do something
// in response to that button being pressed
});
@@ -47,7 +61,7 @@ impl SpawnUi for RotatingMachine {
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) {
let base_entity = machine_ui_base(&mut commands, "Rotating Machine", &theme);
commands.entity(base_entity).with_children(|commands| {
commands.spawn(fuel_gauge_placeholder());
commands.spawn(FuelGauge::bundle(Entity::PLACEHOLDER));
// Center panel (placeholder for input-output rotation)
commands.spawn((
@@ -82,7 +96,7 @@ impl SpawnUi for FlippingMachine {
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) {
let base_entity = machine_ui_base(&mut commands, "Flipping Machine", &theme);
commands.entity(base_entity).with_children(|commands| {
commands.spawn(fuel_gauge_placeholder());
commands.spawn(FuelGauge::bundle(Entity::PLACEHOLDER));
// Center panel (placeholder)
commands.spawn((
@@ -117,7 +131,7 @@ impl SpawnUi for TransposingMachine {
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) {
let base_entity = machine_ui_base(&mut commands, "Transposing Machine", &theme);
commands.entity(base_entity).with_children(|commands| {
commands.spawn(fuel_gauge_placeholder());
commands.spawn(FuelGauge::bundle(Entity::PLACEHOLDER));
// Center panel (placeholder)
commands.spawn((
@@ -147,15 +161,3 @@ impl SpawnUi for TransposingMachine {
});
}
}
fn fuel_gauge_placeholder() -> impl Bundle {
(
Node {
padding: UiRect::all(Px(10.0)),
..default()
},
BackgroundColor(GREEN.into()),
Pickable::default(),
children![(Text::new("Uses: <n>"), TextColor(BLACK.into()),)],
)
}