mgecumshooter/esotera
added chunk generator
Commit 14d7378deb73 · mgecumshooter · 2026-10-22 11:16 +0300
Comments
No comments yet.
Diff
diff --git a/Cargo.lock b/Cargo.lock
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -12,6 +12,12 @@
]
[[package]]
+name = "autocfg"
+version = "1.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
+
+[[package]]
name = "bindgen"
version = "0.72.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -92,10 +98,20 @@
name = "esotera"
version = "0.1.0"
dependencies = [
+ "fastnoise-lite",
"sola-raylib",
]
[[package]]
+name = "fastnoise-lite"
+version = "1.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3e5f3c3cc9081e5d0e18bcd50e80cd33cba47fc22f88a9da9c33ecd1c87ea5c0"
+dependencies = [
+ "num-traits",
+]
+
+[[package]]
name = "find-msvc-tools"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -161,6 +177,15 @@
]
[[package]]
+name = "num-traits"
+version = "0.2.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
+dependencies = [
+ "autocfg",
+]
+
+[[package]]
name = "paste"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,4 +4,5 @@
edition = "2024"
[dependencies]
+fastnoise-lite = "1.1.1"
sola-raylib = "6.3.1"
diff --git a/src/chunk/mod.rs b/src/chunk/mod.rs
--- a/src/chunk/mod.rs
+++ b/src/chunk/mod.rs
@@ -10,7 +10,7 @@
use crate::voxel::*;
// constants
-pub const CHUNK_SIZE: usize = 32;
+pub const CHUNK_SIZE: usize = 16;
pub struct Chunk {
pub chunk_pos: Vector3,
@@ -32,12 +32,49 @@
for z in 0..CHUNK_SIZE {
let y = (x + z).min(CHUNK_SIZE - 1);
let i = x + (y * CHUNK_SIZE) + (z * CHUNK_SIZE * CHUNK_SIZE);
- s.voxels[i] = Voxel::from_kind(VoxelKind::Stone);
+ if i % 2 == 0 {
+ s.voxels[i] = Voxel::from_kind(VoxelKind::Dirt);
+ } else {
+ s.voxels[i] = Voxel::from_kind(VoxelKind::Wood);
+ }
}
}
s
}
+ pub fn generate(&mut self, seed: u64) {
+ let mut noise = fastnoise_lite::FastNoiseLite::with_seed(seed as i32);
+ noise.set_noise_type(Some(fastnoise_lite::NoiseType::OpenSimplex2));
+ noise.set_frequency(Some(0.01f32));
+
+ for x in 0..CHUNK_SIZE {
+ for z in 0..CHUNK_SIZE {
+ let world_x = self.chunk_pos.x * CHUNK_SIZE as f32 + x as f32;
+ let world_z = self.chunk_pos.z * CHUNK_SIZE as f32 + z as f32;
+
+ let noise_val = noise.get_noise_2d(world_x, world_z);
+
+ let height = 16f32 + noise_val * 12f32;
+
+ for y in 0..CHUNK_SIZE {
+ let idx = self.voxel_idx(x, y, z);
+ let world_y = self.chunk_pos.y * CHUNK_SIZE as f32 + y as f32;
+
+ if (world_y as f32) < height && world_y >= 0f32 {
+ if (world_y as f32) >= height - 3f32 {
+ self.voxels[idx] = Voxel::from_kind(VoxelKind::Dirt);
+ } else {
+ self.voxels[idx] = Voxel::from_kind(VoxelKind::Stone);
+ }
+ } else {
+ self.voxels[idx] = Voxel::from_kind(VoxelKind::Air);
+ }
+ }
+ }
+ }
+ self.dirty = true;
+ }
+
pub fn is_visible(&self, cam: &Camera3D) -> bool {
let aabb = self.get_chunk_aabb();
let center = Vector3::new(
diff --git a/src/main.rs b/src/main.rs
--- a/src/main.rs
+++ b/src/main.rs
@@ -54,10 +54,10 @@
*chunk_material.shader_mut() = shader.make_weak();
}
- let mut world = World::new(5);
+ let mut world = World::new(6);
let mut plr = Player::new(
- Vector3::new(0f32, 10f32, 0f32),
+ Vector3::new(0f32, 50f32, 0f32),
Cube::new(Vector3::new(2f32, 6f32, 2f32)),
);
@@ -69,6 +69,7 @@
let active_chunks: Vec<&Chunk> = world.chunks.values().collect();
plr.update(&rl, &active_chunks);
plr.apply_to_camera(&mut cam);
+ // rl.update_camera(&mut cam, CameraMode::CAMERA_FREE);
let fps = rl.get_fps();
diff --git a/src/voxel/mod.rs b/src/voxel/mod.rs
--- a/src/voxel/mod.rs
+++ b/src/voxel/mod.rs
@@ -24,7 +24,7 @@
VoxelKind::Air => VoxelColor(0, 0, 0, 0),
VoxelKind::Stone => VoxelColor(128, 128, 128, 255),
VoxelKind::Dirt => VoxelColor(96, 136, 67, 255),
- VoxelKind::Wood => VoxelColor(150, 128, 128, 255),
+ VoxelKind::Wood => VoxelColor(161, 60, 0, 255),
};
Self { kind, color }
}
diff --git a/src/world/mod.rs b/src/world/mod.rs
--- a/src/world/mod.rs
+++ b/src/world/mod.rs
@@ -44,7 +44,11 @@
let player_chunk = (p_chunk_x, p_chunk_y, p_chunk_z);
if !self.chunks.contains_key(&player_chunk) {
let pos_v3 = Vector3::new(player_chunk.0 as f32, player_chunk.1 as f32, player_chunk.2 as f32);
- let chunk = Chunk::load_from_disk(pos_v3).unwrap_or_else(|| Chunk::new(pos_v3));
+ let chunk = Chunk::load_from_disk(pos_v3).unwrap_or_else(|| {
+ let mut ch = Chunk::new(pos_v3);
+ ch.generate(42);
+ ch
+ });
self.chunks.insert(player_chunk, chunk);
}
@@ -66,8 +70,10 @@
chunk_coords.1 as f32,
chunk_coords.2 as f32,
);
- let chunk = Chunk::load_from_disk(pos_v3).unwrap_or_else(|| {
- Chunk::new(pos_v3)
+ let mut chunk = Chunk::load_from_disk(pos_v3).unwrap_or_else(|| {
+ let mut ch = Chunk::new(pos_v3);
+ ch.generate(42);
+ ch
});
self.chunks.insert(chunk_coords, chunk);