Updated enemy controller

This commit is contained in:
2025-11-25 16:52:12 +03:00
parent b4529a407b
commit 0d2eab4e0a
6 changed files with 37 additions and 4 deletions
+21 -1
View File
@@ -9,6 +9,9 @@ signal shoot()
@export var ship: AbstractEnemyShip
const FREE_FLIGHT_DIST = 50
var target_position : Vector2
var direction : Vector2
@@ -24,4 +27,21 @@ func _on_direction_timer_timeout() -> void:
func get_acceleration_direction() -> Vector2:
return (target_position - ship.position).normalized()
var distance := ship.position.distance_to(target_position)
if distance < FREE_FLIGHT_DIST:
return Vector2.ZERO
var direction_to_target := (target_position - ship.position).normalized()
var speed_to_target := ship.velocity.dot(direction_to_target)
var slow_down_distance := speed_to_target/ship.acceleration * speed_to_target
var speed_coef := distance / (slow_down_distance + FREE_FLIGHT_DIST)
var target_speed := ship.max_speed * clampf(speed_coef, 0.0, 1.0)
var target_velocity := direction_to_target * target_speed
var delta_velocity := target_velocity - ship.velocity
return direction_to_target * ship.max_speed/delta_velocity.length()