extends Node

export var time_per_day : float = 600.0

onready var player = get_tree().get_nodes_in_group("player")[0]
onready var day_timer = $"%day_timer"
onready var day_timer_text = $"%day_timer_text"

func _ready():
	player.connect("starved_to_death", self, "_on_player_starved_to_death")
	day_timer.wait_time = time_per_day
	day_timer.start()

func _process(delta):
	var time_left : int = day_timer.time_left
	var minutes : int = time_left / 60
	var seconds : int = time_left % 60
	day_timer_text.text = "Time Left: %0*d:%0*d" % [2, minutes, 2, seconds]

func _on_player_starved_to_death():
	var node = Dialogic.start("starve")
	node.pause_mode = PAUSE_MODE_PROCESS
	add_child(node)
	get_tree().paused = true
	yield(node, "timeline_end")
	get_tree().paused = false
	get_tree().quit() # go back to main menu when such a thing exists

func reset_day_timer():
	day_timer.start()

func _on_day_timer_timeout():
	player.sleep(false, null)
