From 94e5e592baea2935af7c94ad44805a09d0e30740 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 9 Apr 2025 20:50:14 +0200 Subject: feat(ecs): replace Relationship component with pair UID support --- ecs/src/lib.rs | 80 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 44 insertions(+), 36 deletions(-) (limited to 'ecs/src/lib.rs') diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index 962c542..3b89dac 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -20,6 +20,7 @@ use crate::component::{ use crate::entity::CREATE_STATIC_ENTITIES; use crate::extension::{Collector as ExtensionCollector, Extension}; use crate::lock::{Lock, WriteGuard}; +use crate::pair::{ChildOf, DependsOn, Pair}; use crate::phase::{Phase, START as START_PHASE}; use crate::query::flexible::Query as FlexibleQuery; use crate::query::term::Without; @@ -30,21 +31,19 @@ use crate::query::{ Terms as QueryTerms, TermsBuilderInterface, }; -use crate::relationship::{ChildOf, DependsOn, Relationship}; use crate::sole::Sole; use crate::stats::Stats; use crate::system::{System, SystemComponent}; -use crate::uid::{Kind as UidKind, Uid}; +use crate::uid::{Kind as UidKind, Uid, Wildcard}; pub mod actions; pub mod component; pub mod entity; pub mod event; pub mod extension; -mod lock; +pub mod pair; pub mod phase; pub mod query; -pub mod relationship; pub mod sole; pub mod stats; pub mod system; @@ -55,6 +54,8 @@ pub mod util; #[doc(hidden)] pub mod private; +mod lock; + pub use ecs_macros::{Component, Sole}; pub use crate::query::Query; @@ -151,7 +152,7 @@ impl World { self.create_entity(( SystemComponent { system: system.into_type_erased() }, - Relationship::::new(phase_euid), + Pair::new::(phase_euid), )); } @@ -278,7 +279,7 @@ impl World archetype .component_ids_sorted() .into_iter() - .map(|comp_id| comp_id.id().to_string()) + .map(|comp_id| comp_id.to_string()) .collect::>() .join(", ") )) @@ -305,16 +306,18 @@ impl World fn query_and_run_systems(&self, phase_euid: Uid) { - let system_comps_query = - self.query::<(&SystemComponent, &Relationship), ()>(); - - let system_iter = system_comps_query.iter().filter(|(_, phase_rel)| { - phase_rel - .target_uids() - .any(|target_uid| target_uid == phase_euid) - }); + let system_query = self.flexible_query( + QueryTerms::<2>::builder() + .with_required([ + SystemComponent::id(), + Pair::new::(phase_euid).id(), + ]) + .build(), + ); - for (system_component, _) in system_iter { + for (system_component,) in + QueryIter::<(&SystemComponent,), _>::new(self, system_query.iter()) + { // SAFETY: The world lives long enough unsafe { system_component.system.run(self); @@ -324,33 +327,32 @@ impl World fn perform_child_phases(&self, parent_phase_euid: Uid) { - let phase_query = self.query::<(&Phase, &Relationship), ()>(); - - for (child_phase_euid, (_, phase_rel)) in phase_query.iter_with_euids() { - if !phase_rel - .target_uids() - .any(|phase_euid| phase_euid == parent_phase_euid) - { - continue; - } + let phase_query = self.flexible_query( + QueryTerms::<2>::builder() + .with_required([ + Phase::id(), + Pair::new::(parent_phase_euid).id(), + ]) + .build(), + ); - self.query_and_run_systems(child_phase_euid); - self.perform_child_phases(child_phase_euid); + for child_phase_entity in phase_query.iter() { + self.query_and_run_systems(child_phase_entity.uid()); + self.perform_child_phases(child_phase_entity.uid()); } } fn perform_phases(&self) { - let phase_query = - self.query::<(&Phase,), (Without>,)>(); + let phase_query = self.query::<(&Phase,), (Without>,)>(); - for (phase_euid, (_,)) in phase_query.iter_with_euids() { - if phase_euid == *START_PHASE { + for (phase_entity_id, _) in phase_query.iter_with_euids() { + if phase_entity_id == *START_PHASE { continue; } - self.query_and_run_systems(phase_euid); - self.perform_child_phases(phase_euid); + self.query_and_run_systems(phase_entity_id); + self.perform_child_phases(phase_entity_id); } } @@ -604,19 +606,25 @@ impl Default for WorldData #[derive(Debug)] pub struct EntityComponentRef<'a> { - comp: &'a ArchetypeEntityComponent, + component_id: Uid, + component: &'a ArchetypeEntityComponent, } impl<'a> EntityComponentRef<'a> { fn component(&self) -> &'a Lock> { - self.comp.component() + self.component.component() + } + + pub fn id(&self) -> Uid + { + self.component_id } - fn new(comp: &'a ArchetypeEntityComponent) -> Self + fn new(component_id: Uid, comp: &'a ArchetypeEntityComponent) -> Self { - Self { comp } + Self { component_id, component: comp } } } -- cgit v1.2.3-18-g5258