kubi/src/world/block.rs

85 lines
1.8 KiB
Rust
Raw Normal View History

2023-01-19 17:45:46 -06:00
use strum::EnumIter;
2023-01-25 20:05:29 -06:00
use crate::prefabs::BlockTexture;
2023-01-19 17:45:46 -06:00
2023-01-22 20:11:48 -06:00
#[derive(Clone, Copy, Debug, PartialEq, Eq, EnumIter)]
2023-01-19 17:45:46 -06:00
#[repr(u8)]
pub enum Block {
Air,
Stone,
Dirt,
Grass,
Sand,
}
2023-01-25 20:05:29 -06:00
impl Block {
pub const fn descriptor(self) -> BlockDescriptor {
match self {
Self::Air => BlockDescriptor {
name: "air",
render: RenderType::None,
collision: CollisionType::None,
raycast_collision: false,
},
Self::Stone => BlockDescriptor {
name: "stone",
render: RenderType::SolidBlock(CubeTexture::all(BlockTexture::Stone)),
collision: CollisionType::Solid,
raycast_collision: true,
},
_ => todo!()
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct BlockDescriptor {
pub name: &'static str,
pub render: RenderType,
pub collision: CollisionType,
pub raycast_collision: bool,
}
impl BlockDescriptor {
pub fn of(block: Block) -> Self {
block.descriptor()
}
}
#[derive(Clone, Copy, Debug)]
pub struct CubeTexture {
pub top: BlockTexture,
pub bottom: BlockTexture,
pub left: BlockTexture,
pub right: BlockTexture,
pub front: BlockTexture,
pub back: BlockTexture,
}
impl CubeTexture {
pub const fn top_sides_bottom(top: BlockTexture, sides: BlockTexture, bottom: BlockTexture) -> Self {
Self {
top,
bottom,
left: sides,
right: sides,
front: sides,
back: sides,
}
}
pub const fn horizontal_vertical(horizontal: BlockTexture, vertical: BlockTexture) -> Self {
Self::top_sides_bottom(vertical, horizontal, vertical)
}
pub const fn all(texture: BlockTexture) -> Self {
Self::horizontal_vertical(texture, texture)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CollisionType {
None,
Solid,
}
#[derive(Clone, Copy, Debug)]
pub enum RenderType {
None,
SolidBlock(CubeTexture)
}