From c1cf1b779e66e985774dad29867a57733947b0e8 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 18 Mar 2025 14:35:11 +0100 Subject: refactor(ecs): remove Component::self_id method --- ecs-macros/src/lib.rs | 5 ----- ecs/src/actions.rs | 4 ++-- ecs/src/component.rs | 25 ++++--------------------- ecs/src/component/storage.rs | 23 ++++++++++++++--------- ecs/src/lib.rs | 14 +++++++------- 5 files changed, 27 insertions(+), 44 deletions(-) diff --git a/ecs-macros/src/lib.rs b/ecs-macros/src/lib.rs index b178022..b58671d 100644 --- a/ecs-macros/src/lib.rs +++ b/ecs-macros/src/lib.rs @@ -137,11 +137,6 @@ pub fn component_derive(input: TokenStream) -> TokenStream #get_id } - fn self_id(&self) -> Uid - { - Self::id() - } - fn get_event_uid(&self, event_kind: ComponentEventKind) -> Uid { match event_kind { diff --git a/ecs/src/actions.rs b/ecs/src/actions.rs index 184f811..7dff3a5 100644 --- a/ecs/src/actions.rs +++ b/ecs/src/actions.rs @@ -169,9 +169,9 @@ pub(crate) struct EventIds #[derive(Debug)] pub(crate) enum Action { - Spawn(Vec>, EventIds), + Spawn(Vec<(Uid, Box)>, EventIds), Despawn(Uid), - AddComponents(Uid, Vec>, EventIds), + AddComponents(Uid, Vec<(Uid, Box)>, EventIds), RemoveComponents(Uid, Vec, EventIds), Stop, } diff --git a/ecs/src/component.rs b/ecs/src/component.rs index 77046d0..dc60995 100644 --- a/ecs/src/component.rs +++ b/ecs/src/component.rs @@ -39,9 +39,6 @@ pub trait Component: SystemInput + Any + TypeName where Self: Sized; - /// The ID of the component `self`. Returns the same value as [`Component::id`]. - fn self_id(&self) -> Uid; - /// Returns the component UID of a component event for this component. fn get_event_uid(&self, event_kind: ComponentEventKind) -> Uid; @@ -116,11 +113,6 @@ where ComponentT::id() } - fn self_id(&self) -> Uid - { - Self::id() - } - fn get_event_uid(&self, event_kind: ComponentEventKind) -> Uid { match event_kind { @@ -167,7 +159,7 @@ pub trait Sequence /// The number of components in this component sequence. const COUNT: usize; - type Array: Array>; + type Array: Array<(Uid, Box)>; fn into_array(self) -> Self::Array; @@ -234,15 +226,6 @@ impl Metadata Self { id, is_optional: false } } - #[must_use] - pub fn get(component: &ComponentT) -> Self - { - Self { - id: component.self_id(), - is_optional: component.self_is_optional(), - } - } - #[must_use] pub fn of() -> Self { @@ -291,11 +274,11 @@ macro_rules! inner { { const COUNT: usize = $c + 1; - type Array = [Box; $c + 1]; + type Array = [(Uid, Box); $c + 1]; fn into_array(self) -> Self::Array { - [#(Box::new(self.I) as Box,)*] + [#((Comp~I::id(), Box::new(self.I) as Box),)*] } fn metadata() -> impl Array @@ -372,7 +355,7 @@ seq!(C in 0..=16 { impl Sequence for () { - type Array = [Box; 0]; + type Array = [(Uid, Box); 0]; const COUNT: usize = 0; diff --git a/ecs/src/component/storage.rs b/ecs/src/component/storage.rs index f2306b7..5b7b101 100644 --- a/ecs/src/component/storage.rs +++ b/ecs/src/component/storage.rs @@ -138,9 +138,11 @@ impl Storage pub fn add_entity_component( &mut self, entity_uid: Uid, - component: Box, + component: (Uid, Box), ) -> Result<(), Error> { + let (component_id, component) = component; + debug_assert!( !component.self_is_optional(), "Adding a optional component to a entity is not supported" @@ -159,16 +161,16 @@ impl Storage if archetype_node .archetype() - .has_component_with_id(component.self_id()) + .has_component_with_id(component_id) { return Err(Error::ComponentAlreadyInEntity { entity: entity_uid, - component: component.self_id(), + component: component_id, }); } let add_edge_archetype_id = archetype_node - .get_or_insert_edges(component.self_id(), ArchetypeEdges::default) + .get_or_insert_edges(component_id, ArchetypeEdges::default) .add .unwrap_or_else(|| { let archetype_node = self @@ -177,10 +179,10 @@ impl Storage .expect("Archetype should exist"); let (add_edge_id, add_edge_comp_ids) = - archetype_node.make_add_edge(component.self_id()); + archetype_node.make_add_edge(component_id); archetype_node - .get_edges_mut(component.self_id()) + .get_edges_mut(component_id) .expect("Edges for component in archetype should exist") .add = Some(add_edge_id); @@ -198,7 +200,7 @@ impl Storage .expect("Add edge archetype should exist"); let add_edge_archetype_edges = add_edge_archetype_node - .get_or_insert_edges(component.self_id(), ArchetypeEdges::default); + .get_or_insert_edges(component_id, ArchetypeEdges::default); add_edge_archetype_edges.remove = Some(archetype_id); } @@ -220,10 +222,13 @@ impl Storage .archetype_mut(); let component_index = add_edge_archetype - .get_index_for_component(component.self_id()) + .get_index_for_component(component_id) .expect("Archetype should have index for component"); - entity.components.insert(component_index, component.into()); + entity.components.insert( + component_index, + EntityComponent::new(component_id, component), + ); add_edge_archetype.push_entity(entity); diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index 504a106..ad83d1e 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -452,13 +452,13 @@ impl World fn add_entity_components( entity_uid: Uid, - components: impl IntoIterator>, + components: impl IntoIterator)>, component_storage: &mut ComponentStorage, ) { - for component in components { - if let Err(err) = - component_storage.add_entity_component(entity_uid, component) + for (component_id, component) in components { + if let Err(err) = component_storage + .add_entity_component(entity_uid, (component_id, component)) { tracing::error!("Failed to add component to entity: {err}"); } @@ -553,12 +553,12 @@ pub struct EntityComponent pub component: Lock>, } -impl From> for EntityComponent +impl EntityComponent { - fn from(component: Box) -> Self + pub fn new(id: Uid, component: Box) -> Self { Self { - id: component.self_id(), + id, name: component.type_name(), component: Lock::new(component), } -- cgit v1.2.3-18-g5258