extends Node2D


var point
var isSliding=false

var currentTrail:Line2D

const STARTALPHA=0.5
const MAXIMUM_POINTS=100 #maximum points per trail
const FADETIME=120 #in seconds. how long a trail should last (if alpha would start at 1.0)

func _ready() -> void:
	pass

func _physics_process(delta: float) -> void:
	if isSliding:
		point = global_position
		currentTrail.add_point(point)
		if currentTrail.points.size()>MAXIMUM_POINTS:
			currentTrail.remove_point(0) #remove points from start
			
	for c:Line2D in get_children():
		c.default_color.a=max(c.default_color.a-1.0/FADETIME*delta,0.0)
		if c.default_color.a<=0: #not visible anymore
			c.queue_free()

func setSliding(psliding:bool):
	if not isSliding and psliding: #just started sliding
		#Create new Line
		currentTrail=Line2D.new()
		add_child(currentTrail)
		currentTrail.width=4
		currentTrail.default_color=Color(0,0,0,STARTALPHA)
		var curve=Curve.new()
		curve.add_point(Vector2(0,0),0,5,Curve.TANGENT_FREE,Curve.TANGENT_FREE)
		curve.add_point(Vector2(0.5,1),0,0,Curve.TANGENT_FREE,Curve.TANGENT_FREE)
		curve.add_point(Vector2(1,0),-5,0,Curve.TANGENT_FREE,Curve.TANGENT_FREE)
		currentTrail.width_curve=curve
		currentTrail.set_as_top_level(true) #position is global position
		currentTrail.z_index=8 #above road, behind car
		
	isSliding=psliding
