extends CanvasLayer

var config: Config = Config.new()
var save: Save = Save.new(1)
var timestamp: int

var player: Player
var player_offset: Vector2
var player_velocity: Vector2
var player_target: String

@onready var overlay: ColorRect = $ColorRect
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var music_player: AudioStreamPlayer = $MusicPlayer

func _ready() -> void:
	pass


func goto_scene(scene: PackedScene, fade: bool = true):
	if fade:
		await fade_out()
	_deferred_goto_scene.call_deferred(scene)


func _deferred_goto_scene(scene: PackedScene):
	if !scene:
		return
	get_tree().current_scene.free()
	var inst_scene := scene.instantiate()
	get_tree().root.add_child(inst_scene)
	get_tree().current_scene = inst_scene


func fade_in() -> void:
	animation_player.play("fade_in")
	await animation_player.animation_finished
	overlay.hide()


func fade_out() -> void:
	overlay.show()
	animation_player.play("fade_out")
	await animation_player.animation_finished


func set_music(audio_stream: AudioStream):
	if music_player.stream == audio_stream:
		return
	music_player.stream = audio_stream
	music_player.play()
	pass
