Function torchbearer::path::astar_path_fourwaygrid
source · Expand description
An A* pathfinding implementation for a grid base map, where diagonal movements are disabled. Returns an optional vector containing the several points on the map to walk through, including the origin and destination.
Implements the algorithm and fixes found on the redblobgames.com.
Uses a binary heap as described in the rust-lang doc.
Arguments
map
- a struct implementing theMap
trait.from
- the origin.to
- the destination.
Panics
Panics if from
or to
are out of bounds of the map.
Examples
use torchbearer::{
path::{astar_path_fourwaygrid, PathMap},
Point,
};
struct SampleMap {
width: i32,
height: i32,
walkable: Vec<bool>,
}
impl SampleMap {
fn new(width: i32, height: i32) -> Self {
// (…)
}
}
impl PathMap for SampleMap {
fn dimensions(&self) -> (i32, i32) {
(self.width, self.height)
}
fn is_walkable(&self, (x, y): Point) -> bool {
self.walkable[(x + y * self.width) as usize]
}
}
let sample_map = SampleMap::new(16, 10);
// (…) You probably want at this point to add some walls to your map.
if let Some(path) = astar_path_fourwaygrid(&sample_map, (1, 1), (3, 8)) {
// (…)
}