extends Node2D

@export var nextlevel:PackedScene = null

@onready var start = $Start
@onready var exit = $Exit
var player = null


func _ready():
	player = get_tree().get_first_node_in_group("player")
	if player != null:
		player.global_position = start.get_spawn_pos()
	var traps = get_tree().get_nodes_in_group("traps")
	for trap in traps:
		#trap.connect("touchedPlayer", _on_trap_touched_player)
		trap.touchedPlayer.connect(_on_trap_touched_player)
	exit.body_entered.connect(_on_exit_body_entered)
	


func _process(delta):
	if Input.is_action_just_pressed("quit"):
		get_tree().quit()
	elif Input.is_action_just_pressed("reset"):
		get_tree().reload_current_scene()

func _on_deathzone_body_entered(body):
	resetPlayer()
	
func _on_trap_touched_player():
	resetPlayer()
	
func resetPlayer():
	player.velocity = Vector2.ZERO
	player.global_position = start.get_spawn_pos()

func _on_exit_body_entered(body):
	if body is player:
		if nextlevel != null:
			exit.animate()
			player.active = false
			await get_tree().create_timer(1.5).timeout
			get_tree().change_scene_to_packed(nextlevel)
