extends KinematicBody2D

const GRAVITY = 6000.0

var _velocity = Vector2.ZERO
var air_deaccel = 300.0
var falling_speed_x = 600.0

func _process(delta):
	_velocity.y += GRAVITY * delta
	_velocity.x -= air_deaccel * delta
	move_and_slide(_velocity)

func move(body):
	$AnimationPlayer.play("move")
	$activate.set_deferred("monitoring", false)
	var tween = $Tween
	var railroad = get_parent().rails
	var player = Audio.play_sound("minecart_driving", true)
	for i in railroad.size() - 1:
		var time = range_lerp(railroad[i].distance_to(railroad[i+1]), 0, 2000, 0, 3)
		tween.interpolate_property(self, "position", railroad[i], railroad[i+1], time, Tween.TRANS_LINEAR,Tween.EASE_IN_OUT)
		tween.start()
		yield(tween, "tween_completed")
	player.stop()
	stop()

func stop():
	$AnimationPlayer.stop()
	_velocity.x = falling_speed_x
	set_process(true)

func _ready():
	set_process(false)
	$activate.connect("body_entered", self, "move")
