From 72ad17d27ccbbf31f14de1b7e79e0293d3f3ea44 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Sun, 10 Aug 2025 07:28:05 -0700 Subject: [PATCH] Sometimes I think I am the dumbest programmer alive --- src/bin/flappy/main.rs | 73 +++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 29 deletions(-) diff --git a/src/bin/flappy/main.rs b/src/bin/flappy/main.rs index 26e63eb..c2229ce 100644 --- a/src/bin/flappy/main.rs +++ b/src/bin/flappy/main.rs @@ -81,9 +81,9 @@ fn main() { sync_resource_to_ui::.run_if(resource_changed::), sync_resource_to_ui::.run_if(resource_changed::), ), - update_batch_position.run_if(any_component_changed::), 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, manage_score.run_if(on_event::.or(on_event::)), ), @@ -239,7 +239,6 @@ fn update_batch_position( 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 @@ -710,9 +709,7 @@ fn detect_dead( } } -fn alive_bird( - mut bird: Single<&mut RigidBody, With>, -) { +fn alive_bird(mut bird: Single<&mut RigidBody, With>) { debug!("Setting bird to Dynamic"); **bird = RigidBody::Dynamic; } @@ -789,25 +786,23 @@ fn manage_score( PlayerState::Rewind => { start.read().for_each(|CollisionStarted(a, b)| { // Set score to collided hitbox - if let Ok(Batch(this)) = hitboxes.get(*a) - { - info!("[Rewind] Setting score to {this}"); - score.0 = this - 1; + if let Ok(Batch(this)) = hitboxes.get(*a) { + debug!("[Rewind] Setting score to {this}"); + score.0 = this.saturating_sub(1); } else if let Ok(Batch(this)) = hitboxes.get(*b) { - info!("[Rewind] Setting score to {this}"); - score.0 = this - 1; + debug!("[Rewind] Setting score to {this}"); + score.0 = this.saturating_sub(1); } }) } _ => { end.read().for_each(|CollisionEnded(a, b)| { // Set score to collided hitbox - if let Ok(Batch(this)) = hitboxes.get(*b) - { - info!("[Alive] Setting score to {this}"); + if let Ok(Batch(this)) = hitboxes.get(*b) { + debug!("[Alive] Setting score to {this}"); score.0 = *this; } else if let Ok(Batch(this)) = hitboxes.get(*a) { - info!("[Alive] Setting score to {this}"); + debug!("[Alive] Setting score to {this}"); score.0 = *this; } }) @@ -820,26 +815,46 @@ fn manage_score( fn hitbox_collision_handler( mut start: EventReader, bird: Query>, - hitboxes: Query<(Entity, &Batch), With>, + hitboxes: Query>, + batches: Query<(Entity, &Batch)>, + parents: Query>, state: Res>, mut commands: Commands, ) { 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), + debug!("[batches] Collision {a} -> {b}"); + + let target = { + if hitboxes.contains(*b) { + *b + } else if hitboxes.contains(*a) { + *a + } else { + return + } }; - 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)); + 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)); + }) + } + } }); }