spaceship/Projectile.gd

47 lines
1.4 KiB
GDScript3
Raw Normal View History

extends Area2D
@export var speed: float = 500.0
@export var lifetime: float = 1.5
@export var damage: int = 1
@export var bolt_length: float = 20.0
var velocity: Vector2 = Vector2.ZERO
var age: float = 0.0
func _ready() -> void:
add_to_group("player_projectiles")
area_entered.connect(_on_area_entered)
func launch(direction: Vector2) -> void:
velocity = direction.normalized() * speed
rotation = direction.angle() + PI / 2.0
func _physics_process(delta: float) -> void:
position += velocity * delta
age += delta
# Redrawn every frame. In principle a fixed shape only needs drawing
# once (see Asteroid.gd, where dropping this was a large win), but
# doing that here left bolts invisible on screen - verified by
# rendering a frame mid-combat and finding zero pixels of the bolt
# colour. Rather than ship invisible shots a second time, this keeps
# the redraw: projectiles are few and live under two seconds, so the
# cost is negligible.
queue_redraw()
if age >= lifetime:
queue_free()
func _on_area_entered(area: Area2D) -> void:
if area.is_in_group("enemies"):
area.take_damage(damage)
queue_free()
func _draw() -> void:
# Fixed-length, constant-brightness bolt. Drawn once and left alone.
var tip: Vector2 = Vector2(0, -bolt_length * 0.6)
var tail: Vector2 = Vector2(0, bolt_length * 0.4)
NeonDraw.draw_glowing_line(self, tail, tip, Color(1.0, 0.85, 0.3), 1.0, 0.5)