ecs: documentation improvements for filter queries

This commit is contained in:
SeanOMik 2024-04-10 23:18:11 -04:00
parent 347427a841
commit 0668be06e2
Signed by: SeanOMik
GPG Key ID: FEC9E2FC15235964
4 changed files with 47 additions and 23 deletions

View File

@ -2,6 +2,10 @@ use std::marker::PhantomData;
use crate::{query::{AsQuery, Query}, Archetype, Component, DynTypeId, World}; use crate::{query::{AsQuery, Query}, Archetype, Component, DynTypeId, World};
/// A filter query that fetches when the entity has the component `C`.
///
/// This does not return a reference to the component, it returns `()` if the entity has
/// the component. This query is great when its used with [`Or`](super::Or).
#[derive(Default)] #[derive(Default)]
pub struct Has<C: Component> { pub struct Has<C: Component> {
_marker: PhantomData<C> _marker: PhantomData<C>

View File

@ -1,8 +1,8 @@
pub mod has_component; mod has;
pub use has_component::*; pub use has::*;
pub mod or; mod or;
pub use or::*; pub use or::*;
pub mod not; mod not;
pub use not::*; pub use not::*;

View File

@ -1,22 +1,22 @@
use std::marker::PhantomData;
use crate::{query::{AsQuery, Query}, Archetype, World}; use crate::{query::{AsQuery, Query}, Archetype, World};
#[derive(Default)] /// A filter query that fetches the inverse of `Q`.
///
/// This means that entities that `Q` fetches are skipped, and entities that
/// `Q` does not fetch are not skipped.
///
/// ```rust,nobuild
/// // Iterate over entities that has a transform, and are not the origin of a `ChildOf` relationship.
/// for (en, pos, _) in world
/// .view::<(Entities, &Transform, Not<Has<RelationOriginComponent<ChildOf>>>)>()
/// .iter()
/// {
/// // ...
/// }
/// ```
#[derive(Default, Copy, Clone)]
pub struct Not<Q: Query> { pub struct Not<Q: Query> {
q: Q, query: Q,
_marker: PhantomData<Q>
}
impl<Q: Query> Copy for Not<Q> {}
impl<Q: Query> Clone for Not<Q> {
fn clone(&self) -> Self {
Self {
q: self.q.clone(),
_marker: self._marker.clone()
}
}
} }
impl<Q: Query> Query for Not<Q> { impl<Q: Query> Query for Not<Q> {
@ -26,13 +26,12 @@ impl<Q: Query> Query for Not<Q> {
fn new() -> Self { fn new() -> Self {
Not { Not {
q: Q::new(), query: Q::new(),
_marker: PhantomData
} }
} }
fn can_visit_archetype(&self, archetype: &Archetype) -> bool { fn can_visit_archetype(&self, archetype: &Archetype) -> bool {
!self.q.can_visit_archetype(archetype) !self.query.can_visit_archetype(archetype)
} }
unsafe fn fetch<'a>(&self, _world: &'a World, _: &'a Archetype, _: crate::Tick) -> Self::Fetch<'a> { unsafe fn fetch<'a>(&self, _world: &'a World, _: &'a Archetype, _: crate::Tick) -> Self::Fetch<'a> {

View File

@ -1,5 +1,26 @@
use crate::{query::{AsQuery, Query}, Archetype, World}; use crate::{query::{AsQuery, Query}, Archetype, World};
/// A filter query returning when either `Q1` or `Q2` returns.
///
/// This checks if `Q1` can fetch before checking `Q2`.
///
/// ```rust,nobuild
/// for (en, pos, _) in world
/// .view::<(Entities, &Transform, Or<Has<Mesh>, Has<Scene>>)>()
/// .iter()
/// {
/// // do some things with the position of the entities
///
/// // now handle do things with the Mesh or Scene that the entity could have
/// if let Some(mesh) = world.view_one::<&Mesh>(en).get() {
/// // do mesh things
/// }
///
/// if let Some(scene) = world.view_one::<&Scene>(en).get() {
/// // do scene things
/// }
/// }
/// ```
#[derive(Default)] #[derive(Default)]
pub struct Or<Q1: AsQuery, Q2: AsQuery> { pub struct Or<Q1: AsQuery, Q2: AsQuery> {
left: Q1::Query, left: Q1::Query,