This commit is contained in:
2026-03-21 19:10:04 +00:00
parent 03c3e5d6d7
commit 4fc4843ed7
2 changed files with 10 additions and 27 deletions
-23
View File
@@ -2,7 +2,6 @@ use crate::constants::ITILE_SIZE;
use crate::entities::item::drop_table::dig_rng; use crate::entities::item::drop_table::dig_rng;
use crate::entities::item::prefabs::misc::misc_prefabs::spawn_prefab; use crate::entities::item::prefabs::misc::misc_prefabs::spawn_prefab;
use crate::world::chunks::Z_BELOW; use crate::world::chunks::Z_BELOW;
use crate::world::generation::forestry::{fell_tree, TreePart};
use crate::world::tiles::tile_changed::TileChangedEvent; use crate::world::tiles::tile_changed::TileChangedEvent;
use crate::world::tiles::visibility::TileOcclusionEvent; use crate::world::tiles::visibility::TileOcclusionEvent;
use crate::world::tiles::TileMap; use crate::world::tiles::TileMap;
@@ -28,7 +27,6 @@ pub fn dig_system(
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
time: Res<Time>, time: Res<Time>,
mut query: Query<(&Transform, &mut Digger)>, mut query: Query<(&Transform, &mut Digger)>,
tree_parts: Query<(Entity, &TreePart)>,
mut tilemap: ResMut<TileMap>, mut tilemap: ResMut<TileMap>,
mut tile_changed: MessageWriter<TileChangedEvent>, mut tile_changed: MessageWriter<TileChangedEvent>,
mut occlusion: MessageWriter<TileOcclusionEvent>, mut occlusion: MessageWriter<TileOcclusionEvent>,
@@ -92,27 +90,6 @@ pub fn dig_system(
} }
} }
} }
// If there's a blocking fixture directly above the dug tile, it's a tree trunk —
// fell the whole tree. Temporary wiring until a designation + task system
// triggers fell_tree explicitly.
let above_pos = IVec3::new(below_pos.x, below_pos.y, below_pos.z + ITILE_SIZE);
let has_trunk_above = tilemap
.fixture_tiles
.get(&above_pos)
.map(|f| !f.can_stand_in())
.unwrap_or(false);
if has_trunk_above {
fell_tree(
above_pos,
&tree_parts,
&mut commands,
&mut tilemap,
&mut tile_changed,
&mut occlusion,
);
}
} }
} }
} }
+10 -4
View File
@@ -253,7 +253,7 @@ pub fn generate_chunk_forrestry(
} }
} }
/// Remove all fixture tiles and entities belonging to the tree rooted at `root_pos`. /// Fells the entire tree containing the trunk at `trunk_pos`.
/// ///
/// # How trees are identified /// # How trees are identified
/// Finds all TreePart entities whose XY is within canopy radius of the trunk column. /// Finds all TreePart entities whose XY is within canopy radius of the trunk column.
@@ -266,20 +266,25 @@ pub fn generate_chunk_forrestry(
/// - Fires `TileChangedEvent` for path invalidation /// - Fires `TileChangedEvent` for path invalidation
/// - Fires `TileOcclusionEvent` for the column at each removed position /// - Fires `TileOcclusionEvent` for the column at each removed position
/// ///
/// # chunk_entity_index
/// Despawned tree entities leave stale entries in `chunk_entity_index`. This is harmless:
/// `unload_chunk` calls `despawn()` on each indexed entity, which is a no-op on
/// already-despawned entities.
///
/// # Performance /// # Performance
/// O(tree_size) entity lookups via `Query<(Entity, &TreePart)>`. For a typical tree /// O(tree_size) entity lookups via `Query<(Entity, &TreePart)>`. For a typical tree
/// (4-8 trunk + ~60 leaf tiles) this is ~68 iterations. Acceptable for an infrequent /// (4-8 trunk + ~60 leaf tiles) this is ~68 iterations. Acceptable for an infrequent
/// action. /// action.
pub fn fell_tree( pub fn fell_tree(
root_pos: IVec3, trunk_pos: IVec3,
tree_parts: &Query<(Entity, &TreePart)>, tree_parts: &Query<(Entity, &TreePart)>,
commands: &mut Commands, commands: &mut Commands,
tilemap: &mut TileMap, tilemap: &mut TileMap,
tile_changed: &mut MessageWriter<TileChangedEvent>, tile_changed: &mut MessageWriter<TileChangedEvent>,
occlusion: &mut MessageWriter<TileOcclusionEvent>, occlusion: &mut MessageWriter<TileOcclusionEvent>,
) { ) {
let trunk_x = root_pos.x; let trunk_x = trunk_pos.x;
let trunk_y = root_pos.y; let trunk_y = trunk_pos.y;
let canopy_radius_world = (TREE_LEAF_BASE_RADIUS * TILE_SIZE) as i32 + ITILE_SIZE; let canopy_radius_world = (TREE_LEAF_BASE_RADIUS * TILE_SIZE) as i32 + ITILE_SIZE;
let to_remove: Vec<(Entity, IVec3)> = tree_parts let to_remove: Vec<(Entity, IVec3)> = tree_parts
@@ -298,6 +303,7 @@ pub fn fell_tree(
// Full column occlusion refresh for each removed tile. // Full column occlusion refresh for each removed tile.
// calculate_visibility traces up arbitrarily deep, so refresh the full column. // calculate_visibility traces up arbitrarily deep, so refresh the full column.
// TODO: batch into a dirty-region approach once tree felling is common (~13k events/tree).
let z_depth = let z_depth =
crate::world::chunks::Z_BELOW as i32 + crate::world::chunks::Z_ABOVE as i32 + 1; crate::world::chunks::Z_BELOW as i32 + crate::world::chunks::Z_ABOVE as i32 + 1;
for dz in 0..=z_depth { for dz in 0..=z_depth {