2019-05-25 05:54:47 +00:00
|
|
|
use super::Block;
|
2019-05-24 11:08:38 +00:00
|
|
|
use crate::{
|
2019-06-06 14:48:41 +00:00
|
|
|
assets::{self, Asset},
|
2019-07-08 14:51:38 +00:00
|
|
|
vol::{BaseVol, ReadVol, SizedVol, Vox, WriteVol},
|
2019-05-24 11:08:38 +00:00
|
|
|
volumes::dyna::{Dyna, DynaErr},
|
|
|
|
};
|
2019-05-25 05:54:47 +00:00
|
|
|
use dot_vox::DotVoxData;
|
2019-06-23 20:42:17 +00:00
|
|
|
use std::io::{BufReader, Read};
|
2019-05-25 05:54:47 +00:00
|
|
|
use vek::*;
|
2019-05-24 11:08:38 +00:00
|
|
|
|
2019-06-12 20:22:16 +00:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum StructureBlock {
|
|
|
|
TemperateLeaves,
|
|
|
|
PineLeaves,
|
2019-07-08 19:28:48 +00:00
|
|
|
Acacia,
|
2019-06-12 20:22:16 +00:00
|
|
|
PalmLeaves,
|
2019-07-09 16:08:43 +00:00
|
|
|
Fruit,
|
2019-08-03 20:44:51 +00:00
|
|
|
Hollow,
|
2019-06-12 20:22:16 +00:00
|
|
|
Block(Block),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Vox for StructureBlock {
|
|
|
|
fn empty() -> Self {
|
|
|
|
StructureBlock::Block(Block::empty())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
StructureBlock::Block(block) => block.is_empty(),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-24 11:08:38 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum StructureError {}
|
|
|
|
|
2019-05-24 12:25:31 +00:00
|
|
|
#[derive(Clone)]
|
2019-05-24 11:08:38 +00:00
|
|
|
pub struct Structure {
|
|
|
|
center: Vec3<i32>,
|
2019-06-12 20:22:16 +00:00
|
|
|
vol: Dyna<StructureBlock, ()>,
|
|
|
|
empty: StructureBlock,
|
2019-05-24 11:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Structure {
|
|
|
|
pub fn with_center(mut self, center: Vec3<i32>) -> Self {
|
|
|
|
self.center = center;
|
|
|
|
self
|
|
|
|
}
|
2019-07-08 14:51:38 +00:00
|
|
|
|
|
|
|
pub fn get_bounds(&self) -> Aabb<i32> {
|
|
|
|
Aabb {
|
|
|
|
min: -self.center,
|
|
|
|
max: self.vol.get_size().map(|e| e as i32) - self.center,
|
|
|
|
}
|
|
|
|
}
|
2019-05-24 11:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BaseVol for Structure {
|
2019-06-12 20:22:16 +00:00
|
|
|
type Vox = StructureBlock;
|
2019-05-24 11:08:38 +00:00
|
|
|
type Err = StructureError;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ReadVol for Structure {
|
|
|
|
#[inline(always)]
|
2019-06-12 20:22:16 +00:00
|
|
|
fn get(&self, pos: Vec3<i32>) -> Result<&Self::Vox, StructureError> {
|
2019-05-24 12:25:31 +00:00
|
|
|
match self.vol.get(pos + self.center) {
|
2019-05-24 11:08:38 +00:00
|
|
|
Ok(block) => Ok(block),
|
|
|
|
Err(DynaErr::OutOfBounds) => Ok(&self.empty),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Asset for Structure {
|
2019-06-23 20:42:17 +00:00
|
|
|
fn load(buf_reader: BufReader<impl Read>) -> Result<Self, assets::Error> {
|
|
|
|
let dot_vox_data = DotVoxData::load(buf_reader)?;
|
2019-05-24 11:08:38 +00:00
|
|
|
|
|
|
|
if let Some(model) = dot_vox_data.models.get(0) {
|
|
|
|
let palette = dot_vox_data
|
|
|
|
.palette
|
|
|
|
.iter()
|
|
|
|
.map(|col| Rgba::from(col.to_ne_bytes()).into())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2019-05-25 05:54:47 +00:00
|
|
|
let mut vol = Dyna::filled(
|
|
|
|
Vec3::new(model.size.x, model.size.y, model.size.z),
|
2019-06-12 20:22:16 +00:00
|
|
|
StructureBlock::empty(),
|
2019-05-25 05:54:47 +00:00
|
|
|
(),
|
|
|
|
);
|
2019-05-24 11:08:38 +00:00
|
|
|
|
|
|
|
for voxel in &model.voxels {
|
2019-06-12 20:22:16 +00:00
|
|
|
let block = match voxel.i {
|
|
|
|
0 => StructureBlock::TemperateLeaves,
|
|
|
|
1 => StructureBlock::PineLeaves,
|
|
|
|
2 => StructureBlock::PalmLeaves,
|
2019-07-08 19:28:48 +00:00
|
|
|
4 => StructureBlock::Acacia,
|
2019-07-09 16:08:43 +00:00
|
|
|
7 => StructureBlock::Fruit,
|
2019-08-03 20:44:51 +00:00
|
|
|
15 => StructureBlock::Hollow,
|
2019-06-12 20:22:16 +00:00
|
|
|
index => {
|
2019-06-15 10:36:26 +00:00
|
|
|
let color = palette
|
|
|
|
.get(index as usize)
|
|
|
|
.copied()
|
2019-07-01 20:42:43 +00:00
|
|
|
.unwrap_or_else(|| Rgb::broadcast(0));
|
2019-06-12 20:22:16 +00:00
|
|
|
StructureBlock::Block(Block::new(1, color))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let _ = vol.set(
|
2019-07-01 20:42:43 +00:00
|
|
|
Vec3::new(voxel.x, voxel.y, voxel.z).map(|e| i32::from(e)),
|
2019-06-15 10:36:26 +00:00
|
|
|
block,
|
2019-06-12 20:22:16 +00:00
|
|
|
);
|
2019-05-24 11:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Structure {
|
|
|
|
center: Vec3::zero(),
|
|
|
|
vol,
|
2019-06-12 20:22:16 +00:00
|
|
|
empty: StructureBlock::empty(),
|
2019-05-24 11:08:38 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Ok(Self {
|
|
|
|
center: Vec3::zero(),
|
2019-06-12 20:22:16 +00:00
|
|
|
vol: Dyna::filled(Vec3::zero(), StructureBlock::empty(), ()),
|
|
|
|
empty: StructureBlock::empty(),
|
2019-05-24 11:08:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|