tool
extends Area2D

onready var anim_player: AnimationPlayer = $AnimationPlayer
onready var levelState := get_tree().root.get_child(3).get_node("%LevelState")
onready var signalManager := get_tree().root.get_child(3).get_node("%SignalManager")
onready var levelName := get_tree().get_current_scene().get_name()

export(String, FILE, "*.tscn") var next_scene


func _get_configuration_warning() -> String:
	return "The next scene property can't be empty" if not next_scene else ""

func level_completion() -> void:
	signalManager.emit_signal("level_completed")
	

func teleport() -> void:
	get_tree().paused = true
	anim_player.play("fade_in")
	# TODO This doesn't pause the game but should
	yield(anim_player, "animation_finished")
	if ResourceLoader.exists(next_scene):
		get_tree().change_scene(next_scene)


func _on_body_entered(_body: Node) -> void:
	level_completion()
	teleport()
