Compare commits
5 Commits
f470687494
...
trunk
| Author | SHA1 | Date | |
|---|---|---|---|
| ea9055e46c | |||
| be9cb3fe69 | |||
| 6fb3539329 | |||
| fb8a27325e | |||
| 4a80b4f4ad |
@@ -16,16 +16,22 @@ pub mod machines {
|
||||
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::game::consumables::Fuel;
|
||||
|
||||
#[derive(Component)]
|
||||
#[require(Fuel)]
|
||||
pub struct CuttingMachine;
|
||||
|
||||
#[derive(Component)]
|
||||
#[require(Fuel)]
|
||||
pub struct RotatingMachine;
|
||||
|
||||
#[derive(Component)]
|
||||
#[require(Fuel)]
|
||||
pub struct FlippingMachine;
|
||||
|
||||
#[derive(Component)]
|
||||
#[require(Fuel)]
|
||||
pub struct TransposingMachine;
|
||||
}
|
||||
|
||||
@@ -35,6 +41,11 @@ pub mod consumables {
|
||||
#[derive(Component)]
|
||||
pub struct Fuel(pub u32);
|
||||
|
||||
impl Default for Fuel {
|
||||
fn default() -> Self {
|
||||
Self(5)
|
||||
}
|
||||
}
|
||||
#[derive(Event)]
|
||||
pub struct FuelChanged;
|
||||
}
|
||||
|
||||
@@ -2,10 +2,7 @@ use bevy::{color::palettes::css::*, prelude::*, window::WindowResolution};
|
||||
use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
|
||||
|
||||
use crate::{
|
||||
game::{
|
||||
consumables::Fuel,
|
||||
machines::{CuttingMachine, FlippingMachine, RotatingMachine, TransposingMachine},
|
||||
},
|
||||
game::machines::{CuttingMachine, FlippingMachine, RotatingMachine, TransposingMachine},
|
||||
resources::UiTheme,
|
||||
widgets::SpawnUi,
|
||||
};
|
||||
@@ -50,7 +47,6 @@ fn dummy_machines(mut commands: Commands) {
|
||||
commands
|
||||
.spawn((
|
||||
CuttingMachine,
|
||||
Fuel(10),
|
||||
Sprite::from_color(RED, Vec2::splat(40.0)),
|
||||
Transform::from_translation(Vec3::new(-40.0, 40.0, 0.0)),
|
||||
Pickable::default(),
|
||||
@@ -59,6 +55,7 @@ fn dummy_machines(mut commands: Commands) {
|
||||
|
||||
commands
|
||||
.spawn((
|
||||
RotatingMachine,
|
||||
Sprite::from_color(GREEN, Vec2::splat(40.0)),
|
||||
Transform::from_translation(Vec3::new(40.0, 40.0, 0.0)),
|
||||
Pickable::default(),
|
||||
@@ -68,6 +65,7 @@ fn dummy_machines(mut commands: Commands) {
|
||||
// TODO: The other observers, once they have a spawner struct & impl.
|
||||
commands
|
||||
.spawn((
|
||||
FlippingMachine,
|
||||
Sprite::from_color(PURPLE, Vec2::splat(40.)),
|
||||
Transform::from_translation(Vec3::new(-40.0, -40.0, 0.0)),
|
||||
Pickable::default(),
|
||||
@@ -76,6 +74,7 @@ fn dummy_machines(mut commands: Commands) {
|
||||
|
||||
commands
|
||||
.spawn((
|
||||
TransposingMachine,
|
||||
Sprite::from_color(DARK_ORANGE, Vec2::splat(40.)),
|
||||
Transform::from_translation(Vec3::new(40.0, -40.0, 0.0)),
|
||||
Pickable::default(),
|
||||
|
||||
@@ -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};
|
||||
|
||||
#[derive(Component)]
|
||||
#[require(Label)]
|
||||
#[component(on_add = FuelGauge::refresh_label)]
|
||||
pub struct FuelGauge(Entity);
|
||||
|
||||
impl FuelGauge {
|
||||
@@ -41,4 +47,23 @@ impl FuelGauge {
|
||||
);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ impl SpawnUi for RotatingMachine {
|
||||
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>, machine_id: Entity) {
|
||||
let base_entity = machine_ui_base(&mut commands, "Rotating Machine", &theme);
|
||||
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)
|
||||
commands.spawn((
|
||||
@@ -87,7 +87,7 @@ impl SpawnUi for FlippingMachine {
|
||||
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>, machine_id: Entity) {
|
||||
let base_entity = machine_ui_base(&mut commands, "Flipping Machine", &theme);
|
||||
commands.entity(base_entity).with_children(|commands| {
|
||||
commands.spawn(FuelGauge::bundle(Entity::PLACEHOLDER));
|
||||
commands.spawn(FuelGauge::bundle(machine_id));
|
||||
|
||||
// Center panel (placeholder)
|
||||
commands.spawn((
|
||||
@@ -122,7 +122,7 @@ impl SpawnUi for TransposingMachine {
|
||||
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>, machine_id: Entity) {
|
||||
let base_entity = machine_ui_base(&mut commands, "Transposing Machine", &theme);
|
||||
commands.entity(base_entity).with_children(|commands| {
|
||||
commands.spawn(FuelGauge::bundle(Entity::PLACEHOLDER));
|
||||
commands.spawn(FuelGauge::bundle(machine_id));
|
||||
|
||||
// Center panel (placeholder)
|
||||
commands.spawn((
|
||||
|
||||
Reference in New Issue
Block a user