Browse Source

Refactor how entity information gets stores in collision handles

Getty Ritter 4 years ago
parent
commit
895328e7e4
5 changed files with 34 additions and 36 deletions
  1. 6 6
      src/com.rs
  2. 1 1
      src/main.rs
  3. 18 15
      src/res.rs
  4. 8 13
      src/sys/physics.rs
  5. 1 1
      src/types.rs

+ 6 - 6
src/com.rs

@@ -117,26 +117,26 @@ pub struct Blocking {
 }
 
 impl Blocking {
-    pub fn new_shape<S: ncollide2d::shape::Shape<f32>>(w: &mut World, volume: S) -> Blocking {
+    pub fn new_shape<S: ncollide2d::shape::Shape<f32>>(e: specs::Entity, w: &mut World, volume: S) -> Blocking {
         let (handle, _) = w.add(
             nalgebra::geometry::Isometry::identity(),
             ncollide2d::shape::ShapeHandle::new(volume),
             ncollide2d::pipeline::CollisionGroups::new(),
             ncollide2d::pipeline::object::GeometricQueryType::Proximity(0.0),
-            None,
+            e,
         );
         Blocking {
             handle,
         }
     }
 
-    pub fn new_box(w: &mut World) -> Blocking {
-        Blocking::new_shape(w, ncollide2d::shape::Cuboid::new(nalgebra::Vector2::new(
+    pub fn new_box(e: specs::Entity, w: &mut World) -> Blocking {
+        Blocking::new_shape(e, w, ncollide2d::shape::Cuboid::new(nalgebra::Vector2::new(
             11.0, 11.0,
         )))
     }
 
-    pub fn new_ball(w: &mut World) -> Blocking {
-        Blocking::new_shape(w, ncollide2d::shape::Ball::new(8.0))
+    pub fn new_ball(e: specs::Entity, w: &mut World) -> Blocking {
+        Blocking::new_shape(e, w, ncollide2d::shape::Ball::new(8.0))
     }
 }

+ 1 - 1
src/main.rs

@@ -60,7 +60,7 @@ fn main() -> Result<(), ggez::error::GameError> {
     let mut world = specs::World::new();
     com::register(&mut world);
 
-    world.insert(ncollide2d::world::CollisionWorld::<f32, Option<specs::Entity>>::new(0.1));
+    world.insert(ncollide2d::world::CollisionWorld::<f32, specs::Entity>::new(0.1));
     res::world_from_file(&mut world, "assets/main.tmx");
 
     // Make a Context and an EventLoop.

+ 18 - 15
src/res.rs

@@ -56,15 +56,10 @@ pub fn world_from_file<P: AsRef<Path>>(w: &mut specs::World, path: P) {
                     let y = y as f32 * consts::TILE_SIZE;
                     let u = ((n - 1) % 32) as u8;
                     let v = ((n - u as u32 - 1) / 32) as u8;
-                    let blocking = if tilesets[0].tiles[(n-1) as usize].properties["pass"]
-                        == tiled::PropertyValue::BoolValue(false) {
-                            let mut h = w.write_resource::<World>();
-                            Some(Blocking::new_box(&mut h))
-                        } else {
-                            None
-                        };
+                    let is_blocking = tilesets[0].tiles[(n-1) as usize].properties["pass"]
+                        == tiled::PropertyValue::BoolValue(false);
                     let mut e = w
-                        .create_entity()
+                        .create_entity_unchecked()
                         .with(Position { x, y })
                         .with(Sprite { u, v });
                     e = match phase {
@@ -73,14 +68,20 @@ pub fn world_from_file<P: AsRef<Path>>(w: &mut specs::World, path: P) {
                         DrawLayer::Decoration => e.with(Decoration),
                     };
 
-                    let e = if let Some(b) = blocking { e.with(b) } else { e };
+                    let e = if is_blocking {
+                        let mut h = w.write_resource::<World>();
+                        let entity = e.entity;
+                        e.with(Blocking::new_box(entity, &mut h))
+                    } else {
+                        e
+                    };
                     e.build();
                 }
             }
         }
     }
 
-    w.create_entity_unchecked()
+    let e = w.create_entity_unchecked()
         .with(Position {
             x: 3.0 * consts::TILE_SIZE,
             y: 3.0 * consts::TILE_SIZE,
@@ -91,10 +92,12 @@ pub fn world_from_file<P: AsRef<Path>>(w: &mut specs::World, path: P) {
         .with(Controlled)
         .with(Collision {
             has_collision: false,
-        })
-        .with({
-            let mut h = w.write_resource::<World>();
-            Blocking::new_ball(&mut h)
-        })
+        });
+
+    let entity = e.entity;
+    e.with({
+        let mut h = w.write_resource::<World>();
+        Blocking::new_ball(entity, &mut h)
+    })
         .build();
 }

+ 8 - 13
src/sys/physics.rs

@@ -30,7 +30,6 @@ impl<'a> specs::System<'a> for Collide {
                     pos.clone()
                 };
                 let obj = w.get_mut(bl.handle).unwrap();
-                *obj.data_mut() = Some(e);
                 obj.set_position(
                     Isometry2::new(Vector2::new(np.x, np.y), nalgebra::zero()));
             })
@@ -38,19 +37,15 @@ impl<'a> specs::System<'a> for Collide {
         w.update();
         for ev in w.proximity_events() {
             if ev.new_status == ncollide2d::query::Proximity::Intersecting {
-                if let Some(e) = w.collision_object(ev.collider1).unwrap().data() {
-                    collisions.get_mut(*e).map(|r| r.has_collision = true);
-                }
-                if let Some(e) = w.collision_object(ev.collider2).unwrap().data() {
-                    collisions.get_mut(*e).map(|r| r.has_collision = true);
-                }
+                let e = w.collision_object(ev.collider1).unwrap().data();
+                collisions.get_mut(*e).map(|r| r.has_collision = true);
+                let e = w.collision_object(ev.collider2).unwrap().data();
+                collisions.get_mut(*e).map(|r| r.has_collision = true);
             } else {
-                if let Some(e) = w.collision_object(ev.collider1).unwrap().data() {
-                    collisions.get_mut(*e).map(|r| r.has_collision = false);
-                }
-                if let Some(e) = w.collision_object(ev.collider2).unwrap().data() {
-                    collisions.get_mut(*e).map(|r| r.has_collision = false);
-                }
+                let e = w.collision_object(ev.collider1).unwrap().data();
+                collisions.get_mut(*e).map(|r| r.has_collision = false);
+                let e = w.collision_object(ev.collider2).unwrap().data();
+                collisions.get_mut(*e).map(|r| r.has_collision = false);
             }
         }
         w.clear_events();

+ 1 - 1
src/types.rs

@@ -4,4 +4,4 @@ pub type BPhase = ncollide2d::broad_phase::DBVTBroadPhase<
     specs::Entity,
 >;
 pub type NPhase = ncollide2d::narrow_phase::NarrowPhase<f32, ()>;
-pub type World = ncollide2d::world::CollisionWorld<f32, Option<specs::Entity>>;
+pub type World = ncollide2d::world::CollisionWorld<f32, specs::Entity>;