summaryrefslogtreecommitdiff
path: root/ecs/src/query/term.rs
blob: 78668c5d5b2dcad09f2e3e5f5e05576d69a3806d (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::any::type_name;
use std::marker::PhantomData;

use crate::component::{Component, HandleFromEntityComponentRef, Ref as ComponentRef};
use crate::query::{
    TermWithField,
    TermWithoutField,
    TermsBuilder,
    TermsBuilderInterface,
};

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

impl<ComponentT> TermWithoutField for With<ComponentT>
where
    ComponentT: Component,
{
    fn apply_to_terms_builder<const MAX_TERM_CNT: usize>(
        terms_builder: &mut TermsBuilder<MAX_TERM_CNT>,
    )
    {
        terms_builder.with::<ComponentT>();
    }
}

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

impl<ComponentT> TermWithoutField for Without<ComponentT>
where
    ComponentT: Component,
{
    fn apply_to_terms_builder<const MAX_TERM_CNT: usize>(
        terms_builder: &mut TermsBuilder<MAX_TERM_CNT>,
    )
    {
        terms_builder.without::<ComponentT>();
    }
}

impl<ComponentRefT> TermWithField for Option<ComponentRefT>
where
    ComponentRefT: ComponentRef,
{
    type Field<'a> = Option<ComponentRefT::Handle<'a>>;

    fn apply_to_terms_builder<const MAX_TERM_CNT: usize>(
        _terms_builder: &mut TermsBuilder<MAX_TERM_CNT>,
    )
    {
    }

    fn get_field<'world>(
        entity_handle: &crate::entity::Handle<'world>,
        world: &'world crate::World,
    ) -> Self::Field<'world>
    {
        Some(
            ComponentRefT::Handle::<'world>::from_entity_component_ref(
                Some(entity_handle.get_component(ComponentRefT::Component::id())?),
                world,
            )
            .unwrap_or_else(|err| {
                panic!(
                    "Creating handle to component {} failed: {err}",
                    type_name::<ComponentRefT::Component>()
                );
            }),
        )
    }
}