diff options
author | HampusM <hampus@hampusmat.com> | 2024-08-13 21:17:52 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-08-13 21:17:52 +0200 |
commit | 79ac38c548079f8545dcf06aa443274a5e9e53fd (patch) | |
tree | bc69c12ec9d25dc56203080236f02c6e39fd9c06 | |
parent | 3e8a96e83fb02aa52cfeee9cf9e215b385cfdbcb (diff) |
fix(ecs): check if entity components contains component before add
-rw-r--r-- | ecs/src/component/storage.rs | 25 |
1 files changed, 24 insertions, 1 deletions
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<Item = Box<dyn Component>>, + components: Vec<Box<dyn Component>>, ) -> 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::<String>() + ); + } + let entity = archetype.take_entity(entity_uid)?; self.push_entity( |