Browse Source

Some basic edge physics

Getty Ritter 1 year ago
parent
commit
fa4d7cd21d
3 changed files with 45 additions and 12 deletions
  1. 8 0
      src/input.rs
  2. 28 12
      src/main.rs
  3. 9 0
      src/util.rs

+ 8 - 0
src/input.rs

@@ -1,3 +1,5 @@
+use crate::util;
+
 #[derive(Debug)]
 pub struct Input {
     pub right: f32,
@@ -20,6 +22,9 @@ impl Input {
             winit::VirtualKeyCode::D => self.right += 1.0,
             _ => (),
         }
+
+        self.up = util::clamp(self.up, -1.0, 1.0);
+        self.right = util::clamp(self.right, -1.0, 1.0);
     }
 
     pub fn handle_up(&mut self, kc: winit::VirtualKeyCode) {
@@ -30,5 +35,8 @@ impl Input {
             winit::VirtualKeyCode::D => self.right -= 1.0,
             _ => (),
         }
+
+        self.up = util::clamp(self.up, -1.0, 1.0);
+        self.right = util::clamp(self.right, -1.0, 1.0);
     }
 }

+ 28 - 12
src/main.rs

@@ -8,20 +8,11 @@ use specs::prelude::*;
 use specs::world::WorldExt;
 
 mod input;
+pub mod util;
 
 const WIDTH: f32 = 600.0;
 const HEIGHT: f32 = 400.0;
 
-fn clamp(x: f32, min: f32, max: f32) -> f32 {
-    if x < min {
-        min
-    } else if x > max {
-        max
-    } else {
-        x
-    }
-}
-
 struct Game {
     pub world: specs::World,
 }
@@ -31,6 +22,7 @@ impl Game {
         let mut world = specs::World::new();
         world.register::<Pos>();
         world.register::<Vel>();
+        world.register::<Solid>();
         world.register::<Controlled>();
         world.insert(input::Input::new());
         world
@@ -38,6 +30,7 @@ impl Game {
             .with(Pos { x: 200.0, y: 200.0 })
             .with(Vel { dx: 0.0, dy: 0.0 })
             .with(Controlled)
+            .with(Solid)
             .build();
         Game { world }
     }
@@ -59,10 +52,14 @@ pub struct Vel {
 #[derive(Component)]
 pub struct Controlled;
 
+#[derive(Component)]
+pub struct Solid;
+
 system! {
     Control(resource inp: input::Input, _c: Controlled, mut v: Vel) {
-        v.dx = clamp(v.dx + inp.right, -20.0, 20.0);
-        v.dy = clamp(v.dy + inp.up, -20.0, 20.0);
+        const ACCEL: f32 = 0.5;
+        v.dx = util::clamp(v.dx + inp.right * ACCEL, -20.0, 20.0);
+        v.dy = util::clamp(v.dy + inp.up * ACCEL, -20.0, 20.0);
     }
 }
 
@@ -85,6 +82,24 @@ system! {
     }
 }
 
+system! {
+    Physics(mut pos: Pos, mut v: Vel, _s: Solid) {
+        if pos.x < 0.0 {
+            pos.x = 0.0;
+            v.dx = v.dx.abs() * 0.5;
+        } else if pos.x > WIDTH {
+            pos.x = WIDTH;
+            v.dx = -v.dx.abs() * 0.5;
+        }
+
+        if pos.y < 0.0 {
+            v.dy = v.dy.abs();
+        } else if pos.y > HEIGHT {
+            v.dy = -v.dy.abs();
+        }
+    }
+}
+
 system! {
     Move(mut pos: Pos, v: Vel) {
         pos.x += v.dx;
@@ -129,6 +144,7 @@ impl ggez::event::EventHandler for Game {
 
     fn update(&mut self, _ctx: &mut ggez::Context) -> ggez::GameResult<()> {
         Control.run_now(&self.world);
+        Physics.run_now(&self.world);
         Slowdown.run_now(&self.world);
         Move.run_now(&self.world);
         Ok(())

+ 9 - 0
src/util.rs

@@ -0,0 +1,9 @@
+pub fn clamp(x: f32, min: f32, max: f32) -> f32 {
+    if x < min {
+        min
+    } else if x > max {
+        max
+    } else {
+        x
+    }
+}