New EnemySpawnerComponent.gd (capability_id "spawner"): continuously instantiates another enemy type beside its host at a fixed interval, capped at max_alive concurrently, refilling as spawns die. Spawned enemies are parented under the SPAWNER'S OWN PARENT rather than the spawner itself, so they are siblings, not descendants - killing the spawner can't free them, and a surface-hosted spawner (under PlanetSetupHost) keeps its spawns under the same host instead of leaking them to a hardcoded path, the same hazard #44 already closed for pursuit/standoff/ranged-weapon components. TurretSpawnerEnemyDefinition.tres: stationary (speed 0, no pursuit or standoff component), max_hp 20 (~7x a chaser), a bulky octagon hull, and its own acid-green glow distinct from chaser/sniper/artillery. Registered in EnemyDefinitions.ORDER/DEFINITIONS - the map editor palette (#48) picks it up with no editor code change. Tests: new tests/test_turret_spawner_enemy.gd covers registration and real definition defaults, no spawns without a fleet, spawning capped at max_alive, a dead spawn's slot refilling, never moving, surviving 19 damage and dying on the 20th, and its spawns surviving its death with their pursuit component intact. Screenshot beside a chaser confirms the two read as visually distinct enemies. Full suite: 79/79. Repo notes for the next agent, confirmed empirically this session: - A node's set_process(false) call made before it has fully finished entering the tree doesn't reliably stick (already documented in tests/test_planet_atmosphere_transition.gd, and it bit this test too): BuildController._check_fleet_wipe() kept rebuilding a ship during an intended "empty fleet" test window even after disabling it in _initialize(). Do it in _process()'s first real tick instead, once _ready() cascades are confirmed complete. - Overriding an @export interval (like spawn_interval) on an already-running component does NOT reset its own in-progress countdown timer - only changes what it resets TO on its next expiry. If the countdown already started at the OLD value before your override lands, reset the timer variable itself too, or the first real interval after the override silently uses the stale value. - A capped spawner's freed-slot refill can land within a couple of frames once its interval is short - don't assert on an intermediate "just dropped by one" count that a fast interval can blow straight through before your next check runs. Assert the net effect instead (the specific killed instance is gone AND a replacement filled the slot) rather than an exact mid-transition snapshot. 🤖 Generated with Claude Code Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
55 lines
1.9 KiB
GDScript
55 lines
1.9 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 = []
|
|
var enemy_scene: PackedScene = preload("res://Enemy.tscn")
|
|
|
|
|
|
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
|
|
|
|
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)
|