From 70c7d745f918dd23343599963a619539f4f880cb Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 1 Aug 2024 12:36:35 +0200 Subject: refactor(ecs): add & use component metadata struct --- ecs/src/archetype.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'ecs/src/archetype.rs') diff --git a/ecs/src/archetype.rs b/ecs/src/archetype.rs index a33de66..808d006 100644 --- a/ecs/src/archetype.rs +++ b/ecs/src/archetype.rs @@ -1,6 +1,10 @@ use std::hash::{DefaultHasher, Hash, Hasher}; -use crate::component::Id as ComponentId; +use crate::component::{ + Id as ComponentId, + IsOptional as ComponentIsOptional, + Metadata as ComponentMetadata, +}; /// Archetype ID. #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)] @@ -11,8 +15,25 @@ pub struct Id impl Id { + pub fn from_components_metadata( + components_metadata: impl IntoIterator, + ) -> Self + { + Self::new( + components_metadata + .into_iter() + .filter_map(|component_metadata| { + if component_metadata.is_optional == ComponentIsOptional::Yes { + return None; + } + + Some(component_metadata.id) + }), + ) + } + /// Returns the ID of a archetype with the given components. - pub fn new(component_ids: impl IntoIterator) -> Self + fn new(component_ids: impl IntoIterator) -> Self { let mut hasher = DefaultHasher::new(); -- cgit v1.2.3-18-g5258