extends Node2D class_name GameControler func _unhandled_input(event: InputEvent) -> void: if event.is_action_pressed("menu"): if prev_scene == menu: start_game() else: open_menu() # list of scenes var outside:Node2D = null var dest_outside = preload("res://scenes/outside.tscn") var bakery:Node2D = null var dest_bakery = preload("res://scenes/bakery_interior.tscn") var menu = null var prev_scene = null func _ready() -> void: GameState._game = self menu = get_child(0) prev_scene = menu func start_game(): switch_to(GameState.current_scene) func switch_to(to: String): call_deferred("switch_scene", to) var next_scene = null var toMenu = false; var switching_to func switch_scene(to: String): switching_to= to print("switching to", switching_to) toMenu = to == "menu" match to: "outside": if outside == null: outside = dest_outside.instantiate() next_scene = outside "bakery": if bakery == null: bakery = dest_bakery.instantiate() next_scene = bakery "menu": next_scene = menu $TransitionShader.fade_out() func init_scence(scene): scene.load_from_game_state() $TransitionShader.fade_in() func open_menu(): switch_scene("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(), " ", true, true)) print(OS.get_data_dir()) save_file.close() func _on_transition_shader_fade_in_done() -> void: if !toMenu: GameState.current_scene = switching_to func _on_transition_shader_fade_out_done() -> void: print("fade out done") if !toMenu: next_scene.unload() call_deferred("add_child", next_scene) call_deferred("remove_child", prev_scene) if !toMenu: call_deferred("init_scence", next_scene) else: $TransitionShader.fade_in() prev_scene = next_scene