Skip to content

Commit fea3ffe

Browse files
committed
feat: removing blocks
1 parent c14887b commit fea3ffe

File tree

3 files changed

+48
-61
lines changed

3 files changed

+48
-61
lines changed

src/chunk.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,8 @@ impl Chunk {
116116
});
117117
match target_chunk {
118118
Some(chunk) => {
119-
println!("TARGET CHUNK FOUND");
120119
let chunk = chunk.lock().unwrap();
121120
if Chunk::exists_block_at(&chunk.blocks, &target_block) {
122-
println!(
123-
"TARGET BLOCK TRUE {:?} {} {}",
124-
target_block, target_chunk_x, target_chunk_y
125-
);
126121
is_visible = false;
127122
}
128123
}

src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
5454
size.width = size.width.max(1);
5555
size.width = size.height.max(1);
5656

57+
window.set_cursor_grab(CursorGrabMode::Confined).unwrap();
58+
window.set_cursor_visible(false);
5759
let window = Arc::new(Mutex::new(window));
5860
let mut state = State::new(window.clone()).await;
5961

62+
6063
let mut prev_mouse_pos = glam::vec2(0.0, 0.0);
6164
let mut cursor_in = false;
6265

src/world.rs

Lines changed: 45 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -43,72 +43,61 @@ pub struct World {
4343
}
4444

4545
impl World {
46+
// gets all the chunks except the one passed in the index
47+
pub fn get_other_chunks(&self, chunk_index: usize) -> Vec<Arc<Mutex<Chunk>>> {
48+
self.chunks
49+
.iter()
50+
.enumerate()
51+
.filter_map(|(i, c)| {
52+
return if i != chunk_index {
53+
Some(c.clone())
54+
} else {
55+
None
56+
};
57+
})
58+
.collect()
59+
}
4660
pub fn remove_block(&mut self, block: Arc<Mutex<Block>>) {
47-
let blockbrw = block.lock().unwrap();
48-
// TODO: Handle block on chunk border case
49-
let chunk = blockbrw.get_chunk_coords();
50-
let chunk = self
61+
let block_borrow = block.lock().unwrap();
62+
let chunk_coords = block_borrow.get_chunk_coords();
63+
let (chunk_index, chunk) = self
5164
.chunks
5265
.iter()
53-
.find(|c| {
66+
.enumerate()
67+
.find(|(i, c)| {
5468
let c = c.lock().unwrap();
55-
return c.x == chunk.0 && c.y == chunk.1;
69+
return c.x == chunk_coords.0 && c.y == chunk_coords.1;
5670
})
5771
.expect("Cannot delete a block from unloaded chunk");
5872

59-
let mut chunklock = chunk.lock().unwrap();
60-
let chunkcoords = (chunklock.x, chunklock.y);
61-
println!(
62-
"\n\n JUST DELETED {:?} {:?}",
63-
blockbrw.position, chunkcoords
64-
);
65-
chunklock.remove_block(&(blockbrw.position));
66-
std::mem::drop(chunklock);
73+
let mut chunk_lock = chunk.lock().unwrap();
74+
chunk_lock.remove_block(&(block_borrow.position));
6775

68-
let other_chunks = self
69-
.chunks
70-
.iter()
71-
.filter(|c| {
72-
let c = c.lock().unwrap();
73-
c.x != chunkcoords.0 && c.y != chunkcoords.1
74-
})
75-
.map(|p| p.clone())
76-
.collect::<Vec<_>>();
76+
chunk_lock.build_mesh(self.get_other_chunks(chunk_index));
77+
let block_neighbour_chunks = block_borrow.get_neighbour_chunks_coords();
7778

78-
let mut chunklock = chunk.lock().unwrap();
79-
chunklock.build_mesh(other_chunks);
8079
// I hate this so much
81-
82-
let neighbour_chunks = blockbrw.get_neighbour_chunks_coords();
83-
std::mem::drop(chunklock);
84-
85-
println!("NEIGHBOUR CHUNKS {:?}", neighbour_chunks);
86-
87-
if neighbour_chunks.len() > 0 {
88-
for neighbour_chunk in neighbour_chunks {
89-
let neigh_chunk = self.chunks.iter().enumerate().find_map(|(i, o)| {
90-
let c = o.lock().unwrap();
91-
return if c.x == neighbour_chunk.0 && c.y == neighbour_chunk.1 {
92-
Some((i, o))
93-
} else {
94-
None
95-
};
96-
});
97-
98-
if let Some((pi, chunk)) = neigh_chunk {
99-
let mut neighbour_chunk = chunk.lock().unwrap();
100-
let other_chunks = self
101-
.chunks
102-
.iter()
103-
.enumerate()
104-
.filter_map(|(i, o)| {
105-
return if i != pi { Some(o.clone()) } else { None };
106-
})
107-
.collect::<Vec<_>>();
108-
println!("OTHER CHUNKS {}", other_chunks.len());
109-
110-
neighbour_chunk.build_mesh(other_chunks);
111-
}
80+
std::mem::drop(chunk_lock);
81+
82+
if block_neighbour_chunks.len() > 0 {
83+
for neighbour_chunk in block_neighbour_chunks {
84+
let (neighbour_index, neighbour_chunk) = self
85+
.chunks
86+
.iter()
87+
.enumerate()
88+
.find_map(|(i, o)| {
89+
let c = o.lock().unwrap();
90+
return if c.x == neighbour_chunk.0 && c.y == neighbour_chunk.1 {
91+
Some((i, o))
92+
} else {
93+
None
94+
};
95+
})
96+
.expect("Cannot destroy a block without neighbour being loaded");
97+
98+
let mut neighbour_chunk = neighbour_chunk.lock().unwrap();
99+
100+
neighbour_chunk.build_mesh(self.get_other_chunks(neighbour_index));
112101
}
113102
}
114103
}

0 commit comments

Comments
 (0)