From 855c5ac59b0c04b4e113ac1da7cd3d4d1351436f Mon Sep 17 00:00:00 2001 From: hodasemi Date: Sat, 5 Apr 2025 09:29:19 +0200 Subject: [PATCH] Add component access for entity --- ecs/src/entity.rs | 29 +++++++++++++++++++++++++++++ ecs/src/lib.rs | 4 +++- ecs/src/type_map.rs | 2 ++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/ecs/src/entity.rs b/ecs/src/entity.rs index f53dc33..8144129 100644 --- a/ecs/src/entity.rs +++ b/ecs/src/entity.rs @@ -43,6 +43,10 @@ impl ActivationState { } } +pub trait EntityComponentDisjointMut<'a, T: 'a> { + fn get_components_mut(&'a mut self) -> std::result::Result; +} + pub struct EntityObject { #[cfg(debug_assertions)] pub debug_name: Option, @@ -239,6 +243,31 @@ impl EntityObject { } } +macro_rules! impl_get_components_mut { + ($($t:ident$(,)?)+) => { + impl<'a, $($t,)+> EntityComponentDisjointMut<'a, ($(&'a mut $t,)+)> for EntityObject + where + $( + $t: EntityComponent + ComponentDebug + 'static, + )+ + { + fn get_components_mut(&'a mut self) -> std::result::Result<($(&'a mut $t,)+), ComponentNotFoundError> { + crate::get_disjoint_mut::GetDisjointMut::<'a, ($(&'a mut $t,)+)>::get_mut(&mut self.components) + } + } + }; +} + +impl_get_components_mut!(T, U); +impl_get_components_mut!(T, U, V); +impl_get_components_mut!(T, U, V, W); +impl_get_components_mut!(T, U, V, W, X); +impl_get_components_mut!(T, U, V, W, X, Y); +impl_get_components_mut!(T, U, V, W, X, Y, Z); +impl_get_components_mut!(T, U, V, W, X, Y, Z, A); +impl_get_components_mut!(T, U, V, W, X, Y, Z, A, B); +impl_get_components_mut!(T, U, V, W, X, Y, Z, A, B, C); + #[allow(clippy::derive_hash_xor_eq)] #[derive(Debug, Clone, Copy, Hash, Eq, Serialize, Deserialize)] pub struct Entity { diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index 748904c..2ecc430 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -8,7 +8,9 @@ mod unsafe_component_store; mod updates; mod world; -pub use crate::entity::{Entity, EntityMultiMut, EntityNotFoundError, EntityObject}; +pub use crate::entity::{ + Entity, EntityComponentDisjointMut, EntityMultiMut, EntityNotFoundError, EntityObject, +}; pub use crate::events::Events; pub use crate::resources::Resources; pub use crate::type_map::{ diff --git a/ecs/src/type_map.rs b/ecs/src/type_map.rs index 57ecb3d..7bb9943 100644 --- a/ecs/src/type_map.rs +++ b/ecs/src/type_map.rs @@ -201,6 +201,8 @@ impl_get_disjoint_mut!(TypeMap < T, U, V, W, X, Y, Z > { ComponentNotFoundError impl_get_disjoint_mut!(TypeMap < T, U, V, W, X, Y, Z, A > { ComponentNotFoundError }); #[rustfmt::skip] impl_get_disjoint_mut!(TypeMap < T, U, V, W, X, Y, Z, A, B > { ComponentNotFoundError }); +#[rustfmt::skip] +impl_get_disjoint_mut!(TypeMap < T, U, V, W, X, Y, Z, A, B, C > { ComponentNotFoundError }); #[derive(Debug)] pub enum ComponentRequestType {