extends KinematicBody


export var distance = 10.0

export var speed = 10

var moving = false
var backdown = false

var distance_moved = 0

var bodies = 0


func _physics_process(delta):
	if moving:
		translation.y += speed*delta
		distance_moved += speed*delta
		if distance_moved >= distance:
			moving = false
			
	if !moving:
		if bodies == 0 and distance_moved > 0:
			translation.y -= speed*delta
			distance_moved -= speed*delta
			


func _on_Area_body_entered(body):
	if body.is_in_group("entity"):
		bodies += 1
		moving = true
		backdown = false


func _on_Area_body_exited(body):
	bodies -= 1
