From 09981e0173a2427264e432226804292c91e1f920 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 26 Aug 2025 16:43:40 +0200 Subject: feat(ecs): add component changed event --- ecs/src/entity.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 12 deletions(-) (limited to 'ecs/src/entity.rs') diff --git a/ecs/src/entity.rs b/ecs/src/entity.rs index 34fd19f..ca867f9 100644 --- a/ecs/src/entity.rs +++ b/ecs/src/entity.rs @@ -21,6 +21,7 @@ pub struct Handle<'a> { archetype: &'a Archetype, entity: &'a ArchetypeEntity, + world: &'a World, } impl<'a> Handle<'a> @@ -41,7 +42,7 @@ impl<'a> Handle<'a> /// - The component's ID is not a component ID /// - The component is mutably borrowed elsewhere #[must_use] - pub fn get(&self) -> Option> + pub fn get(&self) -> Option> { assert_eq!(ComponentT::id().kind(), UidKind::Component); @@ -76,12 +77,44 @@ impl<'a> Handle<'a> let component = self.get_matching_components(ComponentT::id()).next()?; Some( - ComponentHandleMut::from_entity_component_ref(&component).unwrap_or_else( - |err| { + ComponentHandleMut::from_entity_component_ref(&component, self.world) + .unwrap_or_else(|err| { panic!( "Creating handle to component {} failed: {err}", type_name::() ); + }), + ) + } + + /// 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 ID is not a component/pair ID + /// - The component is borrowed mutably elsewhere + /// - The component type is incorrect + #[must_use] + pub fn get_with_id( + &self, + id: Uid, + ) -> Option> + { + 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( + ComponentHandle::from_entity_component_ref(&component).unwrap_or_else( + |err| { + panic!( + "Creating handle to component {} failed: {err}", + type_name::() + ); }, ), ) @@ -96,10 +129,10 @@ impl<'a> Handle<'a> /// - The component is borrowed elsewhere /// - The component type is incorrect #[must_use] - pub fn get_with_id_mut( + pub fn get_with_id_mut( &self, id: Uid, - ) -> Option> + ) -> Option> { assert!( matches!(id.kind(), UidKind::Component | UidKind::Pair), @@ -109,14 +142,13 @@ impl<'a> Handle<'a> let component = self.get_matching_components(id).next()?; Some( - ComponentHandleMut::from_entity_component_ref(&component).unwrap_or_else( - |err| { + ComponentHandleMut::from_entity_component_ref(&component, self.world) + .unwrap_or_else(|err| { panic!( "Creating handle to component {} failed: {err}", - type_name::() + type_name::() ); - }, - ), + }), ) } @@ -131,9 +163,13 @@ impl<'a> Handle<'a> } } - pub(crate) fn new(archetype: &'a Archetype, entity: &'a ArchetypeEntity) -> Self + pub(crate) fn new( + archetype: &'a Archetype, + entity: &'a ArchetypeEntity, + world: &'a World, + ) -> Self { - Self { archetype, entity } + Self { archetype, entity, world } } } @@ -155,6 +191,7 @@ impl<'a> Iterator for MatchingComponentIter<'a> Some(EntityComponentRef::new( matching_component_id, self.entity.components().get(index).unwrap(), + self.entity.uid(), )) } } -- cgit v1.2.3-18-g5258