gai-godot-games/pathfinding-algorithms/scenes/custom-solver/custom_graph_solver.gd

42 lines
1.2 KiB
GDScript

extends Node2D
@export var navigation_polygon: Array[Polygon2D] = []
@onready var graph: NavigationGraph = $NavGraph
@onready var character: CharacterBody2D = $CharacterBody2D
var seleted_polygon: int = 0
func _ready() -> void:
for polygon in navigation_polygon:
polygon.hide()
update_polygon()
func update_polygon():
var polygons: Array[PackedVector2Array] = Geometry2D.decompose_polygon_in_convex(navigation_polygon[seleted_polygon].polygon)
graph.erase_and_create_nodes_from_polygons(polygons)
func _process(delta: float) -> void:
# check for left and right arrow keys to change the selected polygon (0, len(navigation_polygon)-1)
if Input.is_action_just_pressed("ui_right"):
seleted_polygon += 1
if seleted_polygon >= len(navigation_polygon):
seleted_polygon = 0
update_polygon()
if Input.is_action_just_pressed("ui_left"):
seleted_polygon -= 1
if seleted_polygon < 0:
seleted_polygon = len(navigation_polygon) - 1
update_polygon()
if Input.is_action_just_pressed("draw_toggle_polygon"):
graph.draw_polygons = !graph.draw_polygons
if Input.is_action_just_pressed("draw_toggle_nodes") or Input.is_action_just_pressed("draw_toggle_edges"):
graph.draw_nodes = !graph.draw_nodes
graph.draw_edges = !graph.draw_edges