extends KinematicBody2D

export(int) var max_y_pos = 30
var y_pos = 0

func next_action():
	randomize()
	var timer = get_node("timer")
	var delay = rand_range(0.5, 7)
	timer.set_wait_time(delay)
	timer.start()
	yield(timer, "timeout")
	
	var new_pos
	while(true):
		new_pos =  round(rand_range(0, max_y_pos))
		if new_pos == y_pos:
			continue
		else:
			break
	var x = sign(y_pos - new_pos)
	var t = abs((y_pos - new_pos)/10)
	move(new_pos, t, x)
	y_pos = new_pos
	
func move(y, t, x):
	if x < 0:
		get_node("anim").play_backwards("move_on_rope")
	else:
		get_node("anim").play("move_on_rope")
	var tween = get_node("tween")
	tween.interpolate_method(self, "set_pos", get_pos(), Vector2(get_pos().x, y), t, Tween.TRANS_LINEAR, Tween.EASE_OUT)
	tween.interpolate_property(get_node("net"), "transform/scale", get_node("net").get_scale(), Vector2(1, -y), t, Tween.TRANS_LINEAR, Tween.EASE_OUT)
	tween.start()
	
	yield(tween, "tween_complete")
	get_node("anim").stop(true)
	next_action()

func _ready():
	next_action()
	pass
