summaryrefslogtreecommitdiff
path: root/ecs/src/query/flexible.rs
blob: d081d31d23f36d1434b7d1b1bd75c52feacaa405 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//! Low-level querying.
use std::iter::{repeat_n, Filter, Flatten, Map, RepeatN, Zip};

use crate::component::storage::{
    Archetype,
    ArchetypeEntity,
    ArchetypeRefIter,
    EntityIter,
    Storage as ComponentStorage,
};
use crate::component::{
    Metadata as ComponentMetadata,
    RefSequence as ComponentRefSequence,
};
use crate::lock::ReadGuard;
use crate::query::options::Options;
use crate::query::ComponentIter;
use crate::uid::Uid;
use crate::util::Sortable;
use crate::{EntityComponent, World};

/// Low-level entity query structure.
#[derive(Debug)]
pub struct Query<'world, CompMetadata>
where
    CompMetadata: Sortable<Item = ComponentMetadata> + AsRef<[ComponentMetadata]>,
{
    component_storage: ReadGuard<'world, ComponentStorage>,
    comp_metadata: CompMetadata,
}

impl<'world, CompMetadata> Query<'world, CompMetadata>
where
    CompMetadata: Sortable<Item = ComponentMetadata> + AsRef<[ComponentMetadata]>,
{
    /// Iterates over the entities matching this query.
    #[must_use]
    pub fn iter<'query, OptionsT: Options>(&'query self) -> Iter<'query>
    {
        Iter {
            iter: self
                .component_storage
                .iter_archetypes_with_comps(&self.comp_metadata)
                .map(
                    (|archetype| {
                        repeat_n(archetype, archetype.entity_cnt())
                            .zip(archetype.entities())
                    }) as ComponentIterMapFn,
                )
                .flatten()
                .filter(|(_, entity)| OptionsT::entity_filter(entity.components())),
        }
    }

    pub(crate) fn new(world: &'world World, mut comp_metadata: CompMetadata) -> Self
    {
        comp_metadata.sort_by_key_b(|metadata| metadata.id);

        Self {
            component_storage: world
                .data
                .component_storage
                .read_nonblock()
                .expect("Failed to acquire read-only component storage lock"),
            comp_metadata,
        }
    }
}

pub struct Iter<'query>
{
    iter: QueryEntityIter<'query>,
}

impl<'query> Iter<'query>
{
    /// Converts this iterator into a [`ComponentIter`].
    ///
    /// Note: The matching entities of this iterator should have all of the non-[`Option`]
    /// components in `Comps`, otherwise iterating the [`ComponentIter`] will cause a
    /// panic.
    #[must_use]
    #[inline]
    pub fn into_component_iter<'world, Comps>(
        self,
        world: &'world World,
    ) -> ComponentIter<'query, 'world, Comps>
    where
        Comps: ComponentRefSequence + 'world,
        'world: 'query,
    {
        ComponentIter::new(world, self)
    }
}

impl<'query> Iterator for Iter<'query>
{
    type Item = EntityHandle<'query>;

    fn next(&mut self) -> Option<Self::Item>
    {
        let (archetype, entity) = self.iter.next()?;

        Some(EntityHandle { archetype, entity })
    }
}

pub struct EntityHandle<'query>
{
    archetype: &'query Archetype,
    entity: &'query ArchetypeEntity,
}

impl<'query> EntityHandle<'query>
{
    /// Returns the [`Uid`] of this entity.
    #[inline]
    pub fn uid(&self) -> Uid
    {
        self.entity.uid()
    }

    #[inline]
    pub fn components(&self) -> &'query [EntityComponent]
    {
        self.entity.components()
    }

    #[inline]
    pub fn get_component_index(&self, component_uid: Uid) -> Option<usize>
    {
        self.archetype.get_index_for_component(component_uid)
    }
}

type ComponentIterMapFn =
    for<'a> fn(&'a Archetype) -> Zip<RepeatN<&'a Archetype>, EntityIter<'a>>;

type ComponentIterFilterFn =
    for<'a, 'b> fn(&'a (&'b Archetype, &'b ArchetypeEntity)) -> bool;

type QueryEntityIter<'query> = Filter<
    Flatten<Map<ArchetypeRefIter<'query>, ComponentIterMapFn>>,
    ComponentIterFilterFn,
>;