diff --git a/src/bin/flappy/main.rs b/src/bin/flappy/main.rs index 921df4c..82cf8b0 100644 --- a/src/bin/flappy/main.rs +++ b/src/bin/flappy/main.rs @@ -83,6 +83,8 @@ fn main() { ), update_batch_position.run_if(any_component_changed::), update_tooltip.run_if(in_state(DebuggingState::On)), + // TODO: Add run_if to this system + manage_batches, ( // Temp debugging systems debug_collision_events.run_if(on_event::), @@ -96,7 +98,6 @@ fn main() { .add_observer(populate_pipe) .add_observer(populate_ground) .add_observer(populate_hitbox) - .add_observer(manage_batches) .add_observer(manage_score) .run(); } @@ -143,8 +144,7 @@ fn init_bird( ) { let material = MeshMaterial2d(materials.add(ColorMaterial { texture: Some(server.load("flappy/bird.png")), - // color: ORANGE.into(), - color: BLACK.into(), + color: ORANGE.into(), alpha_mode: AlphaMode2d::Blend, ..default() })); @@ -469,7 +469,6 @@ fn init_ui(mut commands: Commands) { ..default() }, Button, - // TODO: Add Pause (basically Stasis) state PlayerState::Pause, children![Text::new("Go!"),], )) @@ -775,7 +774,6 @@ impl Display for Deaths { } } -// TODO: Scoring is bugged, need to make it correct. fn manage_score( trigger: Trigger, state: Res>, @@ -836,37 +834,29 @@ fn debug_collision_end_observations( /// When the player moves forward while alive /// spawn more batches and despawn old batches fn manage_batches( - // TODO: Trigger on both start and end?? - trigger: Trigger, - state: Res>, - bird: Query>, + bird: Query<&ColliderAabb, With>, + hitboxes: Query<(Entity, &ColliderAabb), With>, batches: Query<(Entity, &Batch)>, + state: Res>, mut commands: Commands, ) { - debug_assert!( - matches!(state.get(), PlayerState::Alive) || matches!(state.get(), PlayerState::Rewind) - ); - 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!"), + 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 != new_batch_id { + if 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)); + } + } }; - // 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(