use crate::{Navmesh, NavmeshPortal, Point2};
#[derive(Debug, Clone, PartialEq)]
pub struct NavmeshCorridor {
pub start: Point2,
pub goal: Point2,
pub portals: Vec<NavmeshPortal>,
pub cells: Vec<usize>,
}
impl NavmeshCorridor {
#[must_use]
pub fn new(
start: Point2,
goal: Point2,
portals: Vec<NavmeshPortal>,
cells: Vec<usize>,
) -> Self {
Self {
start,
goal,
portals,
cells,
}
}
#[must_use]
pub fn from_cells(
navmesh: &Navmesh,
start: Point2,
goal: Point2,
cells: &[usize],
) -> Option<Self> {
if cells.is_empty() {
return None;
}
let mut portals = Vec::with_capacity(cells.len().saturating_sub(1));
for window in cells.windows(2) {
let left = window[0];
let right = window[1];
let portal = *navmesh.portals().iter().find(|p| {
(p.left_cell == left && p.right_cell == right)
|| (p.left_cell == right && p.right_cell == left)
})?;
portals.push(portal);
}
Some(Self::new(start, goal, portals, cells.to_vec()))
}
}