//! Component events. use std::fmt::{Debug, Formatter}; use std::marker::PhantomData; use ecs_macros::Component; use crate::component::{Component, Id as ComponentId}; use crate::event::{Event, Id}; use crate::tuple::{ReduceElement as TupleReduceElement, With as TupleWith}; /// Event emitted when: /// a) A entity with component `ComponentT` is spawned. /// b) A component `ComponentT` is added to a entity. pub struct Added where ComponentT: Component, { _pd: PhantomData, } impl Debug for Added where ComponentT: Component, { fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result { formatter .debug_struct("Added") .field("_pd", &self._pd) .finish() } } impl Default for Added where ComponentT: Component, { fn default() -> Self { Self { _pd: PhantomData } } } impl Event for Added where ComponentT: Component, { fn id() -> Id where Self: Sized, { Id::new::, _>(Some(ComponentId::of::())) } } /// Event emitted when a `ComponentT` component is removed from a entity. pub struct Removed where ComponentT: Component, { _pd: PhantomData, } impl Debug for Removed where ComponentT: Component, { fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result { formatter .debug_struct("Removed") .field("_pd", &self._pd) .finish() } } impl Default for Removed where ComponentT: Component, { fn default() -> Self { Self { _pd: PhantomData } } } impl Event for Removed where ComponentT: Component, { fn id() -> Id where Self: Sized, { Id::new::, _>(Some(ComponentId::of::())) } } pub fn create_added_id(component_id: ComponentId) -> Id { Id::new::, _>(Some(component_id)) } pub fn create_removed_id(component_id: ComponentId) -> Id { Id::new::, _>(Some(component_id)) } pub struct ComponentToAddedEvent; impl TupleReduceElement for ComponentT where Accumulator: TupleWith>, { type Return = Accumulator::With; } use crate as ecs; #[derive(Debug, Component)] struct ComponentForId;