Display shape and orientation for debugging

main
Elijah Voigt 2 weeks ago
parent 78854196a1
commit fb535106ca

@ -16,7 +16,7 @@ fn main() {
..default() ..default()
}) })
.init_state::<Falling>() .init_state::<Falling>()
.add_systems(Startup, init_pieces) .add_systems(Startup, (init_pieces, init_debug_ui))
.add_systems( .add_systems(
Update, Update,
( (
@ -35,6 +35,8 @@ fn main() {
any_component_added::<RelativePosition> any_component_added::<RelativePosition>
.or(any_component_changed::<RelativePosition>), .or(any_component_changed::<RelativePosition>),
), ),
sync_singleton_to_ui::<Shape>.run_if(any_component_changed::<Shape>),
sync_singleton_to_ui::<Orientation>.run_if(any_component_changed::<Orientation>),
), ),
) )
.add_systems(Update, draw_grid) .add_systems(Update, draw_grid)
@ -44,8 +46,9 @@ fn main() {
const SCALE: f32 = 30.0; const SCALE: f32 = 30.0;
/// A shape, e.g., the long piece /// A shape, e.g., the long piece
#[derive(Component, Debug)] #[derive(Component, Debug, Default)]
enum Shape { enum Shape {
#[default]
O, O,
T, T,
L, L,
@ -55,6 +58,20 @@ enum Shape {
I, I,
} }
impl Display for Shape {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Shape::O => write!(f, "O"),
Shape::T => write!(f, "T"),
Shape::L => write!(f, "L"),
Shape::J => write!(f, "J"),
Shape::S => write!(f, "S"),
Shape::Z => write!(f, "Z"),
Shape::I => write!(f, "I"),
}
}
}
/// A part of a piece, i.e., a single square of a piece /// A part of a piece, i.e., a single square of a piece
#[derive(Component, Debug)] #[derive(Component, Debug)]
struct ShapePiece; struct ShapePiece;
@ -189,6 +206,17 @@ impl Orientation {
} }
} }
impl Display for Orientation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Orientation::Up => write!(f, "up"),
Orientation::Down => write!(f, "down"),
Orientation::Left => write!(f, "<-"),
Orientation::Right => write!(f, "->"),
}
}
}
#[derive(States, Clone, Eq, PartialEq, Debug, Hash, Default, Component)] #[derive(States, Clone, Eq, PartialEq, Debug, Hash, Default, Component)]
enum Falling { enum Falling {
#[default] #[default]
@ -218,13 +246,31 @@ fn init_pieces(
commands.spawn((Orientation::default(), GridPosition::default(), Shape::T)); commands.spawn((Orientation::default(), GridPosition::default(), Shape::T));
} }
fn init_debug_ui(
mut commands: Commands,
) {
commands.spawn((Node {
top: Val::Px(0.0),
left: Val::Px(0.0),
..default()
}, DebuggingState::On)).with_children(|parent| {
parent.spawn((
Node::default(),
children![
(Text::new("SHAPE"), SyncSingleton::<Shape>::default()),
(Text::new("ORIENTATION"), SyncSingleton::<Orientation>::default()),
]
));
});
}
fn set_piece( fn set_piece(
query: Query<(Entity, &Shape, &Orientation), Or<(Added<Shape>, Changed<Shape>, Added<Orientation>, Changed<Orientation>)>>, query: Query<(Entity, &Shape, &Orientation), Or<(Added<Shape>, Changed<Shape>, Added<Orientation>, Changed<Orientation>)>>,
mut commands: Commands, mut commands: Commands,
visuals: Res<Visuals>, visuals: Res<Visuals>,
) { ) {
query.iter().for_each(|(e, s, o)| { query.iter().for_each(|(e, s, o)| {
info!("{e:?} {s:?} {o:?}"); debug!("{e:?} {s:?} {o:?}");
commands commands
.entity(e) .entity(e)
.despawn_related::<Children>() .despawn_related::<Children>()

@ -175,6 +175,23 @@ pub fn sync_resource_to_ui<R: Resource + Default + Display>(
}); });
} }
/// Marker component for handling Resource -> Ui Sync
#[derive(Component, Default, Debug)]
pub struct SyncSingleton<C: Component + Default + Display>(C);
/// Sync a singleton entity's component to the UI
///
/// Mostly useful for quick n' dirty getting data to the user
pub fn sync_singleton_to_ui<C: Component + Default + Display>(
mut q: Query<(&mut Text, &mut Visibility), With<SyncSingleton<C>>>,
c: Single<&C>,
) {
q.iter_mut().for_each(|(mut t, mut v)| {
t.0 = format!("{}", *c);
*v = Visibility::Inherited;
});
}
/// Updates the scroll position of scrollable nodes in response to mouse input /// Updates the scroll position of scrollable nodes in response to mouse input
pub fn scroll(trigger: Trigger<Pointer<Scroll>>, mut scrollers: Query<&mut ScrollPosition>) { pub fn scroll(trigger: Trigger<Pointer<Scroll>>, mut scrollers: Query<&mut ScrollPosition>) {
let Pointer { let Pointer {

Loading…
Cancel
Save