From b6af29ee974109e917735dabb03c099ccb0aebda Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Sun, 10 Aug 2025 06:39:42 -0700 Subject: [PATCH] Make batch management event driven (again) --- src/bin/flappy/main.rs | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/src/bin/flappy/main.rs b/src/bin/flappy/main.rs index 300f726..26e63eb 100644 --- a/src/bin/flappy/main.rs +++ b/src/bin/flappy/main.rs @@ -818,33 +818,28 @@ fn manage_score( /// When the player moves forward while alive /// spawn more batches and despawn old batches fn hitbox_collision_handler( - bird: Query<&ColliderAabb, With>, - hitboxes: Query<(Entity, &ColliderAabb), With>, - batches: Query<(Entity, &Batch)>, + mut start: EventReader, + bird: Query>, + hitboxes: Query<(Entity, &Batch), With>, state: Res>, mut commands: Commands, ) { - bird.iter().for_each(|bird_aabb| { - if let Some(e) = hitboxes - .iter() - .find_map(|(e, hitbox_aabb)| bird_aabb.intersects(hitbox_aabb).then_some(e)) - { - let (_, Batch(curr)) = batches.get(e).unwrap(); - let (target_batch_id, new_batch_id) = 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 target_batch_id > 0 - && new_batch_id > 0 - && target_batch_id != new_batch_id - && let Some(e_old) = batches - .iter() - .find_map(|(e, b)| (b.0 == target_batch_id).then_some(e)) - { - commands.entity(e_old).insert(Batch(new_batch_id)); - } + start.read().for_each(|CollisionStarted(a, b)| { + // Get the current batch + let (_, Batch(curr)) = if hitboxes.contains(*b) { + hitboxes.get(*b).unwrap() + } else { hitboxes.get(*a).unwrap() }; + + 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), }; + + let old_entity = hitboxes.iter().find_map(|(e, Batch(id))| { + (*id == old_batch).then_some(e) + }).unwrap(); + commands.entity(old_entity).insert(Batch(new_batch)); }); }