spaceship/EnemyBigLaserBeam.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

106 lines
3.8 KiB
GDScript

class_name EnemyBigLaserBeam
extends Node2D
# Self-contained charge -> fire -> flash sequence for the mothership's big
# laser (see EnemyBigLaserComponent). Lives independently of the enemy that
# fired it once launched - added to the tree root, exactly like
# ArtilleryShell/EnemyProjectile - so the mothership dying mid-charge
# doesn't cancel a shot already telegraphed to the player.
#
# The aim direction is locked once, in launch(), and never touched again:
# that is what makes the telegraph dodgeable. Nothing in _process() re-reads
# the fleet's position while charging.
#
# Damage: every ship in group "player_ships" whose perpendicular distance
# to the beam segment is within beam_half_width, AND whose projection along
# the beam falls inside [0, beam_range], takes Ship.take_damage(damage) -
# same all-or-nothing shield semantics as every other damage source in this
# game (Ship.take_damage already handles the absorb_hit check itself).
signal finished
@export var charge_time: float = 2.5
@export var beam_range: float = 1200.0
@export var beam_half_width: float = 35.0
@export var damage: int = 3
@export var color: Color = Color(0.55, 0.05, 0.85)
enum Phase { CHARGING, FLASH }
const FLASH_DURATION: float = 0.25
var phase: int = Phase.CHARGING
var origin: Vector2 = Vector2.ZERO
var aim_direction: Vector2 = Vector2.RIGHT
var charge_left: float = 0.0
var flash_time: float = 0.0
func launch(from: Vector2, toward: Vector2) -> void:
origin = from
aim_direction = toward.normalized() if toward.length() > 0.001 else Vector2.RIGHT
position = origin
rotation = aim_direction.angle()
charge_left = charge_time
phase = Phase.CHARGING
queue_redraw()
func _process(delta: float) -> void:
match phase:
Phase.CHARGING:
charge_left -= delta
if charge_left <= 0.0:
_fire()
queue_redraw()
Phase.FLASH:
flash_time -= delta
queue_redraw()
if flash_time <= 0.0:
finished.emit()
queue_free()
func _fire() -> void:
phase = Phase.FLASH
flash_time = FLASH_DURATION
for ship in get_tree().get_nodes_in_group("player_ships"):
if not is_instance_valid(ship):
continue
var to_ship: Vector2 = ship.global_position - origin
var along: float = to_ship.dot(aim_direction)
if along < 0.0 or along > beam_range:
continue
var perpendicular: float = (to_ship - aim_direction * along).length()
if perpendicular <= beam_half_width:
ship.take_damage(damage)
queue_redraw()
func _draw() -> void:
# Drawn in local space, rotated in launch() so local +X already points
# along the locked aim_direction - the same reason the beam segment's
# own math is done in world space up in _fire() (global_position/origin)
# while _draw() only ever needs the length along that local +X axis.
match phase:
Phase.CHARGING:
var progress: float = clampf(1.0 - charge_left / maxf(charge_time, 0.001), 0.0, 1.0)
var width: float = lerpf(4.0, beam_half_width * 2.0, progress)
_draw_beam_line(width, 0.65)
# Countdown arc at the origin, the same idiom as
# ArtilleryShell's growing-ring-plus-arc telegraph.
draw_arc(Vector2.ZERO, 40.0, -PI / 2.0, -PI / 2.0 + TAU * progress, 32,
Color(color, 0.85), 8.0, true)
Phase.FLASH:
var fprogress: float = 1.0 - flash_time / FLASH_DURATION
var width: float = lerpf(beam_half_width * 2.2, 6.0, fprogress)
_draw_beam_line(width, 1.0 - fprogress * 0.3)
func _draw_beam_line(width: float, alpha: float) -> void:
# Three-layer glow along local +X - the same soft-halo / core / bright-
# hairline structure as ArtilleryShell._draw_ring, adapted from a ring
# to a straight line.
var to: Vector2 = Vector2(beam_range, 0.0)
draw_line(Vector2.ZERO, to, Color(color, 0.16 * alpha), width * 2.6, true)
draw_line(Vector2.ZERO, to, Color(color, 0.5 * alpha), width, true)
draw_line(Vector2.ZERO, to, Color(1.0, 0.92, 0.98, 0.9 * alpha), width * 0.35, true)