130 lines
4.7 KiB
GDScript
130 lines
4.7 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 is within beam_half_width takes Ship.take_damage(damage). The
|
|
# ordinary mothership beam remains a finite segment; bosses opt into a ray
|
|
# with no upper distance bound. Ship.take_damage handles shields itself.
|
|
|
|
signal finished
|
|
|
|
@export var charge_time: float = 2.5
|
|
@export var beam_range: float = 1200.0
|
|
@export var infinite_range: bool = false
|
|
@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
|
|
const REDRAW_INTERVAL: float = 1.0 / 30.0
|
|
|
|
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
|
|
var redraw_left: 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:
|
|
redraw_left -= delta
|
|
match phase:
|
|
Phase.CHARGING:
|
|
charge_left -= delta
|
|
if charge_left <= 0.0:
|
|
_fire()
|
|
if redraw_left <= 0.0:
|
|
redraw_left = REDRAW_INTERVAL
|
|
queue_redraw()
|
|
Phase.FLASH:
|
|
flash_time -= delta
|
|
if redraw_left <= 0.0:
|
|
redraw_left = REDRAW_INTERVAL
|
|
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
|
|
if reaches_point(ship.global_position):
|
|
ship.take_damage(damage)
|
|
queue_redraw()
|
|
|
|
|
|
func reaches_point(world_point: Vector2) -> bool:
|
|
var offset: Vector2 = world_point - origin
|
|
var along: float = offset.dot(aim_direction)
|
|
if along < 0.0 or (not infinite_range and along > beam_range):
|
|
return false
|
|
return (offset - aim_direction * along).length() <= beam_half_width
|
|
|
|
|
|
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 length: float = _visible_ray_length() if infinite_range else beam_range
|
|
var to: Vector2 = Vector2(length, 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)
|
|
|
|
|
|
func _visible_ray_length() -> float:
|
|
# Collision is mathematically unbounded, but rasterizing a 20,000-unit
|
|
# primitive wastes fill work outside the screen. Reaching beyond the
|
|
# furthest viewport corner looks identical and follows a moving camera.
|
|
var viewport_world_size: Vector2 = get_viewport_rect().size
|
|
var camera: Camera2D = get_viewport().get_camera_2d()
|
|
if camera == null:
|
|
return viewport_world_size.length() + 256.0
|
|
viewport_world_size /= camera.zoom
|
|
return origin.distance_to(camera.global_position) \
|
|
+ viewport_world_size.length() * 0.5 + 256.0
|