Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
fixed height limit bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamkob12 committed Oct 17, 2023
1 parent cc6041d commit 17580eb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/add_break_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,29 @@ pub fn add_break_detector(
let pos = tran.translation;

if buttons.just_pressed(MouseButton::Left) {
if tran.translation.y - 1.0 >= HEIGHT as f32 || tran.translation.y < 0.0 {
info!("\nIn-Game Log:\n Can't break / place blocks above the maximum height.");
return;
}
block_change_event_writer.send(BlockChange {
blocks: blocks_in_the_way(pos, forward, REACH_DISTANCE)
.iter()
.map(|(x, y, _z)| (*x, one_d_cords(*y, CHUNK_DIMS), None))
.filter_map(|(x, y, _z)| {
if let Some(tmp) = one_d_cords_safe(*y, CHUNK_DIMS) {
Some((*x, tmp, None))
} else {
None
}
})
.collect(),
change: VoxelChange::Broken,
});
}

if buttons.just_pressed(MouseButton::Right) {
if tran.translation.y + 1.0 >= HEIGHT as f32 || tran.translation.y < 0.0 {
info!("\nIn-Game Log:\n Can't break / place blocks above the maximum height.");
return;
}
block_change_event_writer.send(BlockChange {
change: VoxelChange::Added,
blocks: blocks_in_the_way(pos, forward, REACH_DISTANCE)
.iter()
.skip(1)
.filter_map(|&(x, y, z)| {
let tmp = one_d_cords(y, CHUNK_DIMS);
let tmp = one_d_cords_safe(y, CHUNK_DIMS)?;
if let Some(block) = get_neighbor(tmp, z, CHUNK_DIMS) {
Some((x, block, Some((x, tmp))))
} else {
Expand Down
8 changes: 8 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ pub const fn one_d_cords(threed: [usize; 3], dims: (usize, usize, usize)) -> usi
threed[1] * (dims.0 * dims.2) + threed[2] * dims.0 + threed[0]
}

pub const fn one_d_cords_safe(threed: [usize; 3], dims: (usize, usize, usize)) -> Option<usize> {
if threed[0] >= dims.0 || threed[1] >= dims.1 || threed[2] >= dims.2 {
None
} else {
Some(threed[1] * (dims.0 * dims.2) + threed[2] * dims.0 + threed[0])
}
}

// Extract the vertex data for the physics engine.
use bevy::render::mesh::{Mesh, VertexAttributeValues};
pub fn extract_position_vertex_data(mesh: &Mesh) -> Vec<Vec3> {
Expand Down

0 comments on commit 17580eb

Please sign in to comment.