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

34 lines
947 B
GDScript
Raw Normal View History

2024-11-12 15:00:58 +01:00
extends CharacterBody2D
const MAX_SPEED: float = 300.0
const ACCELERATION: int = 2400
@onready var graph: NavigationGraph = $"../NavGraph"
2024-11-12 15:39:09 +01:00
var frozen: bool = false
2024-11-12 15:00:58 +01:00
func _physics_process(delta: float) -> void:
2024-11-12 15:39:09 +01:00
# check if space is pressed
if Input.is_action_just_pressed("ui_select"):
frozen = !frozen
2024-11-12 15:00:58 +01:00
var movement: Vector2 = Vector2()
2024-11-12 15:39:09 +01:00
if frozen:
return
var pathfinding_result: PathfindingResult = graph.determine_next_position(position, get_global_mouse_position())
graph.draw_pathfinding_result(pathfinding_result)
movement = pathfinding_result.next_position - global_position
2024-11-12 15:00:58 +01:00
2024-11-12 15:39:09 +01:00
if pathfinding_result.is_next_target and movement.length() < 20:
2024-11-12 15:00:58 +01:00
movement = -velocity * 0.2
else:
movement = movement.normalized() * ACCELERATION * delta
velocity += movement
if velocity.length() > MAX_SPEED:
velocity = velocity.normalized() * MAX_SPEED
move_and_slide()