Added forward infinite play! not to support backward for rewind

main
Elijah Voigt 3 months ago
parent dad9c05234
commit 8fc58773ce

@ -1,8 +1,6 @@
// Bevy basically forces "complex types" with Querys // Bevy basically forces "complex types" with Querys
#![allow(clippy::type_complexity)] #![allow(clippy::type_complexity)]
use bevy::platform::hash::RandomState;
use std::hash::BuildHasher;
use games::physics2d::*; use games::physics2d::*;
use games::*; use games::*;
@ -82,6 +80,7 @@ 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>),
create_forward_batches.run_if(on_event::<CollisionStarted>).run_if(in_state(PlayerState::Alive)),
), ),
) )
.add_observer(flap) .add_observer(flap)
@ -194,8 +193,12 @@ fn init_first_batches(
fn populate_batch( fn populate_batch(
trigger: Trigger<OnAdd, Batch>, trigger: Trigger<OnAdd, Batch>,
batches: Query<&Batch>, batches: Query<&Batch>,
children: Query<&ChildOf>,
mut commands: Commands, mut commands: Commands,
) { ) {
// Only run this for top level batch entities,
// not children containing a reference to their batch, like hitboxes
if !children.contains(trigger.target()) {
let Batch(batch_id) = batches.get(trigger.target()).unwrap(); let Batch(batch_id) = batches.get(trigger.target()).unwrap();
commands commands
.entity(trigger.target()) .entity(trigger.target())
@ -209,10 +212,11 @@ fn populate_batch(
if *batch_id > 0 { if *batch_id > 0 {
parent.spawn(Pipe::Top); parent.spawn(Pipe::Top);
parent.spawn(Pipe::Bottom); parent.spawn(Pipe::Bottom);
parent.spawn(Hitbox); parent.spawn((Hitbox, Batch(*batch_id)));
} }
}); });
} }
}
/// The ground population spawns a center peace and two pieces of ground /// The ground population spawns a center peace and two pieces of ground
/// to the left and right of the center. /// to the left and right of the center.
@ -648,3 +652,32 @@ fn scoring(
} }
}) })
} }
/// When the player moves forward while alive
/// spawn more batches and despawn old batches
fn create_forward_batches(
mut events: EventReader<CollisionStarted>,
state: Res<State<PlayerState>>,
bird: Query<Entity, With<Bird>>,
hitboxes: Query<&Batch, With<Hitbox>>,
batches: Query<(Entity, &Batch), (With<Children>, Without<Hitbox>)>,
mut commands: Commands,
) {
debug_assert!(*state.get() == PlayerState::Alive);
events
.read()
.filter(|CollisionStarted(a, b)| bird.contains(*a) && hitboxes.contains(*b))
.for_each(|CollisionStarted(_, b)| {
let Batch(idx) = hitboxes.get(*b).unwrap();
info!("Batch {:?}", idx);
if *idx > 2 {
// Spawn future batch
commands.spawn(Batch(idx + 2));
// Despawn old batch
if let Some((despawn_batch, _)) = batches.iter().find(|(_, Batch(n))| *n == idx - 2) {
commands.entity(despawn_batch).despawn();
}
}
});
}

Loading…
Cancel
Save