Skip to content

Commit aba4d97

Browse files
committed
feat: placing blocks
1 parent fea3ffe commit aba4d97

File tree

8 files changed

+150
-353
lines changed

8 files changed

+150
-353
lines changed

src/blocks/block.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,18 @@ use bytemuck::{Pod, Zeroable};
22

33
use super::block_type::BlockType;
44
use crate::world::CHUNK_SIZE;
5+
use glam::Vec3;
56
use std::{
67
cell::RefCell,
78
rc::Rc,
89
sync::{Arc, Mutex, MutexGuard, Weak},
910
};
1011

11-
pub struct BlockFace {
12-
pub face_direction: FaceDirections,
13-
pub block: Weak<Mutex<Block>>,
14-
}
15-
1612
#[derive(Debug)]
1713
pub struct Block {
1814
pub position: glam::Vec3,
1915
pub absolute_position: glam::Vec3,
20-
pub faces: Option<Vec<FaceDirections>>,
2116
pub block_type: BlockType,
22-
pub is_translucent: bool,
2317
}
2418

2519
#[rustfmt::skip]
@@ -151,6 +145,18 @@ pub struct BlockVertexData {
151145
}
152146

153147
impl Block {
148+
pub fn new(position: Vec3, chunk: (i32, i32), block_type: BlockType) -> Block {
149+
let absolute_position = glam::vec3(
150+
(chunk.0 * CHUNK_SIZE as i32 + position.x as i32) as f32,
151+
position.y,
152+
(chunk.1 * CHUNK_SIZE as i32 + position.z as i32) as f32,
153+
);
154+
Block {
155+
position,
156+
block_type,
157+
absolute_position,
158+
}
159+
}
154160
pub fn get_neighbour_chunks_coords(&self) -> Vec<(i32, i32)> {
155161
let chunk = self.get_chunk_coords();
156162
let mut neighbour_chunks = vec![];

src/chunk.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use wgpu::util::DeviceExt;
77
use crate::world::World;
88
use crate::{
99
blocks::{
10-
block::{Block, BlockFace, BlockVertexData, FaceDirections},
10+
block::{Block, BlockVertexData, FaceDirections},
1111
block_type::BlockType,
1212
},
1313
world::{NoiseData, CHUNK_HEIGHT, CHUNK_SIZE, NOISE_CHUNK_PER_ROW, NOISE_SIZE},
@@ -31,6 +31,26 @@ pub struct Chunk {
3131
}
3232

3333
impl Chunk {
34+
pub fn add_block(&mut self, block: Arc<Mutex<Block>>) {
35+
let block_borrow = block.lock().unwrap();
36+
let y_blocks = self
37+
.blocks
38+
.get_mut(
39+
((block_borrow.position.x * CHUNK_SIZE as f32) + block_borrow.position.z) as usize,
40+
)
41+
.expect("Cannot add oob block");
42+
43+
println!("ADD BLOCK");
44+
let start_len = y_blocks.len();
45+
46+
for i in start_len..block_borrow.position.y as usize {
47+
println!("LOLL");
48+
if i >= y_blocks.len() {
49+
y_blocks.push(None);
50+
}
51+
}
52+
y_blocks.push(Some(block.clone()));
53+
}
3454
pub fn remove_block(&mut self, block_r_position: &Vec3) {
3555
let y_blocks = self
3656
.blocks
@@ -90,7 +110,7 @@ impl Chunk {
90110
if let Some(block) = block {
91111
let block = block.lock().unwrap();
92112
let position = block.position;
93-
let faces = block.faces.as_ref().unwrap();
113+
let faces = FaceDirections::all();
94114

95115
for face in faces.iter() {
96116
let mut is_visible = true;
@@ -225,22 +245,12 @@ impl Chunk {
225245
b => b,
226246
};
227247

228-
let block = Arc::new(Mutex::new(Block {
229-
faces: None,
230-
position: glam::vec3(x as f32, y as f32, z as f32),
231-
absolute_position: glam::vec3(
232-
(chunk_x * CHUNK_SIZE as i32 + x as i32) as f32,
233-
y as f32,
234-
(chunk_y * CHUNK_SIZE as i32 + z as i32) as f32,
235-
),
248+
let block = Arc::new(Mutex::new(Block::new(
249+
glam::vec3(x as f32, y as f32, z as f32),
250+
(chunk_x, chunk_y),
236251
block_type,
237-
is_translucent: false,
238-
}));
239-
240-
let face_directions =
241-
FaceDirections::all().iter().map(|f| *f).collect::<Vec<_>>();
252+
)));
242253

243-
block.lock().unwrap().faces = Some(face_directions);
244254
let curr = &mut blocks[((x * CHUNK_SIZE) + z) as usize];
245255
curr.push(Some(block.clone()));
246256
}

src/main.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use std::{
1111
use bytemuck::{Pod, Zeroable};
1212
use glam::vec2;
1313
use material::Texture;
14-
use model::VertexData;
1514
use player::CameraController;
1615
use state::State;
1716
use tobj::{load_obj, load_obj_buf, LoadOptions};
@@ -35,7 +34,6 @@ pub mod blocks;
3534
pub mod chunk;
3635
pub mod collision;
3736
pub mod material;
38-
pub mod model;
3937
pub mod pipeline;
4038
pub mod player;
4139
pub mod state;
@@ -59,7 +57,6 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
5957
let window = Arc::new(Mutex::new(window));
6058
let mut state = State::new(window.clone()).await;
6159

62-
6360
let mut prev_mouse_pos = glam::vec2(0.0, 0.0);
6461
let mut cursor_in = false;
6562

0 commit comments

Comments
 (0)