diff --git a/game/controllers/enemy_controller.gd b/game/controllers/enemy_controller.gd index b3d27cc..94c30f0 100644 --- a/game/controllers/enemy_controller.gd +++ b/game/controllers/enemy_controller.gd @@ -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() diff --git a/game/entities/ships/enemies/heavy/heavy_enemy_ship.tscn b/game/entities/ships/enemies/heavy/heavy_enemy_ship.tscn index d2ed9ed..2db979d 100644 --- a/game/entities/ships/enemies/heavy/heavy_enemy_ship.tscn +++ b/game/entities/ships/enemies/heavy/heavy_enemy_ship.tscn @@ -31,3 +31,6 @@ max_hull = 200 [node name="HeathBar" parent="." index="3"] offset_top = 30.0 offset_bottom = 30.0 + +[node name="VisibleOnScreenNotifier2D" parent="." index="5"] +rect = Rect2(-29, -21, 58, 42) diff --git a/game/entities/ships/enemies/medium/medium_enemy_ship.tscn b/game/entities/ships/enemies/medium/medium_enemy_ship.tscn index f55eafc..fabdf7e 100644 --- a/game/entities/ships/enemies/medium/medium_enemy_ship.tscn +++ b/game/entities/ships/enemies/medium/medium_enemy_ship.tscn @@ -30,3 +30,6 @@ max_hull = 100 [node name="HeathBar" parent="." index="3"] offset_top = 30.0 offset_bottom = 30.0 + +[node name="VisibleOnScreenNotifier2D" parent="." index="5"] +rect = Rect2(-13, -21, 26, 42) diff --git a/game/entities/ships/enemies/small/small_enemy_ship.tscn b/game/entities/ships/enemies/small/small_enemy_ship.tscn index 911631d..c455ce0 100644 --- a/game/entities/ships/enemies/small/small_enemy_ship.tscn +++ b/game/entities/ships/enemies/small/small_enemy_ship.tscn @@ -30,3 +30,6 @@ max_hull = 50 [node name="HeathBar" parent="." index="3"] offset_top = 14.0 offset_bottom = 14.0 + +[node name="VisibleOnScreenNotifier2D" parent="." index="5"] +rect = Rect2(-13, -5, 26, 10) diff --git a/game/entities/weapons/launcher/launcher_projectile.gd b/game/entities/weapons/launcher/launcher_projectile.gd index 3d54540..40b5059 100644 --- a/game/entities/weapons/launcher/launcher_projectile.gd +++ b/game/entities/weapons/launcher/launcher_projectile.gd @@ -45,6 +45,10 @@ func _apply_homing_guidance(delta: float) -> void: func _update_sprite(velocity: Vector2) -> void: + if velocity.is_zero_approx(): + sprites[0].show() + return + var sector := TAU / sprites.size() var angle := velocity.angle() var bisector := angle + sector * 0.5 diff --git a/game/passage.gd b/game/passage.gd index 6faa74d..ddcd36a 100644 --- a/game/passage.gd +++ b/game/passage.gd @@ -15,16 +15,16 @@ func _ready() -> void: func _create_player() -> void: var player : PlayerShip = PLAYER.instantiate() - add_child(player) player.position = Vector2(100, 100) player.destroyed.connect(_create_player, CONNECT_DEFERRED) + add_child(player) func _create_random_enemy() -> void: const ENEMIES := [ SMALL_ENEMY, MEDIUM_ENEMY, HEAVY_ENEMY ] var enemy : AbstractEnemyShip = ENEMIES.pick_random().instantiate() - add_child(enemy) enemy.position = Vector2(750, randi_range(0, 360)) - enemy.controller.target_position = Vector2(550, 180) enemy.destroyed.connect(_create_random_enemy, CONNECT_DEFERRED) + add_child(enemy) + enemy.controller.target_position = Vector2(550, 180)