1
0
Fork 0
space-capture/scenes/trail/trail.gd

56 lines
1.2 KiB
GDScript3
Raw Normal View History

2024-08-06 22:46:05 +02:00
extends Sprite2D
2024-08-07 19:56:30 +02:00
class_name Trail
2024-08-15 21:45:07 +02:00
static var _inv_texture_width := NAN
2024-08-06 22:46:05 +02:00
@export var color: Color :
set(value):
2024-08-15 21:45:07 +02:00
color = value
_update_color(color)
@export var show: bool = true :
set(value):
show = value
visible = true
@export var auto_free: bool
2024-08-07 19:56:30 +02:00
var start_position: Vector2 :
set(value):
start_position = value
_update_transform(value, end_position)
var end_position: Vector2 :
set(value):
end_position = value
_update_transform(start_position, value)
2024-08-15 21:45:07 +02:00
var _opacity := 0.
2024-08-07 19:56:30 +02:00
func _update_transform(start: Vector2, end: Vector2) -> void:
global_position = (start + end) * .5
2024-08-15 21:45:07 +02:00
look_at(end)
if is_nan(_inv_texture_width):
_inv_texture_width = 1. / texture.get_width()
scale = Vector2((end - start).length() * _inv_texture_width, 1.)
func _update_color(color: Color) -> void:
var c := Color(color.r, color.g, color.b, color.a * _opacity)
material.set_shader_parameter('color', c)
func _process(delta: float) -> void:
var prev_opacity := _opacity
_opacity = Utils.damp(_opacity, 1. if show else 0., 1e-4, delta)
if _opacity != prev_opacity:
if _opacity < .01:
_opacity = 0.
visible = false
if auto_free:
queue_free()
elif _opacity > .99:
_opacity = 1.
_update_color(color)