aboutsummaryrefslogtreecommitdiff
path: root/stockton-levels/src/parts/faces.rs
blob: 0168cd8cdefa4a6f6137233779c24e82886af6cf (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
use super::{textures::TextureRef, vertices::Vertex};
use serde::{Deserialize, Serialize};

pub type FaceRef = u32;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Geometry {
    Vertices(Vertex, Vertex, Vertex),
}

pub trait IsFace<C: HasFaces + ?Sized> {
    fn index(&self, container: &C) -> FaceRef;
    fn geometry(&self, container: &C) -> Geometry;
    fn texture_idx(&self, container: &C) -> TextureRef;
}

pub trait HasFaces {
    type Face: IsFace<Self>;

    fn get_face(&self, index: FaceRef) -> Option<&Self::Face>;
    fn iter_faces(&self) -> Faces<Self> {
        Faces {
            next: 0,
            container: self,
        }
    }
}

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

impl<'a, T: HasFaces> Iterator for Faces<'a, T> {
    type Item = &'a T::Face;

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