From 34e2c5a08bf4d4a8f7bd474ffd67b9a0733b346d Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Sun, 10 Aug 2025 07:40:28 -0700 Subject: [PATCH] Sometimes we get a bug, but not consistently... --- src/bin/flappy/main.rs | 50 +++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/src/bin/flappy/main.rs b/src/bin/flappy/main.rs index c2229ce..396baa7 100644 --- a/src/bin/flappy/main.rs +++ b/src/bin/flappy/main.rs @@ -84,7 +84,7 @@ fn main() { update_tooltip.run_if(in_state(DebuggingState::On)), // TODO: Add run_if to this system update_batch_position.run_if(any_component_changed::), - hitbox_collision_handler, + move_batches.run_if(on_event::), manage_score.run_if(on_event::.or(on_event::)), ), ) @@ -810,14 +810,20 @@ fn manage_score( } } -/// When the player moves forward while alive -/// spawn more batches and despawn old batches -fn hitbox_collision_handler( +/// WARNING: This method is somewhat janky +/// +/// We first figure out which collided entity was a hitbox +/// and bial out early if neither is a hitbox +/// Then we find the batch ID for the hitbox that was hit +/// Next we figure out the old -> new batch IDs based on offsets from the current batch +/// skipping the 0th batch intentionally as that is a special case +/// Finally we iterate over all entities with the old batch ID and upsert the new batch ID +/// This includes root batch entities as well as pipes and hitboxes +fn move_batches( mut start: EventReader, bird: Query>, hitboxes: Query>, batches: Query<(Entity, &Batch)>, - parents: Query>, state: Res>, mut commands: Commands, ) { @@ -836,24 +842,22 @@ fn hitbox_collision_handler( let (_, Batch(curr)) = batches.get(target).unwrap(); - debug!("[batches] Current: {curr}"); - if *curr > 0 { - let (old_batch, new_batch) = match state.get() { - PlayerState::Alive => (curr.saturating_sub(2), curr.saturating_add(2)), - PlayerState::Rewind => (curr.saturating_add(2), curr.saturating_sub(2)), - _ => (*curr, *curr), - }; - - if old_batch > 0 && new_batch != 0 { - batches - .iter() - // Filter to just entities with this batch ID - .filter_map(|(e, Batch(id))| (*id == old_batch).then_some(e)) - .for_each(|old| { - info!("Moving batch {old_batch}({old}) -> {new_batch}"); - commands.entity(old).insert(Batch(new_batch)); - }) - } + info!("[batches] Current: {curr}"); + let (old_batch, new_batch) = match state.get() { + PlayerState::Alive => (curr.saturating_sub(2), curr.saturating_add(2)), + PlayerState::Rewind => (curr.saturating_add(2), curr.saturating_sub(2)), + _ => (*curr, *curr), + }; + + if old_batch > 0 && new_batch != 0 { + batches + .iter() + // Filter to just entities with this batch ID + .filter_map(|(e, Batch(id))| (*id == old_batch).then_some(e)) + .for_each(|old| { + info!("Moving batch {old_batch}({old}) -> {new_batch}"); + commands.entity(old).insert(Batch(new_batch)); + }) } }); }