use navmesh::*;
use std::collections::HashMap;
#[derive(Debug, Default)]
pub struct NavMeshes(pub(crate) HashMap<NavMeshID, NavMesh>);
impl NavMeshes {
#[inline]
pub fn register(&mut self, mesh: NavMesh) -> NavMeshID {
let id = mesh.id();
self.0.insert(id, mesh);
id
}
#[inline]
pub fn unregister(&mut self, id: NavMeshID) -> Option<NavMesh> {
self.0.remove(&id)
}
#[inline]
pub fn unregister_all(&mut self) {
self.0.clear()
}
#[inline]
pub fn meshes_iter(&self) -> impl Iterator<Item = &NavMesh> {
self.0.values()
}
#[inline]
pub fn find_mesh(&self, id: NavMeshID) -> Option<&NavMesh> {
self.0.get(&id)
}
#[inline]
pub fn find_mesh_mut(&mut self, id: NavMeshID) -> Option<&mut NavMesh> {
self.0.get_mut(&id)
}
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))
}
}