aboutsummaryrefslogtreecommitdiff
path: root/examples/render-quad/src/level.rs
blob: 74d1cc3a6967b2b59b310b42add834a7e66dac82 (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
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
use stockton_levels::parts::{
    data::{FaceRef, Geometry, TextureRef},
    HasFaces, HasTextures, HasVisData, IsFace, IsTexture,
};
use stockton_skeleton::components::{CameraSettings, Transform};

pub struct DemoLevel {
    pub faces: Box<[Face]>,
    pub textures: Box<[Texture]>,
}

impl DemoLevel {
    fn face_idx(&self, search: &Face) -> FaceRef {
        for (idx, face) in self.faces.iter().enumerate() {
            if face == search {
                return idx as u32;
            }
        }
        panic!("face not in level")
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Face {
    pub geometry: Geometry,
    pub texture_idx: TextureRef,
}

impl HasFaces for DemoLevel {
    type Face = Face;

    fn get_face(&self, index: FaceRef) -> Option<&Self::Face> {
        self.faces.get(index as usize)
    }
}

impl IsFace<DemoLevel> for Face {
    fn index(&self, container: &DemoLevel) -> stockton_levels::parts::data::FaceRef {
        container.face_idx(self)
    }

    fn geometry(&self, _container: &DemoLevel) -> Geometry {
        self.geometry.clone()
    }

    fn texture_idx(&self, _container: &DemoLevel) -> TextureRef {
        self.texture_idx
    }
}

pub struct Texture {
    pub name: String,
}

impl HasTextures for DemoLevel {
    type Texture = Texture;

    fn get_texture(&self, idx: TextureRef) -> Option<&Self::Texture> {
        self.textures.get(idx as usize)
    }
}

impl IsTexture for Texture {
    fn name(&self) -> &str {
        &self.name
    }
}

impl<'a> HasVisData<'a> for DemoLevel {
    type Faces = std::ops::Range<FaceRef>;

    fn get_visible(
        &'a self,
        _transform: &Transform,
        _settings: &CameraSettings,
    ) -> Self::Faces {
        0..self.faces.len() as u32
    }
}