64 lines
1.6 KiB
GDScript3
64 lines
1.6 KiB
GDScript3
|
|
extends Area2D
|
||
|
|
|
||
|
|
signal destroyed
|
||
|
|
|
||
|
|
@export var speed: float = 140.0
|
||
|
|
@export var acceleration: float = 260.0
|
||
|
|
@export var rotation_speed: float = 6.0
|
||
|
|
@export var max_hp: int = 3
|
||
|
|
|
||
|
|
var velocity: Vector2 = Vector2.ZERO
|
||
|
|
var hp: int
|
||
|
|
|
||
|
|
@onready var fleet: Node2D = get_node("../Fleet")
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
func _ready() -> void:
|
||
|
|
hp = max_hp
|
||
|
|
add_to_group("enemies")
|
||
|
|
queue_redraw()
|
||
|
|
|
||
|
|
|
||
|
|
func _physics_process(delta: float) -> void:
|
||
|
|
var target_pos: Vector2 = _nearest_ship_position()
|
||
|
|
var to_target: Vector2 = target_pos - position
|
||
|
|
|
||
|
|
if to_target.length() > 1.0:
|
||
|
|
var desired_velocity: Vector2 = to_target.normalized() * speed
|
||
|
|
velocity = velocity.move_toward(desired_velocity, acceleration * delta)
|
||
|
|
position += velocity * delta
|
||
|
|
|
||
|
|
if velocity.length() > 5.0:
|
||
|
|
var target_angle: float = velocity.angle() + PI / 2.0
|
||
|
|
rotation = lerp_angle(rotation, target_angle, rotation_speed * delta)
|
||
|
|
|
||
|
|
|
||
|
|
func _nearest_ship_position() -> Vector2:
|
||
|
|
if fleet.ships.is_empty():
|
||
|
|
return position
|
||
|
|
|
||
|
|
var nearest: Node2D = fleet.ships[0]
|
||
|
|
var best_dist: float = position.distance_to(nearest.position)
|
||
|
|
for ship in fleet.ships:
|
||
|
|
var d: float = position.distance_to(ship.position)
|
||
|
|
if d < best_dist:
|
||
|
|
best_dist = d
|
||
|
|
nearest = ship
|
||
|
|
return nearest.position
|
||
|
|
|
||
|
|
|
||
|
|
func take_damage(amount: int) -> void:
|
||
|
|
hp -= amount
|
||
|
|
if hp <= 0:
|
||
|
|
EnemyExplosionVFX.spawn(get_tree().root, position)
|
||
|
|
destroyed.emit()
|
||
|
|
queue_free()
|
||
|
|
|
||
|
|
|
||
|
|
func _draw() -> void:
|
||
|
|
var points: PackedVector2Array = PackedVector2Array([
|
||
|
|
Vector2(0, -16), Vector2(12, 0), Vector2(0, 16), Vector2(-12, 0)
|
||
|
|
])
|
||
|
|
# Constant glow - see Ship.gd. Nothing animates, so no per-frame redraw.
|
||
|
|
NeonDraw.draw_glowing_polygon(self, points, Color(1.0, 0.15, 0.2), 1.0)
|