aboutsummaryrefslogtreecommitdiff
path: root/stockton-levels/src/parts/entities.rs
blob: 7a5ac748cbd7004c2777db6d4cbc5ebb24dc2acb (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
use std::iter::Iterator;

pub type EntityRef = u32;

/// A game entity
pub trait IsEntity<C: HasEntities + ?Sized> {
    fn get_attr(&self, container: &C) -> Option<&str>;
}

pub trait HasEntities {
    type Entity: IsEntity<Self>;

    fn get_entity(&self, idx: EntityRef) -> Option<&Self::Entity>;
    fn iter_entities(&self) -> Entities<Self> {
        Entities {
            next: 0,
            container: self,
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub struct Entities<'a, T: HasEntities + ?Sized> {
    next: EntityRef,
    container: &'a T,
}

impl<'a, T: HasEntities> Iterator for Entities<'a, T> {
    type Item = &'a T::Entity;

    fn next(&mut self) -> Option<Self::Item> {
        let res = self.container.get_entity(self.next);
        self.next += 1;
        res
    }
}