diff options
Diffstat (limited to 'src/proc_gen/mod.rs')
| -rw-r--r-- | src/proc_gen/mod.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/proc_gen/mod.rs b/src/proc_gen/mod.rs new file mode 100644 index 0000000..8c965fb --- /dev/null +++ b/src/proc_gen/mod.rs @@ -0,0 +1,48 @@ +use fastnoise_lite::FastNoiseLite; +use glam::{IVec2, Vec2}; + +use crate::{ + Config, + config::CHUNK_SIZE, + content::materials::MaterialId, + sim::{cell::Cell, cell_manager::chunk::Chunk}, +}; + +pub struct WorldGenerator { + cave_noise: FastNoiseLite, +} + +impl WorldGenerator { + pub fn generate_chunk(&mut self, chunk_position: IVec2) -> Chunk { + let mut c = Chunk::void(); + let position = chunk_position * CHUNK_SIZE; + for x in 0..CHUNK_SIZE { + for y in 0..CHUNK_SIZE { + let cell_position = position.as_vec2() + Vec2::new(x as f32, y as f32); + let cnv = self + .cave_noise + .get_noise_2d(cell_position.x, cell_position.y); + if cnv < 0.5 { + c.set_cell_at_local_position( + x as u8, + y as u8, + Cell::from_material(MaterialId::Wood), + ); + } + } + } + c + } + + pub fn update_params(&mut self, config: &Config) { + self.cave_noise = FastNoiseLite::with_seed(config.proc_gen_seed + 50); + self.cave_noise.set_frequency(Some(config.cave_noise_freq)); + } + + pub fn new(config: &Config) -> Self { + let cave_noise = FastNoiseLite::with_seed(config.proc_gen_seed + 50); + let mut wg = WorldGenerator { cave_noise }; + wg.update_params(config); + wg + } +} |
