extends Node
# This class contains controls that should always be accessible, like pausing
# the game or toggling the window full-screen.

var map_name = null
var map_path = null
var player = null

# The "_" prefix is a convention to indicate that variables are private,
# that is to say, another node or script should not access them.
onready var _pause_menu = load("res://src/Main/PauseMenu.tscn")


func _init():
	OS.min_window_size = OS.window_size
	OS.max_window_size = OS.get_screen_size()

func _ready():
	change_scene("res://src/Main/MainMenu.tscn",true,"fade_in",global.STATE.MENU)

func _input(event):
	if event.is_action_pressed("toggle_fullscreen"):
		OS.window_fullscreen = not OS.window_fullscreen
		get_tree().set_input_as_handled()
	# The GlobalControls node, in the Stage scene, is set to process even
	# when the game is paused, so this code keeps running.
	# To see that, select GlobalControls, and scroll down to the Pause category
	# in the inspector.
	elif event.is_action_pressed("toggle_pause") and global.game_state == global.STATE.INGAME:
		if !get_tree().paused:
			pause()
		get_tree().set_input_as_handled()

func pause():
	var pause_menu = _pause_menu.instance()
	add_child(pause_menu)
	get_tree().paused = true

func change_scene(path_to_scene : String, reset_stats : bool = false,
		transition = "fade_out", set_game_mode = global.STATE.INGAME):

	global.show_message("")

	if transition != "none":
		if find_node("Player"):
			player.can_move = false
		var transition_effect = load("res://src/Main/SceneTransition.tscn").instance()
		add_child(transition_effect)
		var anim = transition_effect.get_node("AnimationPlayer")
		transition_effect.start(transition)
		yield(anim,"animation_finished")
		global.game_state = set_game_mode

	if map_name != null:
		var map = get_node(map_name)
		remove_child(map)
		map.call_deferred("free")

	var next_map_path = load(path_to_scene)
	var next_map = next_map_path.instance()
	add_child(next_map)
	map_name = next_map.name
	map_path = next_map.get_path()

	global.game_state = set_game_mode

#	if set_game_mode == global.STATE.INGAME:
#		player = get_node(map_name + "/Entities/Player")

	set_process(true)
