#![allow(dead_code)]

use specs::{prelude::*, ParJoin, Read, ReadStorage, System, WriteStorage};

use crate::{
    components::{Position, Velocity},
    resources::Global,
};

#[derive(Default)]
pub struct Movement;

#[derive(SystemData)]
pub struct MovementData<'a> {
    position: WriteStorage<'a, Position>,
    velocity: ReadStorage<'a, Velocity>,
    global: Read<'a, Global>,
}

impl<'a> System<'a> for Movement {
    type SystemData = MovementData<'a>;

    fn run(&mut self, data: Self::SystemData) {
        let MovementData {
            mut position,
            velocity,
            global,
        } = data;

        (&mut position, &velocity)
            .par_join()
            .for_each(|(position, velocity)| {
                position.pos = position.pos + velocity.dir * velocity.speed * global.delta;
            });
    }
}
