From 07aa59a122cc5e14d2fb2e2c6e3d8f82e4397bde Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 14 Aug 2024 20:05:30 +0200 Subject: feat(ecs): add component added event --- ecs/src/event/component.rs | 75 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 ecs/src/event/component.rs (limited to 'ecs/src/event/component.rs') 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 +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::())) + } +} + +pub fn create_added_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; -- cgit v1.2.3-18-g5258