logo
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
use super::*;

/// Layer component.
///
/// Use to group and identify entities.
/// An added layer to an entity will have its mask initialized to 1 (belonging to the first layer).

pub struct Layer {
    id: Entity,
}

impl std::fmt::Debug for Layer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Layer")
            .field("entity", &self.id.name())
            .field("mask", &format!("{:X}", self.mask().get()))
            .finish()
    }
}

impl Layer {
    impl_world_accessor!(
        /// Returns a `ValueAccessor` for the layer mask of the entity.
        ///
        /// Used to set/get the layer mask.
        /// It is a bit mask that can be used for filtering collisions, spatial queries among other things.
        Layer,
        Mask,
        u64,
        mask,
        ValueAccessorReadWrite
    );

    /// Utility function to set to a single layer (an entity can be a member of multiple).
    pub fn set_single(&mut self, index: usize) {
        self.mask().set(Self::mask_from_index(index));
    }

    /// Generates a layer mask from an index (max index is 63)
    pub const fn mask_from_index(index: usize) -> u64 {
        // TODO: static assert if index > 63
        0x1u64 << index
    }
}

impl_world_component!(Layer);