extends Node2D

onready var userScene = preload("res://objects/User.tscn")

onready var kabinler = $kabinlerPaths

func _ready():
	global.para = 500
	global.begeniler = 0
	get_tree().paused = false
	$UI/game_over.hide()
	kabinler = kabinler.get_children()
	for i in kabinler:
		var user = userScene.instance()
		i.get_node("takip").unit_offset = 0.8
		i.get_node("takip").add_child(user)
		user.global_position = i.get_node("takip").global_position
	pass
	
func _process(delta):
	if global.begeniler < 0:
		get_tree().paused = true
		$UI/game_over.show()
		$UI/game_over/Panel/HBoxContainer/VBoxContainer/score.text = str(global.para)
		pass
	pass
	
func _on_cikis_body_entered(body):
	var path = body.get_parent().get_parent()
	if not global.surecBitti[path.name]:
		global.para += 5
		for i in kabinler:
			var kareterYok = (i.get_child(0).get_child_count() <= 0)
			if i.name == path.name and !kareterYok:
				body.queue_free()
				global.surecBitti[i.name] = true
			if kareterYok:
				global.surecBitti[i.name] = false
				yield(get_tree().create_timer(rand_range(0.2, 1)), "timeout")
				karekterOlustur(i.name, i)
	pass
func karekterOlustur(nodeName, i):
	if i.name == nodeName:
		var user = userScene.instance()
		i.get_node("takip").unit_offset = 0.9
		i.get_node("takip").add_child(user)
		user.global_position = i.get_node("takip").global_position
		global.sureBittiler[i.name] = false
	pass
