Updated enemy controller
This commit is contained in:
@@ -9,6 +9,9 @@ signal shoot()
|
|||||||
@export var ship: AbstractEnemyShip
|
@export var ship: AbstractEnemyShip
|
||||||
|
|
||||||
|
|
||||||
|
const FREE_FLIGHT_DIST = 50
|
||||||
|
|
||||||
|
|
||||||
var target_position : Vector2
|
var target_position : Vector2
|
||||||
var direction : Vector2
|
var direction : Vector2
|
||||||
|
|
||||||
@@ -24,4 +27,21 @@ func _on_direction_timer_timeout() -> void:
|
|||||||
|
|
||||||
|
|
||||||
func get_acceleration_direction() -> Vector2:
|
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()
|
||||||
|
|||||||
@@ -31,3 +31,6 @@ max_hull = 200
|
|||||||
[node name="HeathBar" parent="." index="3"]
|
[node name="HeathBar" parent="." index="3"]
|
||||||
offset_top = 30.0
|
offset_top = 30.0
|
||||||
offset_bottom = 30.0
|
offset_bottom = 30.0
|
||||||
|
|
||||||
|
[node name="VisibleOnScreenNotifier2D" parent="." index="5"]
|
||||||
|
rect = Rect2(-29, -21, 58, 42)
|
||||||
|
|||||||
@@ -30,3 +30,6 @@ max_hull = 100
|
|||||||
[node name="HeathBar" parent="." index="3"]
|
[node name="HeathBar" parent="." index="3"]
|
||||||
offset_top = 30.0
|
offset_top = 30.0
|
||||||
offset_bottom = 30.0
|
offset_bottom = 30.0
|
||||||
|
|
||||||
|
[node name="VisibleOnScreenNotifier2D" parent="." index="5"]
|
||||||
|
rect = Rect2(-13, -21, 26, 42)
|
||||||
|
|||||||
@@ -30,3 +30,6 @@ max_hull = 50
|
|||||||
[node name="HeathBar" parent="." index="3"]
|
[node name="HeathBar" parent="." index="3"]
|
||||||
offset_top = 14.0
|
offset_top = 14.0
|
||||||
offset_bottom = 14.0
|
offset_bottom = 14.0
|
||||||
|
|
||||||
|
[node name="VisibleOnScreenNotifier2D" parent="." index="5"]
|
||||||
|
rect = Rect2(-13, -5, 26, 10)
|
||||||
|
|||||||
@@ -45,6 +45,10 @@ func _apply_homing_guidance(delta: float) -> void:
|
|||||||
|
|
||||||
|
|
||||||
func _update_sprite(velocity: Vector2) -> void:
|
func _update_sprite(velocity: Vector2) -> void:
|
||||||
|
if velocity.is_zero_approx():
|
||||||
|
sprites[0].show()
|
||||||
|
return
|
||||||
|
|
||||||
var sector := TAU / sprites.size()
|
var sector := TAU / sprites.size()
|
||||||
var angle := velocity.angle()
|
var angle := velocity.angle()
|
||||||
var bisector := angle + sector * 0.5
|
var bisector := angle + sector * 0.5
|
||||||
|
|||||||
+3
-3
@@ -15,16 +15,16 @@ func _ready() -> void:
|
|||||||
|
|
||||||
func _create_player() -> void:
|
func _create_player() -> void:
|
||||||
var player : PlayerShip = PLAYER.instantiate()
|
var player : PlayerShip = PLAYER.instantiate()
|
||||||
add_child(player)
|
|
||||||
player.position = Vector2(100, 100)
|
player.position = Vector2(100, 100)
|
||||||
player.destroyed.connect(_create_player, CONNECT_DEFERRED)
|
player.destroyed.connect(_create_player, CONNECT_DEFERRED)
|
||||||
|
add_child(player)
|
||||||
|
|
||||||
|
|
||||||
func _create_random_enemy() -> void:
|
func _create_random_enemy() -> void:
|
||||||
const ENEMIES := [ SMALL_ENEMY, MEDIUM_ENEMY, HEAVY_ENEMY ]
|
const ENEMIES := [ SMALL_ENEMY, MEDIUM_ENEMY, HEAVY_ENEMY ]
|
||||||
|
|
||||||
var enemy : AbstractEnemyShip = ENEMIES.pick_random().instantiate()
|
var enemy : AbstractEnemyShip = ENEMIES.pick_random().instantiate()
|
||||||
add_child(enemy)
|
|
||||||
enemy.position = Vector2(750, randi_range(0, 360))
|
enemy.position = Vector2(750, randi_range(0, 360))
|
||||||
enemy.controller.target_position = Vector2(550, 180)
|
|
||||||
enemy.destroyed.connect(_create_random_enemy, CONNECT_DEFERRED)
|
enemy.destroyed.connect(_create_random_enemy, CONNECT_DEFERRED)
|
||||||
|
add_child(enemy)
|
||||||
|
enemy.controller.target_position = Vector2(550, 180)
|
||||||
|
|||||||
Reference in New Issue
Block a user