Skip to content

Commit 8933f36

Browse files
committed
trees
1 parent f132960 commit 8933f36

File tree

5 files changed

+113
-32
lines changed

5 files changed

+113
-32
lines changed

src/chunk.rs

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use crate::{
33
block::{Block, BlockVertexData, FaceDirections},
44
block_type::BlockType,
55
},
6-
world::{NoiseData, CHUNK_SIZE, NOISE_CHUNK_PER_ROW, NOISE_SIZE},
6+
structures::Structure,
7+
utils::{ChunkFromPosition, RelativeFromAbsolute},
8+
world::{NoiseData, CHUNK_SIZE, MAX_TREES_PER_CHUNK, NOISE_CHUNK_PER_ROW, NOISE_SIZE},
79
};
810
use glam::Vec3;
911
use std::sync::{Arc, Mutex};
@@ -23,11 +25,13 @@ pub struct Chunk {
2325
pub chunk_position_buffer: wgpu::Buffer,
2426
pub chunk_index_buffer: Option<wgpu::Buffer>,
2527
pub chunk_vertex_buffer: Option<wgpu::Buffer>,
28+
pub outside_blocks: Vec<Arc<Mutex<Block>>>,
2629
}
2730

2831
impl Chunk {
2932
pub fn add_block(&mut self, block: Arc<Mutex<Block>>) {
3033
let block_borrow = block.lock().unwrap();
34+
3135
let y_blocks = self
3236
.blocks
3337
.get_mut(
@@ -254,37 +258,39 @@ impl Chunk {
254258
blocks
255259
}
256260
pub fn place_trees(&mut self) {
257-
let yblocks = self.blocks.get(&(7 * CHUNK_SIZE as usize) + 7).unwrap();
258-
let highest_block = yblocks
259-
.last()
260-
.as_ref()
261-
.unwrap()
262-
.as_ref()
263-
.unwrap()
264-
.lock()
265-
.unwrap()
266-
.position
267-
.clone();
268-
269-
let base0 = Arc::new(Mutex::new(Block::new(
270-
highest_block + glam::vec3(0.0, 1.0, 0.0),
271-
(self.x, self.y),
272-
BlockType::wood(),
273-
)));
274-
let base1 = Arc::new(Mutex::new(Block::new(
275-
highest_block + glam::vec3(0.0, 2.0, 0.0),
276-
(self.x, self.y),
277-
BlockType::wood(),
278-
)));
279-
let base2 = Arc::new(Mutex::new(Block::new(
280-
highest_block + glam::vec3(0.0, 3.0, 0.0),
281-
(self.x, self.y),
282-
BlockType::wood(),
283-
)));
284-
285-
self.add_block(base0);
286-
self.add_block(base1);
287-
self.add_block(base2);
261+
let number_of_trees = rand::random::<f32>();
262+
let number_of_trees = f32::floor(number_of_trees * MAX_TREES_PER_CHUNK as f32) as u32;
263+
264+
for i in 0..number_of_trees {
265+
let x = f32::floor(rand::random::<f32>() * CHUNK_SIZE as f32) as usize;
266+
let z = f32::floor(rand::random::<f32>() * CHUNK_SIZE as f32) as usize;
267+
268+
let block_column = self
269+
.blocks
270+
.get((x * CHUNK_SIZE as usize) + z)
271+
.expect("TODO: fix this case");
272+
let highest_block = block_column
273+
.last()
274+
.expect("TODO: Fix this case -h")
275+
.as_ref()
276+
.unwrap()
277+
.lock()
278+
.unwrap()
279+
.absolute_position;
280+
281+
let tree_blocks = crate::structures::Tree::get_blocks(highest_block);
282+
283+
for block in tree_blocks.iter() {
284+
let block_brw = block.lock().unwrap();
285+
let block_chunk = block_brw.get_chunk_coords();
286+
std::mem::drop(block_brw);
287+
if block_chunk == (self.x, self.y) {
288+
self.add_block(block.clone());
289+
} else {
290+
self.outside_blocks.push(block.clone())
291+
}
292+
}
293+
}
288294
}
289295

290296
pub fn new(
@@ -325,6 +331,7 @@ impl Chunk {
325331
chunk_bind_group,
326332
chunk_position_buffer,
327333
indices: 0,
334+
outside_blocks: vec![],
328335
};
329336
chunk.place_trees();
330337
chunk.build_mesh(other_chunks);

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub mod material;
3737
pub mod pipeline;
3838
pub mod player;
3939
pub mod state;
40+
pub mod structures;
4041
pub mod ui;
4142
pub mod utils;
4243
pub mod world;

src/structures/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pub mod tree;
2+
3+
use std::sync::{Arc, Mutex};
4+
5+
pub trait Structure {
6+
// position: Initial absolute position
7+
fn get_blocks(position: glam::Vec3) -> Vec<Arc<Mutex<Block>>>;
8+
}
9+
pub use tree::Tree;
10+
11+
use crate::blocks::block::Block; // Reexport into structures module

src/structures/tree.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use std::sync::{Arc, Mutex};
2+
3+
use crate::{
4+
blocks::{block::Block, block_type::BlockType},
5+
utils::{ChunkFromPosition, RelativeFromAbsolute},
6+
};
7+
8+
use super::Structure;
9+
10+
pub struct Tree;
11+
12+
impl Structure for Tree {
13+
fn get_blocks(position: glam::Vec3) -> Vec<Arc<Mutex<Block>>> {
14+
let trunk_pos = [
15+
position + glam::vec3(0.0, 1.0, 0.0),
16+
position + glam::vec3(0.0, 2.0, 0.0),
17+
position + glam::vec3(0.0, 3.0, 0.0),
18+
];
19+
20+
#[rustfmt::skip]
21+
let leafs_pos = [
22+
position + glam::vec3(0.0, 3.0, 1.0),
23+
position + glam::vec3(0.0, 4.0, 1.0),
24+
position + glam::vec3(1.0, 3.0, 1.0),
25+
position + glam::vec3(1.0, 4.0, 1.0),
26+
position + glam::vec3(-1.0, 3.0, 1.0),
27+
position + glam::vec3(-1.0, 4.0, 1.0),
28+
29+
position + glam::vec3(0.0, 3.0, -1.0),
30+
position + glam::vec3(0.0, 4.0, -1.0),
31+
position + glam::vec3(1.0, 3.0, -1.0),
32+
position + glam::vec3(1.0, 4.0, -1.0),
33+
position + glam::vec3(-1.0, 3.0, -1.0),
34+
position + glam::vec3(-1.0, 4.0, -1.0),
35+
36+
position + glam::vec3(1.0, 3.0, 0.0),
37+
position + glam::vec3(1.0, 4.0, 0.0),
38+
position + glam::vec3(-1.0, 3.0, 0.0),
39+
position + glam::vec3(-1.0, 4.0, 0.0),
40+
41+
position + glam::vec3(0.0, 5.0, 0.0),
42+
];
43+
44+
let blocks = trunk_pos.iter().map(|p| {
45+
Arc::new(Mutex::new(Block::new(
46+
p.relative_from_absolute(),
47+
p.get_chunk_from_position_absolute(),
48+
BlockType::wood(),
49+
)))
50+
});
51+
let leafs_iter = leafs_pos.iter().map(|p| {
52+
Arc::new(Mutex::new(Block::new(
53+
p.relative_from_absolute(),
54+
p.get_chunk_from_position_absolute(),
55+
BlockType::leaf(),
56+
)))
57+
});
58+
let blocks = blocks.chain(leafs_iter).collect::<Vec<_>>();
59+
return blocks;
60+
}
61+
}

src/world.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub const CHUNK_HEIGHT: u8 = u8::MAX;
1212
pub const NOISE_SIZE: u32 = 1024;
1313
pub const FREQUENCY: f32 = 1. / 128.;
1414
pub const NOISE_CHUNK_PER_ROW: u32 = NOISE_SIZE / CHUNK_SIZE;
15+
pub const MAX_TREES_PER_CHUNK: u32 = 3;
1516
// There will be a CHUNKS_PER_ROW * CHUNKS_PER_ROW region
1617
pub const CHUNKS_PER_ROW: u32 = 9;
1718

0 commit comments

Comments
 (0)