-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Not working yet, replace with an events for removed components
Refactor to use a resource of component ids to events Removed components as an iterator over events Clean up unnecessary code in event, update events on clear_trackers Correct documentation Clarify documentation Fix test to expect despawned entity as well Fix some CI and docs Fix links in docs Rename to match previous type names Rename in docs/tests Unused import Fix removal detection example Fix example docs Fix up rebase issue, make panic message more in line with other ones Fix docs for RemovedComponents
- Loading branch information
Showing
7 changed files
with
256 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
//! Removal event types. | ||
|
||
use crate as bevy_ecs; | ||
use crate::component::{Component, ComponentId, Components}; | ||
use crate::entity::Entity; | ||
use crate::event::{EventId, Events, ManualEventReader}; | ||
use crate::storage::SparseSet; | ||
use crate::system::{Local, Res, Resource, SystemParam}; | ||
use std::marker::PhantomData; | ||
|
||
/// A [`SystemParam`] that grants access to the entities that had their `T` [`Component`] removed. | ||
/// | ||
/// Note that this does not allow you to see which data existed before removal. | ||
/// If you need this, you will need to track the component data value on your own, | ||
/// using a regularly scheduled system that requests `Query<(Entity, &T), Changed<T>>` | ||
/// and stores the data somewhere safe to later cross-reference. | ||
/// | ||
/// If you are using `bevy_ecs` as a standalone crate, | ||
/// note that the [`RemovedComponentEvents`] events will not be automatically updated/cleared for you, | ||
/// and will need to be manually flushed using [`crate::world::World::clear_trackers`] or [`RemovedComponentEvents::update`] | ||
/// | ||
/// For users of `bevy` itself, this is automatically done in a system added by `MinimalPlugins` | ||
/// or `DefaultPlugins` at the end of each pass of the game loop during the `CoreStage::Last` | ||
/// stage. | ||
/// | ||
/// # Examples | ||
/// | ||
/// Basic usage: | ||
/// | ||
/// ``` | ||
/// # use bevy_ecs::component::Component; | ||
/// # use bevy_ecs::system::IntoSystem; | ||
/// # use bevy_ecs::removal_detection::RemovedComponents; | ||
/// # | ||
/// # #[derive(Component)] | ||
/// # struct MyComponent; | ||
/// | ||
/// fn react_on_removal(mut removed: RemovedComponents<MyComponent>) { | ||
/// removed.iter().for_each(|removed_entity| println!("{:?}", removed_entity)); | ||
/// } | ||
/// | ||
/// # bevy_ecs::system::assert_is_system(react_on_removal); | ||
/// ``` | ||
#[derive(SystemParam)] | ||
pub struct RemovedComponents<'w, 's, T: Component> { | ||
components: &'w Components, | ||
events: Res<'w, RemovedComponentEvents>, | ||
readers: Local<'s, RemovedComponentReaders>, | ||
#[system_param(ignore)] | ||
phantom: PhantomData<&'s T>, | ||
} | ||
|
||
impl<'w, 's, T: Component> RemovedComponents<'w, 's, T> { | ||
pub fn component_id(&self) -> ComponentId { | ||
self.components | ||
.component_id::<T>() | ||
.expect("component does not exist in the world") | ||
} | ||
|
||
pub fn iter(&mut self) -> impl Iterator<Item = Entity> + '_ { | ||
self.iter_with_id().map(|(e, _)| e) | ||
} | ||
|
||
pub fn iter_with_id(&mut self) -> impl Iterator<Item = (Entity, EventId<Entity>)> + '_ { | ||
let component_id = self.component_id(); | ||
let events = self.events.get(component_id); | ||
let reader = self.readers.get_mut(component_id); | ||
events | ||
.map(|events| reader.iter_with_id(events)) | ||
.into_iter() | ||
.flatten() | ||
.map(|(e, id)| (*e, id)) | ||
} | ||
|
||
pub fn len(&self) -> usize { | ||
let component_id = self.component_id(); | ||
let events = self.events.get(component_id); | ||
let reader = self.readers.get(component_id); | ||
match (reader, events) { | ||
(Some(reader), Some(events)) => reader.len(events), | ||
(None, Some(events)) => events.len(), | ||
_ => 0, | ||
} | ||
} | ||
|
||
pub fn is_empty(&self) -> bool { | ||
self.len() == 0 | ||
} | ||
} | ||
|
||
#[derive(Default, Debug, Resource)] | ||
pub struct RemovedComponentEvents { | ||
events: SparseSet<ComponentId, Events<Entity>>, | ||
} | ||
|
||
impl RemovedComponentEvents { | ||
pub fn get(&self, component_id: ComponentId) -> Option<&Events<Entity>> { | ||
self.events.get(component_id) | ||
} | ||
|
||
pub fn get_mut(&mut self, component_id: ComponentId) -> &mut Events<Entity> { | ||
self.events | ||
.get_or_insert_with(component_id, Default::default) | ||
} | ||
|
||
pub fn extend( | ||
&mut self, | ||
component_id: ComponentId, | ||
entities: impl IntoIterator<Item = Entity>, | ||
) { | ||
self.get_mut(component_id).extend(entities); | ||
} | ||
|
||
pub fn push(&mut self, component_id: ComponentId, entity: Entity) { | ||
self.get_mut(component_id).send(entity); | ||
} | ||
|
||
pub fn update(&mut self) { | ||
for events in self.events.values_mut() { | ||
events.update(); | ||
} | ||
} | ||
} | ||
|
||
#[derive(Default, Debug, Resource)] | ||
pub struct RemovedComponentReaders { | ||
readers: SparseSet<ComponentId, ManualEventReader<Entity>>, | ||
} | ||
|
||
impl RemovedComponentReaders { | ||
pub fn get(&self, component_id: ComponentId) -> Option<&ManualEventReader<Entity>> { | ||
self.readers.get(component_id) | ||
} | ||
|
||
pub fn get_mut(&mut self, component_id: ComponentId) -> &mut ManualEventReader<Entity> { | ||
self.readers | ||
.get_or_insert_with(component_id, Default::default) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate as bevy_ecs; | ||
use crate::{ | ||
component::Component, | ||
removal_detection::RemovedComponents, | ||
system::{Resource, SystemState}, | ||
world::World, | ||
}; | ||
|
||
#[derive(Component, Resource, Debug, PartialEq, Eq, Clone, Copy)] | ||
struct A(usize); | ||
#[derive(Component, Debug, PartialEq, Eq, Clone, Copy)] | ||
struct B(usize); | ||
#[derive(Component, Debug, PartialEq, Eq, Clone, Copy)] | ||
struct C; | ||
|
||
#[derive(Component, Copy, Clone, PartialEq, Eq, Debug)] | ||
#[component(storage = "Table")] | ||
struct TableStored(&'static str); | ||
#[derive(Component, Copy, Clone, PartialEq, Eq, Debug)] | ||
#[component(storage = "SparseSet")] | ||
struct SparseStored(u32); | ||
|
||
#[test] | ||
fn remove_tracking() { | ||
let mut world = World::new(); | ||
|
||
let a = world.spawn().insert_bundle((SparseStored(0), A(123))).id(); | ||
let b = world.spawn().insert_bundle((SparseStored(1),)).id(); | ||
let c = world | ||
.spawn() | ||
.insert_bundle((SparseStored(0), A(123), B(1), C)) | ||
.id(); | ||
|
||
let mut a_system: SystemState<RemovedComponents<A>> = SystemState::new(&mut world); | ||
let mut c_system: SystemState<RemovedComponents<C>> = SystemState::new(&mut world); | ||
let mut sparse_system: SystemState<RemovedComponents<SparseStored>> = | ||
SystemState::new(&mut world); | ||
world.entity_mut(a).despawn(); | ||
world.entity_mut(b).despawn(); | ||
assert_eq!( | ||
a_system.get_mut(&mut world).iter().collect::<Vec<_>>(), | ||
&[a], | ||
"despawning results in 'removed component' state for table components" | ||
); | ||
assert_eq!( | ||
sparse_system.get_mut(&mut world).iter().collect::<Vec<_>>(), | ||
&[a, b], | ||
"despawning results in 'removed component' state for sparse set components" | ||
); | ||
|
||
// When we check again the event iterator should be ahead now. | ||
assert_eq!( | ||
a_system.get_mut(&mut world).iter().collect::<Vec<_>>(), | ||
&[], | ||
"reading from the same system state will result in no new removal events" | ||
); | ||
assert_eq!( | ||
sparse_system.get_mut(&mut world).iter().collect::<Vec<_>>(), | ||
&[], | ||
"reading from the same system state will result in no new removal events" | ||
); | ||
|
||
world.entity_mut(c).remove::<C>(); | ||
assert_eq!( | ||
c_system.get_mut(&mut world).iter().collect::<Vec<_>>(), | ||
&[c], | ||
"removing specific component should result in removal event" | ||
); | ||
assert_eq!( | ||
c_system.get_mut(&mut world).iter().collect::<Vec<_>>(), | ||
&[], | ||
"reading from same system state will result in no new removal events" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.