Add enemy foundation: definitions, components, ranged damage (#44)

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.
This commit is contained in:
Loïc Wisniewski 2026-08-15 14:43:29 +02:00
commit f14ee656ea
20 changed files with 393 additions and 42 deletions

View file

@ -2,50 +2,53 @@ extends Area2D
signal destroyed
@export var speed: float = 140.0
@export var acceleration: float = 260.0
@export var rotation_speed: float = 6.0
@export var max_hp: int = 3
const ENEMY_DEFINITIONS = preload("res://EnemyDefinitions.gd")
var velocity: Vector2 = Vector2.ZERO
@export var enemy_id: String = "chaser"
var definition: Resource
var hp: int
@onready var fleet: Node2D = get_node("../Fleet")
# Mirrors Ship.gd's own max_speed/acceleration: copied from the definition
# once, then mutable, so a test/preview can pin an enemy in place (e.g.
# `enemy.speed = 0.0`) without needing to know about definitions at all.
var speed: float
var acceleration: float
var rotation_speed: float
var max_hp: int
func _ready() -> void:
hp = max_hp
add_to_group("enemies")
definition = ENEMY_DEFINITIONS.get_definition(enemy_id)
speed = definition.speed
acceleration = definition.acceleration
rotation_speed = definition.rotation_speed
max_hp = definition.max_hp
hp = max_hp
for component_scene in definition.component_scenes:
var component: Node = component_scene.instantiate()
add_child(component)
if component.has_method("setup"):
component.setup()
queue_redraw()
func get_component(capability: String) -> Node:
for child in get_children():
if child.get("capability_id") == capability:
return child
return null
func has_capability(capability: String) -> bool:
return get_component(capability) != null
func _physics_process(delta: float) -> void:
var target_pos: Vector2 = _nearest_ship_position()
var to_target: Vector2 = target_pos - position
if to_target.length() > 1.0:
var desired_velocity: Vector2 = to_target.normalized() * speed
velocity = velocity.move_toward(desired_velocity, acceleration * delta)
position += velocity * delta
if velocity.length() > 5.0:
var target_angle: float = velocity.angle() + PI / 2.0
rotation = lerp_angle(rotation, target_angle, rotation_speed * delta)
func _nearest_ship_position() -> Vector2:
if fleet.ships.is_empty():
return position
var nearest: Node2D = fleet.ships[0]
var best_dist: float = position.distance_to(nearest.position)
for ship in fleet.ships:
var d: float = position.distance_to(ship.position)
if d < best_dist:
best_dist = d
nearest = ship
return nearest.position
for child in get_children():
if child.has_method("tick"):
child.tick(delta)
func take_damage(amount: int) -> void:
@ -57,8 +60,9 @@ func take_damage(amount: int) -> void:
func _draw() -> void:
var points: PackedVector2Array = PackedVector2Array([
Vector2(0, -16), Vector2(12, 0), Vector2(0, 16), Vector2(-12, 0)
var points: PackedVector2Array = definition.hull_points if definition != null else PackedVector2Array([
Vector2(0, -16), Vector2(12, 0), Vector2(0, 16), Vector2(-12, 0),
])
var glow: Color = definition.glow_color if definition != null else Color(1.0, 0.15, 0.2)
# Constant glow - see Ship.gd. Nothing animates, so no per-frame redraw.
NeonDraw.draw_glowing_polygon(self, points, Color(1.0, 0.15, 0.2), 1.0)
NeonDraw.draw_glowing_polygon(self, points, glow, 1.0)