From 3b0d25646407e9b1d7e39dee1005d9a24e09ab66 Mon Sep 17 00:00:00 2001 From: rezural Date: Thu, 8 Jul 2021 11:04:06 +1000 Subject: make collider Option<> on EntityWithGraphics --- src_testbed/graphics.rs | 28 +++++++++++++++++++--------- src_testbed/objects/node.rs | 8 ++++---- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src_testbed/graphics.rs b/src_testbed/graphics.rs index 0682f63..00d49f3 100644 --- a/src_testbed/graphics.rs +++ b/src_testbed/graphics.rs @@ -63,8 +63,10 @@ impl GraphicsManager { let body = body.unwrap_or(RigidBodyHandle::invalid()); if let Some(sns) = self.b2sn.get_mut(&body) { for sn in sns.iter_mut() { - if sn.collider == collider { - commands.entity(sn.entity).despawn(); + if let Some(sn_c) = sn.collider { + if sn_c == collider { + commands.entity(sn.entity).despawn(); + } } } } @@ -216,11 +218,11 @@ impl GraphicsManager { for collider_handle in bodies[handle].colliders() { let color = self.c2color.get(collider_handle).copied().unwrap_or(color); let collider = &colliders[*collider_handle]; - self.do_add_shape( + self.add_shape( commands, meshes, materials, - *collider_handle, + Some(*collider_handle), collider.shape(), collider.is_sensor(), collider.position(), @@ -264,11 +266,11 @@ impl GraphicsManager { let color = *self.b2color.get(&collider_parent).unwrap(); let color = self.c2color.get(&handle).copied().unwrap_or(color); let mut nodes = std::mem::replace(self.b2sn.get_mut(&collider_parent).unwrap(), Vec::new()); - self.do_add_shape( + self.add_shape( commands, meshes, materials, - handle, + Some(handle), collider.shape(), collider.is_sensor(), collider.position(), @@ -279,12 +281,12 @@ impl GraphicsManager { self.b2sn.insert(collider_parent, nodes); } - fn do_add_shape( + pub fn add_shape( &mut self, commands: &mut Commands, meshes: &mut Assets, materials: &mut Assets, - handle: ColliderHandle, + handle: Option, shape: &dyn Shape, sensor: bool, pos: &Isometry, @@ -294,7 +296,7 @@ impl GraphicsManager { ) { if let Some(compound) = shape.as_compound() { for (shape_pos, shape) in compound.shapes() { - self.do_add_shape( + self.add_shape( commands, meshes, materials, @@ -394,6 +396,14 @@ impl GraphicsManager { pub fn nodes_mut(&mut self) -> impl Iterator { self.b2sn.values_mut().flat_map(|val| val.iter_mut()) } + + pub fn prefab_meshes(&self) -> &HashMap> { + &self.prefab_meshes + } + + pub fn prefab_meshes_mut(&mut self) -> &mut HashMap> { + &mut self.prefab_meshes + } } impl Default for GraphicsManager { diff --git a/src_testbed/objects/node.rs b/src_testbed/objects/node.rs index 47c9b41..9bbece2 100644 --- a/src_testbed/objects/node.rs +++ b/src_testbed/objects/node.rs @@ -23,7 +23,7 @@ pub struct EntityWithGraphics { pub entity: Entity, pub color: Point3, pub base_color: Point3, - pub collider: ColliderHandle, + pub collider: Option, pub delta: Isometry, pub opacity: f32, material: Handle, @@ -36,7 +36,7 @@ impl EntityWithGraphics { materials: &mut Assets, prefab_meshs: &HashMap>, shape: &dyn Shape, - collider: ColliderHandle, + collider: Option, collider_pos: Isometry, delta: Isometry, color: Point3, @@ -136,7 +136,7 @@ impl EntityWithGraphics { } pub fn update(&mut self, colliders: &ColliderSet, components: &mut Query<(&mut Transform,)>) { - if let Some(co) = colliders.get(self.collider) { + if let Some(Some(co)) = self.collider.map(|c| colliders.get(c)) { if let Ok(mut pos) = components.get_component_mut::(self.entity) { let co_pos = co.position() * self.delta; pos.translation.x = co_pos.translation.vector.x; @@ -159,7 +159,7 @@ impl EntityWithGraphics { } } - pub fn object(&self) -> ColliderHandle { + pub fn object(&self) -> Option { self.collider } -- cgit