Game Dev Advanced
23 Mini ECS
An ECS stores components in parallel arrays and runs systems that iterate them. No engine here -- just the shape: a system reads some components, mutates others, and skips entities that lack a component.
Write the damage_system: subtract dmg from every entity's health, remove the ones that dropped to 0 or below, and return how many survived.
Hints
- Mutate in place with
for h in healths.iter_mut() { h.0 -= dmg; }. healths.retain(|h| h.0 > 0);drops the dead; then returnhealths.len().
Reveal solution
#[derive(Clone, Copy)]
struct Health(i32);
fn damage_system(healths: &mut Vec<Health>, dmg: i32) -> usize {
for h in healths.iter_mut() {
h.0 -= dmg;
}
healths.retain(|h| h.0 > 0);
healths.len()
}