summaryrefslogtreecommitdiff
path: root/ecs/src/query/term.rs
blob: 2e1ecca9fcf31dff45f515509a3848b10990aac6 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use std::any::type_name;
use std::marker::PhantomData;

use crate::component::{
    Component,
    Handle as ComponentHandle,
    HandleFromEntityComponentRef,
    HandleMut as ComponentHandleMut,
};
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<ComponentT: Component> TermWithField for Option<&ComponentT>
{
    type Field<'a> = Option<ComponentHandle<'a, ComponentT>>;

    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(
            ComponentHandle::<'world, ComponentT>::from_entity_component_ref(
                Some(
                    entity_handle
                        .get_matching_components(ComponentT::id())
                        .next()?,
                ),
                world,
            )
            .unwrap_or_else(|err| {
                panic!(
                    "Creating handle to component {} failed: {err}",
                    type_name::<ComponentT>()
                );
            }),
        )
    }
}

impl<ComponentT: Component> TermWithField for Option<&mut ComponentT>
{
    type Field<'a> = Option<ComponentHandleMut<'a, ComponentT>>;

    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(
            ComponentHandleMut::<'world, ComponentT>::from_entity_component_ref(
                Some(
                    entity_handle
                        .get_matching_components(ComponentT::id())
                        .next()?,
                ),
                world,
            )
            .unwrap_or_else(|err| {
                panic!(
                    "Creating handle to component {} failed: {err}",
                    type_name::<ComponentT>()
                );
            }),
        )
    }
}