|
@@ -1,31 +1,10 @@
|
|
|
use crate::com::{Blocking, Collision, Position, Velocity};
|
|
|
use crate::game::MyGame;
|
|
|
-use crate::types::{BPhase, NPhase};
|
|
|
+use crate::types::World;
|
|
|
|
|
|
use nalgebra::{Isometry2, Vector2};
|
|
|
-use ncollide2d::broad_phase::{
|
|
|
- broad_phase::BroadPhase, broad_phase::BroadPhaseProxyHandle, BroadPhaseInterferenceHandler,
|
|
|
-};
|
|
|
use specs::{Join, RunNow};
|
|
|
|
|
|
-struct InterferenceHandler<'a> {
|
|
|
- collisions: specs::WriteStorage<'a, Collision>,
|
|
|
-}
|
|
|
-
|
|
|
-impl<'a> BroadPhaseInterferenceHandler<specs::Entity> for InterferenceHandler<'a> {
|
|
|
- fn is_interference_allowed(&mut self, a: &specs::Entity, b: &specs::Entity) -> bool {
|
|
|
- // Prevent self-collision.
|
|
|
- *a != *b
|
|
|
- }
|
|
|
-
|
|
|
- fn interference_started(&mut self, a: &specs::Entity, b: &specs::Entity) {
|
|
|
- self.collisions.get_mut(*a).map(|r| r.has_collision = true);
|
|
|
- self.collisions.get_mut(*b).map(|r| r.has_collision = true);
|
|
|
- }
|
|
|
-
|
|
|
- fn interference_stopped(&mut self, _: &specs::Entity, _: &specs::Entity) {}
|
|
|
-}
|
|
|
-
|
|
|
struct Collide;
|
|
|
|
|
|
impl<'a> specs::System<'a> for Collide {
|
|
@@ -35,19 +14,14 @@ impl<'a> specs::System<'a> for Collide {
|
|
|
specs::ReadStorage<'a, Velocity>,
|
|
|
specs::ReadStorage<'a, Blocking>,
|
|
|
specs::WriteStorage<'a, Collision>,
|
|
|
- specs::WriteExpect<'a, BPhase>,
|
|
|
- specs::WriteExpect<'a, NPhase>,
|
|
|
+ specs::WriteExpect<'a, World>,
|
|
|
);
|
|
|
|
|
|
fn run(
|
|
|
&mut self,
|
|
|
- (entities, position, velocity, blocking, mut collisions, mut bf, _narrow): Self::SystemData,
|
|
|
+ (entities, position, velocity, blocking, mut collisions, mut w): Self::SystemData,
|
|
|
) {
|
|
|
- let _: Vec<()> = (&mut collisions)
|
|
|
- .join()
|
|
|
- .map(|c| c.has_collision = false)
|
|
|
- .collect();
|
|
|
- let handles: Vec<BroadPhaseProxyHandle> = (&entities, &position, &blocking)
|
|
|
+ let _: Vec<()> = (&entities, &position, &blocking)
|
|
|
.join()
|
|
|
.map(|(e, pos, bl)| {
|
|
|
let np = if let Some(vel) = velocity.get(e) {
|
|
@@ -55,19 +29,31 @@ impl<'a> specs::System<'a> for Collide {
|
|
|
} else {
|
|
|
pos.clone()
|
|
|
};
|
|
|
- bf.create_proxy(
|
|
|
- bl.volume
|
|
|
- .aabb(&Isometry2::new(Vector2::new(np.x, np.y), nalgebra::zero())),
|
|
|
- e,
|
|
|
- )
|
|
|
+ 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()));
|
|
|
})
|
|
|
.collect();
|
|
|
-
|
|
|
- bf.update(&mut InterferenceHandler {
|
|
|
- collisions: collisions,
|
|
|
- });
|
|
|
-
|
|
|
- bf.remove(&handles, &mut |_, _| {});
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ } 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ w.clear_events();
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -131,5 +117,5 @@ pub fn systems(game: &mut MyGame) {
|
|
|
Collide.run_now(&game.world);
|
|
|
// Intersection.run_now(&game.world.res);
|
|
|
Physics.run_now(&game.world);
|
|
|
- ResetCollision.run_now(&game.world);
|
|
|
+ // ResetCollision.run_now(&game.world);
|
|
|
}
|