Skip to content

Commit 7a42148

Browse files
committed
feat: sand
1 parent 79f886d commit 7a42148

File tree

5 files changed

+44
-17
lines changed

5 files changed

+44
-17
lines changed

assets/tex_atlas.png

210 Bytes
Loading

src/blocks/block_type.rs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
use rand::random;
1+
use rand::{random, rngs::StdRng, Rng, SeedableRng};
22
use std::any::Any;
33

4+
use crate::world::{RNG_SEED, WATER_HEIGHT_LEVEL};
5+
46
use super::block::{FaceDirections, TexturedBlock};
57

68
#[derive(Clone, Copy, Debug)]
@@ -49,6 +51,11 @@ impl BlockTypeConfigs {
4951
textures: [FaceTexture(3), FaceTexture(3), FaceTexture(3)],
5052
is_translucent: false,
5153
},
54+
BlockType::Sand => BlockTypeConfigs {
55+
id: 6,
56+
textures: [FaceTexture(9), FaceTexture(9), FaceTexture(9)],
57+
is_translucent: false,
58+
},
5259
}
5360
}
5461
}
@@ -62,6 +69,7 @@ pub enum BlockType {
6269
Wood,
6370
Leaf,
6471
Stone,
72+
Sand,
6573
}
6674
impl BlockType {
6775
pub fn get_config(&self) -> BlockTypeConfigs {
@@ -78,28 +86,44 @@ impl BlockType {
7886
3 => Self::Wood,
7987
4 => Self::Leaf,
8088
5 => Self::Stone,
89+
6 => Self::Sand,
8190
_ => panic!("Invalid id"),
8291
}
8392
}
8493
}
94+
fn calc_scalar(y: u32, t: Threshold) -> f32 {
95+
(y as f32 - t[0] as f32) / (t[1] as f32 - t[0] as f32)
96+
}
97+
// Threshold: ( lowerbound , upperbound )
98+
type Threshold = [u32; 2];
99+
const STONE_THRESHOLD: Threshold = [15, 24];
100+
const SAND_THRESHOLD: Threshold = [WATER_HEIGHT_LEVEL as u32, WATER_HEIGHT_LEVEL as u32 + 2];
85101
impl BlockType {
86-
const U_STONE_THRESHOLD: u32 = 20;
87-
const L_STONE_THRESHOLD: u32 = 1;
102+
pub fn from_position(x: u32, y: u32, z: u32) -> BlockType {
103+
let mut rng = StdRng::seed_from_u64(RNG_SEED + (y * x * z) as u64);
88104

89-
pub fn from_y_position(y: u32) -> BlockType {
90-
if y > Self::U_STONE_THRESHOLD {
91-
let t: f32 = random();
92-
let scaler = (y as f32 - Self::U_STONE_THRESHOLD as f32) / 10.0;
93-
let res = t + scaler;
94-
if res > 1.0 {
105+
if y <= SAND_THRESHOLD[0] {
106+
BlockType::Sand
107+
} else if y <= SAND_THRESHOLD[1] {
108+
let r = rng.gen::<f32>();
109+
let s = calc_scalar(y, SAND_THRESHOLD);
110+
if r + s > 1.0 {
111+
BlockType::Dirt
112+
} else {
113+
BlockType::Sand
114+
}
115+
} else if y < STONE_THRESHOLD[0] {
116+
BlockType::Dirt
117+
} else if y <= STONE_THRESHOLD[1] {
118+
let r = rng.gen::<f32>();
119+
let s = calc_scalar(y, STONE_THRESHOLD);
120+
if r + s >= 1.0 {
95121
BlockType::Stone
96122
} else {
97123
BlockType::Dirt
98124
}
99-
} else if y < Self::L_STONE_THRESHOLD {
100-
BlockType::Stone
101125
} else {
102-
BlockType::Dirt
126+
BlockType::Stone
103127
}
104128
}
105129
}

src/chunk.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::blocks::block_type::BlockType::Water;
22
use crate::persistence::{Loadable, Saveable};
33
use crate::player::Player;
44
use crate::utils::math_utils::Plane;
5-
use crate::world::{WorldChunk, WATER_HEIGHT_LEVEL};
5+
use crate::world::{WorldChunk, RNG_SEED, WATER_HEIGHT_LEVEL};
66
use crate::{
77
blocks::{
88
block::{Block, BlockVertexData, FaceDirections},
@@ -360,7 +360,7 @@ impl Chunk {
360360
let curr = &mut blocks.write().unwrap()[((x * CHUNK_SIZE) + z) as usize];
361361

362362
for y in 0..=y_top {
363-
let block_type = match BlockType::from_y_position(y) {
363+
let block_type = match BlockType::from_position(x, y, z) {
364364
BlockType::Dirt if y == y_top => BlockType::Grass,
365365
b => b,
366366
};
@@ -391,7 +391,7 @@ impl Chunk {
391391
}
392392
// TODO: Use white noise + check that the tree is not being placed on water.
393393
pub fn place_trees(&mut self) {
394-
let mut rng = StdRng::seed_from_u64((self.x * 10 * self.y) as u64);
394+
let mut rng = StdRng::seed_from_u64((self.x * 10 * self.y) as u64 + RNG_SEED);
395395
let number_of_trees = rng.gen::<f32>();
396396
let mut number_of_trees = f32::floor(number_of_trees * MAX_TREES_PER_CHUNK as f32) as u32;
397397

src/utils.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub(crate) mod math_utils {
2323
pub(crate) mod noise {
2424
use std::fmt::Debug;
2525

26+
use crate::world::RNG_SEED;
27+
2628
use super::*;
2729
use glam::Vec2;
2830

@@ -41,7 +43,7 @@ pub(crate) mod noise {
4143
pub fn shuffle<T: Copy + Debug>(vec: &mut Vec<T>) -> &mut Vec<T> {
4244
use rand::prelude::*;
4345

44-
let mut rng = StdRng::seed_from_u64(0);
46+
let mut rng = StdRng::seed_from_u64(RNG_SEED);
4547

4648
for i in (0..vec.len()).rev() {
4749
let a: usize = if i > 0 {

src/world.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ use crate::persistence::Saveable;
1414
use crate::utils::{ChunkFromPosition, RelativeFromAbsolute};
1515
use crate::{blocks::block::Block, chunk::Chunk, player::Player, utils::threadpool::ThreadPool};
1616

17+
pub const RNG_SEED: u64 = 0;
1718
pub const CHUNK_SIZE: u32 = 16;
1819
pub const CHUNK_HEIGHT: u8 = u8::MAX;
1920
pub const NOISE_SIZE: u32 = 1024;
2021
pub const FREQUENCY: f32 = 1. / 128.;
2122
pub const NOISE_CHUNK_PER_ROW: u32 = NOISE_SIZE / CHUNK_SIZE;
2223
pub const MAX_TREES_PER_CHUNK: u32 = 3;
2324

24-
pub const CHUNKS_PER_ROW: u32 = 25;
25+
pub const CHUNKS_PER_ROW: u32 = 9;
2526
pub const CHUNKS_REGION: u32 = CHUNKS_PER_ROW * CHUNKS_PER_ROW;
2627
pub const WATER_HEIGHT_LEVEL: u8 = 5;
2728

0 commit comments

Comments
 (0)