diff options
author | HampusM <hampus@hampusmat.com> | 2025-09-10 15:35:26 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2025-09-10 15:35:26 +0200 |
commit | ce1bade2c21cc3129fa8bc2b4bc67bc4dc2c25c3 (patch) | |
tree | b1c947e7736b69b4fbf16521bc9705450525f61c | |
parent | f5ee3b13a45b58b482a48c97ea6e67b587f1cc52 (diff) |
refactor(ecs): remove component added & removed events
-rw-r--r-- | ecs/src/component/storage.rs | 2 | ||||
-rw-r--r-- | ecs/src/component/storage/archetype.rs | 13 | ||||
-rw-r--r-- | ecs/src/event/component.rs | 29 | ||||
-rw-r--r-- | ecs/src/lib.rs | 172 | ||||
-rw-r--r-- | ecs/src/lock.rs | 9 |
5 files changed, 48 insertions, 177 deletions
diff --git a/ecs/src/component/storage.rs b/ecs/src/component/storage.rs index b27b552..4ec5222 100644 --- a/ecs/src/component/storage.rs +++ b/ecs/src/component/storage.rs @@ -270,7 +270,7 @@ impl Storage entity.insert_component( component_id, - ArchetypeEntityComponent::new(component, component_id, component_name), + ArchetypeEntityComponent::new(component, component_name), add_edge_archetype, ); diff --git a/ecs/src/component/storage/archetype.rs b/ecs/src/component/storage/archetype.rs index 788794a..d96632e 100644 --- a/ecs/src/component/storage/archetype.rs +++ b/ecs/src/component/storage/archetype.rs @@ -314,29 +314,18 @@ impl Entity #[derive(Debug)] pub struct EntityComponent { - id: Uid, component: Lock<Box<dyn Any>>, } impl EntityComponent { - pub fn new( - component: Box<dyn Any>, - component_id: Uid, - component_name: &'static str, - ) -> Self + pub fn new(component: Box<dyn Any>, component_name: &'static str) -> Self { Self { - id: component_id, component: Lock::new(component, component_name), } } - pub fn id(&self) -> Uid - { - self.id - } - pub fn component(&self) -> &Lock<Box<dyn Any>> { &self.component diff --git a/ecs/src/event/component.rs b/ecs/src/event/component.rs index ef09480..b96f23b 100644 --- a/ecs/src/event/component.rs +++ b/ecs/src/event/component.rs @@ -1,18 +1,15 @@ //! Component events. -use std::convert::Infallible; -use std::fmt::Debug; - -use crate::Component; - -/// Pair relation for events emitted when: -/// a) A entity with the target component is spawned. -/// b) The target component is added to a entity. -#[derive(Debug, Component)] -pub struct Added(Infallible); - -/// Pair relation for events emitted when: -/// a) The target component is removed from a entity. -/// b) A entity with the target component is despawned. -#[derive(Debug, Component)] -pub struct Removed(Infallible); +// TODO: Implement +// /// Pair relation for events emitted when: +// /// a) A entity with the target component is spawned. +// /// b) The target component is added to a entity. +// #[derive(Debug, Component)] +// pub struct Added(Infallible); + +// TODO: Implement +// /// Pair relation for events emitted when: +// /// a) The target component is removed from a entity. +// /// b) A entity with the target component is despawned. +// #[derive(Debug, Component)] +// pub struct Removed(Infallible); diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index f5fa9f6..ab30980 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -1,7 +1,6 @@ #![deny(clippy::all, clippy::pedantic)] use std::any::{type_name, Any, TypeId}; -use std::cell::RefCell; use std::fmt::Debug; use std::mem::ManuallyDrop; use std::rc::Rc; @@ -19,10 +18,6 @@ use crate::component::{ Sequence as ComponentSequence, }; use crate::entity::{Declaration as EntityDeclaration, Handle as EntityHandle}; -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, Wildcard}; @@ -115,28 +110,20 @@ impl World return; } - let added_component_ids = Self::add_entity_components( + Self::add_entity_components( entity_uid, components.into_parts_array(), &mut self.data.component_storage, ); - - for comp_id in added_component_ids { - self.emit_event_by_id::<ComponentAddedEvent>(comp_id); - } } pub fn add_component(&mut self, entity_id: Uid, component_parts: ComponentParts) { - let added_component_ids = Self::add_entity_components( + Self::add_entity_components( entity_id, [component_parts], &mut self.data.component_storage, ); - - for comp_id in added_component_ids { - self.emit_event_by_id::<ComponentAddedEvent>(comp_id); - } } pub fn create_declared_entity(&mut self, entity_decl: &EntityDeclaration) @@ -379,22 +366,16 @@ impl World #[tracing::instrument(skip_all)] fn perform_queued_actions(&mut self) { - let mut active_action_queue = match *self.data.action_queue.active_queue.borrow() - { - ActiveActionQueue::A => &self.data.action_queue.queue_a, - ActiveActionQueue::B => &self.data.action_queue.queue_b, - } - .write_nonblock() - .unwrap_or_else(|err| { - panic!( - "Failed to take read-write action queue lock {:?}: {err}", - self.data.action_queue.active_queue - ); - }); - - let mut has_swapped_active_queue = false; + let mut action_queue_lock = self + .data + .action_queue + .queue + .write_nonblock() + .unwrap_or_else(|err| { + panic!("Failed to take read-write action queue lock: {err}",); + }); - for action in active_action_queue.drain(..) { + for action in action_queue_lock.drain(..) { match action { Action::Spawn(components) => { let new_entity_uid = Uid::new_unique(UidKind::Entity); @@ -406,69 +387,32 @@ impl World continue; } - let added_component_ids = Self::add_entity_components( + Self::add_entity_components( new_entity_uid, components, &mut self.data.component_storage, ); - - if !has_swapped_active_queue { - self.swap_event_queue(&mut has_swapped_active_queue); - } - - for comp_id in added_component_ids { - self.emit_event_by_id::<ComponentAddedEvent>(comp_id); - } } Action::Despawn(entity_uid) => { - 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::<ComponentRemovedEvent>( - removed_ent_comp.id(), - ); + if let Err(err) = + self.data.component_storage.remove_entity(entity_uid) + { + tracing::error!("Failed to despawn entity: {err}"); } } Action::AddComponents(entity_uid, components) => { - let added_component_ids = Self::add_entity_components( + Self::add_entity_components( entity_uid, components, &mut self.data.component_storage, ); - - if !has_swapped_active_queue { - self.swap_event_queue(&mut has_swapped_active_queue); - } - - for comp_id in added_component_ids { - self.emit_event_by_id::<ComponentAddedEvent>(comp_id); - } } Action::RemoveComponents(entity_uid, component_ids) => { - let removed_component_ids = Self::remove_entity_components( + Self::remove_entity_components( entity_uid, 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::<ComponentRemovedEvent>(comp_id); - } } Action::Stop => { self.stop.store(true, Ordering::Relaxed); @@ -529,44 +473,6 @@ impl World removed_component_ids } - - fn emit_event_by_id<Event: Component>(&self, target: Uid) - { - if target.kind() == UidKind::Pair { - return; - } - - let query = Query::<(&SystemComponent,)>::from_flexible_query( - self.flexible_query( - QueryTerms::<QUERY_MAX_TERM_CNT>::builder() - .with_required([ - SystemComponent::id(), - Pair::new::<Event>(target).id(), - ]) - .build(), - ), - ); - - for (system_ent_id, (system,)) in query.iter_with_euids() { - unsafe { - system - .system - .run(self, SystemMetadata { ent_id: system_ent_id }); - } - } - } - - fn swap_event_queue(&self, has_swapped_active_queue: &mut bool) - { - let mut active_queue = self.data.action_queue.active_queue.borrow_mut(); - - *active_queue = match *active_queue { - ActiveActionQueue::A => ActiveActionQueue::B, - ActiveActionQueue::B => ActiveActionQueue::A, - }; - - *has_swapped_active_queue = true; - } } impl Default for World @@ -633,50 +539,20 @@ impl<'a> EntityComponentRef<'a> } } -#[derive(Debug, Default, Clone, Copy)] -enum ActiveActionQueue -{ - #[default] - A, - B, -} - -#[derive(Debug)] +#[derive(Debug, Default)] struct ActionQueue { - queue_a: Lock<Vec<Action>>, - queue_b: Lock<Vec<Action>>, - active_queue: RefCell<ActiveActionQueue>, + queue: Lock<Vec<Action>>, } impl ActionQueue { fn push(&self, action: Action) { - match *self.active_queue.borrow() { - ActiveActionQueue::A => self - .queue_a - .write_nonblock() - .expect("Failed to aquire read-write action queue A lock") - .push(action), - ActiveActionQueue::B => self - .queue_b - .write_nonblock() - .expect("Failed to aquire read-write action queue A lock") - .push(action), - } - } -} - -impl Default for ActionQueue -{ - fn default() -> Self - { - Self { - queue_a: Lock::new(Vec::new(), type_name::<Vec<Action>>()), - queue_b: Lock::new(Vec::new(), type_name::<Vec<Action>>()), - active_queue: RefCell::new(ActiveActionQueue::default()), - } + self.queue + .write_nonblock() + .expect("Failed to aquire read-write lock to action queue") + .push(action); } } diff --git a/ecs/src/lock.rs b/ecs/src/lock.rs index 689070b..fe4e08b 100644 --- a/ecs/src/lock.rs +++ b/ecs/src/lock.rs @@ -1,3 +1,4 @@ +use std::any::type_name; use std::mem::forget; use std::ops::{Deref, DerefMut}; @@ -62,6 +63,14 @@ impl<Value> Lock<Value> } } +impl<Value: Default + 'static> Default for Lock<Value> +{ + fn default() -> Self + { + Self::new(Value::default(), type_name::<Value>()) + } +} + #[derive(Debug, thiserror::Error)] pub enum Error { |