use std::time::{Duration, Instant}; use specs::prelude::*; use crate::components::{Pos, Velocity}; pub struct SysMovement { delta_time: Duration, last_run: Instant, } impl SysMovement { pub fn new() -> Self { Self { delta_time: Duration::ZERO, last_run: Instant::now(), } } } impl Default for SysMovement { fn default() -> Self { Self::new() } } impl<'a> System<'a> for SysMovement { type SystemData = (WriteStorage<'a, Pos>, ReadStorage<'a, Velocity>); fn run(&mut self, (mut pos, vel): Self::SystemData) { let now = Instant::now(); self.delta_time = now - self.last_run; self.last_run = now; for (pos, vel) in (&mut pos, &vel).join() { pos.0 += vel.0 * self.delta_time.as_secs_f64(); } } }