Added better nav stuff

main
Yan Wittmann 2024-11-07 18:11:31 +01:00
parent af0d9d57e0
commit 391b026c84
4 changed files with 59 additions and 28 deletions

View File

@ -6,18 +6,18 @@ const ACCELERATION: int = 2400
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
var movement: Vector2 = Vector2() var movement: Vector2 = Vector2()
nav.target_position = get_global_mouse_position() nav.target_position = get_global_mouse_position()
movement = nav.get_next_path_position() - global_position movement = nav.get_next_path_position() - global_position
if movement.length() < 20: if movement.length() < 20:
movement = -velocity * 0.2 movement = -velocity * 0.2
else: else:
movement = movement.normalized() * ACCELERATION * delta movement = movement.normalized() * ACCELERATION * delta
velocity += movement velocity += movement
if velocity.length() > MAX_SPEED: if velocity.length() > MAX_SPEED:
velocity = velocity.normalized() * MAX_SPEED velocity = velocity.normalized() * MAX_SPEED
move_and_slide() move_and_slide()

View File

@ -14,3 +14,7 @@ config/name="pathfinding-algorithms"
run/main_scene="res://scenes/tilemap_nav.tscn" run/main_scene="res://scenes/tilemap_nav.tscn"
config/features=PackedStringArray("4.3", "Forward Plus") config/features=PackedStringArray("4.3", "Forward Plus")
config/icon="res://icon.svg" config/icon="res://icon.svg"
[navigation]
2d/default_edge_connection_margin=40.0

View File

@ -0,0 +1,16 @@
extends Node2D
@onready var floor: NavigationRegion2D = $BaseNavigationRegion2D
@onready var road: NavigationRegion2D = $CheapPathNavigationRegion2D
# @onready var road_poly: Polygon2D = $CheapPathNavigationRegion2D/CheapPath
@onready var road_poly: Polygon2D = $BaseNavigationRegion2D/CheapPath
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
setup.call_deferred()
func setup():
road_poly.hide()
road.navigation_polygon.add_outline(road_poly.polygon)
road.bake_navigation_polygon()

File diff suppressed because one or more lines are too long