Skip to content

Commit

Permalink
Store all Material types in an array
Browse files Browse the repository at this point in the history
  • Loading branch information
patowen committed Jun 24, 2024
1 parent 80a6e42 commit c2817ee
Showing 1 changed file with 44 additions and 43 deletions.
87 changes: 44 additions & 43 deletions common/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,55 +50,56 @@ pub enum Material {

impl Material {
pub const COUNT: usize = 40;

pub const ALL: [Self; Self::COUNT] = [
Material::Void,
Material::Dirt,
Material::Sand,
Material::Silt,
Material::Clay,
Material::Mud,
Material::SandyLoam,
Material::SiltyLoam,
Material::ClayLoam,
Material::RedSand,
Material::Limestone,
Material::Shale,
Material::Dolomite,
Material::Sandstone,
Material::RedSandstone,
Material::Marble,
Material::Slate,
Material::Granite,
Material::Diorite,
Material::Andesite,
Material::Gabbro,
Material::Basalt,
Material::Olivine,
Material::Water,
Material::Lava,
Material::Wood,
Material::Leaves,
Material::WoodPlanks,
Material::GreyBrick,
Material::WhiteBrick,
Material::Ice,
Material::IceSlush,
Material::Gravel,
Material::Snow,
Material::CoarseGrass,
Material::TanGrass,
Material::LushGrass,
Material::MudGrass,
Material::Grass,
Material::CaveGrass,
];
}

impl TryFrom<u16> for Material {
type Error = ();

fn try_from(value: u16) -> Result<Self, Self::Error> {
Ok(match value {
0 => Material::Void,
1 => Material::Dirt,
2 => Material::Sand,
3 => Material::Silt,
4 => Material::Clay,
5 => Material::Mud,
6 => Material::SandyLoam,
7 => Material::SiltyLoam,
8 => Material::ClayLoam,
9 => Material::RedSand,
10 => Material::Limestone,
11 => Material::Shale,
12 => Material::Dolomite,
13 => Material::Sandstone,
14 => Material::RedSandstone,
15 => Material::Marble,
16 => Material::Slate,
17 => Material::Granite,
18 => Material::Diorite,
19 => Material::Andesite,
20 => Material::Gabbro,
21 => Material::Basalt,
22 => Material::Olivine,
23 => Material::Water,
24 => Material::Lava,
25 => Material::Wood,
26 => Material::Leaves,
27 => Material::WoodPlanks,
28 => Material::GreyBrick,
29 => Material::WhiteBrick,
30 => Material::Ice,
31 => Material::IceSlush,
32 => Material::Gravel,
33 => Material::Snow,
34 => Material::CoarseGrass,
35 => Material::TanGrass,
36 => Material::LushGrass,
37 => Material::MudGrass,
38 => Material::Grass,
39 => Material::CaveGrass,
_ => Err(())?,
})
Material::ALL.get(value as usize).ok_or(()).copied()
}
}

Expand Down

0 comments on commit c2817ee

Please sign in to comment.