Add component access for entity

This commit is contained in:
hodasemi 2025-04-05 09:29:19 +02:00
parent 8e717b4974
commit 855c5ac59b
3 changed files with 34 additions and 1 deletions

View file

@ -43,6 +43,10 @@ impl ActivationState {
} }
} }
pub trait EntityComponentDisjointMut<'a, T: 'a> {
fn get_components_mut(&'a mut self) -> std::result::Result<T, ComponentNotFoundError>;
}
pub struct EntityObject { pub struct EntityObject {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
pub debug_name: Option<String>, pub debug_name: Option<String>,
@ -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)] #[allow(clippy::derive_hash_xor_eq)]
#[derive(Debug, Clone, Copy, Hash, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, Hash, Eq, Serialize, Deserialize)]
pub struct Entity { pub struct Entity {

View file

@ -8,7 +8,9 @@ mod unsafe_component_store;
mod updates; mod updates;
mod world; 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::events::Events;
pub use crate::resources::Resources; pub use crate::resources::Resources;
pub use crate::type_map::{ pub use crate::type_map::{

View file

@ -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 }); impl_get_disjoint_mut!(TypeMap < T, U, V, W, X, Y, Z, A > { ComponentNotFoundError });
#[rustfmt::skip] #[rustfmt::skip]
impl_get_disjoint_mut!(TypeMap < T, U, V, W, X, Y, Z, A, B > { ComponentNotFoundError }); 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)] #[derive(Debug)]
pub enum ComponentRequestType { pub enum ComponentRequestType {