summaryrefslogtreecommitdiff
path: root/ecs/src/query/options.rs
blob: d895073834f4c1b48f9629ea7fabca4dbbced13f (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
59
60
61
62
63
64
65
66
use std::collections::HashSet;
use std::marker::PhantomData;

use crate::component::{Component, Id as ComponentId};
use crate::EntityComponent;

/// Query options.
pub trait Options
{
    fn entity_filter<'component>(
        components: impl IntoIterator<Item = &'component EntityComponent>,
    ) -> bool;
}

impl Options for ()
{
    fn entity_filter<'component>(
        _: impl IntoIterator<Item = &'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: impl IntoIterator<Item = &'component EntityComponent>,
    ) -> bool
    {
        let ids_set = components
            .into_iter()
            .map(|component| component.id)
            .collect::<HashSet<_>>();

        ids_set.contains(&ComponentId::of::<ComponentT>())
    }
}

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

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