summaryrefslogtreecommitdiff
path: root/ecs/src/archetype.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-12-21 14:43:53 +0100
committerHampusM <hampus@hampusmat.com>2024-12-21 14:44:56 +0100
commit5feeaf154a8b729873c729b4488f28536cf4ae24 (patch)
tree044a4ebb94f11af6b44ccc411eca9ee55f92bcfc /ecs/src/archetype.rs
parent36bfd4159cb1bd8b3d47a834824465728dfb5bd8 (diff)
feat(ecs): add support for entities without components
Diffstat (limited to 'ecs/src/archetype.rs')
-rw-r--r--ecs/src/archetype.rs22
1 files changed, 10 insertions, 12 deletions
diff --git a/ecs/src/archetype.rs b/ecs/src/archetype.rs
index d2ee36a..354d206 100644
--- a/ecs/src/archetype.rs
+++ b/ecs/src/archetype.rs
@@ -4,7 +4,6 @@ use crate::component::{
IsOptional as ComponentIsOptional,
Metadata as ComponentMetadata,
};
-use crate::uid::Uid;
/// Archetype ID.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
@@ -19,10 +18,9 @@ impl Id
components_metadata: &impl AsRef<[ComponentMetadata]>,
) -> Self
{
- debug_assert!(
- components_metadata.as_ref().len() > 0,
- "Cannot create a archetype ID from a empty component metadata list"
- );
+ if components_metadata.as_ref().len() == 0 {
+ return Self { hash: 0 };
+ }
debug_assert!(
components_metadata
@@ -31,7 +29,7 @@ impl Id
"Cannot create archetype ID from a unsorted component metadata list"
);
- Self::new(
+ let component_ids =
components_metadata
.as_ref()
.iter()
@@ -41,13 +39,8 @@ impl Id
}
Some(component_metadata.id)
- }),
- )
- }
+ });
- /// Returns the ID of a archetype with the given components.
- fn new(component_ids: impl IntoIterator<Item = Uid>) -> Self
- {
let mut hasher = DefaultHasher::new();
for component_id in component_ids {
@@ -56,6 +49,11 @@ impl Id
let hash = hasher.finish();
+ assert_ne!(
+ hash, 0,
+ "Archetype ID 0 is reserved for a archetype with zero components"
+ );
+
Self { hash }
}
}