summaryrefslogtreecommitdiff
path: root/src/systems/movement.rs
diff options
context:
space:
mode:
authorArne Dußin2021-05-07 18:06:02 +0200
committerArne Dußin2021-05-07 18:06:02 +0200
commit6de8cfc84edbc80196ad144f2886031a898f5ed7 (patch)
treeb51d5f147dacce69bbb70bf363067a2528a2601f /src/systems/movement.rs
parentf3178df0a92fb3b87087e78cad7b9313f947be6a (diff)
downloadpmd_coop-main.tar.gz
pmd_coop-main.zip
Add player movementHEADmain
Diffstat (limited to 'src/systems/movement.rs')
-rw-r--r--src/systems/movement.rs43
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();
+ }
+ }
+}