diff options
Diffstat (limited to 'ecs/src/event/component.rs')
-rw-r--r-- | ecs/src/event/component.rs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/ecs/src/event/component.rs b/ecs/src/event/component.rs new file mode 100644 index 0000000..306d43f --- /dev/null +++ b/ecs/src/event/component.rs @@ -0,0 +1,75 @@ +//! 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<ComponentT> +where + ComponentT: Component, +{ + _pd: PhantomData<ComponentT>, +} + +impl<ComponentT> Debug for Added<ComponentT> +where + ComponentT: Component, +{ + fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result + { + formatter + .debug_struct("Added") + .field("_pd", &self._pd) + .finish() + } +} + +impl<ComponentT> Default for Added<ComponentT> +where + ComponentT: Component, +{ + fn default() -> Self + { + Self { _pd: PhantomData } + } +} + +impl<ComponentT> Event for Added<ComponentT> +where + ComponentT: Component, +{ + fn id() -> Id + where + Self: Sized, + { + Id::new::<Added<ComponentForId>, _>(Some(ComponentId::of::<ComponentT>())) + } +} + +pub fn create_added_id(component_id: ComponentId) -> Id +{ + Id::new::<Added<ComponentForId>, _>(Some(component_id)) +} + +pub struct ComponentToAddedEvent; + +impl<ComponentT: Component, Accumulator> + TupleReduceElement<Accumulator, ComponentToAddedEvent> for ComponentT +where + Accumulator: TupleWith<Added<Self>>, +{ + type Return = Accumulator::With; +} + +use crate as ecs; + +#[derive(Debug, Component)] +struct ComponentForId; |