diff options
author | HampusM <hampus@hampusmat.com> | 2024-08-12 19:43:47 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-08-12 19:44:06 +0200 |
commit | 28438c40c09bb52f8bd444b6f5cf9b34bdf71fee (patch) | |
tree | ae6d506f9a9fc30a332deb0f51b098be87f8d78b /ecs/src/component | |
parent | ee69aa92802ba9f5becd533465ca1639cb670ace (diff) |
feat(ecs): add action for removing components(s) from entity
Diffstat (limited to 'ecs/src/component')
-rw-r--r-- | ecs/src/component/storage.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/ecs/src/component/storage.rs b/ecs/src/component/storage.rs index 76e1a7e..05ede9b 100644 --- a/ecs/src/component/storage.rs +++ b/ecs/src/component/storage.rs @@ -174,6 +174,36 @@ impl Storage Some(()) } + pub fn remove_components_from_entity( + &mut self, + entity_uid: EntityUid, + component_ids: impl IntoIterator<Item = ComponentId>, + ) -> Option<()> + { + let archetype_id = self.entity_archetype_lookup.get(&entity_uid)?; + + let archetype_index = + self.find_archetype_index_with_entity(*archetype_id, entity_uid)?; + + let archetype = self.archetypes.get_mut(archetype_index)?; + + let entity = archetype.take_entity(entity_uid)?; + + let component_ids_set = component_ids.into_iter().collect::<HashSet<_>>(); + + self.push_entity( + entity_uid, + entity + .components + .into_iter() + .map(|component| component.component.into_inner()) + .filter(|component| !component_ids_set.contains(&component.id())) + .collect(), + ); + + Some(()) + } + fn populate_matching_archetype_lookup_entries( &mut self, comp_ids_set: &HashSet<ComponentId>, |