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
//! TODO: Add documentation including describing how the derive macros work

use component::ComponentManager;
use entity::EntityData;

pub struct Aspect<C: ComponentManager>(Box<AspectFilter<C> + 'static>);

impl<C: ComponentManager> Aspect<C> {
    pub fn all() -> Self {
        Aspect(Box::new(All))
    }

    pub fn none() -> Self {
        Aspect(Box::new(None))
    }

    pub fn new<A>(aspect_filter: A) -> Self
    where
        A: AspectFilter<C>,
    {
        Aspect(Box::new(aspect_filter))
    }

    pub fn check<'a>(&self, entity: EntityData<'a, C>, components: &C) -> bool {
        self.0.check(entity, components)
    }
}

pub trait AspectFilter<C: ComponentManager>: 'static {
    fn check<'a>(&self, entity: EntityData<'a, C>, components: &C) -> bool;
}

impl<F, C> AspectFilter<C> for F
where
    C: ComponentManager,
    F: Fn(EntityData<C>, &C) -> bool + 'static,
{
    #[inline]
    fn check<'a>(&self, entity: EntityData<'a, C>, components: &C) -> bool {
        (*self)(entity, components)
    }
}

struct All;
struct None;

impl<C> AspectFilter<C> for All
where
    C: ComponentManager,
{
    #[inline]
    fn check<'a>(&self, _: EntityData<'a, C>, _: &C) -> bool {
        true
    }
}

impl<C> AspectFilter<C> for None
where
    C: ComponentManager,
{
    #[inline]
    fn check<'a>(&self, _: EntityData<'a, C>, _: &C) -> bool {
        false
    }
}