31 lines
1.2 KiB
GDScript3
31 lines
1.2 KiB
GDScript3
|
extends Node
|
||
|
|
||
|
var astar = AStarGrid2D.new()
|
||
|
#写入地图数据并更新
|
||
|
func up_map(map_size:Vector2,cell_szie:Vector2,obstacle_arr:PackedVector2Array):
|
||
|
astar.region = Rect2i(0, 0, map_size.x+1, map_size.y+1)
|
||
|
astar.cell_size = cell_szie
|
||
|
astar.offset = cell_szie * 0.5
|
||
|
astar.default_compute_heuristic = AStarGrid2D.HEURISTIC_MANHATTAN
|
||
|
astar.default_estimate_heuristic = AStarGrid2D.HEURISTIC_MANHATTAN
|
||
|
astar.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER
|
||
|
astar.update()
|
||
|
for pos in obstacle_arr:
|
||
|
astar.set_point_solid(pos)
|
||
|
#坐标位置是否可通行
|
||
|
func is_point_walkable(local_position):
|
||
|
var map_position = local_to_map(local_position)
|
||
|
if astar.is_in_boundsv(map_position):
|
||
|
return not astar.is_point_solid(map_position)
|
||
|
return false
|
||
|
#局部转本地
|
||
|
func local_to_map(local_position):
|
||
|
return Vector2(int(local_position.x/astar.cell_size.x),int(local_position.y/astar.cell_size.y))
|
||
|
#获取导航路径
|
||
|
func get_point_path(local_start_point,local_end_point):
|
||
|
var start_point = local_to_map(local_start_point)
|
||
|
var end_point = local_to_map(local_end_point)
|
||
|
return astar.get_point_path(start_point, end_point)
|
||
|
func get_map_point_path(start_point,end_point):
|
||
|
return astar.get_point_path(start_point, end_point)
|