Sometimes I think I am the dumbest programmer alive

main
Elijah Voigt 3 months ago
parent b6af29ee97
commit 72ad17d27c

@ -81,9 +81,9 @@ fn main() {
sync_resource_to_ui::<Deaths>.run_if(resource_changed::<Deaths>), sync_resource_to_ui::<Deaths>.run_if(resource_changed::<Deaths>),
sync_resource_to_ui::<RewindFrames>.run_if(resource_changed::<RewindFrames>), sync_resource_to_ui::<RewindFrames>.run_if(resource_changed::<RewindFrames>),
), ),
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)),
// TODO: Add run_if to this system // TODO: Add run_if to this system
update_batch_position.run_if(any_component_changed::<Batch>),
hitbox_collision_handler, hitbox_collision_handler,
manage_score.run_if(on_event::<CollisionStarted>.or(on_event::<CollisionEnded>)), manage_score.run_if(on_event::<CollisionStarted>.or(on_event::<CollisionEnded>)),
), ),
@ -239,7 +239,6 @@ fn update_batch_position(
info!("Updating batch {:?}", idx); info!("Updating batch {:?}", idx);
t.translation.x = 500.0 * (*idx) as f32; t.translation.x = 500.0 * (*idx) as f32;
}); });
// todo!("Adjust pipe positions");
} }
/// The ground population spawns a center peace and two pieces of ground /// The ground population spawns a center peace and two pieces of ground
@ -710,9 +709,7 @@ fn detect_dead(
} }
} }
fn alive_bird( fn alive_bird(mut bird: Single<&mut RigidBody, With<Bird>>) {
mut bird: Single<&mut RigidBody, With<Bird>>,
) {
debug!("Setting bird to Dynamic"); debug!("Setting bird to Dynamic");
**bird = RigidBody::Dynamic; **bird = RigidBody::Dynamic;
} }
@ -789,25 +786,23 @@ fn manage_score(
PlayerState::Rewind => { PlayerState::Rewind => {
start.read().for_each(|CollisionStarted(a, b)| { start.read().for_each(|CollisionStarted(a, b)| {
// Set score to collided hitbox // Set score to collided hitbox
if let Ok(Batch(this)) = hitboxes.get(*a) if let Ok(Batch(this)) = hitboxes.get(*a) {
{ debug!("[Rewind] Setting score to {this}");
info!("[Rewind] Setting score to {this}"); score.0 = this.saturating_sub(1);
score.0 = this - 1;
} else if let Ok(Batch(this)) = hitboxes.get(*b) { } else if let Ok(Batch(this)) = hitboxes.get(*b) {
info!("[Rewind] Setting score to {this}"); debug!("[Rewind] Setting score to {this}");
score.0 = this - 1; score.0 = this.saturating_sub(1);
} }
}) })
} }
_ => { _ => {
end.read().for_each(|CollisionEnded(a, b)| { end.read().for_each(|CollisionEnded(a, b)| {
// Set score to collided hitbox // Set score to collided hitbox
if let Ok(Batch(this)) = hitboxes.get(*b) if let Ok(Batch(this)) = hitboxes.get(*b) {
{ debug!("[Alive] Setting score to {this}");
info!("[Alive] Setting score to {this}");
score.0 = *this; score.0 = *this;
} else if let Ok(Batch(this)) = hitboxes.get(*a) { } else if let Ok(Batch(this)) = hitboxes.get(*a) {
info!("[Alive] Setting score to {this}"); debug!("[Alive] Setting score to {this}");
score.0 = *this; score.0 = *this;
} }
}) })
@ -820,26 +815,46 @@ fn manage_score(
fn hitbox_collision_handler( fn hitbox_collision_handler(
mut start: EventReader<CollisionStarted>, mut start: EventReader<CollisionStarted>,
bird: Query<Entity, With<Bird>>, bird: Query<Entity, With<Bird>>,
hitboxes: Query<(Entity, &Batch), With<Hitbox>>, hitboxes: Query<Entity, With<Hitbox>>,
batches: Query<(Entity, &Batch)>,
parents: Query<Entity, Without<ChildOf>>,
state: Res<State<PlayerState>>, state: Res<State<PlayerState>>,
mut commands: Commands, mut commands: Commands,
) { ) {
start.read().for_each(|CollisionStarted(a, b)| { start.read().for_each(|CollisionStarted(a, b)| {
// Get the current batch debug!("[batches] Collision {a} -> {b}");
let (_, Batch(curr)) = if hitboxes.contains(*b) {
hitboxes.get(*b).unwrap() let target = {
} else { hitboxes.get(*a).unwrap() }; if hitboxes.contains(*b) {
*b
} else if hitboxes.contains(*a) {
*a
} else {
return
}
};
let (_, Batch(curr)) = batches.get(target).unwrap();
debug!("[batches] Current: {curr}");
if *curr > 0 {
let (old_batch, new_batch) = match state.get() { let (old_batch, new_batch) = match state.get() {
PlayerState::Alive => (curr.saturating_sub(2), curr.saturating_add(2)), PlayerState::Alive => (curr.saturating_sub(2), curr.saturating_add(2)),
PlayerState::Rewind => (curr.saturating_add(2), curr.saturating_sub(2)), PlayerState::Rewind => (curr.saturating_add(2), curr.saturating_sub(2)),
_ => (*curr, *curr), _ => (*curr, *curr),
}; };
let old_entity = hitboxes.iter().find_map(|(e, Batch(id))| { if old_batch > 0 && new_batch != 0 {
(*id == old_batch).then_some(e) batches
}).unwrap(); .iter()
commands.entity(old_entity).insert(Batch(new_batch)); // Filter to just entities with this batch ID
.filter_map(|(e, Batch(id))| (*id == old_batch).then_some(e))
.for_each(|old| {
info!("Moving batch {old_batch}({old}) -> {new_batch}");
commands.entity(old).insert(Batch(new_batch));
})
}
}
}); });
} }

Loading…
Cancel
Save