diff --git a/azalea-physics/src/discrete_voxel_shape.rs b/azalea-physics/src/discrete_voxel_shape.rs index d3f9932d..1750612f 100644 --- a/azalea-physics/src/discrete_voxel_shape.rs +++ b/azalea-physics/src/discrete_voxel_shape.rs @@ -19,17 +19,17 @@ pub trait DiscreteVoxelShape { // } // return false; // } - fn x_size(&self) -> usize; - fn y_size(&self) -> usize; - fn z_size(&self) -> usize; + fn x_size(&self) -> u32; + fn y_size(&self) -> u32; + fn z_size(&self) -> u32; - fn first_full_x(&self) -> usize; - fn first_full_y(&self) -> usize; - fn first_full_z(&self) -> usize; + fn first_full_x(&self) -> u32; + fn first_full_y(&self) -> u32; + fn first_full_z(&self) -> u32; - fn last_full_x(&self) -> usize; - fn last_full_y(&self) -> usize; - fn last_full_z(&self) -> usize; + fn last_full_x(&self) -> u32; + fn last_full_y(&self) -> u32; + fn last_full_z(&self) -> u32; fn is_empty(&self) -> bool { if self.first_full_x() >= self.last_full_x() { @@ -121,3 +121,35 @@ impl BitSetDiscreteVoxelShape { ((x * self.y_size + y) * self.z_size + z) as usize } } + +impl DiscreteVoxelShape for BitSetDiscreteVoxelShape { + fn x_size(&self) -> u32 { + self.x_size + } + fn y_size(&self) -> u32 { + self.y_size + } + fn z_size(&self) -> u32 { + self.z_size + } + + fn first_full_x(&self) -> u32 { + self.x_min + } + fn first_full_y(&self) -> u32 { + self.y_min + } + fn first_full_z(&self) -> u32 { + self.z_min + } + + fn last_full_x(&self) -> u32 { + self.x_max + } + fn last_full_y(&self) -> u32 { + self.y_max + } + fn last_full_z(&self) -> u32 { + self.z_max + } +} diff --git a/azalea-physics/src/lib.rs b/azalea-physics/src/lib.rs index c05b2662..015fc916 100644 --- a/azalea-physics/src/lib.rs +++ b/azalea-physics/src/lib.rs @@ -81,7 +81,7 @@ impl HasPhysics for Entity { dimension: &Dimension, entity_collisions: Vec>, ) -> Vec3 { - let mut collision_boxes = Vec::with_capacity(1); // entity_collisions.len() + 1 + let mut collision_boxes: Vec> = Vec::with_capacity(1); // entity_collisions.len() + 1 if !entity_collisions.is_empty() { collision_boxes.extend(entity_collisions); @@ -91,7 +91,7 @@ impl HasPhysics for Entity { let block_collisions = dimension.get_block_collisions(entity, entity_bounding_box.expand_towards(movement)); - collision_boxes.extend(block_collisions); + collision_boxes.extend(block_collisions.map(Box::new).collect::>()); Self::collide_with_shapes(movement, &entity_bounding_box, &collision_boxes) } diff --git a/azalea-physics/src/shape.rs b/azalea-physics/src/shape.rs index 7ec057ec..e6ff3b40 100644 --- a/azalea-physics/src/shape.rs +++ b/azalea-physics/src/shape.rs @@ -1,15 +1,20 @@ -use std::ops::{Add, Index}; - -use azalea_core::Direction; - use crate::{BitSetDiscreteVoxelShape, DiscreteVoxelShape}; +use std::ops::Add; pub struct Shapes {} pub fn block_shape() -> Box { let mut shape = BitSetDiscreteVoxelShape::new(1, 1, 1); shape.fill(0, 0, 0); - VoxelShape::new(Box::new(shape)) + Box::new(CubeVoxelShape::new(Box::new(shape))) +} +pub fn empty_shape() -> Box { + Box::new(ArrayVoxelShape::new( + Box::new(BitSetDiscreteVoxelShape::new(0, 0, 0)), + vec![0.], + vec![0.], + vec![0.], + )) } pub trait VoxelShape { @@ -19,17 +24,16 @@ pub trait VoxelShape { fn get_y_coords(&self) -> Vec; fn get_z_coords(&self) -> Vec; - fn move_relative(&self, x: f64, y: f64, z: f64) -> ArrayVoxelShape { + fn move_relative(&self, x: f64, y: f64, z: f64) -> Box { if self.shape().is_empty() { - return Shapes::empty(); + return empty_shape(); } - // TODO: just offset the vecs now instead of using an OffsetVec - ArrayVoxelShape::new( + Box::new(ArrayVoxelShape::new( self.shape(), self.get_x_coords().iter().map(|c| c + x).collect(), self.get_y_coords().iter().map(|c| c + y).collect(), self.get_z_coords().iter().map(|c| c + z).collect(), - ) + )) } } @@ -42,20 +46,12 @@ pub struct ArrayVoxelShape { pub zs: Vec, } +pub struct CubeVoxelShape { + shape: Box, + faces: Option>>, +} + impl ArrayVoxelShape { - // ArrayVoxelShape(DiscreteVoxelShape var1, DoubleList var2, DoubleList var3, DoubleList var4) { - // super(var1); - // int var5 = var1.getXSize() + 1; - // int var6 = var1.getYSize() + 1; - // int var7 = var1.getZSize() + 1; - // if (var5 == var2.size() && var6 == var3.size() && var7 == var4.size()) { - // this.xs = var2; - // this.ys = var3; - // this.zs = var4; - // } else { - // throw (IllegalArgumentException)Util.pauseInIde(new IllegalArgumentException("Lengths of point arrays must be consistent with the size of the VoxelShape.")); - // } - // } pub fn new( shape: Box, xs: Vec, @@ -67,9 +63,9 @@ impl ArrayVoxelShape { let z_size = shape.z_size() + 1; // Lengths of point arrays must be consistent with the size of the VoxelShape. - assert_eq!(x_size, xs.len()); - assert_eq!(y_size, ys.len()); - assert_eq!(z_size, zs.len()); + assert_eq!(x_size, xs.len() as u32); + assert_eq!(y_size, ys.len() as u32); + assert_eq!(z_size, zs.len() as u32); Self { faces: None, @@ -81,25 +77,59 @@ impl ArrayVoxelShape { } } -// mojang moment -// this is probably for an optimization and could probably be optimized more -/// A Vec that adds the given offset when indexing into it. -pub struct OffsetVec { - delegate: Vec, - offset: T, +impl CubeVoxelShape { + pub fn new(shape: Box) -> Self { + Self { shape, faces: None } + } } -impl OffsetVec -where - T: Add, -{ - pub fn new(delegate: Vec, offset: T) -> Self { - Self { delegate, offset } +impl VoxelShape for ArrayVoxelShape { + fn shape(&self) -> Box { + todo!() } - pub fn index(&self, index: usize) -> T { - self.delegate[index] + self.offset + + fn get_x_coords(&self) -> Vec { + todo!() } - pub fn len(&self) -> usize { - self.delegate.len() + + fn get_y_coords(&self) -> Vec { + todo!() + } + + fn get_z_coords(&self) -> Vec { + todo!() + } +} + +impl VoxelShape for CubeVoxelShape { + fn shape(&self) -> Box { + todo!() + } + + fn get_x_coords(&self) -> Vec { + let size = self.shape.x_size(); + let mut parts = Vec::with_capacity(size as usize); + for i in 0..size { + parts.push(i as f64 / size as f64); + } + parts + } + + fn get_y_coords(&self) -> Vec { + let size = self.shape.y_size(); + let mut parts = Vec::with_capacity(size as usize); + for i in 0..size { + parts.push(i as f64 / size as f64); + } + parts + } + + fn get_z_coords(&self) -> Vec { + let size = self.shape.z_size(); + let mut parts = Vec::with_capacity(size as usize); + for i in 0..size { + parts.push(i as f64 / size as f64); + } + parts } }