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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
mod fn_query_mut;
pub use fn_query_mut::*;
use std::{
any::{Any, TypeId},
cell::{Ref, RefCell},
marker::PhantomData, rc::Rc,
};
use super::{Entities, Query};
trait FnQueryParams<'a>
{
fn get(entities: &'a Entities) -> Self where Self: Sized;
}
impl<'a, T> FnQueryParams<'a> for FnQuery<'a, T> {
fn get(entities: &'a Entities) -> Self {
Self::new(entities) // because this is the generic constructor for FnQuery
}
}
impl<'a, F, T> IntoQueryFunction<'a, T> for F
where
T: FnQueryParams<'a>,
F: Fn(T),
{
fn run(self, entities: &'a Entities) {
(self)(FnQueryParams::get(entities))
}
}
impl<'a, Func, T1, T2> IntoQueryFunction<'a, (T1, T2)> for Func
where
Func: Fn(T1, T2),
T1: FnQueryParams<'a>, T2: FnQueryParams<'a>,
{
fn run(self, entities: &'a Entities) {
(self)(FnQueryParams::get(entities), FnQueryParams::get(entities))
}
}
impl<'a, Func, T1, T2, T3> IntoQueryFunction<'a, (T1, T2, T3)> for Func
where
Func: Fn(T1, T2, T3),
T1: FnQueryParams<'a>, T2: FnQueryParams<'a>,
T3: FnQueryParams<'a>,
{
fn run(self, entities: &'a Entities) {
(self)(
FnQueryParams::get(entities),
FnQueryParams::get(entities),
FnQueryParams::get(entities)
)
}
}
pub trait IntoQueryFunction<'a, ArgList> {
fn run(self, entities: &'a Entities);
}
// impl<'a, 'b, 'c, Func, T, T2> IntoQueryFunction<(FnQuery<'a, T>, FnQuery<'b, T2>)> for Func
// where
// Func: for<'r, 's> Fn(FnQuery<'r, T>, FnQuery<'s, T2>),
// {
// fn run(self, entities: &Entities) {
// (self)(FnQuery::new(entities), FnQuery::new(entities))
// }
// }
// impl<Func, T, T2, T3> IntoQueryFunction<(FnQuery<'_, T>, FnQuery<'_, T2>, FnQuery<'_, T3>)> for Func
// where
// Func: for<'r, 's, 'e> Fn(FnQuery<'r, T>, FnQuery<'s, T2>, FnQuery<'e, T3>),
// {
// fn run(self, entities: &Entities) {
// (self)(FnQuery::new(entities), FnQuery::new(entities), FnQuery::new(entities))
// }
// }
// impl<'a, F, T> IntoQueryFunction<FnQuery<'a, T>> for F
// where
// F: Fn(FnQuery<'_, T>),
// T: Any,
// // T1: Any, T2: Any
// {
// fn run(self, entities: &Entities) {
// self(FnQuery::new(entities))
// }
// }
/**
The type of the function parameter of an immutable function query. See [FnQueryMut](struct.FnQueryMut.html)
for the mutable implementation of this type.
This struct permits the use of [Query::query_fn], where a function is passed into a query to execute it.
# Examples
```
use sceller::prelude::*;
struct Health(u32);
fn print_healths(healths: FnQuery<Health>) {
for hp in healths.into_iter() {
println!("{}", hp.0);
}
}
let mut world = World::new();
world.spawn().insert(Health(10));
let query = world.query();
query.query_fn(&print_healths); // runs this function with the querys result as a parameter.
```
As of now the struct can handle up to three parameters in a query in the form of a tuple:
```
use sceller::prelude::*;
struct Health(u32);
struct Speed(u32);
struct Size(u32);
fn print_all(healths: FnQuery<(Health, Speed, Size)>) {
for (hp, speed, size) in healths.iter() {
println!("{}, {}, {}", hp.0, speed.0, size.0);
}
}
let mut world = World::new();
world.spawn().insert(Health(10)).insert(Speed(65)).insert(Size(15));
let query = world.query();
query.query_fn(&print_all); // runs this function with the querys result as a parameter.
```
*/
pub struct FnQuery<'a, T> {
entities: &'a Entities,
phantom: PhantomData<T>,
}
impl<'a, T> FnQuery<'a, T> {
fn new(entities: &'a Entities) -> FnQuery<'a, T> {
FnQuery {
entities: entities,
phantom: PhantomData,
}
}
}
// Implementation of actual functions
impl<'a> Query<'a> {
/**
Takes in a function to run with the query's result passed as parameters.
# Examples
```
use sceller::prelude::*;
struct Health(u32);
fn print_healths(healths: FnQuery<Health>) {
for hp in healths.into_iter() {
println!("{}", hp.0);
}
}
let mut world = World::new();
world.spawn().insert(Health(10));
let query = world.query();
query.query_fn(&print_healths); // runs this function with the querys result as a parameter.
```
As of now the struct can handle up to three parameters in a query in the form of a tuple:
```
use sceller::prelude::*;
struct Health(u32);
struct Speed(u32);
struct Size(u32);
fn print_all(healths: FnQuery<(Health, Speed, Size)>) {
for (hp, speed, size) in healths.iter() {
println!("{}, {}, {}", hp.0, speed.0, size.0);
}
}
let mut world = World::new();
world.spawn().insert(Health(10)).insert(Speed(65)).insert(Size(15));
let query = world.query();
query.query_fn(&print_all); // runs this function with the querys result as a parameter.
```
*/
pub fn query_fn<F, T: 'a>(&self, gen: F)
where
F: IntoQueryFunction<'a, T>
{
gen.run(self.entities)
}
}
impl<'a, T: 'static> std::iter::IntoIterator for FnQuery<'a, T>
where T: Any,
{
type IntoIter = FnQueryIntoIterator<'a, Ref<'a, T>>;
type Item = Ref<'a, T>;
fn into_iter(self) -> Self::IntoIter {
let typeid = TypeId::of::<T>();
let selfmap = self.entities.bit_masks.get(&typeid).unwrap();
let all_components = self.entities.components.get(&typeid).unwrap();
// get all components with the type of this AutoQuery
// get all valid components (not deleted or None)
let components = all_components.into_iter().enumerate()
.map(|(ind, c)| {
if (self.entities.map[ind] & selfmap == *selfmap) && c.is_some() {
Some(c.as_ref().unwrap())
} else {
None
}
})
.flatten()
.collect::<Vec<&Rc<RefCell<dyn Any>>>>();
FnQueryIntoIterator {
components: components.into_iter()
.map(|c| {
let component = c.as_ref();
let borrow = component.borrow();
Ref::map(borrow, |any| {
any.downcast_ref::<T>().unwrap()
})
})
.collect::<Vec<Ref<T>>>(),
phantom: PhantomData
}
}
}
impl<'a, T: 'static> std::iter::IntoIterator for &'a FnQuery<'a, T>
where T: Any,
{
type IntoIter = FnQueryIntoIterator<'a, Ref<'a, T>>;
type Item = Ref<'a, T>;
fn into_iter(self) -> Self::IntoIter {
let typeid = TypeId::of::<T>();
let selfmap = self.entities.bit_masks.get(&typeid).unwrap();
let all_components = self.entities.components.get(&typeid).unwrap();
// get all components with the type of this AutoQuery
// get all valid components (not deleted or None)
let components = all_components.into_iter().enumerate()
.map(|(ind, c)| {
if (self.entities.map[ind] & selfmap == *selfmap) && c.is_some() {
Some(c.as_ref().unwrap())
} else {
None
}
})
.flatten()
.collect::<Vec<&Rc<RefCell<dyn Any>>>>();
FnQueryIntoIterator {
components: components.into_iter()
.map(|c| {
let component = c.as_ref();
let borrow = component.borrow();
Ref::map(borrow, |any| {
any.downcast_ref::<T>().unwrap()
})
})
.collect::<Vec<Ref<T>>>(),
phantom: PhantomData
}
}
}
impl<'a, T: 'a, T2: 'a> FnQuery<'a, (T, T2)>
where
T: Any,
T2: Any,
{
pub fn iter(&self) -> FnQueryIntoIterator<'a, (Ref<'a, T>, Ref<'a, T2>)> {
let typeid1 = TypeId::of::<T>();
let typeid2 = TypeId::of::<T2>();
// let selftype_ids = vec![typeid1, typeid2];
let mut selfmap = self.entities.get_bitmask(&typeid1).unwrap();
selfmap |= self.entities.get_bitmask(&typeid2).unwrap();
let indexes = self
.entities
.map
.iter()
.enumerate()
.filter_map(|(index, map)| {
if map & selfmap == selfmap {
Some(index)
} else {
None
}
})
.collect::<Vec<usize>>();
let mut return_vec = Vec::new();
// Make this ^^^^ into the return type
let components = self.entities.components.get(&typeid1).unwrap();
let mut query_components = Vec::new();
for index in &indexes {
query_components.push(components[*index].as_ref());
}
let query_components1 = query_components.into_iter().flatten().collect::<Vec<_>>();
let components = self.entities.components.get(&typeid2).unwrap();
let mut query_components = Vec::new();
for index in &indexes {
query_components.push(components[*index].as_ref());
}
let query_components2 = query_components.into_iter().flatten().collect::<Vec<_>>();
for i in 0..query_components1.len() {
return_vec.push((
Ref::map(query_components1[i].as_ref().borrow(), |any| {
any.downcast_ref::<T>().unwrap()
}),
Ref::map(query_components2[i].as_ref().borrow(), |any| {
any.downcast_ref::<T2>().unwrap()
}),
));
}
FnQueryIntoIterator {
components: return_vec,
phantom: PhantomData,
}
}
}
impl<'a, T: 'a, T2: 'a, T3: 'a> FnQuery<'a, (T, T2, T3)>
where
T: Any,
T2: Any,
T3: Any,
{
pub fn iter(&self) -> FnQueryIntoIterator<'a, (Ref<'a, T>, Ref<'a, T2>, Ref<'a, T3>)> {
let typeid1 = TypeId::of::<T>();
let typeid2 = TypeId::of::<T2>();
let typeid3 = TypeId::of::<T3>();
// let selftype_ids = vec![typeid1, typeid2];
let mut selfmap = self.entities.get_bitmask(&typeid1).unwrap();
selfmap |= self.entities.get_bitmask(&typeid2).unwrap();
selfmap |= self.entities.get_bitmask(&typeid3).unwrap();
let indexes = self
.entities
.map
.iter()
.enumerate()
.filter_map(|(index, map)| {
if map & selfmap == selfmap {
Some(index)
} else {
None
}
})
.collect::<Vec<usize>>();
let mut return_vec = Vec::new();
// Make this ^^^^ into the return type
let components = self.entities.components.get(&typeid1).unwrap();
let mut query_components = Vec::new();
for index in &indexes {
query_components.push(components[*index].as_ref());
}
let query_components1 = query_components.into_iter().flatten().collect::<Vec<_>>();
let components = self.entities.components.get(&typeid2).unwrap();
let mut query_components = Vec::new();
for index in &indexes {
query_components.push(components[*index].as_ref());
}
let query_components2 = query_components.into_iter().flatten().collect::<Vec<_>>();
let components = self.entities.components.get(&typeid3).unwrap();
let mut query_components = Vec::new();
for index in &indexes {
query_components.push(components[*index].as_ref());
}
let query_components3 = query_components.into_iter().flatten().collect::<Vec<_>>();
for i in 0..query_components1.len() {
return_vec.push((
Ref::map(query_components1[i].as_ref().borrow(), |any| {
any.downcast_ref::<T>().unwrap()
}),
Ref::map(query_components2[i].as_ref().borrow(), |any| {
any.downcast_ref::<T2>().unwrap()
}),
Ref::map(query_components3[i].as_ref().borrow(), |any| {
any.downcast_ref::<T3>().unwrap()
}),
));
}
FnQueryIntoIterator {
components: return_vec,
phantom: PhantomData,
}
}
}
pub struct FnQueryIntoIterator<'a, T> {
components: Vec<T>,
phantom: PhantomData<&'a T>,
}
impl<'a, T: 'a> std::iter::Iterator for FnQueryIntoIterator<'a, T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
self.components.pop()
}
}