extends Node

export var song_name = "level_1"

onready var time_label = $gui/game/time

var time = 0.0

func _process(delta):
	time += delta
	var mils = fmod(time, 1) * 1000
	var secs = fmod(time, 60)
	var mins = fmod(time, 60 * 60) / 60
	var time_passed = "%02d:%02d:%03d" % [mins, secs, mils]
	time_label.text = time_passed

func restart_level():
	$fader_layer/fader.fade_in()
	yield($fader_layer/fader, "finished")
	Audio.reset_players()
	get_tree().reload_current_scene()
	
func finish_level():
	$gui.show_final_screen()
	Audio.play_music("level_complete", false)

func _ready():
	Global.level = self
	if Global.checkpoint != -1:
		var checkpoint = $checkpoints.get_child(Global.checkpoint)
		checkpoint.activate()
		var spawn_pos = checkpoint.get_node("spawn_pos").global_position
		$actors/player.global_position = spawn_pos
	if Global.saved_time != 0:
		time = Global.saved_time
	$fader_layer/fader.call_deferred("fade_out")
	$kill_area.player_node = $actors/player
	Audio.play_music(song_name, true)
