139 lines
5.1 KiB
GDScript3
139 lines
5.1 KiB
GDScript3
|
|
extends Area2D
|
||
|
|
|
||
|
|
signal destroyed
|
||
|
|
|
||
|
|
# Deliberately faster than Fleet.cruise_speed: the fleet as a whole travels
|
||
|
|
# at the cruise rate, and this headroom is what lets an individual ship
|
||
|
|
# close on its formation slot - so a pivot mid-flight resolves quickly
|
||
|
|
# instead of the ship having no speed budget left over for it.
|
||
|
|
@export var max_speed: float = 460.0
|
||
|
|
@export var acceleration: float = 450.0
|
||
|
|
@export var rotation_speed: float = 8.0
|
||
|
|
@export var arrive_radius: float = 6.0
|
||
|
|
|
||
|
|
@export var fire_range: float = 320.0
|
||
|
|
@export var fire_cooldown: float = 0.5
|
||
|
|
# How long to wait before looking for targets again after finding none.
|
||
|
|
@export var idle_rescan_interval: float = 0.25
|
||
|
|
@export var projectile_damage: int = 1
|
||
|
|
|
||
|
|
# Neon colours, assigned by Fleet on spawn so a scene can tint its fleet.
|
||
|
|
# The lunar surface is a bright grey, where the default near-white core all
|
||
|
|
# but vanishes - PlanetSurface.tscn passes a dark core there. See NeonDraw.
|
||
|
|
var glow_color: Color = Color(0.25, 0.85, 1.0)
|
||
|
|
var core_color: Color = Color(1.0, 1.0, 1.0)
|
||
|
|
|
||
|
|
var velocity: Vector2 = Vector2.ZERO
|
||
|
|
# The fleet's own velocity, supplied by Fleet as CONTEXT rather than as a
|
||
|
|
# facing command - the ship works out where to point from it.
|
||
|
|
#
|
||
|
|
# It is needed because `velocity` below is not the ship's true motion while
|
||
|
|
# it is holding formation: the slot it chases sits inside arrive_radius, so
|
||
|
|
# the ship snaps onto it and reports zero velocity even though it is being
|
||
|
|
# carried along at cruise speed. Facing derived from `velocity` alone
|
||
|
|
# therefore froze at whatever heading the ship last happened to travel.
|
||
|
|
# Adding the two gives the ship's ACTUAL motion through the world, which is
|
||
|
|
# the honest thing to point along in every case: it equals the fleet's
|
||
|
|
# heading when station-keeping, the ship's own heading when manoeuvring
|
||
|
|
# alone, and the true blend of both while strafing into a new slot.
|
||
|
|
var fleet_velocity: Vector2 = Vector2.ZERO
|
||
|
|
var move_target: Vector2
|
||
|
|
var fire_timer: float = 0.0
|
||
|
|
var projectile_scene: PackedScene = preload("res://Projectile.tscn")
|
||
|
|
|
||
|
|
|
||
|
|
func _ready() -> void:
|
||
|
|
move_target = position
|
||
|
|
add_to_group("player_ships")
|
||
|
|
area_entered.connect(_on_area_entered)
|
||
|
|
queue_redraw()
|
||
|
|
|
||
|
|
|
||
|
|
func set_move_target(world_pos: Vector2) -> void:
|
||
|
|
move_target = world_pos
|
||
|
|
|
||
|
|
|
||
|
|
func _physics_process(delta: float) -> void:
|
||
|
|
var to_target: Vector2 = move_target - position
|
||
|
|
var distance: float = to_target.length()
|
||
|
|
|
||
|
|
if distance <= arrive_radius:
|
||
|
|
# Snap and stop dead - prevents residual float-level jitter that
|
||
|
|
# would otherwise keep nudging back and forth around the target.
|
||
|
|
position = move_target
|
||
|
|
velocity = Vector2.ZERO
|
||
|
|
else:
|
||
|
|
# Braking-distance formula: the fastest speed we can be going right
|
||
|
|
# now and still be able to decelerate to 0 exactly at the target,
|
||
|
|
# given our acceleration. Without this the ship approaches at full
|
||
|
|
# speed, overshoots (stopping distance = max_speed^2 / (2*accel)),
|
||
|
|
# and oscillates back and forth forever instead of settling.
|
||
|
|
var braking_speed: float = sqrt(2.0 * acceleration * distance)
|
||
|
|
var desired_speed: float = min(max_speed, braking_speed)
|
||
|
|
var desired_velocity: Vector2 = to_target.normalized() * desired_speed
|
||
|
|
velocity = velocity.move_toward(desired_velocity, acceleration * delta)
|
||
|
|
position += velocity * delta
|
||
|
|
|
||
|
|
# One owner, one rule: face the way this ship is actually travelling.
|
||
|
|
var motion: Vector2 = velocity + fleet_velocity
|
||
|
|
if motion.length() > 8.0:
|
||
|
|
var target_angle: float = motion.angle() + PI / 2.0
|
||
|
|
rotation = lerp_angle(rotation, target_angle, rotation_speed * delta)
|
||
|
|
|
||
|
|
_update_weapon(delta)
|
||
|
|
|
||
|
|
|
||
|
|
func _update_weapon(delta: float) -> void:
|
||
|
|
fire_timer -= delta
|
||
|
|
if fire_timer > 0.0:
|
||
|
|
return
|
||
|
|
|
||
|
|
var target: Node2D = _find_nearest_enemy()
|
||
|
|
if target == null:
|
||
|
|
# Back off before scanning again. Without this, fire_timer stays at
|
||
|
|
# or below zero and every ship queries the whole "enemies" group and
|
||
|
|
# loops it on EVERY physics frame whenever nothing is in range -
|
||
|
|
# which is most of the time.
|
||
|
|
fire_timer = idle_rescan_interval
|
||
|
|
return
|
||
|
|
|
||
|
|
fire_timer = fire_cooldown
|
||
|
|
var proj: Node2D = projectile_scene.instantiate()
|
||
|
|
proj.position = position
|
||
|
|
proj.damage = projectile_damage
|
||
|
|
get_tree().root.add_child(proj)
|
||
|
|
proj.launch(target.position - position)
|
||
|
|
|
||
|
|
|
||
|
|
func _find_nearest_enemy() -> Node2D:
|
||
|
|
var enemies: Array = get_tree().get_nodes_in_group("enemies")
|
||
|
|
var nearest: Node2D = null
|
||
|
|
var best_dist: float = fire_range
|
||
|
|
|
||
|
|
for e in enemies:
|
||
|
|
if not is_instance_valid(e):
|
||
|
|
continue
|
||
|
|
var d: float = position.distance_to(e.position)
|
||
|
|
if d <= best_dist:
|
||
|
|
best_dist = d
|
||
|
|
nearest = e
|
||
|
|
|
||
|
|
return nearest
|
||
|
|
|
||
|
|
|
||
|
|
func _on_area_entered(area: Area2D) -> void:
|
||
|
|
if area.is_in_group("enemies") or area.is_in_group("asteroids"):
|
||
|
|
ShipExplosionVFX.spawn(get_tree().root, position)
|
||
|
|
destroyed.emit()
|
||
|
|
queue_free()
|
||
|
|
|
||
|
|
|
||
|
|
func _draw() -> void:
|
||
|
|
var nose: Vector2 = Vector2(0, -22)
|
||
|
|
var left: Vector2 = Vector2(-14, 16)
|
||
|
|
var right: Vector2 = Vector2(14, 16)
|
||
|
|
var points: PackedVector2Array = PackedVector2Array([nose, left, right])
|
||
|
|
# Constant glow, drawn once. No pulse means nothing changes frame to
|
||
|
|
# frame, so the ship never needs redrawing after _ready() - which also
|
||
|
|
# removes four antialiased polyline rebuilds per ship per frame.
|
||
|
|
NeonDraw.draw_glowing_polygon(self, points, glow_color, 1.0, core_color)
|