Skip to content

Commit c27139c

Browse files
committed
feat: handle outside blocks of chunks
1 parent 8933f36 commit c27139c

File tree

1 file changed

+64
-32
lines changed

1 file changed

+64
-32
lines changed

src/world.rs

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub struct World {
4242

4343
impl World {
4444
// gets all the chunks except the one passed in the index
45-
pub fn get_other_chunks(&self, chunk_index: usize) -> Vec<Arc<Mutex<Chunk>>> {
45+
pub fn get_other_chunks_by_index(&self, chunk_index: usize) -> Vec<Arc<Mutex<Chunk>>> {
4646
self.chunks
4747
.iter()
4848
.enumerate()
@@ -55,14 +55,26 @@ impl World {
5555
})
5656
.collect()
5757
}
58+
// gets all the chunks except the one passed
59+
pub fn get_other_chunks(&self, chunk_ptr: &Arc<Mutex<Chunk>>) -> Vec<Arc<Mutex<Chunk>>> {
60+
self.chunks
61+
.iter()
62+
.filter_map(|c| {
63+
if !Arc::ptr_eq(&chunk_ptr, c) {
64+
Some(c.clone())
65+
} else {
66+
None
67+
}
68+
})
69+
.collect()
70+
}
5871
pub fn place_block(&mut self, block: Arc<Mutex<Block>>) {
5972
let block_borrow = block.lock().unwrap();
6073
let chunk_coords = block_borrow.get_chunk_coords();
61-
let (chunk_index, chunk) = self
74+
let chunk = self
6275
.chunks
6376
.iter()
64-
.enumerate()
65-
.find(|(_, c)| {
77+
.find(|c| {
6678
let c = c.lock().unwrap();
6779
c.x == chunk_coords.0 && c.y == chunk_coords.1
6880
})
@@ -71,31 +83,25 @@ impl World {
7183
let mut chunk_lock = chunk.lock().unwrap();
7284
std::mem::drop(block_borrow);
7385
chunk_lock.add_block(block.clone());
74-
chunk_lock.build_mesh(self.get_other_chunks(chunk_index));
86+
chunk_lock.build_mesh(self.get_other_chunks(chunk));
7587

7688
let block_borrow = block.lock().unwrap();
7789
let block_neighbour_chunks = block_borrow.get_neighbour_chunks_coords();
7890
std::mem::drop(chunk_lock);
7991

8092
if block_neighbour_chunks.len() > 0 {
8193
for neighbour_chunk in block_neighbour_chunks {
82-
let (neighbour_index, neighbour_chunk) = self
94+
let neighbour_chunk = self
8395
.chunks
8496
.iter()
85-
.enumerate()
86-
.find_map(|(i, o)| {
97+
.find(|o| {
8798
let c = o.lock().unwrap();
88-
return if c.x == neighbour_chunk.0 && c.y == neighbour_chunk.1 {
89-
Some((i, o))
90-
} else {
91-
None
92-
};
99+
return c.x == neighbour_chunk.0 && c.y == neighbour_chunk.1;
93100
})
94101
.expect("Cannot destroy a block without neighbour being loaded");
95102

96-
let mut neighbour_chunk = neighbour_chunk.lock().unwrap();
97-
98-
neighbour_chunk.build_mesh(self.get_other_chunks(neighbour_index));
103+
let mut neighbour_chunkbrw = neighbour_chunk.lock().unwrap();
104+
neighbour_chunkbrw.build_mesh(self.get_other_chunks(neighbour_chunk));
99105
}
100106
}
101107

@@ -104,11 +110,10 @@ impl World {
104110
pub fn remove_block(&mut self, block: Arc<Mutex<Block>>) {
105111
let block_borrow = block.lock().unwrap();
106112
let chunk_coords = block_borrow.get_chunk_coords();
107-
let (chunk_index, chunk) = self
113+
let chunk = self
108114
.chunks
109115
.iter()
110-
.enumerate()
111-
.find(|(_, c)| {
116+
.find(|c| {
112117
let c = c.lock().unwrap();
113118
c.x == chunk_coords.0 && c.y == chunk_coords.1
114119
})
@@ -117,31 +122,26 @@ impl World {
117122
let mut chunk_lock = chunk.lock().unwrap();
118123
chunk_lock.remove_block(&(block_borrow.position));
119124

120-
chunk_lock.build_mesh(self.get_other_chunks(chunk_index));
125+
chunk_lock.build_mesh(self.get_other_chunks(chunk));
121126
let block_neighbour_chunks = block_borrow.get_neighbour_chunks_coords();
122127

123128
// I hate this so much
124129
std::mem::drop(chunk_lock);
125130

126131
if block_neighbour_chunks.len() > 0 {
127132
for neighbour_chunk in block_neighbour_chunks {
128-
let (neighbour_index, neighbour_chunk) = self
133+
let neighbour_chunk = self
129134
.chunks
130135
.iter()
131-
.enumerate()
132-
.find_map(|(i, o)| {
136+
.find(|o| {
133137
let c = o.lock().unwrap();
134-
return if c.x == neighbour_chunk.0 && c.y == neighbour_chunk.1 {
135-
Some((i, o))
136-
} else {
137-
None
138-
};
138+
c.x == neighbour_chunk.0 && c.y == neighbour_chunk.1
139139
})
140140
.expect("Cannot destroy a block without neighbour being loaded");
141141

142-
let mut neighbour_chunk = neighbour_chunk.lock().unwrap();
142+
let mut neighbour_chunkbrw = neighbour_chunk.lock().unwrap();
143143

144-
neighbour_chunk.build_mesh(self.get_other_chunks(neighbour_index));
144+
neighbour_chunkbrw.build_mesh(self.get_other_chunks(neighbour_chunk));
145145
}
146146
}
147147
}
@@ -277,6 +277,7 @@ impl World {
277277
let chunk = receiver.recv().unwrap();
278278
self.chunks.push(Arc::new(Mutex::new(chunk)));
279279
}
280+
self.handle_outside_blocks();
280281
}
281282

282283
player.current_chunk = current_chunk;
@@ -306,14 +307,45 @@ impl World {
306307
});
307308
}
308309
}
309-
println!("END CHUNK");
310310

311311
let mut chunks = vec![];
312312
for _ in 0..CHUNKS_PER_ROW * CHUNKS_PER_ROW {
313313
let chunk = receiver.recv().unwrap();
314314
chunks.push(Arc::new(Mutex::new(chunk)));
315315
}
316-
self.chunks.append(&mut chunks);
316+
self.chunks.append(&mut chunks); // Add chunks to self
317+
318+
self.handle_outside_blocks();
319+
}
320+
fn handle_outside_blocks(&mut self) {
321+
let mut blocks_to_add = vec![];
322+
for chunk in self.chunks.iter() {
323+
let mut chunkbrw = chunk.lock().unwrap();
324+
blocks_to_add.append(&mut chunkbrw.outside_blocks);
325+
}
326+
327+
let mut chunks_to_rerender = vec![];
328+
329+
for block in blocks_to_add.iter() {
330+
let chunk_coords = block.lock().unwrap().get_chunk_coords();
331+
if let Some(chunkptr) = self.chunks.iter().find(|c| {
332+
let c = c.lock().unwrap();
333+
c.x == chunk_coords.0 && c.y == chunk_coords.1
334+
}) {
335+
let mut chunkbrw = chunkptr.lock().unwrap();
336+
chunkbrw.add_block(block.clone());
337+
if let None = chunks_to_rerender.iter().find(|c| Arc::ptr_eq(c, chunkptr)) {
338+
chunks_to_rerender.push(chunkptr.clone());
339+
};
340+
}
341+
}
342+
343+
// This should really be multi threadable imo..
344+
for chunk in chunks_to_rerender.iter() {
345+
let other_chunks = self.get_other_chunks(chunk);
346+
let mut chunk = chunk.lock().unwrap();
347+
chunk.build_mesh(other_chunks);
348+
}
317349
}
318350
pub fn init_world(device: Arc<wgpu::Device>, queue: Arc<wgpu::Queue>) -> Self {
319351
let noise_data = Arc::new(crate::utils::noise::create_world_noise_data(

0 commit comments

Comments
 (0)