Refactor how entity information gets stores in collision handles
Getty Ritter
5 years ago
117 | 117 |
}
|
118 | 118 |
|
119 | 119 |
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 {
|
121 | 121 |
let (handle, _) = w.add(
|
122 | 122 |
nalgebra::geometry::Isometry::identity(),
|
123 | 123 |
ncollide2d::shape::ShapeHandle::new(volume),
|
124 | 124 |
ncollide2d::pipeline::CollisionGroups::new(),
|
125 | 125 |
ncollide2d::pipeline::object::GeometricQueryType::Proximity(0.0),
|
126 | |
None,
|
| 126 |
e,
|
127 | 127 |
);
|
128 | 128 |
Blocking {
|
129 | 129 |
handle,
|
130 | 130 |
}
|
131 | 131 |
}
|
132 | 132 |
|
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(
|
135 | 135 |
11.0, 11.0,
|
136 | 136 |
)))
|
137 | 137 |
}
|
138 | 138 |
|
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))
|
141 | 141 |
}
|
142 | 142 |
}
|
60 | 60 |
let mut world = specs::World::new();
|
61 | 61 |
com::register(&mut world);
|
62 | 62 |
|
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));
|
64 | 64 |
res::world_from_file(&mut world, "assets/main.tmx");
|
65 | 65 |
|
66 | 66 |
// Make a Context and an EventLoop.
|
56 | 56 |
let y = y as f32 * consts::TILE_SIZE;
|
57 | 57 |
let u = ((n - 1) % 32) as u8;
|
58 | 58 |
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);
|
66 | 61 |
let mut e = w
|
67 | |
.create_entity()
|
| 62 |
.create_entity_unchecked()
|
68 | 63 |
.with(Position { x, y })
|
69 | 64 |
.with(Sprite { u, v });
|
70 | 65 |
e = match phase {
|
|
73 | 68 |
DrawLayer::Decoration => e.with(Decoration),
|
74 | 69 |
};
|
75 | 70 |
|
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 |
};
|
77 | 78 |
e.build();
|
78 | 79 |
}
|
79 | 80 |
}
|
80 | 81 |
}
|
81 | 82 |
}
|
82 | 83 |
|
83 | |
w.create_entity_unchecked()
|
| 84 |
let e = w.create_entity_unchecked()
|
84 | 85 |
.with(Position {
|
85 | 86 |
x: 3.0 * consts::TILE_SIZE,
|
86 | 87 |
y: 3.0 * consts::TILE_SIZE,
|
|
91 | 92 |
.with(Controlled)
|
92 | 93 |
.with(Collision {
|
93 | 94 |
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 |
})
|
99 | 102 |
.build();
|
100 | 103 |
}
|
30 | 30 |
pos.clone()
|
31 | 31 |
};
|
32 | 32 |
let obj = w.get_mut(bl.handle).unwrap();
|
33 | |
*obj.data_mut() = Some(e);
|
34 | 33 |
obj.set_position(
|
35 | 34 |
Isometry2::new(Vector2::new(np.x, np.y), nalgebra::zero()));
|
36 | 35 |
})
|
|
38 | 37 |
w.update();
|
39 | 38 |
for ev in w.proximity_events() {
|
40 | 39 |
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);
|
47 | 44 |
} 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);
|
54 | 49 |
}
|
55 | 50 |
}
|
56 | 51 |
w.clear_events();
|
4 | 4 |
specs::Entity,
|
5 | 5 |
>;
|
6 | 6 |
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>;
|