extends Control var normal_boot : bool = false @export_file(".tscn") var youth_room_path: String @export_file(".tscn") var transition_room_path: String @export_file(".tscn") var adulthood_room_path: String @export_file(".tscn") var ending_path: String @onready var curtain: Curtain = %Curtain @onready var credits_roll: Control = %CreditsRoll @onready var main_menu: MainMenu = %MainMenu @onready var pause_menu: PauseManu = %PauseMenu @onready var room_paths := { State.rooms.NULL: youth_room_path, # Maybe Draeven story? State.rooms.YOUTH: youth_room_path, State.rooms.TRANSITION: transition_room_path, State.rooms.ADULTHOOD: adulthood_room_path, State.rooms.ENDING: ending_path } enum AppState {BOOT, MENU, PLAY, PAUSE, CREDITS} var app_state: AppState = AppState.BOOT: set(value): app_state = value print_debug("main.gd: app_state changing to: %s" % str(app_state)) match app_state: AppState.BOOT: credits_roll.hide() main_menu.hide() pause_menu.hide() AppState.MENU: credits_roll.hide() pause_menu.hide() await main_menu.execute() AppState.PLAY: credits_roll.hide() main_menu.hide() pause_menu.hide() curtain.hide() AppState.PAUSE: credits_roll.hide() main_menu.hide() pause_menu.show() AppState.CREDITS: main_menu.hide() pause_menu.hide() credits_roll.play() func _enter_tree() -> void: print_debug("main.gd: _enter_tree()") func _ready() -> void: print_debug("main.gd: _ready()") await get_tree().process_frame main_menu.continue_button.pressed.connect(func(): app_state = AppState.PLAY) main_menu.credits_button.pressed.connect(func(): app_state = AppState.CREDITS) #TODO: Load the last savegame(?) await %Loading.stop() if normal_boot: print_debug("main.gd: normal boot (loading last save and showing main menu)") app_state = AppState.MENU else: print_debug("main.gd: direct boot (hiding menus and entering main loop)") app_state = AppState.PLAY func start_game(save: SaveGame) -> void: print_debug("main.gd: play_game()") var room_path := room_paths.get(save.current_room, youth_room_path) as String await _load_room(room_path) await State.room.play() func _load_room(next_path: String) -> bool: await curtain.show() %Loading.play() if State.room != null: State.room.unload() State.room = null ResourceLoader.load_threaded_request(next_path, "PackedScene", true) while true: await get_tree().process_frame var state := ResourceLoader.load_threaded_get_status(next_path) match state: ResourceLoader.THREAD_LOAD_LOADED: var next_scene := ResourceLoader.load_threaded_get(youth_room_path) as PackedScene State.room = next_scene.instantiate() as RoomTemplate get_tree().root.add_child(State.room) await State.room.get_ready() %Loading.stop() await curtain.hide() return true ResourceLoader.THREAD_LOAD_FAILED: push_error("Failed to load room.") break return false func _unhandled_input(event: InputEvent) -> void: if event is InputEvent: match app_state: AppState.BOOT: pass AppState.MENU: pass AppState.PLAY: if event.is_action_pressed("ui_menu"): app_state = AppState.PAUSE AppState.PAUSE: if event.is_action_pressed("ui_menu"): app_state = AppState.PLAY # if not get_tree().paused: # get_tree().paused = true # var state_machine := menu_animation["parameters/playback"] # state_machine.travel("reveal_pause_menu") # Input.mouse_mode = Input.MOUSE_MODE_VISIBLE # else: # get_tree().paused = false # var state_machine := menu_animation["parameters/playback"] # state_machine.travel("start_game") # # if State.stage_list[0] is Player: # Input.mouse_mode = Input.MOUSE_MODE_CAPTURED #func debug_youth(): # get_child(1).hide() # get_child(2).hide() # get_child(3).hide() # get_child(0).get_ready() # get_child(0).start() # #func _return_to_menu(): # State.active_save_game = null # # menu_animation["parameters/conditions/start_game"] = false # # State.pass_stage_to(main_menu) # # currently_loading_room = get_room_path_from_id(main_menu.save_game_handle.get_most_recent_save().current_room) # # menu_animation["parameters/conditions/return_to_menu"] = true # await(get_tree().create_timer(0.5).timeout) # menu_animation["parameters/conditions/return_to_menu"] = false # _on_ready_to_unload() # #func load_save(save: SaveGame): # # if currently_loading_room != "": # await(room_loaded) # # if save.current_room != current_room.id: # # TODO Prevent race condition from appearing when save is loaded while room is still loading. # currently_loading_room = get_room_path_from_id(save.current_room) # await(room_loaded) # # menu_animation["parameters/conditions/start_game"] = true # # State.active_save_game = save # in_game = true # current_room.start_room() # #func _on_ready_to_unload(): # if get_child(0) is Node3D: # get_child(0).free() # #func get_room_path_from_id(id: State.rooms) -> String: # match id: # State.rooms.YOUTH, State.rooms.NULL: # return youth_room_path # State.rooms.TRANSITION: # return transition_room_path # State.rooms.ADULTHOOD: # return adulthood_room_path # _: # return ending_path # #func get_room_id_from_path(path: String) -> State.rooms: # match path: # youth_room_path: # return State.rooms.YOUTH # transition_room_path: # return State.rooms.TRANSITION # adulthood_room_path: # return State.rooms.ADULTHOOD # _: # return State.rooms.NULL # # #func start_demo(): # #FIXME this causes the room to reload # #load_save(SaveGame.new()) # current_room.start_room() # in_game = true # #func pause_mode_changed(): # print_debug(get_tree().paused) # #var await_swap: bool = false #func prepare_transition(scene_id: Scenes.id, _repeat): # if scene_id == Scenes.id.TRANSITION: # await_swap = true # #FIXME: this does not need to be part of the sequence # await(get_tree().process_frame) # current_room.prepare_transition() # if not _repeat: # currently_loading_room = get_room_path_from_id(State.rooms.TRANSITION) # else: # # currently_loading_room = get_room_path_from_id(State.rooms.ADULTHOOD) # #func transition(scene_id: Scenes.id, _repeat): # if scene_id == Scenes.id.TRANSITION: # State.reset_focus() # await_swap = false # await room_loaded # if not _repeat: # State.queue_for_stage(current_room) # Scenes.end_current_sequence() # else: # State.pass_stage_to(current_room)