extends Node


# Declare member variables here. Examples:
# var a = 2
# var b = "text"

var thread
# Called when the node enters the scene tree for the first time.
func _ready():
	
	pass # Replace with function body.

func event_triggered(event):
	print("evento fue triggereado")
	if event.event_type == "change_level":
		$carga.visible = true
#		var next_level_resource = load(event.info)
		
		thread = Thread.new()
		
		thread.start(self,"load_new_level",ResourceLoader.load_interactive(event.info))
#		var next_level_resource = ResourceLoader.load_interactive(event.info)
		
#		var next_level = next_level_resource.instance()
		
#		$level.add_child(next_level)
		
		pass
		
func load_new_level(interactive_ldr):
	
	while $level.get_child_count()>0:
		for child in $level.get_children():
			child.queue_free()
#			child.queue
	print("eliminado todo el nivel")
	while (true):
		var err = interactive_ldr.poll();
		if(err == ERR_FILE_EOF):
			call_deferred("_on_load_level_done");
			
			$carga.visible = false
			return interactive_ldr.get_resource();
	
	
	
	pass
func _on_load_level_done():
	var level_res = thread.wait_to_finish()
	var scene = level_res.instance();
	$level.add_child(scene);
	
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#	pass

