From aeef78eca2fa491e478d50d91987f7611d54f598 Mon Sep 17 00:00:00 2001 From: popertots Date: Wed, 18 Mar 2026 09:22:47 +0000 Subject: [PATCH] Fix tiled patching --- Cargo.lock | 4 +-- Cargo.toml | 2 +- src/world/tiles/visibility.rs | 52 +++++++++++++++++------------------ 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 38f3aec..d58b8d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1249,9 +1249,9 @@ checksum = "4f98cbc6d34bbdb58240b72ed1731931b4991a893b3a3238bb7c42ae054aa676" [[package]] name = "bevy_rand" -version = "0.14.0" +version = "0.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41e6e263026d08a4ca27c83257ed582939162534e6658ead164886d610df9b13" +checksum = "c76e5db2c274081fdff32a525609436bd7ac2c24b91c1a5d3bc04e43d7558ddf" dependencies = [ "bevy_app", "bevy_ecs", diff --git a/Cargo.toml b/Cargo.toml index 254d4bb..ae71471 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" bevy = { version = "0.18.0", features = ["wayland"] } noise = "0.9.0" rand = "0.10.0" -bevy_rand = { version = "0.14.0", features = ["wyrand"] } +bevy_rand = { version = "0.14.1", features = ["wyrand"] } image = "0.25.9" bevy_platform = "0.18.0" rayon = "1.11.0" diff --git a/src/world/tiles/visibility.rs b/src/world/tiles/visibility.rs index c742685..5c9a041 100644 --- a/src/world/tiles/visibility.rs +++ b/src/world/tiles/visibility.rs @@ -36,8 +36,8 @@ pub fn compute_visibility_of_game_entities( return; } } - - let entity_z = (transform.translation.z / TILE_SIZE) - 1.0; + let scaled_z = transform.translation.z / TILE_SIZE; + let entity_z = scaled_z - 1.0; // if same z-level, always visible if z_index.0 == entity_z { sprite.color = Color::WHITE; @@ -59,11 +59,11 @@ pub fn compute_visibility_of_game_entities( // if visible, calculate saturation *visibility = Visibility::Visible; - let saturation = - ((z_index.0 - (transform.translation.z / TILE_SIZE) + 1.) / 8.0).clamp(0.0, 1.0); + let saturation = ((z_index.0 - scaled_z + 1.) / 8.0).clamp(0.0, 1.0); - sprite.color = Color::hsv(194.7, saturation, 1.0 - (saturation / 2.0)); - sprite.color.set_alpha(1.0 - saturation); + let mut c = Color::hsv(194.7, saturation, 1.0 - (saturation / 2.0)); + c.set_alpha(1.0 - saturation); + sprite.color = c; }); } @@ -115,8 +115,11 @@ pub fn handle_tile_occlusion_updates( } } +#[inline(always)] pub fn calculate_visibility(pos: IVec3, tilemap: &TileMap) -> [u32; 8] { let mut visible_range = [0u32; 8]; + let z_total = Z_TOTAL as i32; + let z_below = Z_BELOW as i32; let touches_air = 'neighbor_check: { for x_offset in -1..=1 { @@ -145,45 +148,42 @@ pub fn calculate_visibility(pos: IVec3, tilemap: &TileMap) -> [u32; 8] { return visible_range; } - let base_camera_index = (pos.z / ITILE_SIZE) + Z_BELOW as i32; + let base_camera_index = (pos.z / ITILE_SIZE) + z_below; - if base_camera_index >= 0 && base_camera_index <= Z_TOTAL as i32 { - if touches_air { - let z2 = base_camera_index as usize; - visible_range[z2 / 32] |= 1 << (z2 % 32); - } + if base_camera_index >= 0 && base_camera_index <= z_total { + let z2 = base_camera_index as usize; + visible_range[z2 / 32] |= 1 << (z2 % 32); } let mut occluded = false; + let mut check_pos = pos; for z_offset in 1..=(Z_TOTAL as i32 + Z_BELOW as i32 + 1) { - let check_z = pos.z + z_offset * ITILE_SIZE; - let camera_z_index = (check_z / ITILE_SIZE) + Z_BELOW as i32; + check_pos.z = pos.z + z_offset * ITILE_SIZE; + let camera_z_index = (check_pos.z / ITILE_SIZE) + z_below; if camera_z_index < 0 { - match tilemap.floor_tiles.get(&IVec3::new(pos.x, pos.y, check_z)) { - Some(&(_, _, _, visibly_transparent, _, _)) => occluded = !visibly_transparent, - None => occluded = false, + if let Some(&(_, _, _, vt, _, _)) = tilemap.floor_tiles.get(&check_pos) { + occluded = !vt; + } else { + occluded = false; } continue; } - if camera_z_index > Z_TOTAL as i32 { + if camera_z_index > z_total { break; } if !occluded { let z2 = camera_z_index as usize; - visible_range[z2 / 32] |= 1 << ((z2 % 32) as u32); + visible_range[z2 / 32] |= 1 << (z2 % 32); } - let tile = tilemap.floor_tiles.get(&IVec3::new(pos.x, pos.y, check_z)); - - match tile { - Some(&(_, _, _, visibly_transparent, _, _)) => { - occluded = !visibly_transparent; - } - None => occluded = false, + if let Some(&(_, _, _, vt, _, _)) = tilemap.floor_tiles.get(&check_pos) { + occluded = !vt; + } else { + occluded = false; } }