From ab2fa8a93de33ccb17ee1efd6f9b8858a8b79253 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 10 Jan 2025 15:09:33 +0100 Subject: refactor(ecs): add struct for querying using component metadata --- ecs/src/lib.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'ecs/src/lib.rs') diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index d217525..1b9a31b 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -23,6 +23,7 @@ use crate::event::component::Kind as ComponentEventKind; use crate::extension::{Collector as ExtensionCollector, Extension}; use crate::lock::{Lock, WriteGuard}; use crate::phase::{Phase, START as START_PHASE}; +use crate::query::flexible::Query as FlexibleQuery; use crate::query::options::{Not, Options as QueryOptions, With}; use crate::relationship::{ChildOf, DependsOn, Relationship}; use crate::sole::Sole; @@ -30,6 +31,7 @@ use crate::stats::Stats; use crate::system::{System, SystemComponent}; use crate::type_name::TypeName; use crate::uid::{Kind as UidKind, Uid}; +use crate::util::Sortable; pub mod actions; pub mod component; @@ -182,6 +184,16 @@ impl World Query::new(self) } + pub fn flexible_query( + &self, + comp_metadata: CompMetadata, + ) -> FlexibleQuery + where + CompMetadata: Sortable + AsRef<[ComponentMetadata]>, + { + FlexibleQuery::new(self, comp_metadata) + } + /// Performs a single tick. /// /// # Panics @@ -440,14 +452,18 @@ impl World fn emit_event_by_id(&self, event_id: Uid) { - for (system,) in self - .query::<(&SystemComponent,), ()>() - .iter_with_extra_comps([ComponentMetadata { + let query = self.flexible_query([ + ComponentMetadata::of::(), + ComponentMetadata { id: event_id, is_optional: ComponentIsOptional::No, - }]) + }, + ]); + + for (system,) in query + .iter::<()>() + .into_component_iter::<(&SystemComponent,)>(self) { - // SAFETY: The world lives long enough unsafe { system.system.run(self); } -- cgit v1.2.3-18-g5258