extends Node

var player_free: bool = true
var player: Player

@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var explosion: Control = $Explosion

enum Events { NONE, PUMPKIN_CARVE, OUTSIDE_NORMAL }
var current_event: Events = Events.NONE

func transition_start() -> void:
	animation_player.play("transition")

func transition_end() -> void:
	animation_player.play_backwards("transition")

func run_event(event: Events, player_postion: Vector2 = Vector2.ZERO):
	current_event = event
	if player != null && player_postion != Vector2.ZERO:
		player.position = player_postion
	match event:
		Events.PUMPKIN_CARVE:
			player_free = false
			transition_scene_file("uid://ccfdsdgaon63m") # scenes/levels/home.tscn
			await get_tree().scene_changed
			explosion.explode()
			await get_tree().create_timer(2).timeout
			var pumpkin_carve: Control = preload("res://scenes/menus/pumpkin_carve.tscn").instantiate()
			add_child(pumpkin_carve)
		Events.NONE:
			player_free = true
			
func transition_scene_file(scene: String) -> void:
	if !animation_player.is_playing():
		transition_start()
		get_tree().call_deferred("change_scene_to_file", scene)
		await get_tree().scene_changed
		transition_end()
