diff options
author | HampusM <hampus@hampusmat.com> | 2025-08-15 13:38:27 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2025-08-22 17:18:54 +0200 |
commit | e229b104593d3afede47cc07e9917a42d6d13e60 (patch) | |
tree | 25cd15befc8bf3e5864158aa791895c6d5bea751 /ecs/src/entity.rs | |
parent | c8d3b211b8328452c9d15955004030430a99d1fc (diff) |
refactor(ecs): store local components as system entity components
Diffstat (limited to 'ecs/src/entity.rs')
-rw-r--r-- | ecs/src/entity.rs | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/ecs/src/entity.rs b/ecs/src/entity.rs index 6c9ec32..34fd19f 100644 --- a/ecs/src/entity.rs +++ b/ecs/src/entity.rs @@ -69,7 +69,7 @@ impl<'a> Handle<'a> #[must_use] pub fn get_mut<ComponentT: Component>( &self, - ) -> Option<ComponentHandleMut<'_, ComponentT>> + ) -> Option<ComponentHandleMut<'a, ComponentT>> { assert_eq!(ComponentT::id().kind(), UidKind::Component); @@ -87,6 +87,39 @@ impl<'a> Handle<'a> ) } + /// Returns a mutable 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 ID is not a component/pair ID + /// - The component is borrowed elsewhere + /// - The component type is incorrect + #[must_use] + pub fn get_with_id_mut<ComponentData: 'static>( + &self, + id: Uid, + ) -> Option<ComponentHandleMut<'a, ComponentData>> + { + assert!( + matches!(id.kind(), UidKind::Component | UidKind::Pair), + "ID {id:?} is not a component/pair ID" + ); + + let component = self.get_matching_components(id).next()?; + + Some( + ComponentHandleMut::from_entity_component_ref(&component).unwrap_or_else( + |err| { + panic!( + "Creating handle to component {} failed: {err}", + type_name::<ComponentData>() + ); + }, + ), + ) + } + #[inline] #[must_use] pub fn get_matching_components(&self, component_uid: Uid) |