Skip to content

Commit 6a8de89

Browse files
committed
cleanup and use of built in methods
1 parent 5dfe7e8 commit 6a8de89

File tree

2 files changed

+27
-58
lines changed

2 files changed

+27
-58
lines changed

src/chunk.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub struct Chunk {
4343
pub chunk_water_index_buffer: Option<wgpu::Buffer>,
4444
pub outside_blocks: Vec<Arc<RwLock<Block>>>,
4545
pub visible: bool,
46-
pub modified: bool, // if it should be saved
46+
pub modified: bool, // if true, it will be saved
4747
}
4848

4949
impl Chunk {
@@ -57,13 +57,8 @@ impl Chunk {
5757
.get_mut(((block_position.x * CHUNK_SIZE as f32) + block_position.z) as usize)
5858
.expect("Cannot add oob block");
5959

60-
let start_len = y_blocks.len();
61-
62-
/* Make sure we don't have enough space in the vector */
63-
for i in start_len..=block_position.y as usize {
64-
if i >= y_blocks.len() {
65-
y_blocks.push(None);
66-
}
60+
if block_position.y as usize >= y_blocks.len() {
61+
y_blocks.resize(block_position.y as usize + 1, None);
6762
}
6863

6964
y_blocks[block_position.y as usize] = Some(block);
@@ -215,16 +210,15 @@ impl Chunk {
215210
}
216211
}
217212
None => {
218-
if face_position.y as u32
219-
<= Chunk::get_height_value(
220-
target_chunk_x,
221-
target_chunk_y,
222-
target_block.x as u32,
223-
target_block.z as u32,
224-
self.noise_data.clone(),
225-
)
226-
&& face_position.y >= WATER_HEIGHT_LEVEL as f32
227-
{
213+
let h = Chunk::get_height_value(
214+
target_chunk_x,
215+
target_chunk_y,
216+
target_block.x as u32,
217+
target_block.z as u32,
218+
self.noise_data.clone(),
219+
);
220+
221+
if face_position.y as u32 <= h {
228222
is_visible = false
229223
};
230224
}

src/world.rs

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub const FREQUENCY: f32 = 1. / 128.;
2424
pub const NOISE_CHUNK_PER_ROW: u32 = NOISE_SIZE / CHUNK_SIZE;
2525
pub const MAX_TREES_PER_CHUNK: u32 = 3;
2626

27-
pub const CHUNKS_PER_ROW: u32 = 60;
27+
pub const CHUNKS_PER_ROW: u32 = 9;
2828
pub const CHUNKS_REGION: u32 = CHUNKS_PER_ROW * CHUNKS_PER_ROW;
2929
pub const WATER_HEIGHT_LEVEL: u8 = 5;
3030

@@ -56,61 +56,39 @@ pub struct World {
5656
}
5757

5858
impl World {
59-
// pub fn get_other_chunks(&self, chunk_ptr: WorldChunk) -> Vec<WorldChunk> {
60-
// self.chunks
61-
// .values()
62-
// .filter_map(|c| {
63-
// return if !Arc::ptr_eq(&chunk_ptr, c) {
64-
// Some(c.clone())
65-
// } else {
66-
// None
67-
// };
68-
// })
69-
// .collect()
70-
// }
7159
pub fn place_block(&mut self, block: Arc<RwLock<Block>>) {
7260
let block_borrow = block.read().unwrap();
7361
let mut chunks_to_rerender = vec![block_borrow.get_chunk_coords()];
62+
chunks_to_rerender.append(&mut block_borrow.get_neighbour_chunks_coords());
63+
7464
let chunk_map = self.chunks.read().unwrap();
7565
let chunk = chunk_map
7666
.get(&chunks_to_rerender[0])
7767
.expect("Cannot delete a block from unloaded chunk");
7868

79-
let mut chunk_lock = chunk.write().unwrap();
80-
chunk_lock.add_block(block.clone(), true);
81-
82-
let block_borrow = block.read().unwrap();
83-
chunks_to_rerender.append(&mut block_borrow.get_neighbour_chunks_coords());
84-
std::mem::drop(chunk_lock);
85-
86-
// if block_neighbour_chunks.len() > 0 {
87-
// for neighbour_chunk in block_neighbour_chunks {
88-
// let neighbour_chunk = self
89-
// .chunks
90-
// .get(&neighbour_chunk)
91-
// .expect("Cannot destroy a block without neighbour being loaded");
92-
93-
// chunks_to_rerender.push(neighbour_chunk.clone());
94-
// }
95-
// }
69+
{
70+
let mut chunk_lock = chunk.write().unwrap();
71+
chunk_lock.add_block(block.clone(), true);
72+
// Drop chunk lock write
73+
}
9674

9775
self.render_chunks(chunks_to_rerender)
98-
// self.render_chunks(block_neighbour_chunks.append(&mut vec![chunk_coords]));
9976
}
10077
pub fn remove_block(&mut self, block: Arc<RwLock<Block>>) {
10178
let block_borrow = block.read().unwrap();
10279
let mut chunks_to_rerender = vec![block_borrow.get_chunk_coords()];
80+
chunks_to_rerender.append(&mut block_borrow.get_neighbour_chunks_coords());
81+
10382
let chunk_map = self.chunks.read().unwrap();
10483
let chunk = chunk_map
10584
.get(&chunks_to_rerender[0])
10685
.expect("Cannot delete a block from unloaded chunk");
10786

108-
let mut chunk_lock = chunk.write().unwrap();
109-
chunk_lock.remove_block(&(block_borrow.position));
110-
// chunk_lock.build_mesh(self.get_other_chunks(chunk.clone()));
111-
chunks_to_rerender.append(&mut block_borrow.get_neighbour_chunks_coords());
112-
// I hate this so much
113-
std::mem::drop(chunk_lock);
87+
{
88+
let mut chunk_lock = chunk.write().unwrap();
89+
chunk_lock.remove_block(&(block_borrow.position));
90+
// Drop chunk lock write
91+
}
11492

11593
self.render_chunks(chunks_to_rerender);
11694
}
@@ -144,9 +122,6 @@ impl World {
144122

145123
for position in positions.iter() {
146124
if let Some(block) = self.get_blocks_absolute(position) {
147-
if block.read().unwrap().block_type == BlockType::Water {
148-
continue;
149-
}
150125
nearby_blocks.push(block)
151126
};
152127
}

0 commit comments

Comments
 (0)