summaryrefslogtreecommitdiff
path: root/ecs/src/lib.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2025-09-10 15:35:26 +0200
committerHampusM <hampus@hampusmat.com>2025-09-10 15:35:26 +0200
commitce1bade2c21cc3129fa8bc2b4bc67bc4dc2c25c3 (patch)
treeb1c947e7736b69b4fbf16521bc9705450525f61c /ecs/src/lib.rs
parentf5ee3b13a45b58b482a48c97ea6e67b587f1cc52 (diff)
refactor(ecs): remove component added & removed events
Diffstat (limited to 'ecs/src/lib.rs')
-rw-r--r--ecs/src/lib.rs172
1 files changed, 24 insertions, 148 deletions
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);
}
}