|
| 1 | +//! Alerting events when a component is removed from an entity. |
| 2 | +
|
| 3 | +use crate::{ |
| 4 | + self as bevy_ecs, |
| 5 | + component::{Component, ComponentId, ComponentIdFor}, |
| 6 | + entity::Entity, |
| 7 | + event::{Events, ManualEventIterator, ManualEventReader}, |
| 8 | + prelude::Local, |
| 9 | + storage::SparseSet, |
| 10 | + system::{ReadOnlySystemParam, SystemMeta, SystemParam}, |
| 11 | + world::World, |
| 12 | +}; |
| 13 | + |
| 14 | +use std::{ |
| 15 | + fmt::Debug, |
| 16 | + iter, |
| 17 | + marker::PhantomData, |
| 18 | + ops::{Deref, DerefMut}, |
| 19 | + option, |
| 20 | +}; |
| 21 | + |
| 22 | +/// Wrapper around a [`ManualEventReader<Entity>`] so that we |
| 23 | +/// can differentiate events between components. |
| 24 | +#[derive(Debug)] |
| 25 | +pub struct RemovedComponentReader<T> |
| 26 | +where |
| 27 | + T: Component, |
| 28 | +{ |
| 29 | + reader: ManualEventReader<Entity>, |
| 30 | + marker: PhantomData<T>, |
| 31 | +} |
| 32 | + |
| 33 | +impl<T: Component> Default for RemovedComponentReader<T> { |
| 34 | + fn default() -> Self { |
| 35 | + Self { |
| 36 | + reader: Default::default(), |
| 37 | + marker: PhantomData, |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl<T: Component> Deref for RemovedComponentReader<T> { |
| 43 | + type Target = ManualEventReader<Entity>; |
| 44 | + fn deref(&self) -> &Self::Target { |
| 45 | + &self.reader |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl<T: Component> DerefMut for RemovedComponentReader<T> { |
| 50 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 51 | + &mut self.reader |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/// Wrapper around a map of components to [`Events<Entity>`]. |
| 56 | +/// So that we can find the events without naming the type directly. |
| 57 | +#[derive(Default, Debug)] |
| 58 | +pub struct RemovedComponentEvents { |
| 59 | + event_sets: SparseSet<ComponentId, Events<Entity>>, |
| 60 | +} |
| 61 | + |
| 62 | +impl RemovedComponentEvents { |
| 63 | + pub fn new() -> Self { |
| 64 | + Self::default() |
| 65 | + } |
| 66 | + |
| 67 | + pub fn update(&mut self) { |
| 68 | + for (_component_id, events) in self.event_sets.iter_mut() { |
| 69 | + events.update(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + pub fn get(&self, component_id: impl Into<ComponentId>) -> Option<&Events<Entity>> { |
| 74 | + self.event_sets.get(component_id.into()) |
| 75 | + } |
| 76 | + |
| 77 | + pub fn send(&mut self, component_id: impl Into<ComponentId>, entity: Entity) { |
| 78 | + self.event_sets |
| 79 | + .get_or_insert_with(component_id.into(), Default::default) |
| 80 | + .send(entity); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/// A [`SystemParam`] that grants access to the entities that had their `T` [`Component`] removed. |
| 85 | +/// |
| 86 | +/// Note that this does not allow you to see which data existed before removal. |
| 87 | +/// If you need this, you will need to track the component data value on your own, |
| 88 | +/// using a regularly scheduled system that requests `Query<(Entity, &T), Changed<T>>` |
| 89 | +/// and stores the data somewhere safe to later cross-reference. |
| 90 | +/// |
| 91 | +/// If you are using `bevy_ecs` as a standalone crate, |
| 92 | +/// note that the `RemovedComponents` list will not be automatically cleared for you, |
| 93 | +/// and will need to be manually flushed using [`World::clear_trackers`](crate::world::World::clear_trackers) |
| 94 | +/// |
| 95 | +/// For users of `bevy` and `bevy_app`, this is automatically done in `bevy_app::App::update`. |
| 96 | +/// For the main world, [`World::clear_trackers`](crate::world::World::clear_trackers) is run after the main schedule is run and after |
| 97 | +/// `SubApp`'s have run. |
| 98 | +/// |
| 99 | +/// # Examples |
| 100 | +/// |
| 101 | +/// Basic usage: |
| 102 | +/// |
| 103 | +/// ``` |
| 104 | +/// # use bevy_ecs::component::Component; |
| 105 | +/// # use bevy_ecs::system::IntoSystem; |
| 106 | +/// # use bevy_ecs::removal_detection::RemovedComponents; |
| 107 | +/// # |
| 108 | +/// # #[derive(Component)] |
| 109 | +/// # struct MyComponent; |
| 110 | +/// fn react_on_removal(mut removed: RemovedComponents<MyComponent>) { |
| 111 | +/// removed.iter().for_each(|removed_entity| println!("{:?}", removed_entity)); |
| 112 | +/// } |
| 113 | +/// # bevy_ecs::system::assert_is_system(react_on_removal); |
| 114 | +/// ``` |
| 115 | +#[derive(SystemParam)] |
| 116 | +pub struct RemovedComponents<'w, 's, T: Component> { |
| 117 | + component_id: Local<'s, ComponentIdFor<T>>, |
| 118 | + reader: Local<'s, RemovedComponentReader<T>>, |
| 119 | + event_sets: &'w RemovedComponentEvents, |
| 120 | +} |
| 121 | + |
| 122 | +/// Iterator over entities that had a specific component removed. |
| 123 | +/// |
| 124 | +/// See [`RemovedComponents`]. |
| 125 | +pub type RemovedIter<'a> = |
| 126 | + iter::Flatten<option::IntoIter<iter::Cloned<ManualEventIterator<'a, Entity>>>>; |
| 127 | + |
| 128 | +impl<'w, 's, T: Component> RemovedComponents<'w, 's, T> { |
| 129 | + pub fn iter(&mut self) -> RemovedIter<'_> { |
| 130 | + self.event_sets |
| 131 | + .get(**self.component_id) |
| 132 | + .map(|events| self.reader.iter(events).cloned()) |
| 133 | + .into_iter() |
| 134 | + .flatten() |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +impl<'a, 'w, 's: 'a, T> IntoIterator for &'a mut RemovedComponents<'w, 's, T> |
| 139 | +where |
| 140 | + T: Component, |
| 141 | +{ |
| 142 | + type Item = Entity; |
| 143 | + type IntoIter = RemovedIter<'a>; |
| 144 | + fn into_iter(self) -> Self::IntoIter { |
| 145 | + self.iter() |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// SAFETY: Only reads World removed component events |
| 150 | +unsafe impl<'a> ReadOnlySystemParam for &'a RemovedComponentEvents {} |
| 151 | + |
| 152 | +// SAFETY: no component value access, removed component events can be read in parallel and are |
| 153 | +// never mutably borrowed during system execution |
| 154 | +unsafe impl<'a> SystemParam for &'a RemovedComponentEvents { |
| 155 | + type State = (); |
| 156 | + type Item<'w, 's> = &'w RemovedComponentEvents; |
| 157 | + |
| 158 | + fn init_state(_world: &mut World, _system_meta: &mut SystemMeta) -> Self::State {} |
| 159 | + |
| 160 | + #[inline] |
| 161 | + unsafe fn get_param<'w, 's>( |
| 162 | + _state: &'s mut Self::State, |
| 163 | + _system_meta: &SystemMeta, |
| 164 | + world: &'w World, |
| 165 | + _change_tick: u32, |
| 166 | + ) -> Self::Item<'w, 's> { |
| 167 | + world.removed_components() |
| 168 | + } |
| 169 | +} |
0 commit comments