summaryrefslogtreecommitdiff
path: root/ecs/src/query.rs
blob: 2f8b285fcc3be6b9a5cce4709cbef2b8ee9d7e1a (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
use std::marker::PhantomData;

use crate::component::RefSequence as ComponentRefSequence;
use crate::query::flexible::{Iter as FlexibleQueryIter, Query as FlexibleQuery};
use crate::query::options::Options;
use crate::system::{Param as SystemParam, System};
use crate::uid::Uid;
use crate::World;

pub mod flexible;
pub mod options;

#[derive(Debug)]
pub struct Query<'world, Comps, OptionsT = ()>
where
    Comps: ComponentRefSequence,
{
    world: &'world World,
    inner: FlexibleQuery<'world, Comps::Metadata>,
    _pd: PhantomData<(Comps, OptionsT)>,
}

impl<'world, Comps, OptionsT> Query<'world, Comps, OptionsT>
where
    Comps: ComponentRefSequence,
    OptionsT: Options,
{
    /// Iterates over the entities matching this query.
    #[must_use]
    pub fn iter<'query>(&'query self) -> ComponentIter<'query, 'world, Comps>
    {
        #[cfg(feature = "debug")]
        tracing::debug!("Searching for {}", std::any::type_name::<Comps>());

        #[allow(clippy::map_flatten)]
        ComponentIter {
            world: self.world,
            iter: self.inner.iter::<OptionsT>(),
            comps_pd: PhantomData,
        }
    }

    /// Returns the UID of the entity at the given query iteration index.
    #[must_use]
    pub fn get_entity_uid(&self, entity_index: usize) -> Option<Uid>
    {
        Some(self.inner.iter::<OptionsT>().nth(entity_index)?.uid())
    }

    pub(crate) fn new(world: &'world World) -> Self
    {
        Self {
            world,
            inner: world.flexible_query(Comps::metadata()),
            _pd: PhantomData,
        }
    }
}

impl<'query, 'world, Comps, OptionsT> IntoIterator
    for &'query Query<'world, Comps, OptionsT>
where
    Comps: ComponentRefSequence + 'world,
    OptionsT: Options,
{
    type IntoIter = ComponentIter<'query, 'world, Comps>;
    type Item = Comps::Handles<'query>;

    fn into_iter(self) -> Self::IntoIter
    {
        self.iter()
    }
}

impl<'world, Comps, OptionsT> SystemParam<'world> for Query<'world, Comps, OptionsT>
where
    Comps: ComponentRefSequence,
    OptionsT: Options,
{
    type Input = ();

    fn initialize<SystemImpl>(
        _system: &mut impl System<'world, SystemImpl>,
        _input: Self::Input,
    )
    {
    }

    fn new<SystemImpl>(
        _system: &'world impl System<'world, SystemImpl>,
        world: &'world World,
    ) -> Self
    {
        Self::new(world)
    }
}

pub struct ComponentIter<'query, 'world, Comps>
{
    world: &'world World,
    iter: FlexibleQueryIter<'query>,
    comps_pd: PhantomData<Comps>,
}

impl<'query, 'world, Comps> ComponentIter<'query, 'world, Comps>
where
    Comps: ComponentRefSequence + 'world,
    'world: 'query,
{
    pub(crate) fn new(world: &'world World, iter: FlexibleQueryIter<'query>) -> Self
    {
        Self { world, iter, comps_pd: PhantomData }
    }
}

impl<'query, 'world, Comps> Iterator for ComponentIter<'query, 'world, Comps>
where
    Comps: ComponentRefSequence + 'world,
    'world: 'query,
{
    type Item = Comps::Handles<'query>;

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

        Some(Comps::from_components(
            entity_handle.components(),
            |component_uid| entity_handle.get_component_index(component_uid),
            self.world,
        ))
    }
}