extends KinematicBody
var path = []
var path_node = 0
var speed = 15
var total_time = 0
var rng = RandomNumberGenerator.new()

onready var nav = get_parent()
onready var player = $"../../Player"

func _ready():
	rng.randomize()
	
func _physics_process(delta):
	if path_node < path.size():
		var direction = (path[path_node] - global_transform.origin)
		if direction.length() < 1:
			path_node +=1
		else:
			var target_position = path[path_node]
			var new_transform = transform.looking_at(target_position, Vector3.UP)
			transform = transform.interpolate_with(new_transform, 5 * delta)
			move_and_slide(direction.normalized() * ((cos(total_time)+1)*7), Vector3.UP)
			total_time += delta*2
			
func move_to(target_pos):
	path = nav.get_simple_path(global_transform.origin, target_pos)
	path_node = 0

func _on_Timer_timeout():
	move_to(player.global_transform.origin)

func _on_Gurgletimer_timeout():
	$AudioStreamPlayer3D2.play(0.0)
