Skip to content

Commit 7726f0f

Browse files
committed
refactor: removed useless field from block struct
1 parent 030aedb commit 7726f0f

File tree

3 files changed

+22
-31
lines changed

3 files changed

+22
-31
lines changed

src/blocks/block.rs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct BlockFace {
1414
pub struct Block {
1515
pub position: glam::Vec3,
1616
pub absolute_position: glam::Vec3,
17-
pub faces: Option<Vec<BlockFace>>,
17+
pub faces: Option<Vec<FaceDirections>>,
1818
pub block_type: BlockType,
1919
pub is_translucent: bool,
2020
}
@@ -45,23 +45,12 @@ pub enum FaceDirections {
4545
Top,
4646
Bottom,
4747
}
48-
49-
#[repr(C)]
50-
#[derive(Pod, Copy, Clone, Zeroable)]
51-
pub struct BlockVertexData {
52-
pub position: [f32; 3],
53-
pub normal: [f32; 3],
54-
pub tex_coords: [f32; 2],
55-
}
56-
57-
impl BlockFace {
48+
impl FaceDirections {
5849
pub fn create_face_data(
5950
&self,
60-
block: &MutexGuard<'_, Block>, // To prevent deadlocks
51+
block: &MutexGuard<'_, Block>,
6152
) -> (Vec<BlockVertexData>, Vec<u32>) {
62-
let face_direction = self.face_direction;
63-
64-
let indices = face_direction.get_indices();
53+
let indices = self.get_indices();
6554

6655
let mut unique_indices: Vec<u32> = Vec::with_capacity(4);
6756

@@ -85,8 +74,8 @@ impl BlockFace {
8574
*indices_map = index_of as u32;
8675
}
8776

88-
let face_texcoords = block.block_type.get_texcoords(face_direction);
89-
let normals = face_direction.get_normal_vector();
77+
let face_texcoords = block.block_type.get_texcoords(*self);
78+
let normals = self.get_normal_vector();
9079

9180
unique_indices.iter().enumerate().for_each(|(i, index)| {
9281
vertex_data.push(BlockVertexData {
@@ -107,9 +96,7 @@ impl BlockFace {
10796
&self,
10897
block: &MutexGuard<'_, Block>, // To prevent deadlocks
10998
) -> (Vec<BlockVertexData>, Vec<u32>) {
110-
let face_direction = self.face_direction;
111-
112-
let indices = face_direction.get_indices();
99+
let indices = self.get_indices();
113100

114101
let mut unique_indices: Vec<u32> = Vec::with_capacity(4);
115102

@@ -133,8 +120,8 @@ impl BlockFace {
133120
*indices_map = index_of as u32;
134121
}
135122

136-
let face_texcoords = block.block_type.get_texcoords(face_direction);
137-
let normals = face_direction.get_normal_vector();
123+
let face_texcoords = block.block_type.get_texcoords(*self);
124+
let normals = self.get_normal_vector();
138125

139126
unique_indices.iter().enumerate().for_each(|(i, index)| {
140127
vertex_data.push(BlockVertexData {
@@ -151,6 +138,15 @@ impl BlockFace {
151138
(vertex_data, indices_map)
152139
}
153140
}
141+
142+
#[repr(C)]
143+
#[derive(Pod, Copy, Clone, Zeroable)]
144+
pub struct BlockVertexData {
145+
pub position: [f32; 3],
146+
pub normal: [f32; 3],
147+
pub tex_coords: [f32; 2],
148+
}
149+
154150
impl Block {
155151
pub fn get_vertex_data_layout() -> wgpu::VertexBufferLayout<'static> {
156152
wgpu::VertexBufferLayout {

src/chunk.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl Chunk {
8888

8989
for face in faces.iter() {
9090
let mut is_visible = true;
91-
let face_position = face.face_direction.get_normal_vector() + position;
91+
let face_position = face.get_normal_vector() + position;
9292

9393
if Chunk::is_outside_bounds(&face_position) {
9494
is_visible = false;
@@ -213,13 +213,8 @@ impl Chunk {
213213
is_translucent: false,
214214
}));
215215

216-
let face_directions = FaceDirections::all()
217-
.iter()
218-
.map(|face_dir| BlockFace {
219-
block: Arc::downgrade(&block),
220-
face_direction: *face_dir,
221-
})
222-
.collect::<Vec<_>>();
216+
let face_directions =
217+
FaceDirections::all().iter().map(|f| *f).collect::<Vec<_>>();
223218

224219
block.lock().unwrap().faces = Some(face_directions);
225220
let curr = &mut blocks[((x * CHUNK_SIZE) + z) as usize];

src/ui.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl UI {
4747
.as_ref()
4848
.unwrap()
4949
.iter()
50-
.find(|f| f.face_direction == player.facing_face.unwrap())
50+
.find(|f| **f == player.facing_face.unwrap())
5151
.unwrap()
5252
.create_face_data_abs(&block);
5353

0 commit comments

Comments
 (0)