From 2d1cf05abb72699d38a7c7db7e131922252e1fc1 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 6 Aug 2025 14:14:58 +0200 Subject: revert(ecs): make component removals not queryable This reverts commit 43cbd47900d23801c584def1b7877fdea700c23a. --- ecs/src/lib.rs | 140 +++++++++++++++------------------------------------------ 1 file changed, 35 insertions(+), 105 deletions(-) (limited to 'ecs/src/lib.rs') diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index 07b1cba..53abc6b 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -15,13 +15,14 @@ use crate::component::storage::archetype::EntityComponent as ArchetypeEntityComp use crate::component::storage::Storage as ComponentStorage; use crate::component::{ Component, - IntoParts, Parts as ComponentParts, - Removals as ComponentRemovals, Sequence as ComponentSequence, }; -use crate::entity::{Handle as EntityHandle, CREATE_STATIC_ENTITIES}; -use crate::event::component::Added as ComponentAddedEvent; +use crate::entity::CREATE_STATIC_ENTITIES; +use crate::event::component::{ + Added as ComponentAddedEvent, + Removed as ComponentRemovedEvent, +}; use crate::extension::{Collector as ExtensionCollector, Extension}; use crate::lock::Lock; use crate::pair::{ChildOf, DependsOn, Pair}; @@ -208,12 +209,8 @@ impl World self.data.component_storage.create_imaginary_archetypes(); - let prev_pending_removals = std::mem::take(&mut self.data.pending_removals); - self.perform_queued_actions(); - self.perform_removals(prev_pending_removals); - if self.stop.load(Ordering::Relaxed) { return StepResult::Stop; } @@ -358,8 +355,6 @@ impl World let mut has_swapped_active_queue = false; - // TODO: Figure out a good way to handle situations where there are multiple - // AddComponents/RemoveComponents actions that affect the same entity. for action in active_action_queue.drain(..) { match action { Action::Spawn(components) => { @@ -387,12 +382,24 @@ impl World } } Action::Despawn(entity_uid) => { - Self::schedule_removal( - &mut self.data.component_storage, - &mut self.data.pending_removals, - entity_uid, - PendingRemoval::Entity, - ); + let removed_entity = + match self.data.component_storage.remove_entity(entity_uid) { + Ok(components) => components, + Err(err) => { + tracing::error!("Failed to despawn entity: {err}"); + return; + } + }; + + if !has_swapped_active_queue { + self.swap_event_queue(&mut has_swapped_active_queue); + } + + for removed_ent_comp in removed_entity.components() { + self.emit_event_by_id::( + removed_ent_comp.id(), + ); + } } Action::AddComponents(entity_uid, components) => { let added_component_ids = Self::add_entity_components( @@ -410,12 +417,19 @@ impl World } } Action::RemoveComponents(entity_uid, component_ids) => { - Self::schedule_removal( - &mut self.data.component_storage, - &mut self.data.pending_removals, + let removed_component_ids = Self::remove_entity_components( entity_uid, - PendingRemoval::Components(component_ids), + component_ids, + &mut self.data.component_storage, ); + + if !has_swapped_active_queue { + self.swap_event_queue(&mut has_swapped_active_queue); + } + + for comp_id in removed_component_ids { + self.emit_event_by_id::(comp_id); + } } Action::Stop => { self.stop.store(true, Ordering::Relaxed); @@ -424,66 +438,6 @@ impl World } } - fn perform_removals(&mut self, removals: Vec<(Uid, PendingRemoval)>) - { - for (entity_id, removal) in removals { - match removal { - PendingRemoval::Components(component_ids) => { - Self::remove_entity_components( - entity_id, - component_ids.into_iter().chain([ComponentRemovals::id()]), - &mut self.data.component_storage, - ); - } - PendingRemoval::Entity => { - if let Err(err) = self.data.component_storage.remove_entity(entity_id) - { - tracing::error!("Failed to remove entity {entity_id}: {err}"); - } - } - } - } - } - - #[tracing::instrument(skip(component_storage, pending_removals))] - fn schedule_removal( - component_storage: &mut ComponentStorage, - pending_removals: &mut Vec<(Uid, PendingRemoval)>, - entity_uid: Uid, - removal: PendingRemoval, - ) - { - let Some(ent_handle) = Self::get_entity(component_storage, entity_uid) else { - tracing::warn!("Cannot schedule removal. Entity does not exist"); - return; - }; - - let component_ids = match removal { - PendingRemoval::Components(ref component_ids) => component_ids, - PendingRemoval::Entity => &ent_handle.component_ids().collect::>(), - }; - - let Some(mut component_removals) = ent_handle.get_mut::() - else { - Self::add_entity_components( - entity_uid, - [ComponentRemovals::from_iter(component_ids.iter().copied()) - .into_parts()], - component_storage, - ); - - pending_removals.push((entity_uid, removal)); - - return; - }; - - component_removals.add_ids(component_ids.iter().copied()); - - drop(component_removals); - - pending_removals.push((entity_uid, removal)); - } - fn add_entity_components( entity_uid: Uid, components: impl IntoIterator, @@ -567,21 +521,6 @@ impl World *has_swapped_active_queue = true; } - - fn get_entity( - component_storage: &mut ComponentStorage, - entity_uid: Uid, - ) -> Option> - { - let archetype = component_storage.get_entity_archetype(entity_uid)?; - - Some(EntityHandle::new( - archetype, - archetype - .get_entity_by_id(entity_uid) - .expect("Not possible"), - )) - } } impl Default for World @@ -603,12 +542,11 @@ pub enum StepResult } #[derive(Debug)] -struct WorldData +pub struct WorldData { component_storage: ComponentStorage, sole_storage: SoleStorage, action_queue: Rc, - pending_removals: Vec<(Uid, PendingRemoval)>, } impl Default for WorldData @@ -619,18 +557,10 @@ impl Default for WorldData component_storage: ComponentStorage::default(), sole_storage: SoleStorage::default(), action_queue: Rc::new(ActionQueue::default()), - pending_removals: Vec::new(), } } } -#[derive(Debug)] -enum PendingRemoval -{ - Components(Vec), - Entity, -} - #[derive(Debug)] pub struct EntityComponentRef<'a> { -- cgit v1.2.3-18-g5258