use crate::alloc_prelude::*;
use crate::data::arena::Arena;
use crate::dynamics::RigidBodyHandle;
#[allow(unused_imports)]
use crate::dynamics::RigidBodySet;
use crate::dynamics::soft_body::{SoftBody, SoftBodyHandle};
#[allow(unused_imports)]
use crate::geometry::ColliderSet;
use parry::utils::hashmap::HashMap;
#[derive(Copy, Clone, Debug)]
pub(crate) struct SoftBodyIslandEvent {
pub handle: SoftBodyHandle,
}
#[derive(Clone, Default)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct SoftBodySet {
pub(crate) bodies: Arena<SoftBody>,
#[cfg_attr(feature = "serde-serialize", serde(skip))]
pub(crate) island_events: Vec<SoftBodyIslandEvent>,
#[cfg_attr(feature = "serde-serialize", serde(skip))]
pub(crate) attached_to: HashMap<RigidBodyHandle, Vec<(SoftBodyHandle, u32)>>,
#[cfg_attr(feature = "serde-serialize", serde(skip, default = "default_true"))]
pub(super) attached_to_stale: bool,
}
#[cfg(feature = "serde-serialize")]
fn default_true() -> bool {
true
}
impl SoftBodySet {
pub fn new() -> Self {
Self::default()
}
pub fn len(&self) -> usize {
self.bodies.len()
}
pub fn is_empty(&self) -> bool {
self.bodies.is_empty()
}
pub fn contains(&self, handle: SoftBodyHandle) -> bool {
self.bodies.contains(handle.0)
}
pub fn get(&self, handle: SoftBodyHandle) -> Option<&SoftBody> {
self.bodies.get(handle.0)
}
pub fn get_mut(&mut self, handle: SoftBodyHandle) -> Option<&mut SoftBody> {
self.bodies.get_mut(handle.0)
}
pub fn iter(&self) -> impl ExactSizeIterator<Item = (SoftBodyHandle, &SoftBody)> {
self.bodies.iter().map(|(h, b)| (SoftBodyHandle(h), b))
}
pub fn iter_mut(&mut self) -> impl ExactSizeIterator<Item = (SoftBodyHandle, &mut SoftBody)> {
self.bodies.iter_mut().map(|(h, b)| (SoftBodyHandle(h), b))
}
pub(crate) fn attached_to(&self, body: RigidBodyHandle) -> &[(SoftBodyHandle, u32)] {
self.attached_to.get(&body).map_or(&[], |v| v.as_slice())
}
}
impl core::ops::Index<SoftBodyHandle> for SoftBodySet {
type Output = SoftBody;
fn index(&self, index: SoftBodyHandle) -> &SoftBody {
&self.bodies[index.0]
}
}
impl core::ops::IndexMut<SoftBodyHandle> for SoftBodySet {
fn index_mut(&mut self, index: SoftBodyHandle) -> &mut SoftBody {
&mut self.bodies[index.0]
}
}