summaryrefslogtreecommitdiff
path: root/src/systems/movement.rs
blob: 61c572a2490c025f082b76398902b1403d285568 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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();
        }
    }
}