veloren/common/src/terrain/block.rs

118 lines
2.3 KiB
Rust
Raw Normal View History

use crate::vol::Vox;
use serde_derive::{Deserialize, Serialize};
use std::ops::Deref;
2019-01-23 20:01:58 +00:00
use vek::*;
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
#[repr(u8)]
pub enum BlockKind {
Air,
Normal,
Dense,
Water,
2019-08-19 20:09:35 +00:00
Wheat,
LongGrass,
Flowers,
}
impl BlockKind {
pub fn is_air(&self) -> bool {
match self {
BlockKind::Air => true,
2019-08-19 20:09:35 +00:00
BlockKind::Wheat => true,
BlockKind::LongGrass => true,
BlockKind::Flowers => true,
_ => false,
}
}
pub fn is_fluid(&self) -> bool {
match self {
BlockKind::Water => true,
_ => false,
}
}
pub fn is_opaque(&self) -> bool {
match self {
BlockKind::Air => false,
BlockKind::Water => false,
2019-08-19 20:09:35 +00:00
BlockKind::Wheat => false,
BlockKind::LongGrass => false,
BlockKind::Flowers => false,
_ => true,
}
}
pub fn is_solid(&self) -> bool {
match self {
BlockKind::Air => false,
BlockKind::Water => false,
2019-08-19 20:09:35 +00:00
BlockKind::Wheat => false,
BlockKind::LongGrass => false,
BlockKind::Flowers => false,
_ => true,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
#[repr(packed)]
2019-01-02 19:22:01 +00:00
pub struct Block {
kind: BlockKind,
2019-01-02 19:22:01 +00:00
color: [u8; 3],
}
2019-01-14 18:49:53 +00:00
2019-01-23 20:01:58 +00:00
impl Block {
pub fn new(kind: BlockKind, color: Rgb<u8>) -> Self {
2019-01-23 20:01:58 +00:00
Self {
kind,
color: color.into_array(),
}
}
pub fn get_color(&self) -> Option<Rgb<u8>> {
if !self.is_air() {
2019-01-23 20:01:58 +00:00
Some(self.color.into())
} else {
None
2019-01-23 20:01:58 +00:00
}
}
2019-05-31 20:37:11 +00:00
pub fn kind(&self) -> BlockKind {
self.kind
}
}
impl Deref for Block {
type Target = BlockKind;
fn deref(&self) -> &Self::Target {
&self.kind
2019-05-31 20:37:11 +00:00
}
2019-01-23 20:01:58 +00:00
}
2019-01-14 18:49:53 +00:00
impl Vox for Block {
fn empty() -> Self {
Self {
kind: BlockKind::Air,
2019-01-14 18:49:53 +00:00
color: [0; 3],
}
}
fn is_empty(&self) -> bool {
self.is_air()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn block_size() {
assert_eq!(std::mem::size_of::<BlockKind>(), 1);
assert_eq!(std::mem::size_of::<Block>(), 4);
2019-01-14 18:49:53 +00:00
}
}