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 1 / 2

Write the movement_system: move every entity that has both a position and a velocity. Entities with None velocity stay put.

Hints
  • Zip the two arrays: pos.iter_mut().zip(vel.iter()).
  • if let Some(v) = v { p.x += v.dx; p.y += v.dy; }.
23_mini_ecs.rs
Reset to starterCompiled in a sandbox; hidden checks must pass.
Reveal solution
#[derive(Clone, Copy, PartialEq, Debug)]
struct P { x: i32, y: i32 }
#[derive(Clone, Copy)]
struct V { dx: i32, dy: i32 }

fn movement_system(pos: &mut [P], vel: &[Option<V>]) {
    for (p, v) in pos.iter_mut().zip(vel.iter()) {
        if let Some(v) = v {
            p.x += v.dx;
            p.y += v.dy;
        }
    }
}