diff options
| author | Arne Dußin | 2021-05-07 18:06:02 +0200 |
|---|---|---|
| committer | Arne Dußin | 2021-05-07 18:06:02 +0200 |
| commit | 6de8cfc84edbc80196ad144f2886031a898f5ed7 (patch) | |
| tree | b51d5f147dacce69bbb70bf363067a2528a2601f /src/systems/movement.rs | |
| parent | f3178df0a92fb3b87087e78cad7b9313f947be6a (diff) | |
| download | pmd_coop-main.tar.gz pmd_coop-main.zip | |
Diffstat (limited to 'src/systems/movement.rs')
| -rw-r--r-- | src/systems/movement.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/systems/movement.rs b/src/systems/movement.rs new file mode 100644 index 0000000..61c572a --- /dev/null +++ b/src/systems/movement.rs @@ -0,0 +1,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(); + } + } +} |
