summaryrefslogtreecommitdiff
path: root/ecs/src/lib.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2025-04-09 20:50:14 +0200
committerHampusM <hampus@hampusmat.com>2025-04-10 17:04:40 +0200
commit94e5e592baea2935af7c94ad44805a09d0e30740 (patch)
tree358f7496ac40e2a7d7cae10cf04bf25246debdec /ecs/src/lib.rs
parentb982b205373f445db9ced7f3cf13c1156ad8a40a (diff)
feat(ecs): replace Relationship component with pair UID support
Diffstat (limited to 'ecs/src/lib.rs')
-rw-r--r--ecs/src/lib.rs80
1 files changed, 44 insertions, 36 deletions
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::<DependsOn, Phase>::new(phase_euid),
+ Pair::new::<DependsOn>(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::<Vec<_>>()
.join(", ")
))
@@ -305,16 +306,18 @@ impl World
fn query_and_run_systems(&self, phase_euid: Uid)
{
- let system_comps_query =
- self.query::<(&SystemComponent, &Relationship<DependsOn, Phase>), ()>();
-
- 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::<DependsOn>(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<ChildOf, Phase>), ()>();
-
- 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::<ChildOf>(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<Relationship<ChildOf, Phase>>,)>();
+ let phase_query = self.query::<(&Phase,), (Without<Pair<ChildOf, Wildcard>>,)>();
- 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<Box<dyn Any>>
{
- 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 }
}
}