67 lines
1.8 KiB
Rust
67 lines
1.8 KiB
Rust
use std::collections::HashMap;
|
|
|
|
use crate::space::three_dimensional::{
|
|
traits::{DefaultVoxelKinds, VoxelContainer},
|
|
vec3generic::Vec3Generic,
|
|
};
|
|
use block_mesh::Voxel;
|
|
#[cfg(feature = "import_dot_vox")]
|
|
use dot_vox::DotVoxData;
|
|
|
|
use super::traits::DefaultVoxel;
|
|
|
|
#[derive(Debug, Default, Clone)]
|
|
#[cfg_attr(feature = "bevy", derive(bevy::prelude::Component))]
|
|
pub struct VoxelHashMapLayer<TVoxel> {
|
|
pub voxels: HashMap<Vec3Generic<i32>, TVoxel>,
|
|
pub size: Vec3Generic<i32>,
|
|
}
|
|
|
|
impl<TVoxel> VoxelContainer<TVoxel> for VoxelHashMapLayer<TVoxel>
|
|
where
|
|
TVoxel: Voxel + Copy,
|
|
{
|
|
fn get_voxel_at_pos<'a>(&self, pos: Vec3Generic<i32>) -> Option<TVoxel> {
|
|
self.voxels.get(&pos).copied()
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "import_dot_vox")]
|
|
pub fn import_from_bytes<T>(data: &[u8]) -> Result<VoxelHashMapLayer<T>, &'static str>
|
|
where
|
|
T: Voxel + DefaultVoxel + Default,
|
|
{
|
|
let v = dot_vox::load_bytes(data)?;
|
|
Ok(v.into())
|
|
}
|
|
|
|
#[cfg(feature = "import_dot_vox")]
|
|
impl<T> From<DotVoxData> for VoxelHashMapLayer<T>
|
|
where
|
|
T: Voxel + DefaultVoxel + Default,
|
|
{
|
|
fn from(d: DotVoxData) -> Self {
|
|
let mut w: VoxelHashMapLayer<T> = Default::default();
|
|
|
|
for model in d.models {
|
|
for voxel in model.voxels {
|
|
w.voxels.insert(
|
|
Vec3Generic {
|
|
x: voxel.x as i32,
|
|
y: voxel.z as i32,
|
|
z: voxel.y as i32,
|
|
},
|
|
DefaultVoxel::get_default(DefaultVoxelKinds::Solid),
|
|
);
|
|
}
|
|
|
|
w.size.x = w.size.x.max(model.size.x as i32);
|
|
w.size.y = w.size.y.max(model.size.z as i32);
|
|
w.size.z = w.size.z.max(model.size.y as i32);
|
|
}
|
|
println!("loaded world size {:?}", w.size);
|
|
|
|
w
|
|
}
|
|
}
|