diff --git a/src/game/mod.rs b/src/game/mod.rs index 159a5b7..74791e3 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -34,4 +34,7 @@ pub mod consumables { #[derive(Component)] pub struct Fuel(u32); + + #[derive(Event)] + pub struct FuelChanged; } diff --git a/src/main.rs b/src/main.rs index ee33d3b..99e92e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,6 +46,7 @@ fn dummy_machines(mut commands: Commands) { // Spawn a dummy Cutting Machine commands .spawn(( + CuttingMachine, Sprite::from_color(RED, Vec2::splat(40.0)), Transform::from_translation(Vec3::new(-40.0, 40.0, 0.0)), Pickable::default(), diff --git a/src/widgets/fuel_gauge.rs b/src/widgets/fuel_gauge.rs new file mode 100644 index 0000000..f57cf3e --- /dev/null +++ b/src/widgets/fuel_gauge.rs @@ -0,0 +1,32 @@ +use bevy::{color::palettes::css::*, prelude::*, ui::Val::*}; + +use crate::game::consumables::{Fuel, FuelChanged}; + +#[derive(Component)] +#[require(Label)] +pub struct FuelGauge(Entity); + +impl FuelGauge { + /// Default bundle for a machine's fuel gauge widget. Give it the ID of the + /// machine whose info it is displaying. + pub fn bundle(target: Entity) -> impl Bundle { + ( + FuelGauge(target), + Node { + min_width: Px(20.0), + min_height: Px(10.0), + ..default() + }, + 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() + )); + } +} diff --git a/src/widgets/machines.rs b/src/widgets/machines.rs index 5b86618..d684239 100644 --- a/src/widgets/machines.rs +++ b/src/widgets/machines.rs @@ -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>, + 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) { 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) { 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) { 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: "), TextColor(BLACK.into()),)], - ) -} diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs index dc14924..a6ff45f 100644 --- a/src/widgets/mod.rs +++ b/src/widgets/mod.rs @@ -1,10 +1,11 @@ //! Catch-all location for UI bits +mod fuel_gauge; pub mod machines; use bevy::{color::palettes::css::*, prelude::*, ui::Val::*}; -use crate::resources::UiTheme; +use crate::{resources::UiTheme, widgets::fuel_gauge::FuelGauge}; /// Plugin to set up systems & resources for the custom UI elements /// @@ -24,7 +25,8 @@ impl Plugin for GameUiPlugin { .add_observer(BigRedButton::button_hover_start) .add_observer(BigRedButton::button_hover_stop) .add_observer(BigRedButton::button_press_start) - .add_observer(BigRedButton::button_press_stop); + .add_observer(BigRedButton::button_press_stop) + .add_observer(FuelGauge::detect_fuel_change); } }