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)
|
|
|
|
|
2024-09-01 14:45:22 +02:00
|
|
|
@export var show_trail: bool = true :
|
2024-08-15 21:45:07 +02:00
|
|
|
set(value):
|
2024-09-01 14:45:22 +02:00
|
|
|
show_trail = value
|
2024-08-15 21:45:07 +02:00
|
|
|
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.)
|
|
|
|
|
2024-09-01 14:45:22 +02:00
|
|
|
func _update_color(trail_color: Color) -> void:
|
|
|
|
var c := Color(trail_color.r, trail_color.g, trail_color.b, trail_color.a * _opacity)
|
2024-08-15 21:45:07 +02:00
|
|
|
material.set_shader_parameter('color', c)
|
|
|
|
|
|
|
|
func _process(delta: float) -> void:
|
|
|
|
var prev_opacity := _opacity
|
2024-09-01 14:45:22 +02:00
|
|
|
_opacity = Utils.damp(_opacity, 1. if show_trail else 0., 1e-4, delta)
|
2024-08-15 21:45:07 +02:00
|
|
|
if _opacity != prev_opacity:
|
|
|
|
if _opacity < .01:
|
|
|
|
_opacity = 0.
|
|
|
|
visible = false
|
|
|
|
if auto_free:
|
|
|
|
queue_free()
|
|
|
|
elif _opacity > .99:
|
|
|
|
_opacity = 1.
|
|
|
|
|
|
|
|
_update_color(color)
|