summaryrefslogtreecommitdiff
path: root/ecs/src/lib.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2025-10-12 17:13:42 +0200
committerHampusM <hampus@hampusmat.com>2025-10-13 17:22:35 +0200
commit88a33aed4115984d496fcd12a965f9c4aaa0faf6 (patch)
tree56e73790c9d1b8662d7997d93756ce9b2fadb8af /ecs/src/lib.rs
parentea1d70c8c28e3b96da6264021fa1c62e28fcd8e4 (diff)
feat(ecs): emit Removed events before component removal
Diffstat (limited to 'ecs/src/lib.rs')
-rw-r--r--ecs/src/lib.rs79
1 files changed, 25 insertions, 54 deletions
diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs
index fce780c..48a770e 100644
--- a/ecs/src/lib.rs
+++ b/ecs/src/lib.rs
@@ -19,7 +19,7 @@ use crate::component::{
Sequence as ComponentSequence,
};
use crate::entity::{Declaration as EntityDeclaration, Handle as EntityHandle};
-use crate::event::component::{Added, Removed};
+use crate::event::component::Added;
use crate::event::{Emitted as EmittedEvent, NewEvents, Submitter as EventSubmitter};
use crate::extension::{Collector as ExtensionCollector, Extension};
use crate::lock::Lock;
@@ -408,21 +408,30 @@ impl World
fn emit_new_events(&self)
{
- let new_events = self
- .data
- .new_events
- .write_nonblock()
- .expect("Failed to acquire read-write lock to new events")
- .take();
-
- for (event_id, event_matches) in new_events {
- self.emit_event_observers(
- event_id,
- &EmittedEvent {
- event: event_id,
- match_ids: &event_matches.match_ids,
- },
- );
+ loop {
+ let new_events = {
+ let mut new_events_lock = self
+ .data
+ .new_events
+ .write_nonblock()
+ .expect("Failed to acquire read-write lock to new events");
+
+ if new_events_lock.is_empty() {
+ break;
+ }
+
+ new_events_lock.take()
+ };
+
+ for (event_id, event_matches) in new_events {
+ self.emit_event_observers(
+ event_id,
+ &EmittedEvent {
+ event: event_id,
+ match_ids: &event_matches.match_ids,
+ },
+ );
+ }
}
}
@@ -456,33 +465,10 @@ impl World
);
}
Action::Despawn(entity_uid) => {
- let component_ids = self
- .get_entity(entity_uid)
- .expect("Not possible")
- .component_ids()
- .collect::<Vec<_>>();
-
if let Err(err) =
self.data.component_storage.remove_entity(entity_uid)
{
tracing::error!("Failed to despawn entity: {err}");
- continue;
- }
-
- let event_submitter = EventSubmitter::new(&self.data.new_events);
-
- for comp_id in component_ids {
- if comp_id.kind() == UidKind::Pair {
- continue;
- }
-
- event_submitter.submit_event(
- &Pair::builder()
- .relation::<Removed>()
- .target_id(comp_id)
- .build(),
- entity_uid,
- );
}
}
Action::AddComponents(entity_uid, components) => {
@@ -498,7 +484,6 @@ impl World
entity_uid,
component_ids,
&mut self.data.component_storage,
- &EventSubmitter::new(&self.data.new_events),
);
}
Action::Stop => {
@@ -548,7 +533,6 @@ impl World
entity_uid: Uid,
component_ids: impl IntoIterator<Item = Uid>,
component_storage: &mut ComponentStorage,
- event_submitter: &EventSubmitter<'_>,
)
{
let component_id_iter = component_ids.into_iter();
@@ -558,20 +542,7 @@ impl World
component_storage.remove_entity_component(entity_uid, component_id)
{
tracing::error!("Failed to remove component to entity: {err}");
- continue;
- }
-
- if component_id.kind() == UidKind::Pair {
- continue;
}
-
- event_submitter.submit_event(
- &Pair::builder()
- .relation::<Removed>()
- .target_id(component_id)
- .build(),
- entity_uid,
- );
}
}