gai-godot-games/pathfinding-algorithms/character.gd

25 lines
595 B
GDScript
Raw Normal View History

2024-11-05 10:44:20 +01:00
extends CharacterBody2D
2024-11-05 12:16:17 +01:00
const MAX_SPEED: float = 300.0
2024-11-05 13:17:36 +01:00
const ACCELERATION: int = 2400
2024-11-05 10:44:20 +01:00
@export var nav: NavigationAgent2D
2024-11-05 12:16:17 +01:00
2024-11-05 10:44:20 +01:00
func _physics_process(delta: float) -> void:
2024-11-21 18:32:27 +01:00
await get_tree().physics_frame
2024-11-07 18:11:31 +01:00
var movement: Vector2 = Vector2()
2024-11-05 10:44:20 +01:00
2024-11-07 18:11:31 +01:00
nav.target_position = get_global_mouse_position()
2024-11-05 10:44:20 +01:00
2024-11-07 18:11:31 +01:00
movement = nav.get_next_path_position() - global_position
if movement.length() < 20:
movement = -velocity * 0.2
else:
movement = movement.normalized() * ACCELERATION * delta
2024-11-05 10:44:20 +01:00
2024-11-07 18:11:31 +01:00
velocity += movement
if velocity.length() > MAX_SPEED:
velocity = velocity.normalized() * MAX_SPEED
2024-11-05 10:44:20 +01:00
2024-11-07 18:11:31 +01:00
move_and_slide()