diff options
Diffstat (limited to 'engine-ecs/src/entity.rs')
| -rw-r--r-- | engine-ecs/src/entity.rs | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/engine-ecs/src/entity.rs b/engine-ecs/src/entity.rs index f551de1..d7b63be 100644 --- a/engine-ecs/src/entity.rs +++ b/engine-ecs/src/entity.rs @@ -1,4 +1,4 @@ -use std::any::type_name; +use std::any::{type_name, Any}; use std::ops::Deref; use std::sync::LazyLock; @@ -122,6 +122,40 @@ impl<'a> Handle<'a> ) } + /// Returns a reference to the component with the ID `id` in this entity. + /// `None` is returned if the component isn't found. + /// + /// # Panics + /// Will panic if: + /// - The component is borrowed mutably elsewhere + #[must_use] + pub fn get_any_by_id(&self, id: Uid) -> Option<ComponentHandle<'a, dyn Any>> + { + let component = self.get_matching_components(id).next()?; + + Some(ComponentHandle::new_any(&component).unwrap_or_else(|err| { + panic!("Creating handle to component with id {id} failed: {err}"); + })) + } + + /// Returns a reference to the component with the ID `id` in this entity. + /// `None` is returned if the component isn't found. + /// + /// # Panics + /// Will panic if: + /// - The component is borrowed elsewhere + #[must_use] + pub fn get_any_by_id_mut(&self, id: Uid) -> Option<ComponentHandleMut<'a, dyn Any>> + { + let component = self.get_matching_components(id).next()?; + + Some( + ComponentHandleMut::new_any(&component, self.world).unwrap_or_else(|err| { + panic!("Creating handle to component with id {id} failed: {err}"); + }), + ) + } + /// Returns a mutable reference to the component with the ID `id` in this entity. /// `None` is returned if the component isn't found. /// |
