fix terrain z scrolling

This commit is contained in:
2025-05-04 01:08:58 +01:00
parent 9df3f7ecaf
commit c061ecc082
3 changed files with 79 additions and 58 deletions
+31 -33
View File
@@ -1,12 +1,16 @@
use crate::constants::{ITILE_SIZE, TILE_SIZE};
use crate::tile::{self, FixtureTile, FloorTile, NeedsOccluded, TileMap};
use crate::tiles::{self, FixtureTilePrefab, FloorTilePrefab};
use crate::tile::{FixtureTile, FloorTile, NeedsOccluded, TileMap};
use crate::tiles::{CurrentWorldSpriteState, FixtureTilePrefab, FloorTilePrefab, WorldSpriteState};
use bevy::prelude::*;
use bevy_platform::collections::hash_map::HashMap;
use noise::{NoiseFn, Perlin};
pub const CHUNK_SIZE: i32 = 8;
pub const Z_BELOW: f32 = 30.0;
pub const Z_ABOVE: f32 = 5.0;
pub const Z_TOTAL: f32 = Z_ABOVE + Z_BELOW;
#[derive(Resource)]
pub struct ChunkMap {
pub loaded_chunks: HashMap<IVec2, bool>,
@@ -38,7 +42,7 @@ pub fn handle_tile_occlusion_updates(
Query<(&mut FixtureTile, &Transform, &mut NeedsOccluded)>,
Query<(Entity, &mut NeedsOccluded)>,
)>,
mut cwss: ResMut<tiles::CurrentWorldSpriteState>,
mut cwss: ResMut<CurrentWorldSpriteState>,
) {
query_set
.p0()
@@ -69,13 +73,13 @@ pub fn handle_tile_occlusion_updates(
commands.entity(entity).remove::<NeedsOccluded>();
}
}
cwss.state = tiles::WorldSpriteState::WaitingForRender;
cwss.state = WorldSpriteState::WaitingForRender;
}
pub fn calculate_visibility(pos: IVec3, tilemap: &TileMap) -> [u32; 8] {
let mut visible_range = [0u32; 8];
for mut camera_z in -15..=5 {
for mut camera_z in -Z_BELOW as i32..=Z_ABOVE as i32 {
camera_z *= ITILE_SIZE;
let mut is_visible = false;
@@ -85,7 +89,7 @@ pub fn calculate_visibility(pos: IVec3, tilemap: &TileMap) -> [u32; 8] {
let mut is_occluded = false;
'vertical_check: for z_offset in 1..10 {
'vertical_check: for z_offset in 1..15 {
let above_pos = IVec3::new(pos.x, pos.y, pos.z + (z_offset * ITILE_SIZE));
if above_pos.z <= camera_z {
if let Some(&(_, opaque, _, _, _)) = tilemap.floor_tiles.get(&above_pos) {
@@ -94,16 +98,6 @@ pub fn calculate_visibility(pos: IVec3, tilemap: &TileMap) -> [u32; 8] {
break 'vertical_check;
}
}
// if z_offset >= 4 {
// is_occluded = true;
// break 'vertical_check;
// }
// if let Some(&(_, solid, _)) = tilemap.fixtures.get(&above_pos) {
// if solid {
// is_occluded = true;
// break 'vertical_check;
// }
// }
} else {
break 'vertical_check;
}
@@ -127,19 +121,13 @@ pub fn calculate_visibility(pos: IVec3, tilemap: &TileMap) -> [u32; 8] {
break 'neighbor_check;
}
}
// if let Some(&(id, _, _)) = tilemap.fixtures.get(&neighbor_pos) {
// if id == 0 {
// is_visible = true;
// break 'neighbor_check;
// }
// }
}
}
}
}
if is_visible {
let z2 = ((camera_z / ITILE_SIZE) + 150) as usize;
let z2 = ((camera_z / ITILE_SIZE) + Z_BELOW as i32) as usize;
visible_range[z2 / 32] |= 1 << ((z2 % 32) as u32);
}
}
@@ -158,7 +146,6 @@ pub fn generate_surface_noise(x: i32, y: i32) -> f32 {
amplitude *= 0.6;
frequency *= 1.8;
}
(noise_value * 2.5) as f32
}
@@ -168,7 +155,7 @@ fn generate_chunks_from_algo(
mut chunk_map: ResMut<ChunkMap>,
mut tilemap: ResMut<TileMap>,
) {
let noise = Perlin::new(0);
let cave_noise = Perlin::new(0);
for event in events.read() {
println!("Loading chunk {}", event.chunk_position);
@@ -197,7 +184,7 @@ fn generate_chunks_from_algo(
);
// Spawn tiles and add them to tilemap
for z in -10..=5 {
for z in -Z_BELOW as isize..=Z_ABOVE as isize {
let position = Vec3::new(
(world_x as f32 * TILE_SIZE).round(),
(world_y as f32 * TILE_SIZE).round(),
@@ -206,7 +193,7 @@ fn generate_chunks_from_algo(
let pos_ivec = position.as_ivec3();
if z < -8 {
let noise_value = noise.get([
let noise_value = cave_noise.get([
world_x as f64 * 0.05,
world_y as f64 * 0.05,
z as f64 * 0.05,
@@ -295,6 +282,22 @@ fn setup_initial_chunks(mut event_writer: EventWriter<LoadChunkEvent>) {
}
}
pub fn index_z_to_absolute_z(z: f32) -> f32 {
z + Z_BELOW
}
pub fn world_z_to_absolute_z(z: f32) -> f32 {
z + Z_BELOW * ITILE_SIZE as f32
}
pub fn absolute_z_to_index_z(z: f32) -> f32 {
z - Z_BELOW
}
pub fn absolute_z_to_world_z(z: f32) -> f32 {
z - Z_BELOW * ITILE_SIZE as f32
}
pub struct TilemapPlugin;
impl Plugin for TilemapPlugin {
@@ -304,12 +307,7 @@ impl Plugin for TilemapPlugin {
.add_systems(Startup, (setup_chunk_system, setup_initial_chunks))
.add_systems(
PostStartup,
(
generate_chunks_from_algo,
handle_tile_occlusion_updates,
tile::update_tile_visibility,
)
.chain(),
(generate_chunks_from_algo, handle_tile_occlusion_updates).chain(),
)
.add_systems(FixedUpdate, (generate_chunks_from_algo).chain());
}