Enemy.gd now resolves stats/hull/color from EnemyDefinition resources and composes behaviour from capability components (EnemyPursuitComponent holds today's chase-the-nearest-ship logic), the same shape Ship.gd already uses for vessels. Fleet lookups go through a "player_fleet" group instead of a hardcoded ../Fleet sibling path, so an enemy parented anywhere (e.g. under a planet surface's SurfaceFeatures) still finds it. Also closes a real gap: enemies previously had no way to damage the fleet at range at all (Ship only reacted to physical contact, Projectile.gd was player-only). Ship.take_damage() is now a proper entry point - contact damage routes through it unchanged - and EnemyProjectile lets an enemy component fire at player_ships. Both ranged enemy archetypes in #45/#46 build on this. Part of the #40 conquest-mechanic breakdown.
38 lines
1.3 KiB
GDScript
38 lines
1.3 KiB
GDScript
class_name EnemyPursuitComponent
|
|
extends Node
|
|
|
|
# Chases the nearest player ship. Moved out of Enemy.gd verbatim so the
|
|
# behaviour is opt-in per definition rather than hardcoded into every enemy.
|
|
|
|
var capability_id: String = "pursuit"
|
|
var velocity: Vector2 = Vector2.ZERO
|
|
|
|
|
|
func tick(delta: float) -> void:
|
|
var enemy: Node2D = get_parent()
|
|
var target_pos: Vector2 = _nearest_ship_position(enemy)
|
|
var to_target: Vector2 = target_pos - enemy.position
|
|
|
|
if to_target.length() > 1.0:
|
|
var desired_velocity: Vector2 = to_target.normalized() * enemy.speed
|
|
velocity = velocity.move_toward(desired_velocity, enemy.acceleration * delta)
|
|
enemy.position += velocity * delta
|
|
|
|
if velocity.length() > 5.0:
|
|
var target_angle: float = velocity.angle() + PI / 2.0
|
|
enemy.rotation = lerp_angle(enemy.rotation, target_angle, enemy.rotation_speed * delta)
|
|
|
|
|
|
func _nearest_ship_position(enemy: Node2D) -> Vector2:
|
|
var fleet: Node2D = get_tree().get_first_node_in_group("player_fleet")
|
|
if fleet == null or fleet.ships.is_empty():
|
|
return enemy.position
|
|
|
|
var nearest: Node2D = fleet.ships[0]
|
|
var best_dist: float = enemy.position.distance_to(nearest.position)
|
|
for ship in fleet.ships:
|
|
var d: float = enemy.position.distance_to(ship.position)
|
|
if d < best_dist:
|
|
best_dist = d
|
|
nearest = ship
|
|
return nearest.position
|