Compare commits
9 Commits
8f6dfc3e49
...
trunk
| Author | SHA1 | Date | |
|---|---|---|---|
| ea9055e46c | |||
| be9cb3fe69 | |||
| 6fb3539329 | |||
| fb8a27325e | |||
| 4a80b4f4ad | |||
| f470687494 | |||
| ed2e1e75ef | |||
| 21c00d4a02 | |||
| 5f1f283500 |
@@ -16,16 +16,22 @@ pub mod machines {
|
|||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
use crate::game::consumables::Fuel;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
|
#[require(Fuel)]
|
||||||
pub struct CuttingMachine;
|
pub struct CuttingMachine;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
|
#[require(Fuel)]
|
||||||
pub struct RotatingMachine;
|
pub struct RotatingMachine;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
|
#[require(Fuel)]
|
||||||
pub struct FlippingMachine;
|
pub struct FlippingMachine;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
|
#[require(Fuel)]
|
||||||
pub struct TransposingMachine;
|
pub struct TransposingMachine;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,8 +39,13 @@ pub mod consumables {
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Fuel(u32);
|
pub struct Fuel(pub u32);
|
||||||
|
|
||||||
|
impl Default for Fuel {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self(5)
|
||||||
|
}
|
||||||
|
}
|
||||||
#[derive(Event)]
|
#[derive(Event)]
|
||||||
pub struct FuelChanged;
|
pub struct FuelChanged;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ fn dummy_machines(mut commands: Commands) {
|
|||||||
|
|
||||||
commands
|
commands
|
||||||
.spawn((
|
.spawn((
|
||||||
|
RotatingMachine,
|
||||||
Sprite::from_color(GREEN, Vec2::splat(40.0)),
|
Sprite::from_color(GREEN, Vec2::splat(40.0)),
|
||||||
Transform::from_translation(Vec3::new(40.0, 40.0, 0.0)),
|
Transform::from_translation(Vec3::new(40.0, 40.0, 0.0)),
|
||||||
Pickable::default(),
|
Pickable::default(),
|
||||||
@@ -64,6 +65,7 @@ fn dummy_machines(mut commands: Commands) {
|
|||||||
// TODO: The other observers, once they have a spawner struct & impl.
|
// TODO: The other observers, once they have a spawner struct & impl.
|
||||||
commands
|
commands
|
||||||
.spawn((
|
.spawn((
|
||||||
|
FlippingMachine,
|
||||||
Sprite::from_color(PURPLE, Vec2::splat(40.)),
|
Sprite::from_color(PURPLE, Vec2::splat(40.)),
|
||||||
Transform::from_translation(Vec3::new(-40.0, -40.0, 0.0)),
|
Transform::from_translation(Vec3::new(-40.0, -40.0, 0.0)),
|
||||||
Pickable::default(),
|
Pickable::default(),
|
||||||
@@ -72,6 +74,7 @@ fn dummy_machines(mut commands: Commands) {
|
|||||||
|
|
||||||
commands
|
commands
|
||||||
.spawn((
|
.spawn((
|
||||||
|
TransposingMachine,
|
||||||
Sprite::from_color(DARK_ORANGE, Vec2::splat(40.)),
|
Sprite::from_color(DARK_ORANGE, Vec2::splat(40.)),
|
||||||
Transform::from_translation(Vec3::new(40.0, -40.0, 0.0)),
|
Transform::from_translation(Vec3::new(40.0, -40.0, 0.0)),
|
||||||
Pickable::default(),
|
Pickable::default(),
|
||||||
@@ -86,5 +89,5 @@ fn spawn_machine_ui<M: SpawnUi>(
|
|||||||
) {
|
) {
|
||||||
let _entity = event.target;
|
let _entity = event.target;
|
||||||
// TODO: Hook up the Fuel component (and spawn one so that I can)
|
// TODO: Hook up the Fuel component (and spawn one so that I can)
|
||||||
M::spawn_ui(commands, theme);
|
M::spawn_ui(commands, theme, event.target());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,15 @@
|
|||||||
use bevy::{color::palettes::css::*, prelude::*, ui::Val::*};
|
use bevy::{
|
||||||
|
color::palettes::css::*,
|
||||||
|
ecs::{component::HookContext, world::DeferredWorld},
|
||||||
|
prelude::*,
|
||||||
|
ui::Val::*,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::game::consumables::{Fuel, FuelChanged};
|
use crate::game::consumables::{Fuel, FuelChanged};
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
#[require(Label)]
|
#[require(Label)]
|
||||||
|
#[component(on_add = FuelGauge::refresh_label)]
|
||||||
pub struct FuelGauge(Entity);
|
pub struct FuelGauge(Entity);
|
||||||
|
|
||||||
impl FuelGauge {
|
impl FuelGauge {
|
||||||
@@ -19,14 +25,45 @@ impl FuelGauge {
|
|||||||
},
|
},
|
||||||
BackgroundColor(GREEN.into()),
|
BackgroundColor(GREEN.into()),
|
||||||
Text::new("Fuel: "),
|
Text::new("Fuel: "),
|
||||||
children![TextSpan::new("<n>"),],
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn detect_fuel_change(event: Trigger<FuelChanged>) {
|
/// Observer system to update the [`FuelGauge`] widget when a machine emits a
|
||||||
dbg!(format!(
|
/// [`FuelChanged`] event.
|
||||||
"Detected fuel change on entity ID: {}",
|
pub fn detect_fuel_change(
|
||||||
event.target()
|
event: Trigger<FuelChanged>,
|
||||||
));
|
fuel_levels: Query<&Fuel>,
|
||||||
|
mut gauges: Query<(&FuelGauge, &mut Text)>,
|
||||||
|
) {
|
||||||
|
// Find the FuelGauge that references the same Entity as the event target.
|
||||||
|
// That gauge's `Text` is the one to update.
|
||||||
|
let (_, mut label) = gauges
|
||||||
|
.iter_mut()
|
||||||
|
.find(|(gauge, _label)| gauge.0 == event.target())
|
||||||
|
.expect("Couldn't find any fuel gauges");
|
||||||
|
|
||||||
|
let fuel = fuel_levels.get(event.target()).expect(
|
||||||
|
"Logic error: a `FuelChanged` event was targetted at an entity with no `Fuel` component.",
|
||||||
|
);
|
||||||
|
label.0 = format!("Fuel: {}", fuel.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn refresh_label(mut world: DeferredWorld, ctx: HookContext) {
|
||||||
|
// Get the machine ID from the FuelGauge component.
|
||||||
|
let Some(machine_id) = world.get::<FuelGauge>(ctx.entity).map(|fg| fg.0) else {
|
||||||
|
panic!("Couldn't get a FuelGauge during it's on-add hook run!");
|
||||||
|
};
|
||||||
|
// and fuel level
|
||||||
|
let Some(fuel) = world.get::<Fuel>(machine_id).map(|lvl| lvl.0) else {
|
||||||
|
// TODO: Maybe don't panic and just emit a warning.
|
||||||
|
panic!("Couldn't get a Fuel for a machine during a FuelGauge on-add hook.");
|
||||||
|
};
|
||||||
|
|
||||||
|
// Get an excl ref to the Text component so we can redraw its text
|
||||||
|
let Some(mut label) = world.get_mut::<Text>(ctx.entity) else {
|
||||||
|
panic!()
|
||||||
|
};
|
||||||
|
|
||||||
|
label.0 = format!("Fuel: {}", fuel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,12 +7,12 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
impl SpawnUi for CuttingMachine {
|
impl SpawnUi for CuttingMachine {
|
||||||
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) {
|
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>, machine_id: Entity) {
|
||||||
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.
|
||||||
// TODO: Pass along target machine, not the UI's root entity.
|
// TODO: Pass along target machine, not the UI's root entity.
|
||||||
commands.spawn(FuelGauge::bundle(Entity::PLACEHOLDER));
|
commands.spawn(FuelGauge::bundle(machine_id));
|
||||||
|
|
||||||
// Center panel (placeholder for the Card view)
|
// Center panel (placeholder for the Card view)
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
@@ -37,19 +37,10 @@ impl SpawnUi for CuttingMachine {
|
|||||||
))
|
))
|
||||||
.with_children(|cmds| {
|
.with_children(|cmds| {
|
||||||
let mut button_cmds = cmds.spawn(BigRedButton::bundle("CUT"));
|
let mut button_cmds = cmds.spawn(BigRedButton::bundle("CUT"));
|
||||||
button_cmds.observe(
|
button_cmds.observe(move |trigger: Trigger<Pointer<Click>>, mut com: Commands| {
|
||||||
|trigger: Trigger<Pointer<Click>>,
|
|
||||||
mut com: Commands,
|
|
||||||
q: Single<(Entity, &CuttingMachine)>| {
|
|
||||||
dbg!("Cut button pressed. Triggering a FuelChanged event");
|
dbg!("Cut button pressed. Triggering a FuelChanged event");
|
||||||
|
com.trigger_targets(FuelChanged, machine_id);
|
||||||
// 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
|
// TODO: Attach on-press observer so this machine can do something
|
||||||
// in response to that button being pressed
|
// in response to that button being pressed
|
||||||
});
|
});
|
||||||
@@ -58,10 +49,10 @@ impl SpawnUi for CuttingMachine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SpawnUi for RotatingMachine {
|
impl SpawnUi for RotatingMachine {
|
||||||
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) {
|
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>, machine_id: Entity) {
|
||||||
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(FuelGauge::bundle(Entity::PLACEHOLDER));
|
commands.spawn(FuelGauge::bundle(machine_id));
|
||||||
|
|
||||||
// Center panel (placeholder for input-output rotation)
|
// Center panel (placeholder for input-output rotation)
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
@@ -93,10 +84,10 @@ impl SpawnUi for RotatingMachine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SpawnUi for FlippingMachine {
|
impl SpawnUi for FlippingMachine {
|
||||||
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) {
|
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>, machine_id: Entity) {
|
||||||
let base_entity = machine_ui_base(&mut commands, "Flipping Machine", &theme);
|
let base_entity = machine_ui_base(&mut commands, "Flipping Machine", &theme);
|
||||||
commands.entity(base_entity).with_children(|commands| {
|
commands.entity(base_entity).with_children(|commands| {
|
||||||
commands.spawn(FuelGauge::bundle(Entity::PLACEHOLDER));
|
commands.spawn(FuelGauge::bundle(machine_id));
|
||||||
|
|
||||||
// Center panel (placeholder)
|
// Center panel (placeholder)
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
@@ -128,10 +119,10 @@ impl SpawnUi for FlippingMachine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SpawnUi for TransposingMachine {
|
impl SpawnUi for TransposingMachine {
|
||||||
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>) {
|
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>, machine_id: Entity) {
|
||||||
let base_entity = machine_ui_base(&mut commands, "Transposing Machine", &theme);
|
let base_entity = machine_ui_base(&mut commands, "Transposing Machine", &theme);
|
||||||
commands.entity(base_entity).with_children(|commands| {
|
commands.entity(base_entity).with_children(|commands| {
|
||||||
commands.spawn(FuelGauge::bundle(Entity::PLACEHOLDER));
|
commands.spawn(FuelGauge::bundle(machine_id));
|
||||||
|
|
||||||
// Center panel (placeholder)
|
// Center panel (placeholder)
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
|
|||||||
@@ -30,12 +30,12 @@ impl Plugin for GameUiPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Anything that can spawn a themed UI should impl this trait.
|
/// Machines spawn their UI through this trait.
|
||||||
///
|
///
|
||||||
/// This exists mainly so that I can write generic functions and have the *compiler*
|
/// 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.
|
/// expand the code for me, instead of doing it by hand.
|
||||||
pub trait SpawnUi {
|
pub trait SpawnUi {
|
||||||
fn spawn_ui(commands: Commands, theme: Res<UiTheme>);
|
fn spawn_ui(commands: Commands, theme: Res<UiTheme>, machine_id: Entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The base panel for the machines that manipulate the room cards.
|
/// The base panel for the machines that manipulate the room cards.
|
||||||
|
|||||||
Reference in New Issue
Block a user