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::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::<RewindFrames>.run_if(resource_changed::<RewindFrames>),
),
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_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(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<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
/// spawn more batches and despawn old batches
fn manage_batches(
mut events: EventReader<CollisionStarted>,
trigger: Trigger<OnCollisionStart>,
state: Res<State<PlayerState>>,
bird: Query<Entity, With<Bird>>,
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(

Loading…
Cancel
Save