//! Component events. use std::convert::Infallible; use crate::component::{Handle as ComponentHandle, HandleMut as ComponentHandleMut}; use crate::entity::Handle as EntityHandle; use crate::pair::Pair; use crate::system::observer::EventMatch; use crate::util::impl_multiple; 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 **before**: /// 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); #[derive(Debug, Component)] pub struct Changed(Infallible); impl_multiple!( EventMatch, ( impl _<'_>> (removed), impl _<'_>> (added), impl _<'_>> (changed) ) cb=(type_params=(observable_type), event_name) => { paste::paste! { #[must_use] pub fn [](&self) -> ComponentHandle<'_, Target> { let ent = self.get_ent_infallible(); let Some(comp) = ent.get::() else { unreachable!(); }; comp } #[must_use] pub fn [](&self) -> ComponentHandleMut<'_, Target> { let ent = self.get_ent_infallible(); let Some(comp) = ent.get_mut::() else { unreachable!(); }; comp } } #[must_use] pub fn get_ent_infallible(&self) -> EntityHandle<'_> { let Some(ent) = self.get_entity() else { unreachable!(); }; ent } } );