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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
use std::marker::PhantomData;
use crate::component::RefSequence as ComponentRefSequence;
use crate::query::flexible::{
EntityHandle,
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, the iterator item being the entity
/// components.
#[must_use]
pub fn iter<'query>(
&'query self,
) -> ComponentIter<'query, 'world, Comps, FlexibleQueryIter<'query>>
{
#[cfg(feature = "debug")]
tracing::debug!("Searching for {}", std::any::type_name::<Comps>());
ComponentIter {
world: self.world,
iter: self.inner.iter::<OptionsT>(),
comps_pd: PhantomData,
}
}
/// Iterates over the entities matching this query, the iterator item being the entity
/// [`Uid`] and the entity components.
#[must_use]
pub fn iter_with_euids<'query>(
&'query self,
) -> ComponentAndEuidIter<'query, 'world, Comps, FlexibleQueryIter<'query>>
{
#[cfg(feature = "debug")]
tracing::debug!("Searching for {}", std::any::type_name::<Comps>());
ComponentAndEuidIter {
world: self.world,
iter: self.inner.iter::<OptionsT>(),
comps_pd: PhantomData,
}
}
/// Iterates over the entities matching this query using the iterator returned by
/// `func`.
///
/// This function exists so that a custom [`EntityHandle`] iterator can be given to
/// [`ComponentIter`] without giving the user access to a reference to the [`World`].
#[must_use]
pub fn iter_with<'query, OutIter>(
&'query self,
func: impl FnOnce(FlexibleQueryIter<'query>) -> OutIter,
) -> ComponentIter<'query, 'world, Comps, OutIter>
where
OutIter: Iterator<Item = EntityHandle<'query>>,
{
#[cfg(feature = "debug")]
tracing::debug!("Searching for {}", std::any::type_name::<Comps>());
ComponentIter {
world: self.world,
iter: func(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, FlexibleQueryIter<'query>>;
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, EntityHandleIter>
where
EntityHandleIter: Iterator<Item = EntityHandle<'query>>,
{
world: &'world World,
iter: EntityHandleIter,
comps_pd: PhantomData<Comps>,
}
impl<'query, 'world, Comps, EntityHandleIter>
ComponentIter<'query, 'world, Comps, EntityHandleIter>
where
Comps: ComponentRefSequence + 'world,
EntityHandleIter: Iterator<Item = EntityHandle<'query>>,
'world: 'query,
{
pub(crate) fn new(world: &'world World, iter: EntityHandleIter) -> Self
{
Self { world, iter, comps_pd: PhantomData }
}
}
impl<'query, 'world, Comps, EntityHandleIter> Iterator
for ComponentIter<'query, 'world, Comps, EntityHandleIter>
where
Comps: ComponentRefSequence + 'world,
EntityHandleIter: Iterator<Item = EntityHandle<'query>>,
'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,
))
}
}
pub struct ComponentAndEuidIter<'query, 'world, Comps, EntityHandleIter>
where
EntityHandleIter: Iterator<Item = EntityHandle<'query>>,
{
world: &'world World,
iter: EntityHandleIter,
comps_pd: PhantomData<Comps>,
}
impl<'query, 'world, Comps, EntityHandleIter> Iterator
for ComponentAndEuidIter<'query, 'world, Comps, EntityHandleIter>
where
Comps: ComponentRefSequence + 'world,
EntityHandleIter: Iterator<Item = EntityHandle<'query>>,
'world: 'query,
{
type Item = (Uid, Comps::Handles<'query>);
fn next(&mut self) -> Option<Self::Item>
{
let entity_handle = self.iter.next()?;
Some((
entity_handle.uid(),
Comps::from_components(
entity_handle.components(),
|component_uid| entity_handle.get_component_index(component_uid),
self.world,
),
))
}
}
|