summaryrefslogtreecommitdiff
path: root/ecs/src/query/term.rs
blob: 597dd1a3d87c5979a76cb2ce35868ce3ff0e29ac (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
81
82
83
84
85
use std::any::type_name;
use std::marker::PhantomData;

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

pub struct With<WithUidT>
where
    WithUidT: WithUid,
{
    _pd: PhantomData<WithUidT>,
}

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

pub struct Without<WithUidT>
where
    WithUidT: WithUid,
{
    _pd: PhantomData<WithUidT>,
}

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

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_matching_components(ComponentRefT::Component::id())
                        .next()?,
                ),
                world,
            )
            .unwrap_or_else(|err| {
                panic!(
                    "Creating handle to component {} failed: {err}",
                    type_name::<ComponentRefT::Component>()
                );
            }),
        )
    }
}