diff options
author | HampusM <hampus@hampusmat.com> | 2024-08-14 20:05:30 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-08-14 20:05:30 +0200 |
commit | 07aa59a122cc5e14d2fb2e2c6e3d8f82e4397bde (patch) | |
tree | 0ac63f5262d97d3d7f50ab1c72d1ace61935608c /ecs/src/event | |
parent | e9074af15cae7b3c354e524e9fa78cbddb20ff84 (diff) |
feat(ecs): add component added event
Diffstat (limited to 'ecs/src/event')
-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; |