From 7b6ccbe72c4a5fb8de5d786964aa9b6e5ee8d664 Mon Sep 17 00:00:00 2001 From: lisk77 Date: Wed, 5 Mar 2025 08:22:09 +0100 Subject: [PATCH] docs: making `world.rs` a little prettier and removing the asserts but not much else --- crates/comet_ecs/src/world.rs | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/crates/comet_ecs/src/world.rs b/crates/comet_ecs/src/world.rs index 129fbc9..8c77cbc 100644 --- a/crates/comet_ecs/src/world.rs +++ b/crates/comet_ecs/src/world.rs @@ -69,15 +69,12 @@ impl World { /// Gets an immutable reference to an entity by its ID. pub fn get_entity(&self, entity_id: usize) -> Option<&Entity> { - //assert_ne!(self.entities.get(entity_id), None, "There is no entity with this ID ({}) in the world!", entity_id); self.entities.get(entity_id).unwrap().as_ref() } /// Gets a mutable reference to an entity by its ID. pub fn get_entity_mut(&mut self, entity_id: usize) -> Option<&mut Entity> { - //assert_ne!(self.entities.get(entity_id), None, "There is no entity with this ID ({}) in the world!", entity_id); self.entities.get_mut(entity_id).unwrap().as_mut() - //self.entities.get_mut(id).unwrap() } /// Deletes an entity by its ID. @@ -103,15 +100,19 @@ impl World { self.archetypes.remove_archetype(&components); } - fn remove_archetype_subsets(&mut self, components: ComponentSet) { + fn get_keys(&self, components: ComponentSet) -> Vec { let component_sets = self.archetypes.component_sets(); - let keys: Vec = component_sets.iter() + component_sets.iter() .enumerate() .filter_map(|(i, &ref elem)| if elem.is_subset(&components) { Some(i) } else { None }) .collect::>() .iter() .map(|index| component_sets[*index].clone()) - .collect::>(); + .collect::>() + } + + fn remove_archetype_subsets(&mut self, components: ComponentSet) { + let keys = self.get_keys(components); for key in keys { self.remove_archetype(key.clone()); @@ -127,14 +128,7 @@ impl World { } fn remove_entity_from_archetype_subsets(&mut self, entity_id: u32, components: ComponentSet) { - let component_sets = self.archetypes.component_sets(); - let keys: Vec = component_sets.iter() - .enumerate() - .filter_map(|(i, &ref elem)| if elem.is_subset(&components) { Some(i) } else { None }) - .collect::>() - .iter() - .map(|index| component_sets[*index].clone()) - .collect::>(); + let keys = self.get_keys(components); for key in keys { self.remove_entity_from_archetype(entity_id, key.clone()); @@ -179,7 +173,6 @@ impl World { /// Adds a component to an entity by its ID and an instance of the component. pub fn add_component(&mut self, entity_id: usize, component: C) { - assert_ne!(self.entities.get(entity_id), None, "There is no entity with this ID ({}) in the world!", entity_id); self.components.set_component(entity_id, component); let component_index = self.components.keys().iter_mut().position(|x| *x == C::type_id()).unwrap(); @@ -204,19 +197,16 @@ impl World { /// Returns a reference to a component of an entity by its ID. pub fn get_component(&self, entity_id: usize) -> Option<&C> { - //assert_ne!(self.entities.get(entity_id), None, "There is no entity with this ID ({}) in the world!", entity_id); self.components.get_component::(entity_id) } pub fn get_component_mut(&mut self, entity_id: usize) -> Option<&mut C> { - assert_ne!(self.entities.get(entity_id), None, "There is no entity with this ID ({}) in the world!", entity_id); - assert!(self.components.get_component::(entity_id) != None, "There is no component {} bound to the entity {} in the world!", C::type_name(), entity_id); self.components.get_component_mut::(entity_id) } /// Returns a list of entities that have the given components. pub fn get_entities_with(&self, components: ComponentSet) -> Vec { - //assert!(self.archetypes.contains_archetype(&components), "The given components {:?} are not registered in the world!", components); + error!("The given components {:?} are not registered in the world!", components); if self.archetypes.contains_archetype(&components) { return self.archetypes.get_archetype(&components).unwrap().clone(); }