about summary refs log tree commit diff
path: root/src/entities/util.rs
blob: 6c11ffadf99418cf5c606be2cd03bdc87427887c (plain) (blame)
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
#[macro_export]
macro_rules! new_entity {
    ($name: ident) => {
        new_entity!($name, {})
    };

    ($name: ident { position: $position:expr $(, $fields:tt)* }) => {
        $name {
            id: None,
            position: $position,
            $($fields)*
        }
    };

    ($name: ident { $position:expr $(, $fields:tt)* }) => {
        $name {
            id: None,
            position: $position,
            $($fields)*
        }
    };
}

#[macro_export]
macro_rules! boring_entity {
    ($name:ident) => {
        entity! {
            pub struct $name {}
        }

        impl $name {
            #[allow(dead_code)]
            pub fn new(position: $crate::types::Position) -> Self {
                $name { id: None, position }
            }
        }
    };

    ($name:ident, char: $char: expr) => {
        boring_entity!($name);

        impl $crate::display::Draw for $name {
            fn do_draw(&self, out: &mut Write) -> io::Result<()> {
                write!(out, "{}", $char)
            }
        }
    };
}

#[macro_export]
macro_rules! entity {
    ($name: ident) => {
        positioned!($name);
        positioned_mut!($name);
        identified!($name, $crate::entities::EntityID);
        impl $crate::entities::entity::Entity for $name {}
    };

    (pub struct $name:ident { $($struct_contents:tt)* } $($rest:tt)*) => {
        #[derive(Debug, PartialEq, Eq, Clone)]
        pub struct $name {
            pub id: Option<$crate::entities::EntityID>,
            pub position: $crate::types::Position,
            $($struct_contents)*
        }

        entity!($name);
        entity!($($rest)*);
    };

    () => {};
}