extends Node2D

func _unhandled_input(event: InputEvent) -> void:
	if event.is_action_pressed("menu"):
		open_menu()

# list of scenes
var outside:Node2D = null
var dest_outside = preload("res://scenes/outside.tscn")
var menu = null

func start_game():
	menu = get_child(0)
	if outside == null:
		outside = dest_outside.instantiate()
	add_child(outside)
	remove_child(menu)
	GameState.current_scene = "outside"
	outside.load_from_game_state()

func open_menu():
	remove_child(outside)
	add_child(menu)
	
func load_game():
	var save_file = FileAccess.open("user://savegame.save", FileAccess.READ)
	var json_string = ""
	while save_file.get_position() < save_file.get_length():
		json_string += save_file.get_line()
	var json = JSON.new()
	if json.parse(json_string) == OK:
		print("load", json.data)
		GameState.load_save(json.data)

func save_game():
	var save_file = FileAccess.open("user://savegame.save", FileAccess.WRITE)
	save_file.store_line(JSON.stringify(GameState.save()))
	print(OS.get_data_dir())
	save_file.close()
