diff options
| author | HampusM <hampus@hampusmat.com> | 2026-05-21 17:55:20 +0200 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-05-21 17:55:20 +0200 |
| commit | 8022e8998290b067b8aa0cb9cba8ba410826bdab (patch) | |
| tree | 7171e79ce530e03079046ee8fd12167160c45480 /ecs/src/entity.rs | |
| parent | 412cee02c252f91bcf0b70a3f5cc5fca6d2b4c62 (diff) | |
chore: rename ecs* crates to engine-ecs*
Diffstat (limited to 'ecs/src/entity.rs')
| -rw-r--r-- | ecs/src/entity.rs | 295 |
1 files changed, 0 insertions, 295 deletions
diff --git a/ecs/src/entity.rs b/ecs/src/entity.rs deleted file mode 100644 index ad9f179..0000000 --- a/ecs/src/entity.rs +++ /dev/null @@ -1,295 +0,0 @@ -use std::any::type_name; -use std::ops::Deref; -use std::sync::LazyLock; - -use crate::component::storage::archetype::{ - Archetype, - Entity as ArchetypeEntity, - MatchingComponentIter as ArchetypeMatchingComponentIter, -}; -use crate::component::{ - Component, - Handle as ComponentHandle, - HandleMut as ComponentHandleMut, -}; -use crate::pair::{ - ComponentOrWildcard, - MultipleWithWildcard as PairMultipleWithWildcard, - Pair, - WithWildcard as PairWithWildcard, -}; -use crate::uid::{Kind as UidKind, Uid}; -use crate::{EntityComponentRef, World}; - -pub mod obtainer; - -/// A handle to a entity. -#[derive(Debug, Clone)] -pub struct Handle<'a> -{ - archetype: &'a Archetype, - entity: &'a ArchetypeEntity, - world: &'a World, -} - -impl<'a> Handle<'a> -{ - /// Returns the [`Uid`] of this entity. - #[inline] - #[must_use] - pub fn uid(&self) -> Uid - { - self.entity.uid() - } - - /// Returns a reference to the specified component in this entity. `None` is - /// returned if the component isn't found in the entity. - /// - /// # Panics - /// Will panic if: - /// - The component's ID is not a component ID - /// - The component is mutably borrowed elsewhere - #[must_use] - pub fn get<ComponentT: Component>(&self) -> Option<ComponentHandle<'a, ComponentT>> - { - assert_eq!(ComponentT::id().kind(), UidKind::Component); - - let component = self.get_matching_components(ComponentT::id()).next()?; - - Some( - ComponentHandle::from_entity_component_ref(&component).unwrap_or_else( - |err| { - panic!( - "Creating handle to component {} failed: {err}", - type_name::<ComponentT>() - ); - }, - ), - ) - } - - /// Returns a mutable reference to the specified component in this entity. `None` is - /// returned if the component isn't found in the entity. - /// - /// # Panics - /// Will panic if: - /// - The component's ID is not a component ID - /// - The component is borrowed elsewhere - #[must_use] - pub fn get_mut<ComponentT: Component>( - &self, - ) -> Option<ComponentHandleMut<'a, ComponentT>> - { - assert_eq!(ComponentT::id().kind(), UidKind::Component); - - let component = self.get_matching_components(ComponentT::id()).next()?; - - Some( - ComponentHandleMut::from_entity_component_ref(&component, self.world) - .unwrap_or_else(|err| { - panic!( - "Creating handle to component {} failed: {err}", - type_name::<ComponentT>() - ); - }), - ) - } - - /// 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<ComponentDataT: 'static>( - &self, - id: Uid, - ) -> Option<ComponentHandle<'a, ComponentDataT>> - { - 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::<ComponentDataT>() - ); - }, - ), - ) - } - - /// 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<ComponentDataT: 'static>( - &self, - id: Uid, - ) -> Option<ComponentHandleMut<'a, ComponentDataT>> - { - 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, self.world) - .unwrap_or_else(|err| { - panic!( - "Creating handle to component {} failed: {err}", - type_name::<ComponentDataT>() - ); - }), - ) - } - - #[must_use] - pub fn get_first_wildcard_pair_match<Relation, Target>( - &self, - ) -> Option<PairWithWildcard<'a, Relation, Target>> - where - Relation: ComponentOrWildcard, - Target: ComponentOrWildcard, - { - let mut matching_comps = self.get_matching_components( - Pair::builder() - .relation_id(Relation::uid()) - .target_id(Target::uid()) - .build() - .id(), - ); - - Some(PairWithWildcard::new(self.world, matching_comps.next()?)) - } - - #[must_use] - pub fn get_wildcard_pair_matches<Relation, Target>( - &self, - ) -> PairMultipleWithWildcard<'a, Relation, Target> - where - Relation: ComponentOrWildcard, - Target: ComponentOrWildcard, - { - PairMultipleWithWildcard::new(self.world, self.clone()) - } - - #[inline] - #[must_use] - pub fn get_matching_components(&self, component_uid: Uid) - -> MatchingComponentIter<'a> - { - MatchingComponentIter { - inner: self.archetype.get_matching_component_indices(component_uid), - entity: self.entity, - } - } - - /// Returns whether or not this entity contains a component with the specified `Uid`. - #[must_use] - pub fn has_component(&self, component_uid: Uid) -> bool - { - self.archetype - .contains_component_with_exact_id(component_uid) - } - - /// Returns the `Uids`s of the components this entity has. - pub fn component_ids(&self) -> impl Iterator<Item = Uid> + '_ - { - self.archetype.component_ids_sorted() - } - - pub(crate) fn new( - archetype: &'a Archetype, - entity: &'a ArchetypeEntity, - world: &'a World, - ) -> Self - { - Self { archetype, entity, world } - } -} - -#[derive(Debug)] -pub struct MatchingComponentIter<'a> -{ - inner: ArchetypeMatchingComponentIter<'a>, - entity: &'a ArchetypeEntity, -} - -impl<'a> Iterator for MatchingComponentIter<'a> -{ - type Item = EntityComponentRef<'a>; - - fn next(&mut self) -> Option<Self::Item> - { - let (matching_component_id, index) = self.inner.next()?; - - Some(EntityComponentRef::new( - matching_component_id, - self.entity.components().get(index).unwrap(), - self.entity.uid(), - )) - } -} - -/// The data type of a declaration of a entity. -#[derive(Debug)] -pub struct Declaration -{ - uid: LazyLock<Uid>, - create_func: fn(&mut World), -} - -impl Declaration -{ - pub(crate) fn create(&self, world: &mut World) - { - (self.create_func)(world); - } - - #[doc(hidden)] - pub const fn new(create_func: fn(&mut World)) -> Self - { - Self { - uid: LazyLock::new(|| Uid::new_unique(UidKind::Entity)), - create_func, - } - } -} - -impl Deref for Declaration -{ - type Target = Uid; - - fn deref(&self) -> &Self::Target - { - &self.uid - } -} - -#[allow(clippy::module_name_repetitions)] -#[macro_export] -macro_rules! declare_entity { - ($visibility: vis $ident: ident, $components: expr) => { - $visibility static $ident: $crate::entity::Declaration = - $crate::entity::Declaration::new(|world| { - world.create_entity_with_uid(*$ident, $components); - }); - } -} |
