game_pathfinding 0.1.3

一个寻路库,包含AStar和Recast,目前还在开发阶段
Documentation
#[cfg(feature = "point_poll")]
use crate::map::PointPool;

pub type IndexType = i32;

#[derive(Debug, Clone, Default)]
pub struct Point {
    pub x: IndexType,
    pub y: IndexType,

    pub f: IndexType,
    pub g: IndexType,
    pub h: IndexType,
}

impl Ord for Point {
    #[inline]
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        other.f.cmp(&self.f)
    }
}

impl PartialOrd for Point {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl PartialEq for Point {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.x == other.x && self.y == other.y
    }

    #[inline]
    fn ne(&self, other: &Self) -> bool {
        self.x != other.x || self.y != other.y
    }
}

impl Eq for Point {}

impl Point {
    #[cfg(feature = "point_poll")]
    #[inline]
    pub fn new_other(
        x: IndexType,
        y: IndexType,
        f: IndexType,
        g: IndexType,
        h: IndexType,
    ) -> Box<Point> {
        PointPool::get_instance()
            .write()
            .unwrap()
            .allocate(x, y, f, g, h)
    }

    #[cfg(not(feature = "point_poll"))]
    #[inline]
    pub fn new_other(
        x: IndexType,
        y: IndexType,
        f: IndexType,
        g: IndexType,
        h: IndexType,
    ) -> Point {
        Point::new(x, y, f, g, h)
    }

    pub fn new(x: IndexType, y: IndexType, f: IndexType, g: IndexType, h: IndexType) -> Point {
        Point { x, y, f, g, h }
    }

    #[inline]
    pub fn f(&self) -> IndexType {
        self.g + self.h
    }

    #[inline]
    pub fn neighbors(&self) -> Vec<(IndexType, IndexType)> {
        vec![
            (-1, 0),
            (-1, -1),
            (0, -1),
            (1, -1),
            (1, 0),
            (1, 1),
            (0, 1),
            (-1, 1),
        ]
    }
}

#[cfg(feature = "point_poll")]
impl Drop for Point {
    fn drop(&mut self) {
        let stolen_point = std::mem::replace(&mut *self, Point::default());
        PointPool::get_instance()
            .write()
            .unwrap()
            .deallocate(stolen_point)
    }
}