Skip to content

Commit 1f37e2a

Browse files
committed
fix: miscalculated array sizing
1 parent aba4d97 commit 1f37e2a

File tree

5 files changed

+24
-14
lines changed

5 files changed

+24
-14
lines changed

src/chunk.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,15 @@ impl Chunk {
4040
)
4141
.expect("Cannot add oob block");
4242

43-
println!("ADD BLOCK");
4443
let start_len = y_blocks.len();
4544

46-
for i in start_len..block_borrow.position.y as usize {
47-
println!("LOLL");
45+
/* Make sure we don't have enough space in the vector */
46+
for i in start_len..=block_borrow.position.y as usize {
4847
if i >= y_blocks.len() {
4948
y_blocks.push(None);
5049
}
5150
}
52-
y_blocks.push(Some(block.clone()));
51+
y_blocks[block_borrow.position.y as usize] = Some(block.clone());
5352
}
5453
pub fn remove_block(&mut self, block_r_position: &Vec3) {
5554
let y_blocks = self
@@ -144,12 +143,12 @@ impl Chunk {
144143
None => {
145144
if face_position.y as u32
146145
<= Chunk::get_height_value(
147-
target_chunk_x,
148-
target_chunk_y,
149-
target_block.x as u32,
150-
target_block.z as u32,
151-
self.noise_data.clone(),
152-
)
146+
target_chunk_x,
147+
target_chunk_y,
148+
target_block.x as u32,
149+
target_block.z as u32,
150+
self.noise_data.clone(),
151+
)
153152
{
154153
is_visible = false
155154
};

src/collision.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl Ray {
6969
tzmax = (collision_box.min_z - self.origin.z) * invdirz;
7070
}
7171

72-
if tmin > tzmax || tzmin > tmax {
72+
if tmin > tzmax || tzmin > tmax || tmin < 0.0 || tmax < 0.0 {
7373
return None;
7474
}
7575

@@ -87,6 +87,7 @@ impl Ray {
8787
}
8888
}
8989

90+
#[derive(Debug)]
9091
pub struct RayResult<'a> {
9192
pub points: Vec<glam::Vec3>,
9293
pub collision: &'a CollisionBox,
@@ -117,7 +118,7 @@ impl CollisionBox {
117118
}
118119
}
119120
pub fn to_block_position(&self) -> glam::Vec3 {
120-
glam::vec3(self.min_x, self.min_y, self.min_z)
121+
return glam::vec3(self.min_x, self.min_y, self.min_z);
121122
}
122123
pub fn new(x: f32, y: f32, z: f32, width: f32, height: f32, depth: f32) -> CollisionBox {
123124
CollisionBox {

src/state.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ impl State {
249249
);
250250
if let Some((block, face_dir)) = self.player.get_facing_block(&collisions) {
251251
let block = self.world.get_blocks_absolute(&block.to_block_position());
252+
252253
self.player.facing_block = block;
253254
self.player.facing_face = Some(face_dir);
254255
} else {
@@ -386,7 +387,7 @@ impl State {
386387
ui_renderpass.set_vertex_buffer(0, self.ui.vertex_buffer.slice(..));
387388
ui_renderpass
388389
.set_index_buffer(self.ui.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
389-
ui_renderpass.draw_indexed(0..6, 0, 0..1);
390+
ui_renderpass.draw_indexed(0..self.ui.indices as u32, 0, 0..1);
390391
}
391392

392393
self.queue.submit(Some(encoder.finish()));

src/ui.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use wgpu::{BindGroup, Buffer, RenderPipeline};
1010
pub struct UI {
1111
pub vertex_buffer: wgpu::Buffer,
1212
pub index_buffer: wgpu::Buffer,
13+
pub indices: usize,
1314
}
1415

1516
impl UI {
@@ -34,6 +35,7 @@ impl UI {
3435
});
3536

3637
Self {
38+
indices: 0,
3739
vertex_buffer,
3840
index_buffer,
3941
}
@@ -50,12 +52,17 @@ impl UI {
5052

5153
let blocks_position = face_data.0.iter().map(|v| v.position).collect::<Vec<_>>();
5254

55+
self.indices = face_data.1.len();
5356
queue.write_buffer(
5457
&self.vertex_buffer,
5558
0,
5659
bytemuck::cast_slice(&blocks_position),
5760
);
5861
queue.write_buffer(&self.index_buffer, 0, bytemuck::cast_slice(&face_data.1));
62+
} else {
63+
self.indices = 0;
64+
queue.write_buffer(&self.vertex_buffer, 0, &[]);
65+
queue.write_buffer(&self.index_buffer, 0, &[]);
5966
}
6067
}
6168

src/world.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use glam::Vec3;
12
use std::any::Any;
23
use std::ffi::c_void;
34
use std::ops::Deref;
@@ -148,7 +149,7 @@ impl World {
148149
}
149150
}
150151
}
151-
pub fn get_blocks_absolute(&self, position: &glam::Vec3) -> Option<Arc<Mutex<Block>>> {
152+
pub fn get_blocks_absolute(&self, position: &Vec3) -> Option<Arc<Mutex<Block>>> {
152153
let (chunk_x, chunk_y) = position.get_chunk_from_position_absolute();
153154

154155
let chunk = self.chunks.iter().find(|c| {
@@ -159,6 +160,7 @@ impl World {
159160

160161
let relative_position = position.relative_from_absolute();
161162
let block = chunk.get_block_at_relative(&relative_position)?;
163+
162164
return Some(block);
163165
}
164166
pub fn get_blocks_nearby(&self, player: &Player) -> Option<Vec<Arc<Mutex<Block>>>> {

0 commit comments

Comments
 (0)