Skip to main content

dynamis_model/
surface.rs

1#[derive(Clone, Copy, Debug, PartialEq)]
2pub struct SurfaceDesc {
3    pub friction: f32,
4    pub restitution: f32,
5    pub rolling_friction: f32,
6    pub spin_friction: f32,
7}
8
9impl SurfaceDesc {
10    pub fn new() -> Self {
11        Self {
12            friction: 0.5,
13            restitution: 0.0,
14            rolling_friction: 0.0,
15            spin_friction: 0.0,
16        }
17    }
18
19    pub fn friction(mut self, friction: f32) -> Self {
20        assert!(friction >= 0.0, "surface friction must be non-negative");
21        self.friction = friction;
22        self
23    }
24
25    pub fn restitution(mut self, restitution: f32) -> Self {
26        self.restitution = restitution;
27        self
28    }
29
30    pub fn rolling_friction(mut self, rolling_friction: f32) -> Self {
31        assert!(
32            rolling_friction >= 0.0,
33            "surface rolling friction must be non-negative"
34        );
35        self.rolling_friction = rolling_friction;
36        self
37    }
38
39    pub fn spin_friction(mut self, spin_friction: f32) -> Self {
40        assert!(
41            spin_friction >= 0.0,
42            "surface spin friction must be non-negative"
43        );
44        self.spin_friction = spin_friction;
45        self
46    }
47}
48
49impl Default for SurfaceDesc {
50    fn default() -> Self {
51        Self::new()
52    }
53}
54
55#[derive(Clone, Copy, Debug, PartialEq)]
56pub struct SurfaceTable<'a> {
57    palette: &'a [SurfaceDesc],
58    indices: &'a [u32],
59}
60
61impl<'a> SurfaceTable<'a> {
62    pub fn new(palette: &'a [SurfaceDesc], indices: &'a [u32]) -> Self {
63        assert!(
64            !palette.is_empty(),
65            "a surface table carries at least one surface"
66        );
67        assert!(
68            indices
69                .iter()
70                .all(|index| (*index as usize) < palette.len()),
71            "a surface index must address its own palette"
72        );
73        Self { palette, indices }
74    }
75
76    pub fn palette(self) -> &'a [SurfaceDesc] {
77        self.palette
78    }
79
80    pub fn indices(self) -> &'a [u32] {
81        self.indices
82    }
83
84    pub fn count(self) -> usize {
85        self.indices.len()
86    }
87}