-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_scene_manager.gd
66 lines (53 loc) · 2.44 KB
/
load_scene_manager.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
extends Node
signal progress_changed(progress)
signal new_screen_loading_completed
const LOADING_SCREEN := preload("res://Components/UI/Screens/LoadingScreen/loading_screen.tscn")
const LOAD_ANIMATION_NONE := ""
const LOAD_ANIMATION_INTRO_DEFAULT := "swipe_in_from_right"
const LOAD_ANIMATION_OUTRO_DEFAULT := "swipe_out_to_right"
var _game_screens := preload("res://Components/GameScreens/game_screens.tres")
var _next_screen_path: String
var _progress := []
func _ready():
set_process(false)
func load_scene(
next_screen: GameScreens.Screen,
intro_animation: String = LoadSceneManager.LOAD_ANIMATION_INTRO_DEFAULT,
outro_animation: String = LoadSceneManager.LOAD_ANIMATION_OUTRO_DEFAULT,
) -> void:
_next_screen_path = _game_screens.get_screen_path(next_screen)
var new_loading_screen = LOADING_SCREEN.instantiate()
new_loading_screen.setup(intro_animation, outro_animation)
get_tree().get_root().add_child.call_deferred(new_loading_screen)
progress_changed.connect(new_loading_screen.update_progress_bar)
new_screen_loading_completed.connect(new_loading_screen.start_outro_animation)
await new_loading_screen.loading_screen_coverage_completed
_start_load()
func _start_load() -> void:
var use_sub_threads := false
var state = ResourceLoader.load_threaded_request(_next_screen_path, "", use_sub_threads)
if state == OK:
set_process(true)
func _process(_delta):
var load_status := ResourceLoader.load_threaded_get_status(_next_screen_path, _progress)
# TODO: Logger.debug
match load_status:
ResourceLoader.ThreadLoadStatus.THREAD_LOAD_FAILED:
assert(false, "LoadSceneManager failed to load scene: " + _next_screen_path)
set_process(false)
ResourceLoader.ThreadLoadStatus.THREAD_LOAD_IN_PROGRESS:
progress_changed.emit(_progress[0])
ResourceLoader.ThreadLoadStatus.THREAD_LOAD_LOADED:
var load_resource := ResourceLoader.load_threaded_get(_next_screen_path)
progress_changed.emit(1.0)
var load_result = get_tree().change_scene_to_packed(load_resource)
set_process(false)
# get_current_scene() returns null until next frame
await get_tree().process_frame
var current_screen = get_tree().get_current_scene()
# scene may continue loading after scene changed, until calling `new_screen_loading_completed` signal is emitted
var is_a_base_screen = current_screen as BaseScreen
if !is_a_base_screen or !current_screen.handle_finish_loading_manually:
new_screen_loading_completed.emit()
func exit_game():
get_tree().quit()