Chunking 3
This commit is contained in:
+60
-37
@@ -1,5 +1,5 @@
|
||||
use crate::constants::TILE_SIZE;
|
||||
use crate::tile::{self, FixtureTile, FloorTile, NeedsOccluded, TileMap};
|
||||
use crate::tile::{FixtureTile, FloorTile, NeedsOccluded, TileMap};
|
||||
use crate::tiles::{FixtureTilePrefab, FloorTilePrefab};
|
||||
use bevy::prelude::*;
|
||||
use noise::{NoiseFn, Perlin};
|
||||
@@ -30,7 +30,7 @@ fn setup_chunk_system(mut commands: Commands) {
|
||||
commands.insert_resource(ChunkMap::default());
|
||||
}
|
||||
|
||||
// System to handle individual tile occlusion updates
|
||||
// System to handle tile occlusion updates
|
||||
pub fn handle_tile_occlusion_updates(
|
||||
mut commands: Commands,
|
||||
tilemap: Res<TileMap>,
|
||||
@@ -144,6 +144,22 @@ pub fn calculate_visibility(pos: IVec3, tilemap: &TileMap) -> [u32; 8] {
|
||||
visible_range
|
||||
}
|
||||
|
||||
pub fn generate_surface_noise(x: i32, y: i32) -> f32 {
|
||||
let noise = Perlin::new(0);
|
||||
let mut noise_value = 0.0;
|
||||
let mut amplitude = 1.0;
|
||||
let mut frequency = 0.008; // Reduced initial frequency for smoother base terrain
|
||||
|
||||
// Reduced number of octaves for less rough detail
|
||||
for _ in 0..6 {
|
||||
noise_value += noise.get([x as f64 * frequency, y as f64 * frequency]) * amplitude;
|
||||
amplitude *= 0.6; // Gentler amplitude falloff
|
||||
frequency *= 1.8; // Gentler frequency increase
|
||||
}
|
||||
|
||||
(noise_value * 2.5) as f32 // Reduced height multiplier for less extreme elevation
|
||||
}
|
||||
|
||||
fn handle_chunk_loading(
|
||||
mut commands: Commands,
|
||||
asset_server: Res<AssetServer>,
|
||||
@@ -173,16 +189,10 @@ fn handle_chunk_loading(
|
||||
let world_x = start_x + local_x;
|
||||
let world_y = start_y + local_y;
|
||||
|
||||
let noise_value_a =
|
||||
noise.get([world_x as f64 * 0.01, world_y as f64 * 0.01]) * 1.25;
|
||||
let noise_value_b =
|
||||
noise.get([world_x as f64 * 0.05, world_y as f64 * 0.05]) * 0.25;
|
||||
let combined_noise_value = noise_value_a + noise_value_b;
|
||||
|
||||
let noise_position = Vec3::new(
|
||||
(world_x as f32 * TILE_SIZE).round(),
|
||||
(world_y as f32 * TILE_SIZE).round(),
|
||||
(combined_noise_value * 4.).round() as f32 * TILE_SIZE,
|
||||
(generate_surface_noise(world_x, world_y) * TILE_SIZE).round(),
|
||||
);
|
||||
|
||||
// Spawn tiles and add them to tilemap
|
||||
@@ -194,41 +204,43 @@ fn handle_chunk_loading(
|
||||
);
|
||||
let pos_ivec = position.as_ivec3();
|
||||
|
||||
if noise_position.z > position.z {
|
||||
if z < -8 {
|
||||
let noise_value = noise.get([
|
||||
world_x as f64 * 0.05,
|
||||
world_y as f64 * 0.05,
|
||||
z as f64 * 0.05,
|
||||
]);
|
||||
if noise_value < -0.5 {
|
||||
FloorTilePrefab::air(position, &asset_server).spawn(&mut commands);
|
||||
tilemap.floors.insert(pos_ivec, (0, false, true, 1, [0; 8])); // Air tile
|
||||
floor_positions.push((position, "air"));
|
||||
} else if noise_value < 0.8 {
|
||||
FloorTilePrefab::rock(position, &asset_server).spawn(&mut commands);
|
||||
tilemap
|
||||
.floors
|
||||
.insert(pos_ivec, (2, true, false, 255, [0; 8])); // Rock tile
|
||||
floor_positions.push((position, "rock"));
|
||||
} else {
|
||||
FloorTilePrefab::dirt(position, &asset_server).spawn(&mut commands);
|
||||
tilemap.floors.insert(pos_ivec, (1, true, true, 1, [0; 8])); // Dirt tile
|
||||
floor_positions.push((position, "dirt"));
|
||||
}
|
||||
if z < -8 {
|
||||
let noise_value = noise.get([
|
||||
world_x as f64 * 0.05,
|
||||
world_y as f64 * 0.05,
|
||||
z as f64 * 0.05,
|
||||
]);
|
||||
if noise_value < -0.5 {
|
||||
FloorTilePrefab::air(position, &asset_server).spawn(&mut commands);
|
||||
tilemap.floors.insert(pos_ivec, (0, false, true, 1, [0; 8])); // Air tile
|
||||
floor_positions.push((position, "air"));
|
||||
} else if noise_value < 0.8 {
|
||||
FloorTilePrefab::rock(position, &asset_server).spawn(&mut commands);
|
||||
tilemap
|
||||
.floors
|
||||
.insert(pos_ivec, (2, true, false, 255, [0; 8])); // Rock tile
|
||||
floor_positions.push((position, "rock"));
|
||||
} else {
|
||||
FloorTilePrefab::dirt(position, &asset_server).spawn(&mut commands);
|
||||
tilemap.floors.insert(pos_ivec, (1, true, true, 1, [0; 8])); // Dirt tile
|
||||
floor_positions.push((position, "dirt"));
|
||||
}
|
||||
} else if noise_position.z < position.z {
|
||||
} else if noise_position.z > position.z {
|
||||
if (generate_surface_noise(world_x, world_y) * TILE_SIZE).round()
|
||||
<= position.z + TILE_SIZE
|
||||
{
|
||||
FloorTilePrefab::grass(position, &asset_server).spawn(&mut commands);
|
||||
tilemap.floors.insert(pos_ivec, (1, true, true, 1, [0; 8])); // Dirt tile (grass)
|
||||
floor_positions.push((position, "dirt"));
|
||||
} else {
|
||||
FloorTilePrefab::dirt(position, &asset_server).spawn(&mut commands);
|
||||
tilemap.floors.insert(pos_ivec, (1, true, true, 1, [0; 8])); // Dirt tile
|
||||
floor_positions.push((position, "dirt"));
|
||||
}
|
||||
} else {
|
||||
FloorTilePrefab::air(position, &asset_server).spawn(&mut commands);
|
||||
tilemap.floors.insert(pos_ivec, (0, false, true, 1, [0; 8])); // Air tile
|
||||
floor_positions.push((position, "air"));
|
||||
} else {
|
||||
FloorTilePrefab::grass(position, &asset_server).spawn(&mut commands);
|
||||
tilemap.floors.insert(pos_ivec, (1, true, true, 1, [0; 8])); // Grass tile
|
||||
floor_positions.push((position, "dirt"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -258,13 +270,24 @@ fn handle_chunk_loading(
|
||||
}
|
||||
}
|
||||
|
||||
fn setup_initial_chunks(mut event_writer: EventWriter<LoadChunkEvent>) {
|
||||
println!("setup_initial_chunks");
|
||||
for x in -5..=5 {
|
||||
for y in -5..=5 {
|
||||
event_writer.send(LoadChunkEvent {
|
||||
chunk_position: IVec2::new(x, y),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TilemapPlugin;
|
||||
|
||||
impl Plugin for TilemapPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.init_resource::<ChunkMap>()
|
||||
.add_event::<LoadChunkEvent>()
|
||||
.add_systems(Startup, setup_chunk_system)
|
||||
.add_systems(Startup, (setup_chunk_system, setup_initial_chunks))
|
||||
.add_systems(
|
||||
FixedUpdate,
|
||||
(handle_chunk_loading, handle_tile_occlusion_updates).chain(),
|
||||
|
||||
Reference in New Issue
Block a user