From 4f6e054630e67c6d3ca0962d7f5b96e86bec00ef Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Tue, 5 Aug 2025 10:29:21 -0700 Subject: [PATCH] Tooltip over 2d meshes --- src/bin/flappy/main.rs | 55 +++++++++++++++++++++++++++--------------- src/debug.rs | 6 ++--- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/src/bin/flappy/main.rs b/src/bin/flappy/main.rs index 2d61e70..cd3340b 100644 --- a/src/bin/flappy/main.rs +++ b/src/bin/flappy/main.rs @@ -80,11 +80,12 @@ fn main() { sync_resource_to_ui::.run_if(resource_changed::), ), scoring.run_if(on_event::), - create_forward_batches.run_if(on_event::).run_if(in_state(PlayerState::Alive)), + manage_batches.run_if(on_event::).run_if(in_state(PlayerState::Alive).or(in_state(PlayerState::Rewind))), ), ) .add_observer(flap) .add_observer(populate_batch) + .add_observer(update_batch) .add_observer(populate_pipe) .add_observer(populate_ground) .add_observer(populate_hitbox) @@ -218,6 +219,17 @@ fn populate_batch( } } +fn update_batch( + trigger: Trigger, + mut root: Query<(&mut Transform, &Batch), With>, +) { + if let Ok((mut t, Batch(idx))) = root.get_mut(trigger.target()) { + info!("Updating batch {:?}", idx); + t.translation.x = 500.0 * (*idx) as f32; + } + // todo!("Adjust pipe positions"); +} + /// The ground population spawns a center peace and two pieces of ground /// to the left and right of the center. fn populate_ground( @@ -227,12 +239,13 @@ fn populate_ground( mut commands: Commands, ) { let Ground(idx) = grounds.get(trigger.target()).unwrap(); + info!("populating ground {:?}", idx); commands.entity(trigger.target()).insert(( ground_assets.material.clone(), ground_assets.mesh.clone(), Name::new("ground"), RigidBody::Static, Collider::rectangle(1.0, 1.0), - Transform::from_xyz(100.0 * (*idx) as f32, -400.0, -1.0).with_scale(Vec3::splat(100.0)) + Transform::from_xyz(100.0 * (*idx) as f32, -300.0, -1.0).with_scale(Vec3::splat(100.0)) )); } @@ -655,29 +668,33 @@ fn scoring( /// When the player moves forward while alive /// spawn more batches and despawn old batches -fn create_forward_batches( +fn manage_batches( mut events: EventReader, state: Res>, bird: Query>, - hitboxes: Query<&Batch, With>, - batches: Query<(Entity, &Batch), (With, Without)>, + batches: Query<(Entity, &Batch)>, mut commands: Commands, ) { - debug_assert!(*state.get() == PlayerState::Alive); + debug_assert!(matches!(state.get(), PlayerState::Alive) || matches!(state.get(), PlayerState::Rewind)); events .read() - .filter(|CollisionStarted(a, b)| bird.contains(*a) && hitboxes.contains(*b)) - .for_each(|CollisionStarted(_, b)| { - let Batch(idx) = hitboxes.get(*b).unwrap(); - info!("Batch {:?}", idx); - if *idx > 2 { - // Spawn future batch - commands.spawn(Batch(idx + 2)); - - // Despawn old batch - if let Some((despawn_batch, _)) = batches.iter().find(|(_, Batch(n))| *n == idx - 2) { - commands.entity(despawn_batch).despawn(); - } - } + // This is written in a wonky way to avoid borrow checker rules + // Instaed of updating the Batch in place we use Commands to get it and upsert a new batch + .filter_map(|CollisionStarted(a, b)| { + if bird.contains(*a) && let Ok((e, Batch(idx))) = batches.get(*b) && *idx > 2 { + Some((e, idx)) + } else { None } + }).for_each(|(_, idx)| { + let (old_batch_idx, new_batch_idx) = match state.get() { + PlayerState::Alive => (idx - 2, idx + 2), + PlayerState::Rewind => (idx + 2, idx - 2), + _ => panic!("Should not happen!"), + }; + // Find all entities with the old batch and adjust them + batches.iter().filter_map(|(e, batch)| { + (batch.0 == old_batch_idx).then_some(e) + }).for_each(|e| { + commands.entity(e).insert(Batch(new_batch_idx)); + }) }); } diff --git a/src/debug.rs b/src/debug.rs index a710e90..8733597 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -196,7 +196,7 @@ impl Display for Notice { } /// Add a generic Tooltip that follows the mouse in debug mode -#[derive(Default, Resource)] +#[derive(Default, Resource, Component)] pub struct ToolTip(HashMap); impl ToolTip { @@ -236,7 +236,7 @@ fn hover_mesh( mut over_events: EventReader>, mut out_events: EventReader>, mut tooltip: ResMut, - meshes: Query<(&Transform, Option<&Name>), With>, + meshes: Query<(&Transform, Option<&Name>), Or<(With, With)>>, ) { out_events .read() @@ -257,7 +257,7 @@ fn hover_mesh( None => "N/A", }; tooltip.insert("ID", format!("{e}")); - tooltip.insert("Pos", format!("{pos:.3?}")); + tooltip.insert("Pos", format!("{pos:.2?}")); tooltip.insert("Name", name.into()); } else { warn!("Failed to query data");