2023-10-18 01:58:34 +02:00
|
|
|
extends CharacterBody2D
|
|
|
|
|
2024-06-01 12:12:18 +02:00
|
|
|
var movement_speed := 200.0
|
2023-10-18 01:58:34 +02:00
|
|
|
|
|
|
|
@onready var navigation_agent: NavigationAgent2D = $NavigationAgent2D
|
|
|
|
|
2024-06-01 12:12:18 +02:00
|
|
|
func _ready() -> void:
|
2023-10-18 01:58:34 +02:00
|
|
|
# These values need to be adjusted for the actor's speed
|
|
|
|
# and the navigation layout.
|
|
|
|
navigation_agent.path_desired_distance = 2.0
|
|
|
|
navigation_agent.target_desired_distance = 2.0
|
|
|
|
navigation_agent.debug_enabled = true
|
|
|
|
|
|
|
|
|
|
|
|
# The "click" event is a custom input action defined in
|
|
|
|
# Project > Project Settings > Input Map tab.
|
2024-06-01 12:12:18 +02:00
|
|
|
func _unhandled_input(event: InputEvent) -> void:
|
2023-10-18 01:58:34 +02:00
|
|
|
if not event.is_action_pressed("click"):
|
|
|
|
return
|
2024-06-01 12:12:18 +02:00
|
|
|
|
2023-10-18 01:58:34 +02:00
|
|
|
set_movement_target(get_global_mouse_position())
|
|
|
|
|
|
|
|
|
2024-06-01 12:12:18 +02:00
|
|
|
func set_movement_target(movement_target: Vector2) -> void:
|
2023-10-18 01:58:34 +02:00
|
|
|
navigation_agent.target_position = movement_target
|
|
|
|
|
|
|
|
|
2024-06-01 12:12:18 +02:00
|
|
|
func _physics_process(_delta: float) -> void:
|
2023-10-18 01:58:34 +02:00
|
|
|
if navigation_agent.is_navigation_finished():
|
|
|
|
return
|
|
|
|
|
|
|
|
var current_agent_position: Vector2 = global_position
|
|
|
|
var next_path_position: Vector2 = navigation_agent.get_next_path_position()
|
|
|
|
|
|
|
|
velocity = current_agent_position.direction_to(next_path_position) * movement_speed
|
|
|
|
move_and_slide()
|