From 9863b431950c681225f8774af244a56adbd18937 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 19 May 2024 21:12:07 +0200 Subject: feat(ecs): add support for optional query components --- ecs/src/lib.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'ecs/src/lib.rs') diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index 009ff21..c2fc9c7 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -10,7 +10,11 @@ use std::sync::Arc; use std::vec::Drain; use crate::actions::Action; -use crate::component::{Component, Sequence as ComponentSequence}; +use crate::component::{ + Component, + IsOptional as ComponentIsOptional, + Sequence as ComponentSequence, +}; use crate::event::{Event, Id as EventId, Ids, Sequence as EventSequence}; use crate::extension::{Collector as ExtensionCollector, Extension}; use crate::lock::Lock; @@ -281,7 +285,7 @@ impl ComponentStorage fn find_entity_with_components( &self, start_index: usize, - component_type_ids: &[TypeId], + component_type_ids: &[(TypeId, ComponentIsOptional)], ) -> Option<(usize, &Entity)> { // TODO: This is a really dumb and slow way to do this. Refactor the world @@ -294,9 +298,13 @@ impl ComponentStorage .map(|component| component.id) .collect::>(); - if component_type_ids.iter().all(|component_type_id| { - entity_components.contains(component_type_id) - }) { + if component_type_ids + .iter() + .filter(|(_, is_optional)| *is_optional == ComponentIsOptional::No) + .all(|(component_type_id, _)| { + entity_components.contains(component_type_id) + }) + { return true; } -- cgit v1.2.3-18-g5258