gdritter repos wenaglia / 895328e
Refactor how entity information gets stores in collision handles Getty Ritter 4 years ago
5 changed file(s) with 34 addition(s) and 36 deletion(s). Collapse all Expand all
117117 }
118118
119119 impl Blocking {
120 pub fn new_shape<S: ncollide2d::shape::Shape<f32>>(w: &mut World, volume: S) -> Blocking {
120 pub fn new_shape<S: ncollide2d::shape::Shape<f32>>(e: specs::Entity, w: &mut World, volume: S) -> Blocking {
121121 let (handle, _) = w.add(
122122 nalgebra::geometry::Isometry::identity(),
123123 ncollide2d::shape::ShapeHandle::new(volume),
124124 ncollide2d::pipeline::CollisionGroups::new(),
125125 ncollide2d::pipeline::object::GeometricQueryType::Proximity(0.0),
126 None,
126 e,
127127 );
128128 Blocking {
129129 handle,
130130 }
131131 }
132132
133 pub fn new_box(w: &mut World) -> Blocking {
134 Blocking::new_shape(w, ncollide2d::shape::Cuboid::new(nalgebra::Vector2::new(
133 pub fn new_box(e: specs::Entity, w: &mut World) -> Blocking {
134 Blocking::new_shape(e, w, ncollide2d::shape::Cuboid::new(nalgebra::Vector2::new(
135135 11.0, 11.0,
136136 )))
137137 }
138138
139 pub fn new_ball(w: &mut World) -> Blocking {
140 Blocking::new_shape(w, ncollide2d::shape::Ball::new(8.0))
139 pub fn new_ball(e: specs::Entity, w: &mut World) -> Blocking {
140 Blocking::new_shape(e, w, ncollide2d::shape::Ball::new(8.0))
141141 }
142142 }
6060 let mut world = specs::World::new();
6161 com::register(&mut world);
6262
63 world.insert(ncollide2d::world::CollisionWorld::<f32, Option<specs::Entity>>::new(0.1));
63 world.insert(ncollide2d::world::CollisionWorld::<f32, specs::Entity>::new(0.1));
6464 res::world_from_file(&mut world, "assets/main.tmx");
6565
6666 // Make a Context and an EventLoop.
5656 let y = y as f32 * consts::TILE_SIZE;
5757 let u = ((n - 1) % 32) as u8;
5858 let v = ((n - u as u32 - 1) / 32) as u8;
59 let blocking = if tilesets[0].tiles[(n-1) as usize].properties["pass"]
60 == tiled::PropertyValue::BoolValue(false) {
61 let mut h = w.write_resource::<World>();
62 Some(Blocking::new_box(&mut h))
63 } else {
64 None
65 };
59 let is_blocking = tilesets[0].tiles[(n-1) as usize].properties["pass"]
60 == tiled::PropertyValue::BoolValue(false);
6661 let mut e = w
67 .create_entity()
62 .create_entity_unchecked()
6863 .with(Position { x, y })
6964 .with(Sprite { u, v });
7065 e = match phase {
7368 DrawLayer::Decoration => e.with(Decoration),
7469 };
7570
76 let e = if let Some(b) = blocking { e.with(b) } else { e };
71 let e = if is_blocking {
72 let mut h = w.write_resource::<World>();
73 let entity = e.entity;
74 e.with(Blocking::new_box(entity, &mut h))
75 } else {
76 e
77 };
7778 e.build();
7879 }
7980 }
8081 }
8182 }
8283
83 w.create_entity_unchecked()
84 let e = w.create_entity_unchecked()
8485 .with(Position {
8586 x: 3.0 * consts::TILE_SIZE,
8687 y: 3.0 * consts::TILE_SIZE,
9192 .with(Controlled)
9293 .with(Collision {
9394 has_collision: false,
94 })
95 .with({
96 let mut h = w.write_resource::<World>();
97 Blocking::new_ball(&mut h)
98 })
95 });
96
97 let entity = e.entity;
98 e.with({
99 let mut h = w.write_resource::<World>();
100 Blocking::new_ball(entity, &mut h)
101 })
99102 .build();
100103 }
3030 pos.clone()
3131 };
3232 let obj = w.get_mut(bl.handle).unwrap();
33 *obj.data_mut() = Some(e);
3433 obj.set_position(
3534 Isometry2::new(Vector2::new(np.x, np.y), nalgebra::zero()));
3635 })
3837 w.update();
3938 for ev in w.proximity_events() {
4039 if ev.new_status == ncollide2d::query::Proximity::Intersecting {
41 if let Some(e) = w.collision_object(ev.collider1).unwrap().data() {
42 collisions.get_mut(*e).map(|r| r.has_collision = true);
43 }
44 if let Some(e) = w.collision_object(ev.collider2).unwrap().data() {
45 collisions.get_mut(*e).map(|r| r.has_collision = true);
46 }
40 let e = w.collision_object(ev.collider1).unwrap().data();
41 collisions.get_mut(*e).map(|r| r.has_collision = true);
42 let e = w.collision_object(ev.collider2).unwrap().data();
43 collisions.get_mut(*e).map(|r| r.has_collision = true);
4744 } else {
48 if let Some(e) = w.collision_object(ev.collider1).unwrap().data() {
49 collisions.get_mut(*e).map(|r| r.has_collision = false);
50 }
51 if let Some(e) = w.collision_object(ev.collider2).unwrap().data() {
52 collisions.get_mut(*e).map(|r| r.has_collision = false);
53 }
45 let e = w.collision_object(ev.collider1).unwrap().data();
46 collisions.get_mut(*e).map(|r| r.has_collision = false);
47 let e = w.collision_object(ev.collider2).unwrap().data();
48 collisions.get_mut(*e).map(|r| r.has_collision = false);
5449 }
5550 }
5651 w.clear_events();
44 specs::Entity,
55 >;
66 pub type NPhase = ncollide2d::narrow_phase::NarrowPhase<f32, ()>;
7 pub type World = ncollide2d::world::CollisionWorld<f32, Option<specs::Entity>>;
7 pub type World = ncollide2d::world::CollisionWorld<f32, specs::Entity>;