diff --git a/src/bin/flappy/main.rs b/src/bin/flappy/main.rs index 3e57344..3f77b82 100644 --- a/src/bin/flappy/main.rs +++ b/src/bin/flappy/main.rs @@ -35,7 +35,7 @@ fn main() { .add_systems(OnEnter(PlayerState::Alive), alive_bird) .add_systems(OnEnter(PlayerState::Pause), pause_bird) .add_systems(OnEnter(PlayerState::Stasis), pause_bird) - .add_systems(OnEnter(PlayerState::Rewind), pause_bird) + // .add_systems(OnEnter(PlayerState::Rewind), pause_bird) .add_systems( Update, ( @@ -80,18 +80,22 @@ fn main() { sync_resource_to_ui::.run_if(resource_changed::), ), 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)), + ( + // Temp debugging systems + debug_collision_events.run_if(on_event::), + ), ), ) + .add_observer(debug_collision_start_observations) + .add_observer(debug_collision_end_observations) .add_observer(flap) .add_observer(populate_batch) .add_observer(populate_pipe) .add_observer(populate_ground) .add_observer(populate_hitbox) + .add_observer(manage_batches) .run(); } @@ -700,10 +704,38 @@ fn scoring( }) } + +fn debug_collision_events( + mut end: EventReader, + mut start: EventReader, + state: Res>, +) { + start.read().for_each(|CollisionStarted(a, b)| { + debug!("Collision started between {a} and {b} in {:?}", state.get()); + }); + end.read().for_each(|CollisionEnded(a, b)| { + debug!("Collision ended between {a} and {b} in {:?}", state.get()); + }); +} + +fn debug_collision_start_observations( + trigger: Trigger, + state: Res>, +) { + debug!("Collision started between {} and {} in {:?}", trigger.target(), trigger.collider, state.get()); +} + +fn debug_collision_end_observations( + trigger: Trigger, + state: Res>, +) { + debug!("Collision end between {} and {} in {:?}", trigger.target(), trigger.collider, state.get()); +} + /// When the player moves forward while alive /// spawn more batches and despawn old batches fn manage_batches( - mut events: EventReader, + trigger: Trigger, state: Res>, bird: Query>, batches: Query<(Entity, &Batch)>, @@ -712,34 +744,27 @@ fn manage_batches( debug_assert!( matches!(state.get(), PlayerState::Alive) || matches!(state.get(), PlayerState::Rewind) ); - events - .read() - // This is written in a wonky way to avoid borrow checker rules - // Instaed of updating the Batch in place we use Commands to get it and upsert a new batch - .filter_map(|CollisionStarted(a, b)| { - if bird.contains(*a) - && let Ok((e, Batch(idx))) = batches.get(*b) - && *idx > 2 - { - Some((e, idx)) - } else { - None - } - }) - .for_each(|(_, idx)| { - let (old_batch_idx, new_batch_idx) = match state.get() { - PlayerState::Alive => (idx - 2, idx + 2), - PlayerState::Rewind => (idx + 2, idx - 2), - _ => panic!("Should not happen!"), - }; - // Find all entities with the old batch and adjust them - batches - .iter() - .filter_map(|(e, batch)| (batch.0 == old_batch_idx).then_some(e)) - .for_each(|e| { - commands.entity(e).insert(Batch(new_batch_idx)); - }) - }); + let a = trigger.target(); + let b = trigger.collider; + if bird.contains(*a) + && let Ok((e, Batch(idx))) = batches.get(*b) + && *idx > 2 + { + info!("Managing batches in player state {:?}", state.get()); + let (old_batch_idx, new_batch_idx) = match state.get() { + PlayerState::Alive => (idx - 2, idx + 2), + PlayerState::Rewind => (idx + 2, idx - 2), + _ => panic!("Should not happen!"), + }; + // Find all entities with the old batch and adjust them + batches + .iter() + .filter_map(|(e, batch)| (batch.0 == old_batch_idx).then_some(e)) + .for_each(|e| { + info!("Moving batch {} -> {}", old_batch_idx, new_batch_idx); + commands.entity(e).insert(Batch(new_batch_idx)); + }) + } } fn update_tooltip(