1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#![allow(unused)]

use std::marker::PhantomData;

use bevy::{prelude::*, reflect::TypePath};

use crate::{RaycastMesh, RaycastSource};

/// Updates the 3d cursor to be in the pointed world coordinates
#[allow(clippy::too_many_arguments)]
pub fn update_debug_cursor<T: TypePath + Send + Sync>(
    mut commands: Commands,
    mut meshes: Query<&RaycastSource<T>>,
    mut gizmos: Gizmos,
) {
    for (is_first, intersection) in meshes.iter().flat_map(|m| {
        m.intersections()
            .iter()
            .map(|i| i.1.clone())
            .enumerate()
            .map(|(i, hit)| (i == 0, hit))
    }) {
        let color = match is_first {
            true => Color::GREEN,
            false => Color::PINK,
        };
        gizmos.ray(intersection.position(), intersection.normal(), color);
        gizmos.circle(intersection.position(), intersection.normal(), 0.1, color);
    }
}

pub fn print_intersections<T: TypePath + Send + Sync>(query: Query<&RaycastMesh<T>>) {
    for (_, intersection) in query.iter().flat_map(|mesh| mesh.intersections.iter()) {
        info!(
            "Distance {:?}, Position {:?}",
            intersection.distance(),
            intersection.position()
        );
    }
}