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