extends Node2D

@export var next_level : PackedScene

@onready var player : Player = $Player
@onready var goal: Goal = $PathNodes/Goal


func _ready() -> void:
	goal.goal_reached.connect(_on_goal_reached)
	player.player_died.connect(_on_player_died)


func load_next_level():
	goal.goal_reached.disconnect(_on_goal_reached)
	if next_level:
		set_physics_process(false)
		var level_instance = next_level.instantiate()
		get_parent().call_deferred("add_child", level_instance)
		await get_parent().child_entered_tree
		queue_free()


func _on_goal_reached():
	load_next_level()


func _on_player_died():
	get_tree().reload_current_scene()
