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:
@@ -34,4 +34,7 @@ pub mod consumables {
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Fuel(u32);
|
||||
|
||||
#[derive(Event)]
|
||||
pub struct FuelChanged;
|
||||
}
|
||||
|
||||
@@ -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(),
|
||||
|
||||
32
src/widgets/fuel_gauge.rs
Normal file
32
src/widgets/fuel_gauge.rs
Normal file
@@ -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("<n>"),],
|
||||
)
|
||||
}
|
||||
|
||||
pub fn detect_fuel_change(event: Trigger<FuelChanged>) {
|
||||
dbg!(format!(
|
||||
"Detected fuel change on entity ID: {}",
|
||||
event.target()
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -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()),)],
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user