extends Node

var game_scene_path = "res://Game.tscn"
var level_to_set
var game_scene


func load_scene(path):
	var scene = load(path).instantiate()
	get_tree().get_current_scene().set_name("deleted_game_scene")
	get_tree().get_current_scene().queue_free()
	get_tree().get_root().add_child(scene)
	get_tree().set_current_scene(scene)
	game_scene = scene

func restart_level():
	if level_to_set != null:
		print (level_to_set)
		load_level(level_to_set)
	else:
		print("error loading level")

func load_level(level):
	if not ResourceLoader.exists(level):
		return
	load_scene(game_scene_path)
	
	var current_scene = get_tree().get_current_scene()
	if current_scene.has_node("Timer"):
		var timer = current_scene.get_node("Timer")
		timer.timeout.connect(Callable(self, "_on_timer_timeout"))
		level_to_set = level

		timer.start()
	
	
func _on_timer_timeout():
	_set_level(level_to_set)

func _set_level(level):
	var current_scene = get_tree().get_current_scene()
	print ("-- loading level --")
	current_scene.load_level(level)
	#if current_scene.has_node("LevelHolder"):
	#	print ("set level...")
	#	current_scene.get_node("LevelHolder").load_level(level)
	#else:
	#	print ("oops...")
	#	call_deferred("_set_level", level)
