6 Commits

Author SHA1 Message Date
8f6dfc3e49 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.
2025-08-28 12:01:30 -05:00
e169750923 Fill out UI parts for the remaining machines 2025-08-27 15:27:09 -05:00
c7fd053fd1 Add other machine placeholder sprites 2025-08-27 13:48:55 -05:00
0c254b2ed0 Expand the dummy_machines spawner
Spawn a machine entity for each kind of machine. This is where the trait
impl becomes important -- now the compiler will make copies of that
function for each concrete machine struct so I don't have to.
2025-08-27 13:37:36 -05:00
3b0dad7063 Impl SpawnUi for the machine structs
Just add the `SpawnUi for` text to the impl blocks on the machine
widgets.
2025-08-27 13:35:31 -05:00
04fb8519f6 Trait SpawnUi for things that can spawn a UI
I don't want to copy-paste a bunch of code, so I'm going to get the
compiler to do it for me.
2025-08-27 13:34:31 -05:00
5 changed files with 187 additions and 48 deletions

View File

@@ -34,4 +34,7 @@ pub mod consumables {
#[derive(Component)]
pub struct Fuel(u32);
#[derive(Event)]
pub struct FuelChanged;
}

View File

@@ -1,9 +1,10 @@
use bevy::{color::palettes::css::GREEN, prelude::*, window::WindowResolution};
use bevy::{color::palettes::css::*, prelude::*, window::WindowResolution};
use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
use crate::{
game::machines::{CuttingMachine, RotatingMachine},
game::machines::{CuttingMachine, FlippingMachine, RotatingMachine, TransposingMachine},
resources::UiTheme,
widgets::SpawnUi,
};
mod assets;
@@ -24,7 +25,7 @@ fn main() {
.add_plugins(EguiPlugin::default())
.add_plugins(WorldInspectorPlugin::new())
.add_plugins(widgets::GameUiPlugin)
.add_systems(Startup, (setup, assets::load_assets, dummy_machine))
.add_systems(Startup, (setup, assets::load_assets, dummy_machines))
.run();
}
@@ -39,26 +40,51 @@ fn despawn<T: Component>(mut commands: Commands, to_despawn: Query<Entity, With<
}
}
fn dummy_machine(
mut commands: Commands,
// mut meshes: ResMut<Assets<Mesh>>,
// mut materials: ResMut<Assets<ColorMaterial>>,
) {
/// Dev tool for spawning dummy entities. I'm developing their UIs by hooking
/// up to these while the TileMap gets ready.
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(),
))
.observe(spawn_machine_ui::<CuttingMachine>);
commands
.spawn((
Sprite::from_color(GREEN, Vec2::splat(40.0)),
// WARN: Mesh picking is not part of the `DefaultPlugins` plugin collection!
// (but sprite picking is!)
// Mesh2d(meshes.add(Rectangle::new(25., 25.))),
// MeshMaterial2d(materials.add(ColorMaterial::from_color(GREEN))),
Transform::default(),
Transform::from_translation(Vec3::new(40.0, 40.0, 0.0)),
Pickable::default(),
))
.observe(
|event: Trigger<Pointer<Click>>, commands: Commands, theme: Res<UiTheme>| {
let entity = event.target;
CuttingMachine::spawn_ui(commands, theme);
},
);
.observe(spawn_machine_ui::<RotatingMachine>);
// TODO: The other observers, once they have a spawner struct & impl.
commands
.spawn((
Sprite::from_color(PURPLE, Vec2::splat(40.)),
Transform::from_translation(Vec3::new(-40.0, -40.0, 0.0)),
Pickable::default(),
))
.observe(spawn_machine_ui::<FlippingMachine>);
commands
.spawn((
Sprite::from_color(DARK_ORANGE, Vec2::splat(40.)),
Transform::from_translation(Vec3::new(40.0, -40.0, 0.0)),
Pickable::default(),
))
.observe(spawn_machine_ui::<TransposingMachine>);
}
fn spawn_machine_ui<M: SpawnUi>(
event: Trigger<Pointer<Click>>,
commands: Commands,
theme: Res<UiTheme>,
) {
let _entity = event.target;
// TODO: Hook up the Fuel component (and spawn one so that I can)
M::spawn_ui(commands, theme);
}

32
src/widgets/fuel_gauge.rs Normal file
View 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()
));
}
}

View File

@@ -1,25 +1,18 @@
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, machine_ui_base},
widgets::{BigRedButton, SpawnUi, fuel_gauge::FuelGauge, machine_ui_base},
};
impl CuttingMachine {
pub fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) {
impl SpawnUi for CuttingMachine {
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) {
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((
Node {
padding: UiRect::all(Px(10.0)),
..default()
},
BackgroundColor(GREEN.into()),
Pickable::default(),
children![(Text::new("Uses: <n>"), TextColor(BLACK.into()),)],
));
// 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((
@@ -43,7 +36,20 @@ impl 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
});
@@ -51,19 +57,11 @@ impl CuttingMachine {
}
}
impl RotatingMachine {
pub fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) {
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((
Node {
padding: UiRect::all(Px(10.0)),
..Default::default()
},
BackgroundColor(GREEN.into()),
Pickable::default(),
children![(Text::new("Uses: <n>"), TextColor(BLACK.into()))],
));
commands.spawn(FuelGauge::bundle(Entity::PLACEHOLDER));
// Center panel (placeholder for input-output rotation)
commands.spawn((
@@ -93,3 +91,73 @@ impl RotatingMachine {
});
}
}
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(FuelGauge::bundle(Entity::PLACEHOLDER));
// Center panel (placeholder)
commands.spawn((
Node::default(),
BackgroundColor(BLUE.into()),
Pickable::default(),
children![
Text::new("Card Flipping placeholder"),
TextColor(MAGENTA.into()),
],
));
// Right panel go button
commands
.spawn((
Node {
align_items: AlignItems::End,
..Default::default()
},
BackgroundColor(DARK_GRAY.into()),
Pickable::default(),
))
.with_children(|cmds| {
let _button_cmds = cmds.spawn(BigRedButton::bundle("FLIP"));
// TODO: Attach on-press observer to the button.
});
});
}
}
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(FuelGauge::bundle(Entity::PLACEHOLDER));
// Center panel (placeholder)
commands.spawn((
Node::default(),
BackgroundColor(BLUE.into()),
Pickable::default(),
children![
Text::new("Card Transposition placeholder"),
TextColor(MAGENTA.into()),
],
));
// Right panel go button
commands
.spawn((
Node {
align_items: AlignItems::End,
..Default::default()
},
BackgroundColor(DARK_GRAY.into()),
Pickable::default(),
))
.with_children(|cmds| {
let _button_cmds = cmds.spawn(BigRedButton::bundle("SWAP"));
// TODO: Attach on-press observer to the button.
});
});
}
}

View File

@@ -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,10 +25,19 @@ 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);
}
}
/// Anything that can spawn a themed UI should impl this trait.
///
/// 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>);
}
/// The base panel for the machines that manipulate the room cards.
///
/// This function is not a valid Bevy System because of how the Commands struct