azalea/pathfinder/
debug.rs1use azalea_client::{chat::SendChatEvent, InstanceHolder};
2use azalea_core::position::Vec3;
3use bevy_ecs::prelude::*;
4
5use super::ExecutingPath;
6
7#[derive(Component)]
31pub struct PathfinderDebugParticles;
32
33pub fn debug_render_path_with_particles(
34 mut query: Query<(Entity, &ExecutingPath, &InstanceHolder), With<PathfinderDebugParticles>>,
35 chat_events: Option<ResMut<Events<SendChatEvent>>>,
38 mut tick_count: Local<usize>,
39) {
40 let Some(mut chat_events) = chat_events else {
41 return;
42 };
43 if *tick_count >= 2 {
44 *tick_count = 0;
45 } else {
46 *tick_count += 1;
47 return;
48 }
49 for (entity, executing_path, instance_holder) in &mut query {
50 if executing_path.path.is_empty() {
51 continue;
52 }
53
54 let chunks = &instance_holder.instance.read().chunks;
55
56 let mut start = executing_path.last_reached_node;
57 for (i, movement) in executing_path.path.iter().enumerate() {
58 let end = movement.target;
59
60 let start_vec3 = start.center();
61 let end_vec3 = end.center();
62
63 let step_count = (start_vec3.distance_squared_to(&end_vec3).sqrt() * 4.0) as usize;
64
65 let target_block_state = chunks.get_block_state(&movement.target).unwrap_or_default();
66 let above_target_block_state = chunks
67 .get_block_state(&movement.target.up(1))
68 .unwrap_or_default();
69 let is_mining = !super::world::is_block_state_passable(target_block_state)
73 || !super::world::is_block_state_passable(above_target_block_state);
74
75 let (r, g, b): (f64, f64, f64) = if i == 0 {
76 (0., 1., 0.)
77 } else if is_mining {
78 (1., 0., 0.)
79 } else {
80 (0., 1., 1.)
81 };
82
83 for i in 0..step_count {
85 let percent = i as f64 / step_count as f64;
86 let pos = Vec3 {
87 x: start_vec3.x + (end_vec3.x - start_vec3.x) * percent,
88 y: start_vec3.y + (end_vec3.y - start_vec3.y) * percent,
89 z: start_vec3.z + (end_vec3.z - start_vec3.z) * percent,
90 };
91 let particle_command = format!(
92 "/particle dust{{color:[{r},{g},{b}],scale:{size}}} {start_x} {start_y} {start_z} {delta_x} {delta_y} {delta_z} 0 {count}",
93 size = 1,
94 start_x = pos.x,
95 start_y = pos.y,
96 start_z = pos.z,
97 delta_x = 0,
98 delta_y = 0,
99 delta_z = 0,
100 count = 1
101 );
102 chat_events.send(SendChatEvent {
103 entity,
104 content: particle_command,
105 });
106 }
107
108 start = movement.target;
109 }
110 }
111}