Skip to content

Commit c14887b

Browse files
committed
refactor: removing blocks across borders, not fully working
1 parent 7726f0f commit c14887b

File tree

7 files changed

+326
-134
lines changed

7 files changed

+326
-134
lines changed

src/blocks/block.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use bytemuck::{Pod, Zeroable};
22

33
use super::block_type::BlockType;
4+
use crate::world::CHUNK_SIZE;
45
use std::{
56
cell::RefCell,
67
rc::Rc,
@@ -11,6 +12,8 @@ pub struct BlockFace {
1112
pub face_direction: FaceDirections,
1213
pub block: Weak<Mutex<Block>>,
1314
}
15+
16+
#[derive(Debug)]
1417
pub struct Block {
1518
pub position: glam::Vec3,
1619
pub absolute_position: glam::Vec3,
@@ -25,7 +28,6 @@ pub const CUBE_VERTEX: [f32; 24] = [
2528
-0.5, 0.5, -0.5,
2629
0.5, 0.5, -0.5,
2730
0.5, -0.5, -0.5,
28-
2931
-0.5, -0.5, 0.5,
3032
-0.5, 0.5, 0.5,
3133
0.5, 0.5, 0.5,
@@ -45,6 +47,7 @@ pub enum FaceDirections {
4547
Top,
4648
Bottom,
4749
}
50+
4851
impl FaceDirections {
4952
pub fn create_face_data(
5053
&self,
@@ -148,6 +151,36 @@ pub struct BlockVertexData {
148151
}
149152

150153
impl Block {
154+
pub fn get_neighbour_chunks_coords(&self) -> Vec<(i32, i32)> {
155+
let chunk = self.get_chunk_coords();
156+
let mut neighbour_chunks = vec![];
157+
158+
if self.position.x == 15.0 {
159+
neighbour_chunks.push((chunk.0 + 1, chunk.1));
160+
}
161+
if self.position.x == 0.0 {
162+
neighbour_chunks.push((chunk.0 - 1, chunk.1));
163+
}
164+
if self.position.z == 15.0 {
165+
neighbour_chunks.push((chunk.0, chunk.1 + 1));
166+
}
167+
if self.position.z == 0.0 {
168+
neighbour_chunks.push((chunk.0, chunk.1 - 1));
169+
}
170+
return neighbour_chunks;
171+
}
172+
pub fn is_on_chunk_border(&self) -> bool {
173+
return self.position.x == 0.0
174+
|| self.position.x == 15.0
175+
|| self.position.z == 0.0
176+
|| self.position.z == 15.0;
177+
}
178+
pub fn get_chunk_coords(&self) -> (i32, i32) {
179+
return (
180+
(f32::floor(self.absolute_position.x / CHUNK_SIZE as f32)) as i32,
181+
(f32::floor(self.absolute_position.z / CHUNK_SIZE as f32)) as i32,
182+
);
183+
}
151184
pub fn get_vertex_data_layout() -> wgpu::VertexBufferLayout<'static> {
152185
wgpu::VertexBufferLayout {
153186
array_stride: std::mem::size_of::<BlockVertexData>() as wgpu::BufferAddress,
@@ -175,6 +208,7 @@ impl Block {
175208
}
176209
}
177210
}
211+
178212
impl FaceDirections {
179213
pub fn all() -> [FaceDirections; 6] {
180214
[

src/chunk.rs

Lines changed: 119 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
use std::sync::{Arc, Mutex};
1+
use glam::Vec3;
2+
use std::ffi::c_void;
3+
use std::sync::{Arc, Mutex, Weak};
24

35
use wgpu::util::DeviceExt;
46

7+
use crate::world::World;
58
use crate::{
69
blocks::{
710
block::{Block, BlockFace, BlockVertexData, FaceDirections},
@@ -10,40 +13,49 @@ use crate::{
1013
world::{NoiseData, CHUNK_HEIGHT, CHUNK_SIZE, NOISE_CHUNK_PER_ROW, NOISE_SIZE},
1114
};
1215

13-
pub type BlockVec = Vec<Vec<Arc<Mutex<Block>>>>;
16+
pub type BlockVec = Vec<Vec<Option<Arc<Mutex<Block>>>>>;
17+
1418
pub struct Chunk {
1519
// probably there needs to be a cube type with more info ( regarding type, etc. )
1620
pub x: i32,
1721
pub y: i32,
1822
pub blocks: BlockVec,
1923
pub indices: u32,
24+
pub device: Arc<wgpu::Device>,
25+
pub queue: Arc<wgpu::Queue>,
26+
pub noise_data: Arc<NoiseData>,
2027
pub chunk_bind_group: wgpu::BindGroup,
2128
pub chunk_position_buffer: wgpu::Buffer,
22-
// pub chunk_vertex_buffer: wgpu::Buffer,
23-
pub chunk_index_buffer: wgpu::Buffer,
24-
pub chunk_vertex_buffer: wgpu::Buffer,
29+
pub chunk_index_buffer: Option<wgpu::Buffer>,
30+
pub chunk_vertex_buffer: Option<wgpu::Buffer>,
2531
}
2632

2733
impl Chunk {
34+
pub fn remove_block(&mut self, block_r_position: &Vec3) {
35+
let y_blocks = self
36+
.blocks
37+
.get_mut(((block_r_position.x * CHUNK_SIZE as f32) + block_r_position.z) as usize)
38+
.expect("Cannot delete oob block");
39+
y_blocks[block_r_position.y as usize] = None;
40+
}
2841
pub fn exists_block_at(blocks: &BlockVec, position: &glam::Vec3) -> bool {
2942
if let Some(y_blocks) =
3043
blocks.get(((position.x as u32 * CHUNK_SIZE) + position.z as u32) as usize)
3144
{
32-
if let Some(_) = y_blocks.get(position.y as usize) {
33-
return true;
34-
} else {
35-
return false;
45+
if let Some(block_opt) = y_blocks.get(position.y as usize) {
46+
if let Some(_) = block_opt {
47+
return true;
48+
}
3649
}
37-
} else {
38-
return false;
39-
};
50+
}
51+
return false;
4052
}
4153
pub fn get_block_at_relative(&self, position: &glam::Vec3) -> Option<Arc<Mutex<Block>>> {
4254
if let Some(y_blocks) = self
4355
.blocks
4456
.get(((position.x * CHUNK_SIZE as f32) + position.z) as usize)
4557
{
46-
if let Some(block) = y_blocks.get(position.y as usize) {
58+
if let Some(block) = y_blocks.get(position.y as usize)? {
4759
return Some(Arc::clone(block));
4860
}
4961
}
@@ -67,87 +79,103 @@ impl Chunk {
6779
false
6880
}
6981
}
70-
// Returns the number of indices added to the chunk - it would've been better to be a mutable method but i can't do it because of borrow checker
71-
pub fn build_mesh(
72-
chunk_x: i32,
73-
chunk_y: i32,
74-
blocks: &BlockVec,
75-
noise_data: Arc<NoiseData>,
76-
device: Arc<wgpu::Device>,
77-
) -> (u32, wgpu::Buffer, wgpu::Buffer) {
82+
pub fn build_mesh(&mut self, other_chunks: Vec<Arc<Mutex<Chunk>>>) {
7883
let mut vertex: Vec<BlockVertexData> = vec![];
7984
let mut indices: Vec<u32> = vec![];
8085
for x in 0..CHUNK_SIZE {
8186
for z in 0..CHUNK_SIZE {
82-
let region = &blocks[(x * CHUNK_SIZE + z) as usize];
87+
let region = &self.blocks[(x * CHUNK_SIZE + z) as usize];
8388
for y in 0..region.len() {
8489
let block = &region[y];
85-
let block = block.lock().unwrap();
86-
let position = block.position;
87-
let faces = block.faces.as_ref().unwrap();
90+
if let Some(block) = block {
91+
let block = block.lock().unwrap();
92+
let position = block.position;
93+
let faces = block.faces.as_ref().unwrap();
8894

89-
for face in faces.iter() {
90-
let mut is_visible = true;
91-
let face_position = face.get_normal_vector() + position;
95+
for face in faces.iter() {
96+
let mut is_visible = true;
97+
let face_position = face.get_normal_vector() + position;
9298

93-
if Chunk::is_outside_bounds(&face_position) {
94-
is_visible = false;
95-
} else if Chunk::is_outside_chunk(&face_position) {
96-
let target_chunk_x =
97-
chunk_x + (f32::floor(face_position.x / CHUNK_SIZE as f32) as i32);
98-
let target_chunk_y =
99-
chunk_y + (f32::floor(face_position.z / CHUNK_SIZE as f32) as i32);
99+
if Chunk::is_outside_bounds(&face_position) {
100+
is_visible = false;
101+
} else if Chunk::is_outside_chunk(&face_position) {
102+
let target_chunk_x = self.x
103+
+ (f32::floor(face_position.x / CHUNK_SIZE as f32) as i32);
104+
let target_chunk_y = self.y
105+
+ (f32::floor(face_position.z / CHUNK_SIZE as f32) as i32);
100106

101-
let target_block = glam::vec3(
102-
(face_position.x + CHUNK_SIZE as f32) % CHUNK_SIZE as f32,
103-
face_position.y,
104-
(face_position.z + CHUNK_SIZE as f32) % CHUNK_SIZE as f32,
105-
);
107+
let target_block = glam::vec3(
108+
(face_position.x + CHUNK_SIZE as f32) % CHUNK_SIZE as f32,
109+
face_position.y,
110+
(face_position.z + CHUNK_SIZE as f32) % CHUNK_SIZE as f32,
111+
);
106112

107-
// This probably needs to be looked at again when the blocks can be placed/destroyed
108-
if face_position.y as u32
109-
<= Chunk::get_height_value(
110-
target_chunk_x,
111-
target_chunk_y,
112-
target_block.x as u32,
113-
target_block.z as u32,
114-
noise_data.clone(),
115-
)
116-
{
117-
is_visible = false
118-
};
119-
} else if Chunk::exists_block_at(&blocks, &face_position) {
120-
is_visible = false;
121-
}
113+
let target_chunk = other_chunks.iter().find(|c| {
114+
let c = c.lock().unwrap();
115+
c.x == target_chunk_x && c.y == target_chunk_y
116+
});
117+
match target_chunk {
118+
Some(chunk) => {
119+
println!("TARGET CHUNK FOUND");
120+
let chunk = chunk.lock().unwrap();
121+
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+
);
126+
is_visible = false;
127+
}
128+
}
129+
None => {
130+
if face_position.y as u32
131+
<= Chunk::get_height_value(
132+
target_chunk_x,
133+
target_chunk_y,
134+
target_block.x as u32,
135+
target_block.z as u32,
136+
self.noise_data.clone(),
137+
)
138+
{
139+
is_visible = false
140+
};
141+
}
142+
}
143+
} else if Chunk::exists_block_at(&self.blocks, &face_position) {
144+
is_visible = false;
145+
}
122146

123-
if is_visible {
124-
let (mut vertex_data, index_data) = face.create_face_data(&block);
125-
vertex.append(&mut vertex_data);
126-
let indices_offset = vertex.len() as u32 - 4;
127-
indices.append(
128-
&mut index_data.iter().map(|i| i + indices_offset).collect(),
129-
)
147+
if is_visible {
148+
let (mut vertex_data, index_data) = face.create_face_data(&block);
149+
vertex.append(&mut vertex_data);
150+
let indices_offset = vertex.len() as u32 - 4;
151+
indices.append(
152+
&mut index_data.iter().map(|i| i + indices_offset).collect(),
153+
)
154+
}
130155
}
131156
}
132157
}
133158
}
134159
}
135160

136-
let chunk_vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
137-
contents: bytemuck::cast_slice(&vertex),
138-
label: Some(&format!("chunk-vertex-{chunk_x}-{chunk_y}")),
139-
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
140-
});
141-
let chunk_index_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
142-
contents: bytemuck::cast_slice(&indices),
143-
label: Some(&format!("chunk-index-{chunk_x}-{chunk_y}")),
144-
usage: wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST,
145-
});
146-
return (
147-
indices.len() as u32,
148-
chunk_vertex_buffer,
149-
chunk_index_buffer,
150-
);
161+
let chunk_vertex_buffer =
162+
self.device
163+
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
164+
contents: bytemuck::cast_slice(&vertex),
165+
label: Some(&format!("chunk-vertex-{}-{}", self.x, self.y)),
166+
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
167+
});
168+
let chunk_index_buffer =
169+
self.device
170+
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
171+
contents: bytemuck::cast_slice(&indices),
172+
label: Some(&format!("chunk-vertex-{}-{}", self.x, self.y)),
173+
usage: wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST,
174+
});
175+
176+
self.indices = indices.len() as u32;
177+
self.chunk_vertex_buffer = Some(chunk_vertex_buffer);
178+
self.chunk_index_buffer = Some(chunk_index_buffer);
151179
}
152180
pub fn get_bind_group_layout() -> wgpu::BindGroupLayoutDescriptor<'static> {
153181
wgpu::BindGroupLayoutDescriptor {
@@ -164,6 +192,7 @@ impl Chunk {
164192
}],
165193
}
166194
}
195+
167196
pub fn get_height_value(
168197
chunk_x: i32,
169198
chunk_y: i32,
@@ -218,7 +247,7 @@ impl Chunk {
218247

219248
block.lock().unwrap().faces = Some(face_directions);
220249
let curr = &mut blocks[((x * CHUNK_SIZE) + z) as usize];
221-
curr.push(block.clone());
250+
curr.push(Some(block.clone()));
222251
}
223252
}
224253
}
@@ -233,10 +262,9 @@ impl Chunk {
233262
device: Arc<wgpu::Device>,
234263
queue: Arc<wgpu::Queue>,
235264
chunk_data_layout: Arc<wgpu::BindGroupLayout>,
265+
other_chunks: Vec<Arc<Mutex<Chunk>>>,
236266
) -> Self {
237267
let blocks = Self::create_blocks_data(x, y, noise_data.clone());
238-
let (indices, chunk_vertex_buffer, chunk_index_buffer) =
239-
Self::build_mesh(x, y, &blocks, noise_data.clone(), device.clone());
240268

241269
let chunk_position_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
242270
contents: bytemuck::cast_slice(&[x, y]),
@@ -253,15 +281,21 @@ impl Chunk {
253281
}],
254282
});
255283

256-
return Chunk {
284+
let mut chunk = Chunk {
285+
blocks,
257286
x,
258287
y,
259-
blocks,
288+
device,
289+
queue,
290+
noise_data,
291+
chunk_vertex_buffer: None,
292+
chunk_index_buffer: None,
260293
chunk_bind_group,
261-
chunk_index_buffer,
262294
chunk_position_buffer,
263-
chunk_vertex_buffer,
264-
indices,
295+
indices: 0,
265296
};
297+
chunk.build_mesh(other_chunks);
298+
299+
return chunk;
266300
}
267301
}

src/collision.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct CollisionPoint {
1515
pub y: f32,
1616
pub z: f32,
1717
}
18+
1819
pub struct Ray {
1920
pub origin: glam::Vec3,
2021
pub direction: glam::Vec3,
@@ -85,10 +86,12 @@ impl Ray {
8586
]);
8687
}
8788
}
89+
8890
pub struct RayResult<'a> {
8991
pub points: Vec<glam::Vec3>,
9092
pub collision: &'a CollisionBox,
9193
}
94+
9295
impl CollisionPoint {
9396
pub fn new(x: f32, y: f32, z: f32) -> CollisionPoint {
9497
CollisionPoint { x, y, z }

0 commit comments

Comments
 (0)