use bevy::prelude::*;
#[derive(Component)]
pub struct Health(pub f32);
#[derive(Component)]
pub struct HealthDecay(pub f32);
fn apply_health_decay(mut query: Query<(&mut Health, &HealthDecay)>) {
for (mut health, decay) in query.contiguous_iter_mut().unwrap() {
assert!(health.len() == decay.len());
for (health, decay) in health.iter_mut().zip(decay) {
health.0 *= decay.0;
}
}
}
fn finish_off_first(mut commands: Commands, mut query: Query<(Entity, &mut Health)>) {
if let Some((entity, mut health)) = query.iter_mut().next() {
health.0 -= 1.0;
if health.0 <= 0.0 {
commands.entity(entity).despawn();
println!("Finishing off {entity:?}");
}
}
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Update, (apply_health_decay, finish_off_first).chain())
.add_systems(Startup, setup)
.run();
}
fn setup(mut commands: Commands) {
let mut i = 0;
commands.spawn_batch(std::iter::from_fn(move || {
i += 1;
if i == 10_000 {
None
} else {
Some((Health(i as f32 * 5.0), HealthDecay(0.9)))
}
}));
}