From c32da78f2a6014c491aa3e975fb83ddb7c80610e Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Mon, 26 Apr 2021 17:59:25 +0200 Subject: Split rigid-bodies and colliders into multiple components --- src/data/component_set.rs | 106 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/data/component_set.rs (limited to 'src/data/component_set.rs') diff --git a/src/data/component_set.rs b/src/data/component_set.rs new file mode 100644 index 0000000..76e076a --- /dev/null +++ b/src/data/component_set.rs @@ -0,0 +1,106 @@ +use crate::data::Index; + +// TODO ECS: use this to handle optional components properly. +// pub trait OptionalComponentSet { +// fn get(&self, handle: Index) -> Option<&T>; +// } + +pub trait ComponentSetOption { + fn get(&self, handle: Index) -> Option<&T>; +} + +pub trait ComponentSet: ComponentSetOption { + fn size_hint(&self) -> usize; + // TODO ECS: remove this, its only needed by the query pipeline update + // which should only take the modified colliders into account. + fn for_each(&self, f: impl FnMut(Index, &T)); + fn index(&self, handle: Index) -> &T { + self.get(handle).unwrap() + } +} + +pub trait ComponentSetMut: ComponentSet { + fn map_mut_internal( + &mut self, + handle: crate::data::Index, + f: impl FnOnce(&mut T) -> Result, + ) -> Option; + fn set_internal(&mut self, handle: crate::data::Index, val: T); +} + +pub trait BundleSet<'a, T> { + fn index_bundle(&'a self, handle: Index) -> T; +} + +impl<'a, T, A, B> BundleSet<'a, (&'a A, &'a B)> for T +where + T: ComponentSet + ComponentSet, +{ + #[inline(always)] + fn index_bundle(&'a self, handle: Index) -> (&'a A, &'a B) { + (self.index(handle), self.index(handle)) + } +} + +impl<'a, T, A, B, C> BundleSet<'a, (&'a A, &'a B, &'a C)> for T +where + T: ComponentSet + ComponentSet + ComponentSet, +{ + #[inline(always)] + fn index_bundle(&'a self, handle: Index) -> (&'a A, &'a B, &'a C) { + (self.index(handle), self.index(handle), self.index(handle)) + } +} + +impl<'a, T, A, B, C, D> BundleSet<'a, (&'a A, &'a B, &'a C, &'a D)> for T +where + T: ComponentSet + ComponentSet + ComponentSet + ComponentSet, +{ + #[inline(always)] + fn index_bundle(&'a self, handle: Index) -> (&'a A, &'a B, &'a C, &'a D) { + ( + self.index(handle), + self.index(handle), + self.index(handle), + self.index(handle), + ) + } +} + +impl<'a, T, A, B, C, D, E> BundleSet<'a, (&'a A, &'a B, &'a C, &'a D, &'a E)> for T +where + T: ComponentSet + ComponentSet + ComponentSet + ComponentSet + ComponentSet, +{ + #[inline(always)] + fn index_bundle(&'a self, handle: Index) -> (&'a A, &'a B, &'a C, &'a D, &'a E) { + ( + self.index(handle), + self.index(handle), + self.index(handle), + self.index(handle), + self.index(handle), + ) + } +} + +impl<'a, T, A, B, C, D, E, F> BundleSet<'a, (&'a A, &'a B, &'a C, &'a D, &'a E, &'a F)> for T +where + T: ComponentSet + + ComponentSet + + ComponentSet + + ComponentSet + + ComponentSet + + ComponentSet, +{ + #[inline(always)] + fn index_bundle(&'a self, handle: Index) -> (&'a A, &'a B, &'a C, &'a D, &'a E, &'a F) { + ( + self.index(handle), + self.index(handle), + self.index(handle), + self.index(handle), + self.index(handle), + self.index(handle), + ) + } +} -- cgit From 5cf805075ec8612249d692c319d099f4454931da Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Thu, 29 Apr 2021 11:42:44 +0200 Subject: Fix compilation of the parallel version --- src/data/component_set.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/data/component_set.rs') diff --git a/src/data/component_set.rs b/src/data/component_set.rs index 76e076a..ca7df67 100644 --- a/src/data/component_set.rs +++ b/src/data/component_set.rs @@ -5,7 +5,7 @@ use crate::data::Index; // fn get(&self, handle: Index) -> Option<&T>; // } -pub trait ComponentSetOption { +pub trait ComponentSetOption: Sync { fn get(&self, handle: Index) -> Option<&T>; } -- cgit From 2dfbd9ae92c139e306afc87994adac82489f30eb Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Fri, 30 Apr 2021 11:37:58 +0200 Subject: Add comments. --- src/data/component_set.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/data/component_set.rs') diff --git a/src/data/component_set.rs b/src/data/component_set.rs index ca7df67..6e0461c 100644 --- a/src/data/component_set.rs +++ b/src/data/component_set.rs @@ -5,30 +5,47 @@ use crate::data::Index; // fn get(&self, handle: Index) -> Option<&T>; // } +/// A set of optional elements of type `T`. pub trait ComponentSetOption: Sync { + /// Get the element associated to the given `handle`, if there is one. fn get(&self, handle: Index) -> Option<&T>; } +/// A set of elements of type `T`. pub trait ComponentSet: ComponentSetOption { + /// The estimated number of elements in this set. + /// + /// This value is typically used for preallocating some arrays for + /// better performances. fn size_hint(&self) -> usize; // TODO ECS: remove this, its only needed by the query pipeline update // which should only take the modified colliders into account. + /// Iterate through all the elements on this set. fn for_each(&self, f: impl FnMut(Index, &T)); + /// Get the element associated to the given `handle`. fn index(&self, handle: Index) -> &T { self.get(handle).unwrap() } } +/// A set of mutable elements of type `T`. pub trait ComponentSetMut: ComponentSet { + /// Applies the given closure to the element associated to the given `handle`. + /// + /// Return `None` if the element doesn't exist. fn map_mut_internal( &mut self, handle: crate::data::Index, f: impl FnOnce(&mut T) -> Result, ) -> Option; + + /// Set the value of this element. fn set_internal(&mut self, handle: crate::data::Index, val: T); } +/// Helper trait to address multiple elements at once. pub trait BundleSet<'a, T> { + /// Access multiple elements from this set. fn index_bundle(&'a self, handle: Index) -> T; } -- cgit