summaryrefslogtreecommitdiff
path: root/src/component.rs
blob: 1b36c3aaea3d1a3b35dd51991eb42e5a74964ac1 (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
use super::Entity;
use super::sparse_set::SparseSet;

pub trait Component {}

#[macro_export]
macro_rules! component {
    ($name:ident) => {
        #[derive(Clone, Copy, Debug)]
        pub struct $name;
        impl Component for $name {}
    };

    ($name:ident, $ty:ty) => {
        #[derive(Clone, Copy, Debug)]
        pub struct $name(pub $ty);

        impl std::ops::Deref for $name {
            type Target = $ty;
            fn deref(&self) -> &Self::Target {
                &self.0
            }
        }

        impl std::ops::DerefMut for $name {
            fn deref_mut(&mut self) -> &mut Self::Target {
                &mut self.0
            }
        }

        impl Component for $name {}
    };

    ($name:ident, { $($field:ident : $ty:ty),+ $(,)? }) => {
        #[derive(Clone, Debug)]
        pub struct $name {
            $(pub $field: $ty),+
        }

        impl Component for $name {}
    };
}

pub trait ComponentStorage<T: Component> {
    fn storage(&self) -> &SparseSet<T>;
    fn storage_mut(&mut self) -> &mut SparseSet<T>;
    fn storage_ptr(&mut self) -> *mut SparseSet<T>;

    fn insert(&mut self, entity: Entity, component: T) {
        self.storage_mut().insert(entity, component);
    }
}