2019-06-06 14:48:41 +00:00
|
|
|
use crate::vol::Vox;
|
2019-04-29 20:37:19 +00:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2019-08-14 21:28:37 +00:00
|
|
|
use std::ops::Deref;
|
2019-01-23 20:01:58 +00:00
|
|
|
use vek::*;
|
|
|
|
|
2019-08-19 23:31:11 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
|
2019-08-14 21:28:37 +00:00
|
|
|
#[repr(u8)]
|
|
|
|
pub enum BlockKind {
|
|
|
|
Air,
|
|
|
|
Normal,
|
|
|
|
Dense,
|
|
|
|
Water,
|
2019-08-21 17:22:05 +00:00
|
|
|
LargeCactus,
|
|
|
|
BarrelCactus,
|
2019-09-01 19:04:03 +00:00
|
|
|
RoundCactus,
|
|
|
|
ShortCactus,
|
|
|
|
MedFlatCactus,
|
|
|
|
ShortFlatCactus,
|
2019-08-21 17:22:05 +00:00
|
|
|
BlueFlower,
|
|
|
|
PinkFlower,
|
|
|
|
PurpleFlower,
|
|
|
|
RedFlower,
|
|
|
|
WhiteFlower,
|
|
|
|
YellowFlower,
|
|
|
|
Sunflower,
|
2019-08-19 23:31:11 +00:00
|
|
|
LongGrass,
|
2019-08-21 17:22:05 +00:00
|
|
|
MediumGrass,
|
|
|
|
ShortGrass,
|
|
|
|
Apple,
|
2019-09-01 19:04:03 +00:00
|
|
|
Mushroom,
|
|
|
|
Liana,
|
2019-09-25 20:22:39 +00:00
|
|
|
Velorite,
|
2019-08-14 21:28:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockKind {
|
2019-09-26 13:03:41 +00:00
|
|
|
pub fn is_tangible(&self) -> bool {
|
2019-09-25 14:56:57 +00:00
|
|
|
match self {
|
|
|
|
BlockKind::Air => false,
|
|
|
|
kind => !kind.is_fluid(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 21:28:37 +00:00
|
|
|
pub fn is_air(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
BlockKind::Air => true,
|
2019-10-04 18:27:12 +00:00
|
|
|
BlockKind::LargeCactus => true,
|
2019-08-21 17:22:05 +00:00
|
|
|
BlockKind::BarrelCactus => true,
|
2019-09-01 19:04:03 +00:00
|
|
|
BlockKind::RoundCactus => true,
|
|
|
|
BlockKind::ShortCactus => true,
|
|
|
|
BlockKind::MedFlatCactus => true,
|
|
|
|
BlockKind::ShortFlatCactus => true,
|
2019-08-21 17:22:05 +00:00
|
|
|
BlockKind::BlueFlower => true,
|
|
|
|
BlockKind::PinkFlower => true,
|
|
|
|
BlockKind::PurpleFlower => true,
|
|
|
|
BlockKind::RedFlower => true,
|
|
|
|
BlockKind::WhiteFlower => true,
|
|
|
|
BlockKind::YellowFlower => true,
|
|
|
|
BlockKind::Sunflower => true,
|
2019-08-19 23:31:11 +00:00
|
|
|
BlockKind::LongGrass => true,
|
2019-08-21 17:22:05 +00:00
|
|
|
BlockKind::MediumGrass => true,
|
|
|
|
BlockKind::ShortGrass => true,
|
|
|
|
BlockKind::Apple => true,
|
2019-09-01 19:04:03 +00:00
|
|
|
BlockKind::Mushroom => true,
|
|
|
|
BlockKind::Liana => true,
|
2019-09-25 20:22:39 +00:00
|
|
|
BlockKind::Velorite => true,
|
2019-08-14 21:28:37 +00:00
|
|
|
_ => 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-21 17:22:05 +00:00
|
|
|
BlockKind::LargeCactus => false,
|
|
|
|
BlockKind::BarrelCactus => false,
|
2019-09-01 19:04:03 +00:00
|
|
|
BlockKind::RoundCactus => false,
|
|
|
|
BlockKind::ShortCactus => false,
|
|
|
|
BlockKind::MedFlatCactus => false,
|
|
|
|
BlockKind::ShortFlatCactus => false,
|
2019-08-21 17:22:05 +00:00
|
|
|
BlockKind::BlueFlower => false,
|
|
|
|
BlockKind::PinkFlower => false,
|
|
|
|
BlockKind::PurpleFlower => false,
|
|
|
|
BlockKind::RedFlower => false,
|
|
|
|
BlockKind::WhiteFlower => false,
|
|
|
|
BlockKind::YellowFlower => false,
|
|
|
|
BlockKind::Sunflower => false,
|
2019-08-19 23:31:11 +00:00
|
|
|
BlockKind::LongGrass => false,
|
2019-08-21 17:22:05 +00:00
|
|
|
BlockKind::MediumGrass => false,
|
|
|
|
BlockKind::ShortGrass => false,
|
|
|
|
BlockKind::Apple => false,
|
2019-09-01 19:04:03 +00:00
|
|
|
BlockKind::Mushroom => false,
|
|
|
|
BlockKind::Liana => false,
|
2019-09-25 20:22:39 +00:00
|
|
|
BlockKind::Velorite => false,
|
2019-08-14 21:28:37 +00:00
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|
2019-08-16 11:38:59 +00:00
|
|
|
|
|
|
|
pub fn is_solid(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
BlockKind::Air => false,
|
|
|
|
BlockKind::Water => false,
|
2019-08-21 17:22:05 +00:00
|
|
|
BlockKind::LargeCactus => true,
|
|
|
|
BlockKind::BarrelCactus => true,
|
2019-09-01 19:04:03 +00:00
|
|
|
BlockKind::RoundCactus => true,
|
|
|
|
BlockKind::ShortCactus => true,
|
|
|
|
BlockKind::MedFlatCactus => true,
|
|
|
|
BlockKind::ShortFlatCactus => true,
|
2019-08-21 17:22:05 +00:00
|
|
|
BlockKind::BlueFlower => false,
|
|
|
|
BlockKind::PinkFlower => false,
|
|
|
|
BlockKind::PurpleFlower => false,
|
|
|
|
BlockKind::RedFlower => false,
|
|
|
|
BlockKind::WhiteFlower => false,
|
|
|
|
BlockKind::YellowFlower => false,
|
|
|
|
BlockKind::Sunflower => false,
|
2019-08-19 23:31:11 +00:00
|
|
|
BlockKind::LongGrass => false,
|
2019-08-21 17:22:05 +00:00
|
|
|
BlockKind::MediumGrass => false,
|
|
|
|
BlockKind::ShortGrass => false,
|
|
|
|
BlockKind::Apple => true,
|
2019-09-01 19:04:03 +00:00
|
|
|
BlockKind::Mushroom => false,
|
|
|
|
BlockKind::Liana => false,
|
2019-08-16 11:38:59 +00:00
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|
2019-09-25 21:17:43 +00:00
|
|
|
|
|
|
|
pub fn is_collectible(&self) -> bool {
|
|
|
|
match self {
|
2019-09-25 22:04:18 +00:00
|
|
|
BlockKind::BlueFlower => true,
|
|
|
|
BlockKind::PinkFlower => true,
|
|
|
|
BlockKind::PurpleFlower => true,
|
|
|
|
BlockKind::RedFlower => true,
|
|
|
|
BlockKind::WhiteFlower => true,
|
|
|
|
BlockKind::YellowFlower => true,
|
|
|
|
BlockKind::Sunflower => true,
|
|
|
|
BlockKind::LongGrass => true,
|
|
|
|
BlockKind::MediumGrass => true,
|
|
|
|
BlockKind::ShortGrass => true,
|
2019-09-25 21:17:43 +00:00
|
|
|
BlockKind::Apple => true,
|
|
|
|
BlockKind::Mushroom => true,
|
|
|
|
BlockKind::Velorite => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2019-08-14 21:28:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[repr(packed)]
|
2019-01-02 19:22:01 +00:00
|
|
|
pub struct Block {
|
2019-08-14 21:28:37 +00:00
|
|
|
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 {
|
2019-08-14 21:28:37 +00:00
|
|
|
pub fn new(kind: BlockKind, color: Rgb<u8>) -> Self {
|
2019-01-23 20:01:58 +00:00
|
|
|
Self {
|
|
|
|
kind,
|
|
|
|
color: color.into_array(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 21:28:37 +00:00
|
|
|
pub fn get_color(&self) -> Option<Rgb<u8>> {
|
|
|
|
if !self.is_air() {
|
2019-01-23 20:01:58 +00:00
|
|
|
Some(self.color.into())
|
2019-08-14 21:28:37 +00:00
|
|
|
} else {
|
|
|
|
None
|
2019-01-23 20:01:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-31 20:37:11 +00:00
|
|
|
|
2019-08-14 21:28:37 +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 {
|
2019-08-14 21:28:37 +00:00
|
|
|
kind: BlockKind::Air,
|
2019-01-14 18:49:53 +00:00
|
|
|
color: [0; 3],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_empty(&self) -> bool {
|
2019-08-14 21:28:37 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|