extends Spatial


var mousecaptured = true


#Play a random song and maximize the window
func _ready():
	if has_node("Entities/CircularMan"):
		if globalvar.roomspawn != "":
			if has_node(globalvar.roomspawn):
				get_node("Entities/CircularMan").transform = get_node(globalvar.roomspawn).transform
	
	if $AudioStreamPlayer.stream == null:
		play_new_song()
	else:
		var pos = 0.0
		if globalvar.currentsong == $AudioStreamPlayer.stream:
			pos = globalvar.currentsongpos
		$AudioStreamPlayer.play(pos)
	#OS.set_window_maximized(true)


#Handle pausing and restarting
func _process(delta):
	if Input.is_action_just_pressed("ui_cancel"):
		if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
			Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
			get_tree().paused = true
			
		elif Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE:
			Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
			get_tree().paused = false
			
	if Input.is_action_just_pressed("restart") and !get_tree().paused:
		get_tree().reload_current_scene()


#Choose a random song
func play_new_song():
	pass


#Choose a new song when the current one stops
func _on_AudioStreamPlayer_finished():
	play_new_song()
	
func _exit_tree():
	globalvar.set_current_song($AudioStreamPlayer.stream, $AudioStreamPlayer.get_playback_position())
