From 79ac38c548079f8545dcf06aa443274a5e9e53fd Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 13 Aug 2024 21:17:52 +0200 Subject: fix(ecs): check if entity components contains component before add --- ecs/src/component/storage.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/ecs/src/component/storage.rs b/ecs/src/component/storage.rs index 05ede9b..944beb3 100644 --- a/ecs/src/component/storage.rs +++ b/ecs/src/component/storage.rs @@ -149,7 +149,7 @@ impl Storage pub fn add_components_to_entity( &mut self, entity_uid: EntityUid, - components: impl IntoIterator>, + components: Vec>, ) -> Option<()> { let archetype_id = self.entity_archetype_lookup.get(&entity_uid)?; @@ -159,6 +159,29 @@ impl Storage let archetype = self.archetypes.get_mut(archetype_index)?; + let contains_component_already = components + .iter() + .find(|component| archetype.component_ids.contains_key(&component.id())) + .is_some(); + + if contains_component_already { + let component_cnt = components.len(); + + // TODO: return error + panic!( + "Entity with UID {:?} already contains one or more component(s) ({})", + entity_uid, + components + .iter() + .map(|component| [component.type_name(), ", "]) + .flatten() + .enumerate() + .take_while(|(index, _)| { *index < (component_cnt * 2) - 1 }) + .map(|(_, component)| component) + .collect::() + ); + } + let entity = archetype.take_entity(entity_uid)?; self.push_entity( -- cgit v1.2.3-18-g5258