Rewind batches works, but now we have a fun new bug!

main
Elijah Voigt 3 months ago
parent c20bd94b79
commit 31a15704ca

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

Loading…
Cancel
Save