spaceship/EnemySpawnerComponent.gd
Loïc Wisniewski 472de1d93e Enemy mothership (#53)
A large, slow capital enemy that periodically launches chasers and
artillery on their own intervals and fires a heavily telegraphed line
beam capable of killing several player vessels at once.

Spawning reuses #52's EnemySpawnerComponent as-is via two preset
scenes (EnemySpawnerComponentChaser.tscn: 5s/chaser,
EnemySpawnerComponentArtillery.tscn: 18s/max 2/artillery) rather than
adding multi-type support to the component - two instances of a
single-purpose component is the composition this codebase already
uses everywhere ("enemy bomber" in the original request is read here
as the existing artillery enemy's slow telegraphed AOE, not a new
enemy type - flagged as a judgment call in the ticket).

New EnemyBigLaserComponent.gd ("big_laser") fires a standalone
EnemyBigLaserBeam on a long cooldown - the beam owns its whole charge/
fire/flash sequence once launched, so the mothership dying mid-charge
doesn't cancel an already-telegraphed shot, same design as
EnemyArtilleryComponent/ArtilleryShell. The aim direction is locked
once in launch() and never re-read - nothing in the charging phase
touches the fleet's position again, which is what makes it dodgeable.
Damage is a line-segment check (projection + perpendicular distance)
against every player_ships member; Ship.take_damage() already handles
shield absorption per-ship.

MothershipEnemyDefinition.tres: max_hp 60, speed 35 (slow), a ~96-unit
hull, pursuit + two spawners + big_laser, no type switch anywhere -
every stat from the definition, every behaviour from a component.

Tests: new tests/test_enemy_mothership.gd, split into beam mechanics
(tested directly, not through the component's own cooldown timing,
which would make the geometry assertions timing-fragile) and the
composed definition (tested through a real Enemy.tscn instance).
Screenshot of the charge telegraph mid-countdown. Full suite: 80/80.

Found and fixed a real bug along the way, in EnemySpawnerComponent.gd
(#52): its `enemy_scene: PackedScene = preload("res://Enemy.tscn")`
member-level preload could get permanently baked in as a broken,
empty PackedScene when a definition's component_scenes chain led back
to Enemy.tscn while it was still mid-load - confirmed empirically via
ResourceLoader.has_cached() being true (a good copy existed) while the
preloaded reference's own resource_path was empty. This only
surfaces when a SINGLE definition composes two instances of a script
carrying such a preload (the mothership's chaser/artillery spawner
presets; #52's single-spawner turret never hit it). Fixed by
resolving enemy_scene lazily via load() on first use instead of at
compile time, and additionally made a fresh spawner count down from a
full interval before its first spawn rather than firing on tick one,
which also happens to keep two sibling spawners with different
intervals from ever landing their first attempt on the same frame.

🤖 Generated with Claude Code

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-16 10:19:02 +02:00

78 lines
3.1 KiB
GDScript

class_name EnemySpawnerComponent
extends Node
# Continuously spawns another enemy type beside its host, at a fixed
# interval, capped at max_alive concurrently. The spawned enemies are
# added as children of the spawner's OWN PARENT - not the spawner itself -
# so killing the spawner never frees its already-spawned children (they
# are siblings, not descendants) and so a surface-hosted spawner keeps its
# spawns under the same host a `../Fleet`-style path assumption would miss
# (see #44's note on why that hazard exists for anything parented under a
# planet surface's PlanetSetupHost rather than directly under Main.tscn).
var capability_id: String = "spawner"
@export var spawn_interval: float = 4.0
@export var spawned_enemy_id: String = "chaser"
@export var max_alive: int = 6
@export var spawn_radius: float = 90.0
var spawn_timer: float = 0.0
var spawned: Array = []
# Resolved lazily via load() on first use rather than preload()d as a
# compile-time constant: when a definition's component_scenes chain leads
# BACK to Enemy.tscn while it is still mid-load (e.g. two different preset
# scenes of this same script both live on one definition, as the
# mothership's chaser/artillery spawners are), the preloaded constant can
# get permanently baked in as a broken, empty PackedScene - confirmed by
# ResourceLoader.has_cached() being true (a good copy exists) while the
# preloaded reference's own resource_path was empty. load() at first real
# use instead always resolves against whatever is actually in the cache
# by then.
var enemy_scene: PackedScene = null
func _ready() -> void:
# Start counting down from a full interval rather than firing on the
# very first tick: besides reading more naturally (a freshly-placed
# spawner doesn't insta-spawn), this keeps two sibling spawners with
# different intervals (e.g. the mothership's chaser/artillery presets)
# from both landing their first attempt on the exact same physics
# frame - which hit a transient PackedScene.instantiate() failure on
# this engine build when it happened.
spawn_timer = spawn_interval
func tick(delta: float) -> void:
spawned = spawned.filter(func(node): return is_instance_valid(node))
if spawned.size() >= max_alive:
return
spawn_timer -= delta
if spawn_timer > 0.0:
return
spawn_timer = spawn_interval
var fleet: Node2D = get_tree().get_first_node_in_group("player_fleet")
if fleet == null or fleet.ships.is_empty():
return
var spawner: Node2D = get_parent()
var host: Node = spawner.get_parent()
if host == null:
return
var angle: float = randf() * TAU
var offset: Vector2 = Vector2(cos(angle), sin(angle)) * spawn_radius
if enemy_scene == null:
enemy_scene = load("res://Enemy.tscn")
var new_enemy: Node2D = enemy_scene.instantiate()
new_enemy.enemy_id = spawned_enemy_id
# spawner.position is already expressed in `host`'s local space, and
# new_enemy is about to be added to that same `host` - unlike
# EnemyRangedWeaponComponent's projectile, which reparents to root and
# needs global_position, there is no space conversion needed here.
new_enemy.position = spawner.position + offset
host.add_child(new_enemy)
spawned.append(new_enemy)