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

34 lines
881 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-21 18:32:27 +01:00
# check if space is pressed
if Input.is_action_just_pressed("ui_select"):
frozen = !frozen
2024-11-12 15:39:09 +01:00
2024-11-21 18:32:27 +01:00
var movement: Vector2 = Vector2()
if frozen:
return
2024-11-12 15:39:09 +01:00
2024-11-21 18:32:27 +01:00
var pathfinding_result: PathfindingResult = graph.determine_next_position(position, get_global_mouse_position())
graph.draw_pathfinding_result(pathfinding_result)
2024-11-12 15:39:09 +01:00
2024-11-21 18:32:27 +01:00
movement = pathfinding_result.next_position - global_position
2024-11-12 15:00:58 +01:00
2024-11-21 18:32:27 +01:00
if pathfinding_result.is_next_target and movement.length() < 20:
movement = -velocity * 0.2
else:
movement = movement.normalized() * ACCELERATION * delta
2024-11-12 15:00:58 +01:00
2024-11-21 18:32:27 +01:00
velocity += movement
if velocity.length() > MAX_SPEED:
velocity = velocity.normalized() * MAX_SPEED
2024-11-12 15:00:58 +01:00
2024-11-21 18:32:27 +01:00
move_and_slide()