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/scene_outside.tscn") var bakery:Node2D = null var dest_bakery = preload("res://scenes/scene_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): GameState._is_switiching = true switching_to= to print("switching to ", switching_to) toMenu = to == "menu" if !toMenu: GameState.current_scene = switching_to 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 if prev_scene != menu: prev_scene.pause() $TransitionShader.fade_out() 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 next_scene != menu: next_scene.resume() GameState._is_switiching = false prev_scene = next_scene func init_scence(scene): if !toMenu: scene.load_from_game_state() $TransitionShader.fade_in() func _on_transition_shader_fade_out_done() -> void: if prev_scene != menu: prev_scene.unload() call_deferred("remove_child", prev_scene) call_deferred("add_child", next_scene) call_deferred("init_scence", next_scene)