¶
luxe
API (2025.1.2
)¶
luxe: astar
module¶
AStar¶
import "luxe: astar" for AStar
A generic implementation of A* pathfinding in luxe.
For details about the pathfinding and things like costs, heuristics and implementation details, please see https://www.redblobgames.com/pathfinding/a-star/introduction.html
- MAX
- MAX=(v : Num)
- path2D(start:
Vec
, end:Vec
, cost_get_fn:Fn
, neighbors_get_fn:Fn
, heuristic_fn:Fn
)
Num
A value that defaults to
250
, for the max number of iterations that will be considered valid. If the max is reached, no path is returned. To update it, useAstar.MAX = 400
.
unknown
no docs found
Vec
, end: Vec
, cost_get_fn: Fn
, neighbors_get_fn: Fn
, heuristic_fn: Fn
)
¶List
Returns a path between
start
andend
if one was found, ornull
otherwise. The path is aList
of nodes received fromstart
,end
orneighbors_get_fn
and are unmodified.Note: Check if
start
/end
are walkable before calling this function.Cost-calculating Function:
//no cost? _cost_get_fn = Fn.new {|from, to| 1 } //cost from a tilemap, simple (fake) example _cost_get_fn = Fn.new {|from, to| tiles.get_cost(to.x, to.y) }
Getting the neighbors of a node:
_neighbors_get_fn = Fn.new {|node| var list = [] //check above, below, left and right. if(is_walkable(node.x, node.y+1)) list.add(Node.new(node.x, node.y+1)) if(is_walkable(node.x, node.y-1)) list.add(Node.new(node.x, node.y-1)) if(is_walkable(node.x+1, node.y)) list.add(Node.new(node.x+1, node.y)) if(is_walkable(node.x-1, node.y)) list.add(Node.new(node.x-1, node.y)) return list }
Getting the heuristic value of a point:
_heuristic_fn = Fn.new {|end, point| var manhattan = ((end.x - point.x).abs + (end.y - point.y).abs) return manhattan * 1.001 //fudge factor, see the linked articles on pathfinding }
Getting a path:
get_path(start, end) { if(!is_walkable(start)) return null if(!is_walkable(end)) return null return AStar.path2D(start, end, _cost_get_fn, _neighbors_get_fn, _heuristic_fn) }