kubi/kubi-shared/src/block.rs

272 lines
6.4 KiB
Rust
Raw Normal View History

2024-05-02 04:03:38 -05:00
use glam::{vec4, Vec4};
2023-03-07 18:31:38 -06:00
use serde::{Serialize, Deserialize};
2023-02-15 15:07:47 -06:00
use strum::EnumIter;
2023-11-20 13:59:34 -06:00
use num_enum::TryFromPrimitive;
2023-11-25 18:29:02 -06:00
use crate::item::Item;
2023-02-15 15:07:47 -06:00
2023-03-07 18:31:38 -06:00
#[derive(Serialize, Deserialize, Clone, Copy, Debug, EnumIter)]
2023-02-15 15:07:47 -06:00
#[repr(u8)]
pub enum BlockTexture {
Stone,
Dirt,
GrassTop,
GrassSide,
Sand,
Bedrock,
Wood,
WoodTop,
Leaf,
Torch,
TallGrass,
Snow,
GrassSideSnow,
Cobblestone,
2023-02-15 19:44:21 -06:00
Planks,
2023-02-17 17:58:31 -06:00
WaterSolid,
2024-05-01 17:32:43 -05:00
Water,
2023-02-15 15:07:47 -06:00
}
2023-11-20 13:59:34 -06:00
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, EnumIter, TryFromPrimitive)]
2023-02-15 15:07:47 -06:00
#[repr(u8)]
pub enum Block {
Air,
2023-02-17 17:37:17 -06:00
Marker,
2023-02-15 15:07:47 -06:00
Stone,
Dirt,
Grass,
Sand,
Cobblestone,
2023-02-15 16:32:06 -06:00
TallGrass,
2023-02-15 19:44:21 -06:00
Planks,
Torch,
Wood,
Leaf,
2023-02-17 17:58:31 -06:00
Water,
2023-02-15 15:07:47 -06:00
}
2023-01-19 17:45:46 -06:00
2023-02-15 15:11:15 -06:00
impl Block {
2023-02-15 15:08:48 -06:00
#[inline]
2023-02-15 15:11:15 -06:00
pub const fn descriptor(self) -> BlockDescriptor {
2023-01-25 20:05:29 -06:00
match self {
Self::Air => BlockDescriptor {
name: "air",
render: RenderType::None,
collision: CollisionType::None,
raycast_collision: false,
2023-11-25 18:29:02 -06:00
drops: None,
2024-05-02 04:03:38 -05:00
submerge: None,
2023-01-25 20:05:29 -06:00
},
2023-02-17 17:37:17 -06:00
Self::Marker => BlockDescriptor {
name: "marker",
render: RenderType::None,
collision: CollisionType::None,
raycast_collision: false,
2023-11-25 18:29:02 -06:00
drops: None,
2024-05-02 04:03:38 -05:00
submerge: None,
2023-02-17 17:37:17 -06:00
},
2023-11-25 18:29:02 -06:00
Self::Stone => BlockDescriptor {
name: "stone",
2024-05-01 18:21:17 -05:00
render: RenderType::Cube(
Transparency::Solid,
CubeTexture::all(BlockTexture::Stone)
),
2023-11-25 18:29:02 -06:00
collision: CollisionType::Solid,
raycast_collision: true,
drops: None,
2024-05-02 04:03:38 -05:00
submerge: None,
2023-01-25 20:05:29 -06:00
},
2023-11-25 18:29:02 -06:00
Self::Dirt => BlockDescriptor {
name: "dirt",
2024-05-01 18:21:17 -05:00
render: RenderType::Cube(
Transparency::Solid,
CubeTexture::all(BlockTexture::Dirt)
),
2023-11-25 18:29:02 -06:00
collision: CollisionType::Solid,
raycast_collision: true,
drops: None,
2024-05-02 04:03:38 -05:00
submerge: None,
2023-01-25 20:50:38 -06:00
},
2023-11-25 18:29:02 -06:00
Self::Grass => BlockDescriptor {
name: "grass",
2024-05-01 18:21:17 -05:00
render: RenderType::Cube(
Transparency::Solid,
CubeTexture::top_sides_bottom(
BlockTexture::GrassTop,
BlockTexture::GrassSide,
BlockTexture::Dirt
)
),
2023-11-25 18:29:02 -06:00
collision: CollisionType::Solid,
raycast_collision: true,
drops: None,
2024-05-02 04:03:38 -05:00
submerge: None,
2023-01-25 20:50:38 -06:00
},
2023-11-25 18:29:02 -06:00
Self::Sand => BlockDescriptor {
name: "sand",
2024-05-01 18:21:17 -05:00
render: RenderType::Cube(
Transparency::Solid,
CubeTexture::all(BlockTexture::Sand)
),
2023-11-25 18:29:02 -06:00
collision: CollisionType::Solid,
raycast_collision: true,
drops: None,
2024-05-02 04:03:38 -05:00
submerge: None,
2023-01-25 20:50:38 -06:00
},
2023-02-04 18:43:47 -06:00
Self::Cobblestone => BlockDescriptor {
name: "cobblestone",
2024-05-01 18:21:17 -05:00
render: RenderType::Cube(
Transparency::Solid,
CubeTexture::all(BlockTexture::Cobblestone)
),
2023-02-04 18:43:47 -06:00
collision: CollisionType::Solid,
raycast_collision: true,
2023-11-25 18:29:02 -06:00
drops: None,
2024-05-02 04:03:38 -05:00
submerge: None,
2023-02-15 16:32:06 -06:00
},
Self::TallGrass => BlockDescriptor {
name: "tall grass",
2024-05-01 18:21:17 -05:00
render: RenderType::Cross(CrossTexture::all(BlockTexture::TallGrass)),
2023-02-15 16:32:06 -06:00
collision: CollisionType::None,
raycast_collision: true,
2023-11-25 18:29:02 -06:00
drops: None,
2024-05-02 04:03:38 -05:00
submerge: None,
2023-02-15 19:44:21 -06:00
},
2023-11-25 18:29:02 -06:00
Self::Planks => BlockDescriptor {
name: "planks",
2024-05-01 18:21:17 -05:00
render: RenderType::Cube(
Transparency::Solid,
CubeTexture::all(BlockTexture::Planks)
),
2023-02-15 19:44:21 -06:00
collision: CollisionType::Solid,
raycast_collision: true,
2023-11-25 18:29:02 -06:00
drops: None,
2024-05-02 04:03:38 -05:00
submerge: None,
2023-02-15 19:44:21 -06:00
},
Self::Torch => BlockDescriptor {
name: "torch",
2024-05-01 18:21:17 -05:00
render: RenderType::Cross(CrossTexture::all(BlockTexture::Torch)),
collision: CollisionType::None,
raycast_collision: true,
2023-11-25 18:29:02 -06:00
drops: None,
2024-05-02 04:03:38 -05:00
submerge: None,
},
Self::Wood => BlockDescriptor {
name: "leaf",
2024-05-01 18:21:17 -05:00
render: RenderType::Cube(
Transparency::Solid,
CubeTexture::horizontal_vertical(BlockTexture::Wood, BlockTexture::WoodTop)
),
collision: CollisionType::Solid,
raycast_collision: true,
2023-11-25 18:29:02 -06:00
drops: None,
2024-05-02 04:03:38 -05:00
submerge: None,
},
Self::Leaf => BlockDescriptor {
name: "leaf",
2024-05-01 18:21:17 -05:00
render: RenderType::Cube(
Transparency::Binary,
CubeTexture::all(BlockTexture::Leaf)
),
collision: CollisionType::Solid,
raycast_collision: true,
2023-11-25 18:29:02 -06:00
drops: None,
2024-05-02 04:03:38 -05:00
submerge: None,
},
2023-02-17 17:58:31 -06:00
Self::Water => BlockDescriptor {
name: "water",
2024-05-01 18:21:17 -05:00
render: RenderType::Cube(
Transparency::Trans,
CubeTexture::all(BlockTexture::Water)
),
2023-02-17 17:58:31 -06:00
collision: CollisionType::None,
2023-02-17 18:04:54 -06:00
raycast_collision: true,
2023-11-25 18:29:02 -06:00
drops: None,
2024-05-02 04:03:38 -05:00
submerge: Some(vec4(0., 0., 0.25, 0.75)),
2023-02-17 17:58:31 -06:00
},
2023-01-25 20:05:29 -06:00
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct BlockDescriptor {
pub name: &'static str,
pub render: RenderType,
pub collision: CollisionType,
pub raycast_collision: bool,
2023-11-25 18:29:02 -06:00
pub drops: Option<Item>,
2024-05-02 04:03:38 -05:00
pub submerge: Option<Vec4>,
2023-01-25 20:05:29 -06:00
}
#[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)
}
}
2023-02-15 15:07:47 -06:00
#[derive(Clone, Copy, Debug)]
2023-02-15 19:00:09 -06:00
pub struct CrossTextureSides {
pub front: BlockTexture,
pub back: BlockTexture
2023-02-15 15:11:15 -06:00
}
2023-02-15 19:00:09 -06:00
impl CrossTextureSides {
pub const fn all(texture: BlockTexture) -> Self {
2023-02-15 16:32:06 -06:00
Self {
2023-02-15 19:00:09 -06:00
front: texture,
back: texture
2023-02-15 16:32:06 -06:00
}
}
2023-02-15 19:00:09 -06:00
}
#[derive(Clone, Copy, Debug)]
pub struct CrossTexture(pub CrossTextureSides, pub CrossTextureSides);
impl CrossTexture {
2023-02-15 16:32:06 -06:00
pub const fn all(texture: BlockTexture) -> Self {
2023-02-15 19:00:09 -06:00
Self(
CrossTextureSides::all(texture),
CrossTextureSides::all(texture)
)
2023-02-15 16:32:06 -06:00
}
2023-02-15 15:07:47 -06:00
}
2023-01-25 20:05:29 -06:00
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CollisionType {
None,
Solid,
}
2024-05-01 18:21:17 -05:00
#[derive(Clone, Copy, Debug)]
pub enum Transparency {
Solid,
Binary,
Trans,
}
2023-01-25 20:05:29 -06:00
#[derive(Clone, Copy, Debug)]
pub enum RenderType {
None,
2024-05-01 18:21:17 -05:00
Cube(Transparency, CubeTexture),
Cross(CrossTexture),
2023-01-25 20:05:29 -06:00
}