extends Node2D

signal complete()

func reload_current_level():
	var timer = get_node("reloading_timer")
	timer.start()
	yield(timer,"timeout")
	if get_node("level") == null:
		return
	get_node("level").free()
	var level = load("res://scenes/levels/level_" + str(global.current_zone_id) + "_" + str(global.current_level_id) + ".scn").instance()
	add_child(level)
	get_node("player").set_pos(get_node("level").get_node("door").get_global_pos())
	get_node("player").resurrect()
	emit_signal("complete")
	
func load_next_level():
	var timer = get_node("reloading_timer")
	timer.start()
	yield(timer,"timeout")
	if get_node("level") == null:
		return
	get_node("level").free()
	var level = load("res://scenes/levels/level_" + str(global.current_zone_id) + "_" + str(global.current_level_id) + ".scn").instance()
	add_child(level)
	get_node("player").set_pos(get_node("level").get_node("door").get_global_pos())
	emit_signal("complete")
	
func load_level():
	var level = load("res://scenes/levels/level_" + str(global.current_zone_id) + "_" + str(global.current_level_id) + ".scn").instance()
	add_child(level)
	get_node("player").set_pos(get_node("level").get_node("door").get_global_pos())

func _ready():
	global.game = self
	load_level()

func _on_game_exit_tree():
	global.game = null
