3 Commits

Author SHA1 Message Date
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
3 changed files with 42 additions and 24 deletions

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 bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
use crate::{ use crate::{
game::machines::{CuttingMachine, RotatingMachine}, game::machines::{CuttingMachine, RotatingMachine},
resources::UiTheme, resources::UiTheme,
widgets::SpawnUi,
}; };
mod assets; mod assets;
@@ -24,7 +25,7 @@ fn main() {
.add_plugins(EguiPlugin::default()) .add_plugins(EguiPlugin::default())
.add_plugins(WorldInspectorPlugin::new()) .add_plugins(WorldInspectorPlugin::new())
.add_plugins(widgets::GameUiPlugin) .add_plugins(widgets::GameUiPlugin)
.add_systems(Startup, (setup, assets::load_assets, dummy_machine)) .add_systems(Startup, (setup, assets::load_assets, dummy_machines))
.run(); .run();
} }
@@ -39,26 +40,35 @@ fn despawn<T: Component>(mut commands: Commands, to_despawn: Query<Entity, With<
} }
} }
fn dummy_machine( /// Dev tool for spawning dummy entities. I'm developing their UIs by hooking
mut commands: Commands, /// up to these while the TileMap gets ready.
// mut meshes: ResMut<Assets<Mesh>>, fn dummy_machines(mut commands: Commands) {
// mut materials: ResMut<Assets<ColorMaterial>>, // Spawn a dummy Cutting Machine
) {
commands commands
.spawn(( .spawn((
Sprite::from_color(GREEN, Vec2::splat(40.0)), Sprite::from_color(RED, 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::default(),
Pickable::default(), Pickable::default(),
)) ))
.observe( .observe(spawn_machine_ui::<CuttingMachine>);
|event: Trigger<Pointer<Click>>, commands: Commands, theme: Res<UiTheme>| {
let entity = event.target; commands
CuttingMachine::spawn_ui(commands, theme); .spawn((
}, Sprite::from_color(GREEN, Vec2::splat(40.0)),
); Transform::from_translation(Vec3::new(80.0, 0.0, 0.0)),
Pickable::default(),
))
.observe(spawn_machine_ui::<RotatingMachine>);
// TODO: The other machines, once they have a spawner struct & impl.
}
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);
} }

View File

@@ -3,11 +3,11 @@ use bevy::{color::palettes::css::*, prelude::*, ui::Val::*};
use crate::{ use crate::{
game::machines::*, game::machines::*,
resources::UiTheme, resources::UiTheme,
widgets::{BigRedButton, machine_ui_base}, widgets::{BigRedButton, SpawnUi, machine_ui_base},
}; };
impl CuttingMachine { impl SpawnUi for CuttingMachine {
pub fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) { fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) {
let base_entity = machine_ui_base(&mut commands, "Cutting Machine", &theme); let base_entity = machine_ui_base(&mut commands, "Cutting Machine", &theme);
commands.entity(base_entity).with_children(|commands| { commands.entity(base_entity).with_children(|commands| {
// Left panel. For fuel or machine stats or whatever. // Left panel. For fuel or machine stats or whatever.
@@ -51,8 +51,8 @@ impl CuttingMachine {
} }
} }
impl RotatingMachine { impl SpawnUi for RotatingMachine {
pub fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) { fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) {
let base_entity = machine_ui_base(&mut commands, "Rotating Machine", &theme); let base_entity = machine_ui_base(&mut commands, "Rotating Machine", &theme);
commands.entity(base_entity).with_children(|commands| { commands.entity(base_entity).with_children(|commands| {
commands.spawn(( commands.spawn((

View File

@@ -28,6 +28,14 @@ impl Plugin for GameUiPlugin {
} }
} }
/// 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. /// 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 /// This function is not a valid Bevy System because of how the Commands struct