2024-11-12 15:00:58 +01:00
|
|
|
extends Node2D
|
|
|
|
|
2024-11-12 16:07:31 +01:00
|
|
|
@export var navigation_polygon: Array[Polygon2D] = []
|
|
|
|
|
2024-11-12 15:00:58 +01:00
|
|
|
@onready var graph: NavigationGraph = $NavGraph
|
|
|
|
@onready var character: CharacterBody2D = $CharacterBody2D
|
|
|
|
|
2024-11-12 16:07:31 +01:00
|
|
|
var seleted_polygon: int = 0
|
|
|
|
|
|
|
|
|
2024-11-12 15:00:58 +01:00
|
|
|
func _ready() -> void:
|
2024-11-21 18:32:27 +01:00
|
|
|
for polygon in navigation_polygon:
|
|
|
|
polygon.hide()
|
2024-11-12 16:07:31 +01:00
|
|
|
|
2024-11-21 18:32:27 +01:00
|
|
|
update_polygon()
|
2024-11-12 16:07:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
func update_polygon():
|
2024-11-21 18:32:27 +01:00
|
|
|
var polygons: Array[PackedVector2Array] = Geometry2D.decompose_polygon_in_convex(navigation_polygon[seleted_polygon].polygon)
|
|
|
|
graph.erase_and_create_nodes_from_polygons(polygons)
|
2024-11-12 16:07:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _process(delta: float) -> void:
|
2024-11-21 18:32:27 +01:00
|
|
|
# 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
|