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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use std::collections::HashMap;

pub use navmesh::*;

/// ECS resource that holds and manages nav meshes.
#[derive(Debug, Default)]
pub struct NavMeshesRes(pub(crate) HashMap<NavMeshID, NavMesh>);

impl NavMeshesRes {
    /// Register new nav mesh.
    ///
    /// # Arguments
    /// * `mesh` - nav mesh object.
    ///
    /// # Returns
    /// Identifier of registered nav mesh.
    #[inline]
    pub fn register(&mut self, mesh: NavMesh) -> NavMeshID {
        let id = mesh.id();
        self.0.insert(id, mesh);
        id
    }

    /// Unregister nav mesh.
    ///
    /// # Arguments
    /// * `id` - nav mesh identifier.
    ///
    /// # Returns
    /// `Some` with nav mesh object if nav mesh with given identifier was found, `None` otherwise.
    #[inline]
    pub fn unregister(&mut self, id: NavMeshID) -> Option<NavMesh> {
        self.0.remove(&id)
    }

    /// Unregister all nav meshes.
    #[inline]
    pub fn unregister_all(&mut self) {
        self.0.clear()
    }

    /// Get nav meshes iterator.
    #[inline]
    pub fn meshes_iter(&self) -> impl Iterator<Item = &NavMesh> {
        self.0.values()
    }

    /// Find nav mesh by its identifier.
    ///
    /// # Arguments
    /// * `id` - nav mesh identifier.
    ///
    /// # Returns
    /// `Some` with nav mesh if exists or `None` otherwise.
    #[inline]
    pub fn find_mesh(&self, id: NavMeshID) -> Option<&NavMesh> {
        self.0.get(&id)
    }

    /// Find nav mesh by its identifier.
    ///
    /// # Arguments
    /// * `id` - nav mesh identifier.
    ///
    /// # Returns
    /// `Some` with mutable nav mesh if exists or `None` otherwise.
    #[inline]
    pub fn find_mesh_mut(&mut self, id: NavMeshID) -> Option<&mut NavMesh> {
        self.0.get_mut(&id)
    }

    /// Find closest point on nav meshes.
    ///
    /// # Arguments
    /// * `point` - query point.
    /// * `query` - query quality.
    ///
    /// # Returns
    /// `Some` with nav mesh identifier and point on nav mesh if found or `None` otherwise.
    pub fn closest_point(&self, point: NavVec3, query: NavQuery) -> Option<(NavMeshID, NavVec3)> {
        self.0
            .iter()
            .filter_map(|(id, mesh)| {
                mesh.closest_point(point, query)
                    .map(|p| (p, (p - point).sqr_magnitude(), *id))
            })
            .min_by(|a, b| a.1.partial_cmp(&b.1).unwrap())
            .map(|(p, _, id)| (id, p))
    }
}