From 8002c8bae98b34b5d6e41334637ef9245c76f433 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Tue, 5 Aug 2025 13:37:50 -0700 Subject: [PATCH] Moving batches works forward but not in reverse I suspect it has to do with physics sensors not recognizing it in reverse... Maybe we can apply the physics backward instead of setting the transform? --- src/bin/flappy/main.rs | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/bin/flappy/main.rs b/src/bin/flappy/main.rs index 65db225..911d1a3 100644 --- a/src/bin/flappy/main.rs +++ b/src/bin/flappy/main.rs @@ -81,12 +81,12 @@ fn main() { ), scoring.run_if(on_event::), manage_batches.run_if(on_event::).run_if(in_state(PlayerState::Alive).or(in_state(PlayerState::Rewind))), + update_batch_position.run_if(any_component_changed::), update_tooltip.run_if(in_state(DebuggingState::On)), ), ) .add_observer(flap) .add_observer(populate_batch) - .add_observer(update_batch) .add_observer(populate_pipe) .add_observer(populate_ground) .add_observer(populate_hitbox) @@ -213,22 +213,21 @@ fn populate_batch( parent.spawn(Ground(1)); parent.spawn(Ground(2)); if *batch_id > 0 { - parent.spawn(Pipe::Top); - parent.spawn(Pipe::Bottom); + parent.spawn((Pipe::Top, Batch(*batch_id))); + parent.spawn((Pipe::Bottom, Batch(*batch_id))); parent.spawn((Hitbox, Batch(*batch_id))); } }); } } -fn update_batch( - trigger: Trigger, - mut root: Query<(&mut Transform, &Batch), With>, +fn update_batch_position( + mut root_changes: Query<(&mut Transform, &Batch), (Changed, Without)>, ) { - if let Ok((mut t, Batch(idx))) = root.get_mut(trigger.target()) { - debug!("Updating batch {:?}", idx); + root_changes.iter_mut().for_each(|(mut t, Batch(idx))|{ + info!("Updating batch {:?}", idx); t.translation.x = 500.0 * (*idx) as f32; - } + }); // todo!("Adjust pipe positions"); } @@ -652,19 +651,16 @@ fn scoring( mut events: EventReader, state: Res>, bird: Query>, - hitboxes: Query>, + hitboxes: Query<&Batch, With>, mut score: ResMut, ) { events .read() .filter(|CollisionEnded(a, b)| bird.contains(*a) && hitboxes.contains(*b)) - .for_each(|_| { + .for_each(|CollisionEnded(_a, b)| { debug!("Hit event while {:?}", state.get()); - match state.get() { - PlayerState::Alive => score.0 = score.0.saturating_add(1), - PlayerState::Rewind => score.0 = score.0.saturating_sub(1), - PlayerState::Pause | PlayerState::Stasis => (), - } + let Batch(id) = hitboxes.get(*b).unwrap(); + score.0 = *id; }) } @@ -702,9 +698,16 @@ fn manage_batches( } fn update_tooltip( - mut query: Query<(&mut ToolTip, &LinearVelocity, Entity), With>, + mut query: Query<(&mut ToolTip, Option<&LinearVelocity>, Option<&Batch>, Entity)>, ) { - query.iter_mut().for_each(|(mut tt, lv, _)| { - tt.insert("Velocity", format!("{}", lv.0)); + query.iter_mut().for_each(|(mut tt, lv, b, _e)| { + // Add Linear Velocity if present on entity + lv.iter().for_each(|it| { + tt.insert("Velocity", format!("{}", it.0)); + }); + // Add Batch ID to if present + b.iter().for_each(|it| { + tt.insert("Batch", format!("{}", it.0)); + }); }); }