WIP: enhance dynamic loading and unloading of rooms

This commit is contained in:
betalars 2025-12-02 21:35:19 +01:00
parent ed89874306
commit 44a6ac3184
4 changed files with 32 additions and 8 deletions

View File

@ -27,6 +27,7 @@ func start_room():
func get_ready(): func get_ready():
id = State.rooms.YOUTH
self.show() self.show()
$sfx/distant_rain.play() $sfx/distant_rain.play()
$"sfx/rain on window".play() $"sfx/rain on window".play()
@ -43,7 +44,7 @@ func pull_save_state(save: SaveGame) -> void:
save.current_room = State.rooms.YOUTH save.current_room = State.rooms.YOUTH
save.mementos_complete = Scenes.completed_sequences save.mementos_complete = Scenes.completed_sequences
func _on_scene_finished(id: int, _repeat:bool): func _on_scene_finished(_id: int, _repeat:bool):
await get_tree().create_timer(3).timeout await get_tree().create_timer(3).timeout
save_room() save_room()

View File

@ -1,6 +1,7 @@
class_name RoomTemplate extends Node3D class_name RoomTemplate extends Node3D
var initialised: bool = false var initialised: bool = false
var id: State.rooms
var has_stage: bool: var has_stage: bool:
set(value): set(value):

View File

@ -39,11 +39,11 @@ var currently_loading_room: String = "":
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
currently_loading_room = get_room_path_from_id(main_menu.save_game_handle.get_most_recent_save().current_room)
main_menu.start_game.connect(load_save) main_menu.start_game.connect(load_save)
main_menu.roll_credits.connect(func(): state_machine.travel("credits_roll")) main_menu.roll_credits.connect(func(): state_machine.travel("credits_roll"))
get_tree().tree_process_mode_changed.connect(pause_mode_changed) get_tree().tree_process_mode_changed.connect(pause_mode_changed)
await get_tree().process_frame await get_tree().process_frame
currently_loading_room = get_room_path_from_id(main_menu.save_game_handle.get_most_recent_save().current_room)
State.pass_stage_to(main_menu) State.pass_stage_to(main_menu)
Scenes.scene_starting.connect(prepare_transition) Scenes.scene_starting.connect(prepare_transition)
@ -54,9 +54,11 @@ func _process(_delta: float) -> void:
if ResourceLoader.load_threaded_get_status(currently_loading_room) == 3: if ResourceLoader.load_threaded_get_status(currently_loading_room) == 3:
if not current_room == null: if not current_room == null:
current_room.queue_free() current_room.queue_free()
State.onready_room = get_room_id_from_path(currently_loading_room)
current_room = ResourceLoader.load_threaded_get(currently_loading_room).instantiate() current_room = ResourceLoader.load_threaded_get(currently_loading_room).instantiate()
print("add room") print("add room")
add_child(current_room) add_child(current_room)
State.onready_room = State.rooms.NULL
move_child(current_room, 0) move_child(current_room, 0)
currently_loading_room = "" currently_loading_room = ""
room_loaded.emit() room_loaded.emit()
@ -110,7 +112,7 @@ func load_save(save: SaveGame):
if currently_loading_room != "": if currently_loading_room != "":
await(room_loaded) await(room_loaded)
if get_room_path_from_id(save.current_room) != get_room_path_from_id(current_room_id): if save.current_room != current_room.id:
# TODO Prevent race condition from appearing when save is loaded while room is still loading. # 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) currently_loading_room = get_room_path_from_id(save.current_room)
await(room_loaded) await(room_loaded)
@ -136,6 +138,18 @@ func get_room_path_from_id(id: State.rooms) -> String:
_: _:
return ending_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(): func start_demo():
#FIXME this causes the room to reload #FIXME this causes the room to reload
#load_save(SaveGame.new()) #load_save(SaveGame.new())
@ -148,17 +162,24 @@ func pause_mode_changed():
var await_swap: bool = false var await_swap: bool = false
func prepare_transition(scene_id: Scenes.id, _repeat): func prepare_transition(scene_id: Scenes.id, _repeat):
if scene_id == Scenes.id.TRANSITION: if scene_id == Scenes.id.TRANSITION:
await_swap = true
#FIXME: this does not need to be part of the sequence #FIXME: this does not need to be part of the sequence
await(get_tree().process_frame) await(get_tree().process_frame)
current_room.prepare_transition() current_room.prepare_transition()
await_swap = true if not _repeat:
currently_loading_room = get_room_path_from_id(State.rooms.TRANSITION) 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): func transition(scene_id: Scenes.id, _repeat):
if scene_id == Scenes.id.TRANSITION: if scene_id == Scenes.id.TRANSITION:
State.reset_focus() State.reset_focus()
await_swap = false await_swap = false
await room_loaded await room_loaded
State.queue_for_stage(current_room) if not _repeat:
Scenes.end_current_sequence() State.queue_for_stage(current_room)
Scenes.end_current_sequence()
else:
State.pass_stage_to(current_room)

View File

@ -358,4 +358,5 @@ enum sequences {
BURNOUT BURNOUT
} }
var current_room = rooms.NULL var current_room: rooms = rooms.NULL
var onready_room: rooms = rooms.NULL