Make batch management event driven (again)

main
Elijah Voigt 3 months ago
parent 8260185860
commit b6af29ee97

@ -818,33 +818,28 @@ fn manage_score(
/// When the player moves forward while alive /// When the player moves forward while alive
/// spawn more batches and despawn old batches /// spawn more batches and despawn old batches
fn hitbox_collision_handler( fn hitbox_collision_handler(
bird: Query<&ColliderAabb, With<Bird>>, mut start: EventReader<CollisionStarted>,
hitboxes: Query<(Entity, &ColliderAabb), With<Hitbox>>, bird: Query<Entity, With<Bird>>,
batches: Query<(Entity, &Batch)>, hitboxes: Query<(Entity, &Batch), With<Hitbox>>,
state: Res<State<PlayerState>>, state: Res<State<PlayerState>>,
mut commands: Commands, mut commands: Commands,
) { ) {
bird.iter().for_each(|bird_aabb| { start.read().for_each(|CollisionStarted(a, b)| {
if let Some(e) = hitboxes // Get the current batch
.iter() let (_, Batch(curr)) = if hitboxes.contains(*b) {
.find_map(|(e, hitbox_aabb)| bird_aabb.intersects(hitbox_aabb).then_some(e)) hitboxes.get(*b).unwrap()
{ } else { hitboxes.get(*a).unwrap() };
let (_, Batch(curr)) = batches.get(e).unwrap();
let (target_batch_id, new_batch_id) = match state.get() { let (old_batch, new_batch) = match state.get() {
PlayerState::Alive => (curr.saturating_sub(2), curr.saturating_add(2)), PlayerState::Alive => (curr.saturating_sub(2), curr.saturating_add(2)),
PlayerState::Rewind => (curr.saturating_add(2), curr.saturating_sub(2)), PlayerState::Rewind => (curr.saturating_add(2), curr.saturating_sub(2)),
_ => (*curr, *curr), _ => (*curr, *curr),
}; };
if target_batch_id > 0
&& new_batch_id > 0 let old_entity = hitboxes.iter().find_map(|(e, Batch(id))| {
&& target_batch_id != new_batch_id (*id == old_batch).then_some(e)
&& let Some(e_old) = batches }).unwrap();
.iter() commands.entity(old_entity).insert(Batch(new_batch));
.find_map(|(e, b)| (b.0 == target_batch_id).then_some(e))
{
commands.entity(e_old).insert(Batch(new_batch_id));
}
};
}); });
} }

Loading…
Cancel
Save