add debug

This commit is contained in:
2026-03-21 14:50:31 +00:00
parent ec5287ca86
commit 89693f968c
3 changed files with 146 additions and 4 deletions
+141 -3
View File
@@ -15,6 +15,13 @@ use rand::RngExt;
/// 5 seconds at normal speed — slow enough to observe, fast enough to test.
pub const DIG_INTERVAL_SECS: f32 = 5.0;
/// Temporary debug component. Attached to a rabbit for N ticks after it digs.
/// Removed automatically when tick_count reaches 0.
#[derive(Component)]
pub struct RabbitFallDebug {
pub ticks_remaining: u8,
}
/// Tracks time until next dig action. Debug component — rabbits dig to demonstrate
/// the TileChangedEvent + path invalidation pipeline. Remove or replace when
/// real digging mechanics are implemented.
@@ -110,13 +117,14 @@ pub fn spawn_rabbits(
/// z-level. Also clears the corresponding ChunkData bits via TileMap::remove_floor.
/// Fires TileChangedEvent so path invalidation reacts automatically.
pub fn rabbit_dig_system(
mut query: Query<(&Transform, &mut RabbitDigTimer)>,
mut commands: Commands,
mut query: Query<(Entity, &Transform, &mut RabbitDigTimer)>,
mut tilemap: ResMut<TileMap>,
mut tile_changed: MessageWriter<TileChangedEvent>,
mut occlusion: MessageWriter<TileOcclusionEvent>,
time: Res<Time>,
) {
for (transform, mut dig_timer) in query.iter_mut() {
for (entity, transform, mut dig_timer) in query.iter_mut() {
if !tilemap.is_standable(transform.translation.as_ivec3()) {
dig_timer.secs_remaining = DIG_INTERVAL_SECS;
continue;
@@ -135,7 +143,50 @@ pub fn rabbit_dig_system(
continue;
}
if tilemap.remove_floor(&below_pos).is_some() {
// --- DEBUG: pre-dig state ---
let (lx, ly, lz) =
crate::world::tiles::ChunkData::world_to_local(transform.translation.as_ivec3());
let chunk_pos = crate::world::chunks::world_to_chunk(transform.translation.as_ivec3());
let standable_now = tilemap.is_standable(transform.translation.as_ivec3());
println!(
"[DIG PRE] entity={:?} world_pos=({:.1},{:.1},{:.1}) ivec={:?} \
tile_local=({},{},{}) chunk={:?} standable={}",
entity,
transform.translation.x,
transform.translation.y,
transform.translation.z,
entity_pos,
lx,
ly,
lz,
chunk_pos,
standable_now
);
println!(
"[DIG PRE] below_pos={:?} below_tile_z={}",
below_pos,
below_pos.z / ITILE_SIZE
);
let removed = tilemap.remove_floor(&below_pos);
println!(
"[DIG] remove_floor({:?}) => {}",
below_pos,
if removed.is_some() {
"REMOVED"
} else {
"NOT FOUND"
}
);
// --- DEBUG: post-dig standability ---
let standable_after = tilemap.is_standable(transform.translation.as_ivec3());
println!(
"[DIG POST] standable at entity pos after dig: {}",
standable_after
);
if removed.is_some() {
tile_changed.write(TileChangedEvent { pos: below_pos });
// Refresh the full column below the dig and its XY neighbours.
@@ -151,6 +202,93 @@ pub fn rabbit_dig_system(
}
}
}
// Attach fall debug component — tracks for 12 ticks
commands.entity(entity).insert(RabbitFallDebug {
ticks_remaining: 12,
});
}
}
}
/// Temporary debug system. Prints rabbit position and standability for N ticks
/// after a dig event. Removes the RabbitFallDebug component when done.
pub fn rabbit_fall_debug_system(
mut commands: Commands,
mut query: Query<(Entity, &Transform, &mut RabbitFallDebug)>,
tilemap: Res<TileMap>,
) {
use crate::world::chunks::world_to_chunk;
use crate::world::tiles::ChunkData;
for (entity, transform, mut debug) in query.iter_mut() {
let world_pos = transform.translation;
let ivec = world_pos.as_ivec3();
let (lx, ly, lz) = ChunkData::world_to_local(ivec);
let lz_euclid = ivec.z.div_euclid(ITILE_SIZE); // compare truncation vs floor
let chunk_pos = world_to_chunk(ivec);
let standable = tilemap.is_standable(ivec);
// Read ChunkData bits directly if chunk is loaded
let (stand_in, stand_on_below) = if let Some(chunk) = tilemap.chunks.get(&chunk_pos) {
let in_f = if lz >= -(crate::world::chunks::Z_BELOW as i32)
&& lz <= crate::world::chunks::Z_ABOVE as i32
&& lx >= 0
&& lx < crate::world::chunks::CHUNK_SIZE
&& ly >= 0
&& ly < crate::world::chunks::CHUNK_SIZE
{
let idx = ChunkData::pos_to_index(lx, ly, lz);
let word = idx / 32;
let mask = 1u32 << (idx % 32);
let in_floor = (chunk.stand_in_floor[word] & mask) != 0;
let in_fix = (chunk.stand_in_fixture[word] & mask) != 0;
format!("in_floor={} in_fixture={}", in_floor, in_fix)
} else {
format!("OUT_OF_BOUNDS(lz={})", lz)
};
let on_f = if lz - 1 >= -(crate::world::chunks::Z_BELOW as i32)
&& lz - 1 <= crate::world::chunks::Z_ABOVE as i32
&& lx >= 0
&& lx < crate::world::chunks::CHUNK_SIZE
&& ly >= 0
&& ly < crate::world::chunks::CHUNK_SIZE
{
let idx = ChunkData::pos_to_index(lx, ly, lz - 1);
let word = idx / 32;
let mask = 1u32 << (idx % 32);
let on_floor = (chunk.stand_on_floor[word] & mask) != 0;
let on_fix = (chunk.stand_on_fixture[word] & mask) != 0;
format!("on_floor[z-1]={} on_fixture[z-1]={}", on_floor, on_fix)
} else {
"BELOW_BOUNDS".to_string()
};
(in_f, on_f)
} else {
(
"chunk_not_loaded".to_string(),
"chunk_not_loaded".to_string(),
)
};
println!(
"[FALL {:2}] entity={:?} world_z={:.1} ivec_z={} lz_trunc={} lz_euclid={} \
standable={} | {} | {}",
debug.ticks_remaining,
entity,
world_pos.z,
ivec.z,
lz,
lz_euclid,
standable,
stand_in,
stand_on_below
);
debug.ticks_remaining -= 1;
if debug.ticks_remaining == 0 {
commands.entity(entity).remove::<RabbitFallDebug>();
}
}
}
+4
View File
@@ -45,6 +45,10 @@ fn main() {
.insert_resource(ClearColor(Color::srgb(0., 0., 0.)))
.add_plugins(world::WorldPlugin)
.add_plugins(entities::pathfinding::PathfindingPlugin)
.add_systems(
FixedUpdate,
entities::livestock::rabbit::rabbit_fall_debug_system,
)
.add_systems(
Startup,
(camera::spawn_panning_camera, cursor::setup_cursor),
+1 -1
View File
@@ -284,7 +284,7 @@ impl ChunkData {
pub fn world_to_local(world_pos: IVec3) -> (i32, i32, i32) {
let local_x = ((world_pos.x / ITILE_SIZE) % CHUNK_SIZE + CHUNK_SIZE) % CHUNK_SIZE;
let local_y = ((world_pos.y / ITILE_SIZE) % CHUNK_SIZE + CHUNK_SIZE) % CHUNK_SIZE;
let z = world_pos.z / ITILE_SIZE;
let z = world_pos.z.div_euclid(ITILE_SIZE);
(local_x, local_y, z)
}