about summary refs log tree commit diff
path: root/src/entities/entity.rs
blob: 7fedb77b2562ca1719caf6bac5cdc7aec29be8ec (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
73
74
75
76
77
78
79
80
81
82
use crate::display::DrawWithNeighbors;
use crate::entities::EntityID;
use crate::types::Neighbors;
use crate::types::{Positioned, PositionedMut};
use downcast_rs::Downcast;
use std::fmt::Debug;
use std::io::{self, Write};

pub trait Identified<ID>: Debug {
    fn opt_id(&self) -> Option<ID>;
    fn set_id(&mut self, id: ID);

    fn id(&self) -> ID {
        self.opt_id()
            .expect(format!("Entity ({:?}) is not in the game", self).as_str())
    }
}

impl<'a, A, ID> Identified<ID> for &'a mut A
where
    A: Identified<ID>,
{
    fn opt_id(&self) -> Option<ID> {
        (**self).opt_id()
    }
    fn set_id(&mut self, id: ID) {
        (**self).set_id(id);
    }
}

impl<ID, A: Identified<ID>> Identified<ID> for Box<A> {
    fn opt_id(&self) -> Option<ID> {
        (**self).opt_id()
    }
    fn set_id(&mut self, id: ID) {
        (**self).set_id(id);
    }
}

pub trait Entity:
    Positioned + PositionedMut + Identified<EntityID> + DrawWithNeighbors + Downcast
{
}

impl Identified<EntityID> for Box<dyn Entity> {
    fn opt_id(&self) -> Option<EntityID> {
        (**self).opt_id()
    }
    fn set_id(&mut self, id: EntityID) {
        (**self).set_id(id);
    }
}

#[macro_export]
macro_rules! identified {
    ($name: ident, $typ: path) => {
        identified!($name, $typ, id);
    };
    ($name: ident, $typ: path, $attr: ident) => {
        impl crate::entities::entity::Identified<$typ> for $name {
            fn opt_id(&self) -> Option<$typ> {
                self.$attr
            }

            fn set_id(&mut self, id: $typ) {
                self.$attr = Some(id)
            }
        }
    };
}

impl_downcast!(Entity);

impl DrawWithNeighbors for Box<dyn Entity> {
    fn do_draw_with_neighbors<'a, 'b>(
        &'a self,
        out: &'b mut Write,
        neighbors: &'a Neighbors<Vec<&'a Box<dyn Entity>>>,
    ) -> io::Result<()> {
        (**self).do_draw_with_neighbors(out, neighbors)
    }
}