Call gridless pathfinder from rust code

This commit is contained in:
Manuel Vögele
2022-01-31 22:33:02 +01:00
parent 1ee406a047
commit 673fa42a20
3 changed files with 81 additions and 22 deletions
+35 -4
View File
@@ -20,7 +20,7 @@ extern "C" {
}
#[wasm_bindgen(
inline_js = "export function collidesWithWall(p1, p2) { return canvas.walls.checkCollision(new Ray(p1, p2)); }"
inline_js = "export function collidesWithWall(p1, p2) { return canvas.walls.checkCollision(new Ray(p1, p2));}"
)]
extern "C" {
#[wasm_bindgen(js_name=collidesWithWall)]
@@ -39,6 +39,26 @@ extern "C" {
fn c(this: &JsWallData) -> Vec<f64>;
}
#[wasm_bindgen]
extern "C" {
pub type JsPoint;
#[wasm_bindgen(method, getter)]
fn x(this: &JsPoint) -> f64;
#[wasm_bindgen(method, getter)]
fn y(this: &JsPoint) -> f64;
}
impl From<JsPoint> for Point {
fn from(point: JsPoint) -> Self {
Point {
x: point.x(),
y: point.y(),
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct Wall {
pub p1: Point,
@@ -61,7 +81,7 @@ impl Wall {
}
#[allow(dead_code)]
#[wasm_bindgen(js_name=buildCache)]
#[wasm_bindgen]
pub fn initialize(js_walls: Vec<JsValue>) -> Pathfinder {
let mut walls = Vec::with_capacity(js_walls.len());
for wall in js_walls {
@@ -79,14 +99,25 @@ pub fn free(pathfinder: Pathfinder) {
#[allow(dead_code)]
#[wasm_bindgen(js_name=findPath)]
pub fn find_path(pathfinder: &mut Pathfinder, from: Point, to: Point) -> Option<Array> {
if let Some(first_node) = pathfinder.find_path(from, to) {
pub fn find_path(pathfinder: &mut Pathfinder, from: JsPoint, to: JsPoint) -> Option<Array> {
if let Some(first_node) = pathfinder.find_path(from.into(), to.into()) {
Some(first_node.iter_path().map(JsValue::from).collect())
} else {
None
}
}
#[allow(dead_code)]
#[wasm_bindgen(js_name=debugGetPathfindingPoints)]
pub fn debug_get_pathfinding_points(pathfinder: &Pathfinder) -> Array {
pathfinder
.nodes
.iter()
.map(|node| node.borrow().point)
.map(JsValue::from)
.collect()
}
trait IteratePath {
fn iter_path(&self) -> PathIterator;
}
+4
View File
@@ -83,6 +83,10 @@ impl NodeStorage {
node.borrow_mut().edges = Some(edges);
}
pub fn iter(&self) -> std::slice::Iter<'_, NodePtr> {
self.0.iter()
}
}
#[wasm_bindgen]