|
@@ -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(())
|