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

use crate::component::Component;
use crate::EntityComponent;

/// Query options.
pub trait Options
{
    fn entity_filter<'component>(components: &'component [EntityComponent]) -> bool;
}

impl Options for ()
{
    fn entity_filter<'component>(_components: &'component [EntityComponent]) -> bool
    {
        true
    }
}

pub struct With<ComponentT>
where
    ComponentT: Component,
{
    _pd: PhantomData<ComponentT>,
}

impl<ComponentT> Options for With<ComponentT>
where
    ComponentT: Component,
{
    fn entity_filter<'component>(components: &'component [EntityComponent]) -> bool
    {
        let ids_set = components
            .into_iter()
            .map(|component| component.id)
            .collect::<HashSet<_>>();

        ids_set.contains(&ComponentT::id())
    }
}

pub struct Not<OptionsT>
where
    OptionsT: Options,
{
    _pd: PhantomData<OptionsT>,
}

impl<OptionsT> Options for Not<OptionsT>
where
    OptionsT: Options,
{
    fn entity_filter<'component>(components: &'component [EntityComponent]) -> bool
    {
        !OptionsT::entity_filter(components)
    }
}