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.

Step 2 / 2

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 return healths.len().
23_mini_ecs.rs
Reset to starterCompiled in a sandbox; hidden checks must pass.
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()
}