Use Point3::map where possible

This commit is contained in:
Sijmen 2021-06-10 18:28:13 +02:00
parent f6bb14ba4b
commit 1960da95df
Signed by: vijfhoek
GPG key ID: 82D05C89B28B0DAE
2 changed files with 8 additions and 33 deletions

View file

@ -129,12 +129,7 @@ impl Player {
}; };
for corner in &aabb.get_corners() { for corner in &aabb.get_corners() {
let block = world.get_block( let block = world.get_block(corner.map(|x| x.floor() as isize));
corner.x.floor() as isize,
corner.y.floor() as isize,
corner.z.floor() as isize,
);
if block.is_some() { if block.is_some() {
return Some(aabb); return Some(aabb);
} }

View file

@ -410,33 +410,20 @@ impl World {
block_type: BlockType, block_type: BlockType,
) { ) {
if let Some((pos, face_normal)) = self.raycast(camera.position, camera.direction()) { if let Some((pos, face_normal)) = self.raycast(camera.position, camera.direction()) {
let new_pos = pos.cast().unwrap() + face_normal; let new_pos = (pos.cast().unwrap() + face_normal).cast().unwrap();
self.set_block(new_pos.x, new_pos.y, new_pos.z, Some(Block { block_type }));
self.set_block(
new_pos.x as isize,
new_pos.y as isize,
new_pos.z as isize,
Some(Block { block_type }),
);
self.update_chunk_geometry(render_context, pos / CHUNK_ISIZE); self.update_chunk_geometry(render_context, pos / CHUNK_ISIZE);
} }
} }
pub fn get_block(&self, x: isize, y: isize, z: isize) -> Option<&Block> { pub fn get_block(&self, point: Point3<isize>) -> Option<&Block> {
let chunk = match self.chunks.get(&Point3::new( let chunk = match self.chunks.get(&point.map(|x| x.div_euclid(CHUNK_ISIZE))) {
x.div_euclid(CHUNK_ISIZE),
y.div_euclid(CHUNK_ISIZE),
z.div_euclid(CHUNK_ISIZE),
)) {
Some(chunk) => chunk, Some(chunk) => chunk,
None => return None, None => return None,
}; };
let bx = x.rem_euclid(CHUNK_ISIZE) as usize; let b = point.map(|x| x.rem_euclid(CHUNK_ISIZE) as usize);
let by = y.rem_euclid(CHUNK_ISIZE) as usize; chunk.blocks[b.y][b.z][b.x].as_ref()
let bz = z.rem_euclid(CHUNK_ISIZE) as usize;
chunk.blocks[by][bz][bx].as_ref()
} }
pub fn set_block(&mut self, x: isize, y: isize, z: isize, block: Option<Block>) { pub fn set_block(&mut self, x: isize, y: isize, z: isize, block: Option<Block>) {
@ -518,14 +505,7 @@ impl World {
return None; return None;
} }
if self if self.get_block(position.cast().unwrap()).is_some() {
.get_block(
position.x as isize,
position.y as isize,
position.z as isize,
)
.is_some()
{
// Intersection occurred // Intersection occurred
return Some((position.cast().unwrap(), face)); return Some((position.cast().unwrap(), face));
} }