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
use super::*;
/// Query mixin implementation
impl World {
/// Create a new uncached [`Query`].
///
/// # Type Parameters
///
/// * `Components` - The components to match on.
///
/// # See also
///
/// * [`World::new_query()`]
/// * [`World::new_query_named()`]
/// * [`World::query()`]
/// * [`World::query_named()`]
pub fn new_query<Components>(&self) -> Query<Components>
where
Components: QueryTuple,
{
QueryBuilder::<Components>::new(self).build()
}
/// Create a new named [`Query`].
///
/// # Type Parameters
///
/// * `Components` - The components to match on.
///
/// # Arguments
///
/// * `name` - The name of the query.
///
/// # Returns
///
/// A new query.
///
/// # See also
///
/// * [`World::new_query()`]
/// * [`World::new_query_named()`]
/// * [`World::query()`]
/// * [`World::query_named()`]
pub fn new_query_named<Components>(&self, name: &str) -> Query<Components>
where
Components: QueryTuple,
{
QueryBuilder::<Components>::new_named(self, name).build()
}
/// Create a new [`QueryBuilder`].
///
/// # Type Parameters
///
/// * `Components` - The components to match on.
///
/// # Returns
///
/// A new query builder.
///
/// # See also
///
/// * [`World::new_query()`]
/// * [`World::new_query_named()`]
/// * [`World::query_named()`]
pub fn query<Components>(&self) -> QueryBuilder<'_, Components>
where
Components: QueryTuple,
{
QueryBuilder::<Components>::new(self)
}
/// Create a new named [`QueryBuilder`].
///
/// # Type Parameters
///
/// * `Components` - The components to match on.
///
/// # Arguments
///
/// * `name` - The name of the query.
///
/// # Returns
///
/// A new query builder.
///
/// # See also
///
/// * [`World::new_query()`]
/// * [`World::new_query_named()`]
/// * [`World::query()`]
pub fn query_named<'a, Components>(&'a self, name: &str) -> QueryBuilder<'a, Components>
where
Components: QueryTuple,
{
QueryBuilder::<Components>::new_named(self, name)
}
/// Create a query from a query description.
///
/// # Safety
///
/// Caller needs to ensure the query type is correct of the provided `desc`.
pub unsafe fn query_from_desc<Components>(
&self,
desc: &mut sys::ecs_query_desc_t,
) -> QueryBuilder<'_, Components>
where
Components: QueryTuple,
{
QueryBuilder::<Components>::new_from_desc(self, desc)
}
/// Attempts to convert an entity into a query.
///
/// Returns the untyped query if the entity is alive and valid; otherwise, returns `None`.
///
/// # Safety
///
/// Proceed with caution. Use `.iter_only` instead.
///
/// # See Also
///
/// * [`World::query_from`]
pub fn try_query_from(&self, query_entity: impl Into<Entity>) -> Option<Query<()>> {
Query::<()>::new_from_entity(self, query_entity)
}
/// Converts an entity into a query, automatically unwrapping the result.
///
/// This method panics if the entity is not alive or not a valid query.
/// For safer usage, consider using [`World::try_query_from`].
///
/// # Panics
///
/// Panics if the entity is not alive or not a valid query.
///
/// # Safety
///
/// Proceed with caution. Use `.iter_only` instead.
///
/// # See Also
///
/// * [`World::try_query_from`]
pub fn query_from(&self, query_entity: impl Into<Entity>) -> Query<()> {
self.try_query_from(query_entity)
.expect("entity / query is not alive or valid")
}
/// Create and iterate an uncached query.
///
/// This function creates a query and immediately iterates it.
///
/// # Returns
///
/// The query.
///
/// # Type Parameters
///
/// * `Components`: The components to match on.
///
/// # See also
///
/// * [`QueryAPI::each()`]
/// * [`World::each_entity()`]
pub fn each<Components>(&self, func: impl FnMut(Components::TupleType<'_>)) -> Query<Components>
where
Components: QueryTuple,
{
let query = QueryBuilder::<Components>::new(self).build();
query.each(func);
query
}
/// Create and iterate an uncached query.
///
/// This function creates a query and immediately iterates it.
///
/// # Returns
///
/// The query.
///
/// # Type Parameters
///
/// * `Components`: The components to match on.
///
/// # See also
///
/// * [`QueryAPI::each_entity()`]
/// * [`World::each()`]
pub fn each_entity<Components>(
&self,
func: impl FnMut(EntityView, Components::TupleType<'_>),
) -> Query<Components>
where
Components: QueryTuple,
{
let query = QueryBuilder::<Components>::new(self).build();
query.each_entity(func);
query
}
}