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

34 lines
881 B
GDScript

extends CharacterBody2D
const MAX_SPEED: float = 300.0
const ACCELERATION: int = 2400
@onready var graph: NavigationGraph = $"../NavGraph"
var frozen: bool = false
func _physics_process(delta: float) -> void:
# check if space is pressed
if Input.is_action_just_pressed("ui_select"):
frozen = !frozen
var movement: Vector2 = Vector2()
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
if pathfinding_result.is_next_target and movement.length() < 20:
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()