Răsfoiți Sursa

Check for obstacle before teleporting player in Grid-based Pathfinding with Astar (#771)

Co-authored-by: Hugo Locurcio <[email protected]>
dev-gilbride 2 ani în urmă
părinte
comite
c8f0706055

+ 1 - 1
2d/navigation_astar/character.gd

@@ -34,7 +34,7 @@ func _process(_delta):
 func _unhandled_input(event):
 	if event.is_action_pressed("click"):
 		var global_mouse_pos = get_global_mouse_position()
-		if Input.is_key_pressed(KEY_SHIFT):
+		if Input.is_key_pressed(KEY_SHIFT) and get_parent().get_node("TileMap").check_start_position(global_mouse_pos):
 			global_position = global_mouse_pos
 		else:
 			_target_position = global_mouse_pos

+ 8 - 0
2d/navigation_astar/pathfind_astar.gd

@@ -127,6 +127,14 @@ func is_outside_map_bounds(point):
 	return point.x < 0 or point.y < 0 or point.x >= map_size.x or point.y >= map_size.y
 
 
+func check_start_position(world_start):
+	var start_point = world_to_map(world_start)
+	if start_point in obstacles:
+		return false
+
+	return true
+
+
 func get_astar_path(world_start, world_end):
 	self.path_start_position = world_to_map(world_start)
 	self.path_end_position = world_to_map(world_end)